Ask me to build a Java program that runs parallel programs ( ls
, firefox
) that are contained in a file.
I already 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 that I pass through the command line. My question was:
- How do I know that processes are actually running in parallel?
My logic was:
- use as you can see in the code above the printout for the home screen and the end of the program. My output gives the following:
zeluis @ zeluis-HP-EliteBook-8460p ~ / NetBeansProjects / SOCP2 / src / socp2 $ java -cp .. socp2.Shell file SO2.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"