Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to create processes using fork() and check orphan state

Posted By: Ella Campbell     Category: C Programming     Views: 5709

Program to create processes using fork() and check different states i.e. zombie, orphan, etc.

Code for Program to create processes using fork() and check orphan 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 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

*/
  
Share: 



Ella Campbell
Ella Campbell author of Program to create processes using fork() and check orphan state is from Toronto, Canada.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!