Code for Program to create processes using fork() and check zombie state in C Programming
# 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);
}
elseif(pid==0)
{
/* Child Process */
printf("Child Process Started ...\n");
printf("Child Process Completed ...\n");
}
else
{
/* Parent Process */
sleep(10);
printf("Parent Process Running ... \n");
printf("I am In Zombie State ...\n");
while(1)
{
/*
Infinite Loop that Keeps the
Process Running
*/
}
}
return 0;
}
/* Output
[divyen@localhost PP-TW1]$ ./Prog01-Z &
[1] 2320
Current Process ID is : 2320
[ Forking Child Process ... ]
Child Process Started ...
Child 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
0 S 500 2320 2193 0 75 0 - 336 schedu pts/2 00:00:00 Prog01-Z
1 Z 500 2321 2320 0 75 0 - 0 t> pts/2 00:00:00 Prog01-Z <defunct>
0 R 500 2322 2193 0 81 0 - 788 - pts/2 00:00:00 ps
[divyen@localhost PP-TW1]$ Parent Process Running ...
I am In Zombie State ...
[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 76 0 - 1078 wait4 pts/2 00:00:00 bash
0 R 500 2320 2193 26 80 0 - 336 - pts/2 00:00:04 Prog01-Z
1 Z 500 2321 2320 0 75 0 - 0 t> pts/2 00:00:00 Prog01-Z <defunct>
0 R 500 2323 2193 5 81 0 - 787 - pts/2 00:00:00 ps
*/