The C program shown below illustrates the UNIX system calls
previously described.
#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");
exit(-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");
exit(O);
}
}
- We now have two different processes running a copy of the same program.
- The value of pid for the child process is zero; that for the parent is an integer value greater than zero.
- The child process overlays its address space with the UNIX command
(used to get a directory listing) using the
system call (
is a version of the
system call).
- The parent waits for the child process to complete with the
system call.
- When the child process completes (by either implicitly or explicitly invoking
) the parent process resumes from the call to
, where it completes using the
system call.
- This is also illustrated in Fig. 3.12.
Figure 3.12:
Process creation.
|