Execution of programs in pararelo WITHOUT use of Threads

Asked

Viewed 156 times

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"

2 answers

5


how I know that the processes are actually running in parallel?

Simple, if you have more than one processor, and today it is common to have at least 4, they are running in parallel. If there is only one processor they are not running in parallel. The operating system takes care of this.

Even if it is not running in parallel will be running concurrently since the operating system also takes care of it.

You essentially don’t have much control over how the execution of the processes will take place.

You could not run other programs within threads, they only exist within a process.

Things to read and understand better:

  • Thanks for the reply, I’ll read better!

4

Roughly speaking, if you want something MIMD, you need to use separate processing heads. The traditional modes of this are:

  1. Threads
  2. Lawsuits
  3. Distributed program

In Unix environments, you create new processes from fork. But this new process is a copy of the image of the previous process. Inclusive, na when calling in C the fork, both the new process and the copy will be exactly on the same line, the line right after the call of this method.

To replace the image, you have the family call exec. Like execve, if I am not mistaken. This call will replace the previous process image with the new process.

So if you’re running program A, and then you call program B, two things can happen:

  1. the program A is completely replaced by program B; there is no salvation for A, even the threads will be overwritten; this happens when you do not call the fork before the exec
  2. program A is cloned and then that clone is replaced by program B; this happens when you call the fork before the creation of programme B

In your case, you are in Java. It hides all this ugliness from the OS so you worry about your stuff. If you saw the firefox or the ls being executed, then there was surely a call to replace the image of the Java program with the image of the new program; ie, had a exec. You also noticed that your program did not stop the execution, which indicates that there was before the override of the executable image, a fork. Java alone does this so you don’t have to worry about any details.

When you have the process start through the processBuilder.start(), it is already born as a process different from yours and will run, parallel to your program (if you do not compete for CPU or other machine resource, as repaired by @Maniero in his reply). If you want to wait for the created process to stop, you need to call process.waitFor(), or simply processBuilder.start().waitFor(), in this particular case you are sure that the programs are not in parallel non-competitive.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.