01: /**
02:  * This program forks a separate process using the fork()/exec() system calls.
03:  *
04:  * Figure 4.8
05:  *
06:  * @author Gagne, Galvin, Silberschatz
07:  * Operating System Concepts with Java - Sixth Edition
08:  * Copyright John Wiley & Sons - 2003.
09:  */
10: 
11: #include <stdio.h>
12: #include <unistd.h>
13: 
14: int main(int argc, char *argv[])
15: {
16: int pid;
17: 
18:         /* fork another process */
19:         pid = fork();
20: 
21:         if (pid < 0) { /* error occurred */
22:                 fprintf(stderr, "Fork Failed\n");
23:                 exit(-1);
24:         }
25:         else if (pid == 0) { /* child process */
26:                 execlp("/bin/ls","ls",NULL); // overlay address space of parent
27:         }
28:         else { /* parent process */
29:                 /* parent will wait for the child to complete */
30:                 wait(NULL);
31:                 
32:                 printf("Child Complete\n");
33:                 exit(0);
34:         }
35: }