Fork Lecture
Fork Lecture
Fork Lecture
Fork system call use for creates a new process, which is called child process, which runs
concurrently with process (which process called system call fork) and this process is called
parent process. After a new child process created, both processes will execute the next
instruction following the fork() system call. A child process use same pc(program counter), same
CPU registers, same open files which use in parent process.
It take no parameters and return integer value. Below are different values returned by fork().
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main() {
printf("Hello world!\n");
return 0;
}
Output:
Hello world!
Hello world!
Calculate number of times hello is printed.
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
Output:
hello
hello
hello
hello
hello
hello
hello
hello
Number of times hello printed is equal to number of process created. Total Number
of Processes = 2n where n is number of fork system calls. So here n = 3, 23 = 8
So there are total eight processes (new child processes and one original process).
Predict the Output of the following program
#include<stdio.h>
#include <sys/types.h>
#include<unistd.h>
void forkexample()
{
// child process because return value zero
if (fork()==0)
printf("Hello from Child!\n");
Output:
1.
Hello from Child!
Hello from Parent!
(or)
2.
Hello from Parent!
Hello from Child!
In the above code, a child process is created, fork() returns 0 in the child process and positive
integer to the parent process.
Here, two outputs are possible because parent process and child process are running
concurrently. So we don’t know if OS first give control to which process a parent process or a
child process.
Important: Parent process and child process are running same program, but it does not mean
they are identical. OS allocate different data and state for these two processes and also control
flow of these processes can be different . See next example
Predict the Output of the following program.
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
void forkexample()
{
int x = 1;
if (fork() == 0)
printf("Child has x = %d\n", ++x);
else
printf("Parent has x = %d\n", --x);
}
int main()
{
forkexample();
return 0;
}
4. Output:
5. Parent has x = 0
6. Child has x = 2
7. (or)
8. Child has x = 2
9. Parent has x = 0
Here, global variable change in one process does not affected two other processes
because data/state of two processes are different. And also parent and child run
simultaneously so two outputs are possible.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
if(fork())
fork();
return 0;
}
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
int main()
{
printf("hello world from a process ID %d\n", getpid());
printf("hello world from a parent process ID %d\n\n", getppid());
return 0;
}
Exec() can also be used without fork() its not necessary to use it in Fork Call..
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
if (fork()== 0)
else
wait(NULL);
printf("Bye\n");
return 0;
EXEC
exec is a functionality of an operating system that runs an executable file in the context of an
already existing process, replacing the previous executable. This act is also referred to as an
overlay. Since a new process is not created, the original process identifier (PID) does not change,
but the machine code, data, heap, and stack of the process are replaced by those of the new
program.
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execvp(args[0],args);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */