Write a C program to illustrate the concept of orphan process. Parent process creates a child and terminates before child has finished its task. So child process becomes orphan process. (Use fork(), sleep(), getpid(), getppid()).
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int pid=fork();
if(pid>0)
{
printf("\n\nParent Process.\n");
printf("ID : %d \n\n",getpid());
}
else if(pid==0)
{
printf("Child Process\n");
printf("ID : %d \n",getpid());
printf("Parent-ID :%d \n", getppid());
sleep(10);
printf("Child Process\n");
printf("ID : %d \n",getpid());
printf("\nParent-ID :%d \n\n", getppid());
}
else
printf("\nFailed to create Child Process!");
return 0;
}

Comments
Post a Comment