/* fork.c */ /* create a child process */ #include #include #include #include #include int main() { pid_t pid; /* process id (int) */ pid = fork(); /* create a child process */ if ( pid < 0 ) { /* parent unable to produce a child */ fprintf( stderr, "ERROR: unable to fork()\n" ); return EXIT_FAILURE; } /* both parent and child processes return to this point of the code */ if ( pid == 0 ) /* child process */ { printf( "CHILD: happy birthday!\n" ); sleep( 5 ); printf( "CHILD: bye bye\n" ); } else /* pid > 0 so we're in the parent process */ { printf( "PARENT: my child pid is %d\n", pid ); wait( NULL ); /* wait for the child to terminate */ printf( "PARENT: my child's done with his/her work\n" ); } return EXIT_SUCCESS; }