Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adapting C fork code to a Java program

Tags:

java

c

I am trying to create a small program using Java to fork two new child processes. It's for a beginner's programming class who's tutorials are in C, so I'm looking for some help to understand what this code tidbit is trying to do and what is the best way to adapt it to a Java-based program (to eventually build on it).

#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 am supposed to attach an id to each child process and and its parent, printing the info when the child process executes and printing a termination notification when it terminates. I now see that this bit of code above lists the contents of the current directory and prints "Child Complete" when the process has terminated. Is the listing of the entire directory considered one process? If so, where/how does the second new child process come into the picture?

like image 918
user25976 Avatar asked Mar 19 '23 05:03

user25976


2 Answers

Well, to answer what the program does:

When fork() executes, you get two processes. They do exactly the same thing, except that one of them (the child) gets 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(), the process can determine if it's child or parent. In your case, you let the child execute the "ls" command, which lists files in current directory.

You let the parent wait() for all its child processes to finish. Then you say "Child complete".

You can try removing the wait() system call, to see clearer that the two processes actually run concurrently. Have a look at the man pages for ps(1), ls(1), fork(2) and exec(3).

like image 178
Rein Avatar answered Mar 26 '23 03:03

Rein


In Java, that might look something like -

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();
    }
}
like image 33
Elliott Frisch Avatar answered Mar 26 '23 01:03

Elliott Frisch