Fork( ) System Call
Single Concept Learning Module
The objective of this module is to introduce students to the fork systemcall. After the audience has listened to the lecture and executed the sampleprogram, students will have the ability to develop multi - process programs.
Concepts:I. Fork definition.
II. Process Creation.
III. Process Assignment.
IV. Example.
V. Discussion.
Concept Hours:3
Lecture:The fork() System Call
I. Fork definition
To create a new process, in UNIX, the fork() system call is used. Fork()creates a new context based on the context of the calling process. Thefork() call is unusual in that it returns twice: It returns in both theprocess calling fork() and in the newly created process. The child processreturns zero and the parent process returns a number greater then zero.
The synopsis for fork() is as follows:
#include
pid_t fork(void);
pid_t vfork(void);
If fork() is successful, it returns a number of type pid_t which isgreater than 0 and represents the PID of the newly created child process.In the child process, fork() returns 0. If fork() fails then its returnvalue will be less than 0. vfork() is a m ore efficient version of fork(),which does not duplicate the entire parent context.
II. Process Creation.A example of fork() follows. Here, the parent process prints Hello tostdout, and the new child process prints World. Note that the order ofprinting is not guaranteed. Without some method of synchronizing the processesexecution, Hello may or may not be printed before World.
#include
#include
char string1[] = "\n Hello";
char string2[] = " CS560.\n";
int main(void)
{
pid_t PID;
PID = fork();
if (PID == 0) /* Child process */
printf("%s", string2);
else /* Parent process */
printf("%s", string1);
exit(0); /* Executed by both processes */
}
III. Process AssignmentThe fork() creates a copy of the process that was executing. The fork()is called once but returns twice (once in the parent and once in the child).
The line PID = fork(); returns the value of the fork() system call.The if (PID == 0) evaluates the return value. If PID is equal to zero thenprintf() is executed in the child process, but not in the parent process.
The else part of the condition is executed in the parent process andthe child process but only the parent process will execute the else partof the condition.
IV. ExampleThe child process can obtain the process ID of the parent by using thegetppid system call. Below is an example of the fork and getppid systemcalls.
#include
void
main(void)
{
int childpid;
if( (childpid = fork() ) == -1)
{
printf("\n Can't fork.\n");
exit(0);
}
else
if(childpid == 0)
{ /* Child process */
printf("\n Child: Child pid = %d, Parent pid = %d \n", getpid(),getppid());
exit(0);
}
else
{ /* Parent Process */
printf("\n Parent: Child pid = %d, Parent pid = %d \n", childpid,getpid());
exit(0);
}
}
V. DiscussionOne important feature of the fork system call is that the files thatwere open in the parent process before the fork are shared by the childprocess after the fork. This feature allows the parent to pass open filehandles and device driver handles to th e child.
The values of the following variables are copied from the parent processto the child process.
· real user ID
· real group
· effective user ID
· effective group ID
· process group ID
· terminal group ID
· root directory
· current working directory
· signal handling settings
· file mode creation mask
The child process differs from the parent process in the followingways:· the child process has a new, unique process ID,
· the child process has a different parent process ID,
· the child process has its own copies of the parent's file descriptors,
· the time left until an alarm clock signal is set to zero inthe child.
In review, there are basically two uses for the fork operation.1. A process wants to make a copy of itself so that one copy can handlean operation while the other copy does another task.
2. A process wants to execute another program. Since the only way tocreate a new process is with the fork operation, the process must firstfork to make a copy of itself, then one of the copies issues an exec systemcall operation to execute a new program. This is typical for programs suchas shells.