SANDIP UNIVERSITY, SIJOUL
School of Computer Science & Engineering
Name of Student:
Date of Performance: Date of completion:
EXPERIMENT NO: 04
TITLE: Program for process creation using fork() and vfork() function.
AIM: Write a C program in Unix that create the process using fork() and vfork()
function. Also perform computation in child process & result will print parent process
using vfork() function.
REQUIRED S/W: Ubuntu 12.4 or Extended version.
THEORY:
fork Function
An existing process can create a new one by calling the fork function.
#include<unistd.h>
pid_t fork(void);
Returns: 0 in child, process ID of child in parent, -1 on error
The new process created by fork is called the child process.
This function is called once but returns twice.
The only difference in the returns is that the return value in the child is 0, whereas the
return value in the parent is the process ID of the new child.
The child is a copy of the parent. For example, the child gets a copy of the parent's
data space, heap, and stack.
The parent and the child share the text segment.
Current implementations don't perform a complete copy of the parent's data, stack,
and heap. They use a technique called copy-on-write (COW) is used.
We never know whether the child starts executing before the parent or vice versa.
This depends on the scheduling algorithm used by the kernel.
vfork Function
#include<unistd.h>
pid_t vfork(void);
Returns: 0 in child, process ID of child in parent, -1 on error
The vfork function is intended to create a new process when the purpose of the new
process is to exec a new program.
The vfork function creates the new process, just like fork, without copying the
address space of the parent into the child.
The child calls exec right after the vfork.
The vfork guarantees that the child runs first until the child calls exec or exit.
The child runs in the address space of the parent until it calls either exec or exit.
ALGORITHM:
1) Declare a variable pid to check the type of process.
2) Call fork () or vfork() to create an new process.
3) Get process id (pid) return by fork () function.
4) If pid<0, then display the errors message.
5) If pid>0, then it is parent process.
6) If pid=0 then it is child process
7) Terminate the program.
Program:-
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int pid;
pid=fork();
if(pid<0)
{
printf("\n Error ");
exit(1);
}
else if(pid==0)
{
printf("\n Hello I am the child process ");
printf("\n My pid is %d \n",getpid());
exit(0);
}
else
{
printf("\n Hello I am the parent process ");
printf("\n My actual pid is %d \n ",getpid());
exit(1);
}
}
/*OUTPUT:
student08@student:~$ gcc AUP_O4A.c
student08@student:~$ ./[Link]
Hello I am the parent process
My actual pid is 4383
Hello I am the child process
My pid is 4384
student08@student:~$ */
ALGORITHM:
CONCLUSION:
QUESTIONS:
1) What is difference between fork() and vfork() function?
_ _
_ _
_ _
2) What is process?
_ _
_ _
_ _
_ _
3) What is process control?
_ _
_ _
_ _
REFRENCES:
Advanced Programming in the UNIX Environment, W Richard Stevens
NAME & SIGN OF TEACHER: