Adapting C fork code to Java program

I am trying to create a small program using Java to develop two new child processes. This is for a beginner programming class where the tutorials are in C, so I'm looking for some help to understand what this piece of code is trying to do, and what is the best way to adapt it to a Java program (ultimately it is).

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
pid t pid;

    /*fork a child process*/
    pid = fork();

    if (pid < 0) { /*error occurred*/
        fprintf(stderr, "Fork Failed");
        return 1;
    }
    else if (pid == 0) {/*child process */
        execlp("/bin/ls", "ls", NULL); 
    }
    else { /*parent process*/
        /*parent will wait for the child to complete */
        wait(NULL);
        printf("Child Complete");
    }
    return 0;
}

      

UPDATE:

I have to attach an ID to each child process and its parent, printing out information when the child process executes and prints a termination notice when it exits. I can now see that this bit of code above lists the contents of the current directory and prints "Child Complete" when the process has finished. Is listing the entire directory viewed by one process? If so, where / how does the second new child process enter the image?

+3


source to share


2 answers


In Java, it might look something like this:



public static void main(String[] args) {
    try {
        Process p = Runtime.getRuntime().exec("/bin/ls");
        final InputStream is = p.getInputStream();
        Thread t = new Thread(new Runnable() {
            public void run() {
                InputStreamReader isr = new InputStreamReader(is);
                int ch;
                try {
                    while ((ch = isr.read()) != -1) {
                        System.out.print((char) ch);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        p.waitFor();
        t.join();
        System.out.println("Child Complete");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

      

+3


source


Okay, to answer what the program does:

When fork () is executed, you end up with two processes. They do the same thing, except that one of them (the child) gets the 0 returned from fork (), while the parent gets any other positive value from fork (). A negative return from fork () means it failed.

So, by looking at the return from fork (), a process can determine if it is a child or a parent. In your case, you let your child run the "ls" command, which lists the files in the current directory.



You allow the parent's wait () to terminate all of its child processes. Then you say "Baby is complete".

You can try removing the wait () system call to see that the two processes are actually running at the same time. Check out the man pages for ps (1), ls (1), fork (2), and exec (3).

+4


source







All Articles