C Forking Made Easy

Bryan Young By Bryan Young
Expert Author
Article Date:

Arguably one of the more advanced undertakings in the C language is process handling. This is a tricky subject that requires a good grasp on the language and a deep understanding of the underlying operating system. Well I am here to tell you that it is nothing to fear and you can master the art of branching processes with just a little practice.

The bread and butter of C process handling comes from two OS level functions, fork() and wait(), the prior to split a process and the latter to bring it back together. Let us first look at a simple example to get started with.

 1 #include <sys/types.h>
 2 #include <unistd.h>
 3 #include <wait.h>
 5 int main( int argc, char **argv ) {
 6   pid_t pid;
 8   // use the fork() command to start a child process
 9   pid = fork();
11   // there was an error
12   if ( 0 > pid_t ) {
13      printf( "Failed to open a child processn" );
14      exit( EXIT_ERROR );
15   }
17   // parent process
18   if ( 0 < pid_t ) {
19      // wait for the child to finish
20      wait( NULL );
21      printf( "worldn" );
22      exit( EXIT_SUCCESS );
23   }
25   // child process
26   printf( "hello, " );
27   _exit( EXIT_SUCESS );
28 }

Now to break it down piece by piece. First we need to include three base libraries, <sys/types.h> to use the pid_t variable declaration, <unistd.h> for the fork() command, and <wait.h> for the wait() command.

The next line actually splits the process into two individual processes with pid = fork(). The important thing to remember is that this splits the process exactly where the function is called, so everything before fork() applies to both processes. The return value is always one of three possible values. If pid is -1, we know there was an error, so we print an error message and exit. If successful, fork() will return 0 to the newly created process and the process id of the child process to the parent process.

So now we have two separate processes, a parent and a child. In the parent (where pid is greater than 0) we call wait( NULL ). This function blocks execution until the child process dies. In the child process, we print "hello, " and exit sucessfully. Now the the child has exited, the parent continues execution by printing "world" and exits as well. Thus, even though printing "world" comes first in the code, it is actually printed last.

The final line is the last puzzler. Why use _exit() instead of exit(). While both functions will terminate the current process, _exit() does so without triggering any trigger functions, which is important if you have set up a trigger you want to go off when the parent exits, but not when the child does.

About Bryan Young
Bryan Young is a staff writer for WebProNews.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • 160×600
  • 152X252
  • Newsletter Signup

    Subscribe to our Newsletter

    * Your Email Address:
    * Preferred Format:
       First Name:
       Last Name:
  • 336×280