Fork
fork() creates a child that is duplicate of the parent process. All statements after the fork() function call are executed both in the parent as well as the child process. Any statements before the fork() would be executed only once.
In the following program, To the parent process fork() returns the PID of the child process or -1 in case of errors. In the child process the value of PID will always be 0.
int pid;
printf(“Before\n”);
pid = fork();
printf(“After\n”);
}
Output:
Before
After
After
main(){
fork();
fork();
printf(“Hello\n”);
}
Output:
Hello
Hello
Hello
Hello
Simply put, for n calls of fork(), there would be 2^n process’.

0 Comments:
Post a Comment
<< Home