Thursday 29 September 2011

IMPLEMENTATION OF LINUX SYSTEM CALLS


AIM:
To write a C program using LINUX system calls (fork(),wait(),getpid()).

ALGORITHM:
Step1: Start the process.
Step2: Create a new process (parent process) using fork() system call.
Step3: Create a identical copy of parent process(child process) using fork () system call
Step4: Process id is created for both child and parent process using getpid() system call
Step5: The Child process is made to execute.
Step6: After the exiting of the child process parent process will get exit
Step7: Stop the process.

PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
void parent ();
void child();
main ()
{
int r;
r=fork();
{
printf(“fork empty”);
}
if(r>0)
parent(r);
else
child(r);
}
void child()
{
int i;
printf(“child starts executing”);
for(i=0;i<5;i++)
{
printf(“\n child %d is executing %d time”,getpid(),i);
sleep(i);
}
printf(“child %d exit \n”,getpid());
}
void parent(pid_t child_pid)
{
printf(“\n Parent %d watiting for child %d to start the executing “, getpid(),child_pid);
printf(“\n”);
waitpid(child_pid,NULL,0);
printf(“Parent %d exit \n”,getpid());
}

OUTPUT:
Parent 269 watiting for child 70 to start the executing
Child starts executing
Child 270 is executing 0 time
Child 270 is executing 1 time
Child 270 is executing 2 time
Child 270 is executing 3 time
Child 270 is executing 4 time
Child 270 exit.
Parent 269 exit

RESULT:
 Thus the given C Program is executed and output is verified.

0 comments:

Post a Comment