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?