7 Demonstrating Orphan State in C

Program:

#include<stdio.h>

void main()
{
    int cpid = fork();

    // printf("%d",cpid);

    if(cpid > 0)
    {
        printf("\n\n Parent is Running:\n");
        printf("\n Parent ID: %d\n",getpid());
        printf("\n Parent is Going to Sleep:\n\n");
        sleep(2);
        printf("\n\n Parent Wake's Up:\n");
        printf("\n Parent is Exiting\n");
    }
    else if(cpid == 0)
    {
        printf("\n\n Child is Running:\n");
        printf("\n Child is Running:\n It's ID: %d \n Parent ID: %d\n",getpid(),getppid());
        printf("\n Child is Going to Sleep:\n\n");
        sleep(5);
        printf("\n\n Child Wake's Up:\n");
        printf("\n Child is Running:\n It's ID: %d \n Parent ID: %d\n",getpid(),getppid());
    }
}


Output:



Previous
Next Post »