Execution of programs in halt WITHOUT use of Threads

5

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"
    
asked by anonymous 21.11.2017 / 13:32

2 answers

5
  

How do I know that 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 it only has one processor they are not running in parallel. The operating system takes care of this.

Even though it is not running in parallel, it will be running concurrently because the operating system also handles this.

You essentially do not have much control over how the execution of the processes will occur.

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

Things to read and understand better:

21.11.2017 / 13:48
4

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

  • Threads
  • Processes
  • Distributed program
  • In Unix environments, you create new processes from fork . But this new process is a copy of the previous process image. Even when calling C% w / o%, both the new process and the copy will lie exactly on the same line, the line just after calling this method.

    To replace the image, you have the call of the fork family. Like exec , if I'm not mistaken. This call will replace the previous process image with the new process.

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

  • program A is completely replaced by program B; has no salvation for A, even the threads will be overwritten; this happens when you do not call execve before fork
  • program A is cloned and then this clone is replaced by program B; this happens when you call exec before program creation B
  • In your case, you are in Java. He hides this ugliness from the OS so you worry about your stuff. If you saw the fork or firefox being executed, then surely there was a call to replace the image of the Java program with the image of the new program; that is, it has ls . You also noticed that your program did not stop execution, which indicates that there was a exec before the executable image was overwritten. Java alone does this so you do not have to worry about any details.

    When you start the process with fork , it is already born as a process different from yours and will run parallel to your program (if it does not compete for CPU or other machine resources, as repaired by @Maniero in your answer). If you want to wait for the created process to stop, you need to call processBuilder.start() , or simply process.waitFot() , in this specific case you are sure that the programs are not in non-concurrent parallel.     

    21.11.2017 / 13:55