Write a C program that illustrates how an orphan is created.

AIM:

Write  a C program that illustrates how an orphan is created.

Program:

#include <stdio.h>
main()
{
int pid ;
printf("I'am the original process with PID %d and PPID %d.\n",getpid(),getppid());
pid=fork();
if(pid!=0 )
{
printf("I'am the parent with PID %d and PPID %d.\n",getpid(),getppid());
printf("My child's PID is %d\n",pid) ;
}
else
{
sleep(4);
printf("I'm the child with PID %d and PPID %d.\n",getpid(), getppid()) ;
}
printf ("PID %d terminates.\n", getpid()) ;
}

Output:

guest-glcbIs@ubuntu:~$gcc  –o prg18.out prg18.c
guest-glcbIs@ubuntu:~$./prg18.out
I am the original process with PID2242 and PPID1677.
I am the parent with PID2242 and PPID1677
My child’s PID is 2243
PID2243 terminates.
$  I am the child with PID2243 and PPID1.
PID2243 termanates.