Sunday, 8 May 2011

Unix System Calls like fork,wait,execlp,sleep for Process Management...

************************ Instructions how to make program ****************************

(1) Open Terminal.
(2) type  vi <prog-name>.<extension> & then Press Enter...
(3) whenever you want to insert code or statements press i.
(4) Save program bye typing this :-   Press Esc key then Shift + ;  then wq & then Press Enter...
(5) How to compile :- just enter this :- cc <prog-name>.<extension> & pres Enter...
(6) How To Run :- just enter this :- ./a.out & press Enter...
*********************************** final.c **************************************
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>

int main()
{
pid_t gp,p,c;
printf("\n Gramd Parent Executing...");     
gp=fork();
printf("Grand Parent's pid => %d",gp);
if(gp==0) // its Child executes...
{
printf("\n Parent Executing...");     
p=fork();
printf("Parent's pid => %d",p);
if(p==0) // its Child executes...
{
printf("\n Child Executing...");     
c=fork();
sleep(10);
printf("Child's pid => %d",c);
printf("\n Child after execution...");
}
printf("\n Parent after execution...");
execlp("/bin/ls/","ls",NULL);
printf("\n execlp() Completed...");
}
gp=wait();
printf("\n After Waiting Grant Parent's Process id = > %d",gp);
exit(0);
}
*******************************************************************************
For this Program You can refer following links :- 
       http://linux.about.com/library/cmd/blcmdl3_execlp.htm
       http://linux.about.com/od/commands/l/blcmdl.htm

No comments:

Post a Comment