All about programming in GNU/LINUX

ILLUSTRATION OF a ORPHAN PROCESS AND ASSIGNMENT OF INIT AS A PARENT TO SUCH A PROCESS USING FORK

//DOWNLOAD LINK FOR THE FILE:http://www.slideshare.net/hackintoshrao/forkexpeImage

/* Hackintoshrao:codes for GNU/LINUX

   ILLUSTRATION OF ORPHAN PROCESS AND ASSIGNMENT OF INIT AS A PARENT TO SUCH A PROCESS
   SYSTEM CALLS USED
   1.fork()
   2.getpid()
   3.getppid()
   4.sleep()
   */
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
int main()
{
    int pid;
    pid=fork();
    if(pid==0)
    {

        printf(“\nChild process here \n”);
        printf(“\nchild here..Parent id=%d,Process id=%d\n”,getppid(),getpid());
        sleep(3);
        printf(“\nChild process here,now its a orphan process,so init is my parent process now  \n”);
        printf(“\nchild here…Parent id=%d,Process id=%d\n”,getppid(),getpid());
        /*its clearly evident in the o/p that the parent process Id of the child process will now be =1,
         the init process is made as a parent */
    }
    else{
        sleep(1);
        /*since parent process finishes earlier than the child ,the latter becomes a orphan process */
        printf(“\nparent process here \n”);
        printf(“\nparent process..Process id=%d\n”,getpid());
        printf(“\nparent process Exiting,making the child process a orphan …….\n\n”) ;

    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s