Execution of programs in parallel SEM use of Threads



  • They ask me to draw up a Java program, which runs programs (ls, firefox) in parallel 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 I pass by command line. My doubt was:

    • How do I know that the processes are actually running parallel?

    My logic was:

    • use as you can see in the above code printing for the start 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 fileSO2. tx

    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"



  • How do I know that the processes are actually running parallel?

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

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

    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:

    • https://pt.stackoverflow.com/q/131108/101
    • https://pt.stackoverflow.com/q/166032/101



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2