Prog01-O.c

 /*	
Name    : Divyen K Patel Roll No:24 MCA-4
Program : 1 A [orphan State]
Create processes using fork() and check different states i.e. zombie, orphan, etc.
*/

# include <stdio.h>
int main()
{
int pid;
pid=getpid();

printf("Current Process ID is : %d\n",pid);

printf("[ Forking Child Process ... ] \n");
pid=fork(); /* This will Create Child Process and
       Returns Child's PID */

if(pid < 0)
{
/* Process Creation Failed ... */
exit(-1);
}
else if(pid==0)
{
/* Child Process */
printf("Child Process is Sleeping ...");
sleep(5);

/*
   Orphan Child's Parent ID is 1
                */
printf("\nOrphan Child's Parent ID : %d",getppid());
}
else
{
/* Parent Process */
printf("Parent Process Completed ...");
}
return 0;
}

/* Output

[divyen@localhost PP-TW1]$ ./Prog01-O &
[1] 2277
Current Process ID is : 2277
[ Forking Child Process ... ]
Parent Process Completed ...[divyen@localhost PP-TW1]$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR    SZ WCHAN  TTY          TIME CMD
0 S   500  2193  2192  0  75   0    -  1078 wait4  pts/2    00:00:00 bash
1 S   500  2278     1  0  75   0    -   336 schedu pts/2    00:00:00 Prog01-O
0 R   500  2279  2193  0  81   0    -   787 -      pts/2    00:00:00 ps
[1]+  Done                    ./Prog01-O
[divyen@localhost PP-TW1]$ Child Process is Sleeping ...
Orphan Child's Parent ID : 1

*/

Project Homepage: