Friday, 8 November 2013

C program to create a Orphan process.

[singh@00-13-02-56-15-7c programs]$ vi orphan.c


#include <stdio.h>
void main()
{
    int pid;
    pid = fork();
    if(pid==0)
    {
        printf("I am a child");
        printf("\nChild pricessID is: %d\n Child parentID is: %d",getpid(),getppid());
        sleep(10);
        printf("\nChild processID is: %d\n Child parentID is: %d",getpid(),getppid());
    }
    else
    {
        printf("\nI am parent");
        printf("\nParent processID is: %d\n Parent parentID is: %d\n",getpid(),getppid());
    }
}
     
   
Output :

[singh@00-13-02-56-15-7c programs]$ gcc orphan.c
[singh@00-13-02-56-15-7c programs]$ ./a.out
I am a child
Child pricessID is: 9395
I am parent
Parent processID is: 9394
 Parent parentID is: 8845
[singh@00-13-02-56-15-7c programs]$  Child parentID is: 9394
Child processID is: 9395
 Child parentID is: 1

No comments:

Post a Comment