Source code for fork.c


    1: #include <stdio.h>
    2: #include <unistd.h>
    3: 
    4: int main( void )
    5: {
    6:     int pid;
    7: 
    8:     printf( "The original process has pid %d\n", getpid() );
    9: 
   10:     pid = fork();
   11: 
   12:     switch ( pid )  {
   13:         case 0:
   14:             printf( "Me: %d  Parent: %d  Fork: %d\n", 
   15:                     getpid(), getppid(), pid );
   16:             break;
   17: 
   18:         case -1:
   19:             perror( "fork" );
   20:             break;
   21: 
   22:         default:
   23:             printf( "Me: %d  Parent: %d  Fork: %d\n", 
   24:                     getpid(), getppid(), pid );
   25: 
   26:             /* this is the parent, so we'll wait on the child */
   27:             printf( "Child %d exited\n", wait( 0 ) );
   28:             break;
   29:     }
   30: }
   31: 
   32: