Vous êtes sur la page 1sur 2

Program to Implement System Calls Using Fork Function

Understanding the functions of fork system call would help to know in detail about child process
creation. The resulting child process is mostly identical to the process which calls the
function.The child process has a unique process ID and it executes independently of the parent
process.

When you call p = fork() , a new process (the child process) is created based on the current
process. Thus a separated address space is created for the child.
If fork() system call returns a negative value, it implies that the creation of the process
was unsuccessful.
If a new child process is created, fork() system call returns zero.
fork() system call returns a positive value and the process ID of the child process to the
parent.
AIM
To write a program for execution of fork function.

ALGORITHM
Step 1: Start
Step 2: Declare p1 and p2
Step 3: Call the fork function for p1.If p1 is equal to -1 print error and return 0.
Step 4: Else print the parent and child by getppid() and getpid().
Step 5: Again call fork () for p2. Print the parent and child by getppid() and getpid().
Step 6: Stop

Fork system call program


#include<stdio.h>
#include<sys/type.h>
int main()
{
int p1;
p1=fork();
if(p1==-1)
{
printf(Error);
return 0;
}
else
{
printf(Parent is %d\n,getppid());
printf(Child is %d\n\n,getpid());
}
return 0;
}

OUTPUT
Parent is 12031
Child is 26504

Parent is 26504
Child is 26505

RESULT
Thus the program for execution of fork function was executed successfully and the output
was verified.

Vous aimerez peut-être aussi