Sunday, March 11, 2012

Process management using FORK() System Call




Ex. No.: 4(b)
Process management using FORK() System Call


AIM:
               Write a program in C to create the process hierarchy P1, P2, P3 using fork system call  
 
fork :  This system call produces new process. The new process contains the address space of the original space.
 
wait : The parent can issue a wait system call to wait until the parent process completes.
 
exit : The process completes execution by executing the exit system call.
 
Program:
 
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
void main()
{
   int pid1,pid2;
   //Fork create another process
   pid1=fork();
   if(pid1 != -1)
{
   if(pid1 != 0)
   {
        printf(“p1:%d\t%d”,getpid(), getppid());
        }
   {
      pid2=fork();
      if(pid2 != 0)
    {
            printf(“p2:%d\t%d”,getpid(), getppid());
          }
        else 
          printf("p3:%d\t%d”, getpid(), getppid());
            }
}
}
 
Output
[cs281016@linux]$cc fork.c
[cs281016@linux]$ ./a.out
   P3: 21019           21018
   P2: 21018           21017
   P1: 21017           20068

RESULT :
            Thus the program using the system call fork was written in C and executed successfully.

No comments:

Post a Comment