5
They ask me to develop a Java program, to run programs (ls
, firefox
) in parallel that are contained in a file.
For now I have the following code:
File file = new File()`;
List<String> lista = file.readFile(args[0]);
for (int i = 0; i < lista.size(); i++) { //percorre o ciclo de comandos que são enviados como argumentos no terminal
ProcessBuilder pb = new ProcessBuilder(lista.get(i)); //criacao de processos externos à JVM (Java Virtual Machine)
Process process = pb.start();
System.out.println("Inicio do comando \"" + lista.get(i) + "\".");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bf = new BufferedReader(isr);
String line;
while((line = bf.readLine())!= null) {
System.out.println(line);
}
System.out.println("Fim do comando \"" +args[i] + "\".");
args[0]
refers to the name of the file I pass per command line. My question was:
- how I know that the processes are actually running in parallel?
My logic was:
- use as you can see in the above code the print for the start screen and the end of the program. Mine output gives the following:
zeluis@zeluis-HP-EliteBook-8460p ~/Netbeansprojects/SOCP2/src/socp2 $ java -cp .. socp2.Shell fichSO2.txt
Isto é um programa que processa em paralelo os seus processos
Inicio do comando "ls".
ficheiroSO2.txt
File.class
File.java
Shell.class
Shell.java
Fim do comando "ficheiroSO2.txt".
Inicio do comando "firefox"
Thanks for the reply, I’ll read better!
– ZelDias