OS-(I) Assignment 1: Set B- 2)

(2) 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()).

Orphan process

The process whose parent process has finished (Completed execution) or terminated and do not exists in the process table are called orphan process. Usually, a parent process waits for its child to terminate or finish their job and report to it after execution but if he fails to do so it results in the Orphan process. In most cases, the Orphan process is immediately adopted by the init process (a very first process of the system).

Solution:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pid=fork();
if(pid>0)
{
printf("\nIAM PARENT PROCESS,MY PROCESS ID IS: %d",getpid());
printf("\n THE PARENT'S PARENT PROCESS ID IS : %d\n",getppid());
}
else if(pid==0) 
{
printf("\n In Child process\n");
printf("\n IAM CHILD PROCESS,MY PROCESS ID IS: %d",getpid());
printf("\n THE CHILDS'S PARENT PROCESSS ID IS : %d\n",getppid());
sleep(10);
// At this time parent process has finished.
// So if u will check parent process id it will show different process id
printf("\n **************AFTER 10 SECUNDS**********");
printf("\n IAM CHILD PROCESS,MY PROCESS ID IS: %d",getpid());
printf("\n THE CHILDS'S PARENT PROCESS ID IS :%d\n",getppid());
}
else 
{
printf("Failed to create child process");
}
return 0;
}

0 Comments:

Post a Comment