Reputation: 35
I've got this program which creates an orphan process but I don't quite understand it's control flow and thus the output given by it:
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t p;
/* create child process */
p=fork();
if(p==0) {
/* fork() returns Zero to child */
sleep(10);
}
printf("The child process pid is %d parent pid %d\n", getpid(), getppid());
/*parent/child waits for 20 secs and exits*/
sleep(20);
printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());
return 0;
}
Output:
shiv@ubuntu:~/ds/unix$ ./a.out
The child process pid is 2575 parent pid 1922
The child process pid is 2576 parent pid 2575
Process 2575 is done its Parent pid 1922...
shiv@ubuntu:~/ds/unix$
Process 2576 is done its Parent pid 1...
So it generates the 'child process......' first and then sleeps for 10 sec. and again executes the the 'child process blah blah' . And again see thing happens with 'The parent is done....'
Please help me even if this question looks silly.
Upvotes: 0
Views: 151
Reputation: 2121
Your program is started, creating a process which we shall call the parent process.
The call to fork()
copies the parent process and its entire state and executes it, creating the child process. Parent and child processes are now running simultaneously.
a) The child process receives 0 as the return value from fork()
, so it sleeps 10 seconds.
b) The parent process receives the pid
of the child process as the return value from fork()
, which of course is unequal to 0; so the parent process does not sleep 10 seconds.
The parent process prints "the child process pid is 2575 parent pid 1922"
. Then the parent process encounters the instructions to sleep for 20 seconds, and does so.
The child process wakes up from its 10 second sleep. It prints "The child process pid is 2576 parent pid 2575"
. After that, the child process encounters the instructions to sleep for 20 seconds, and does so.
The parent process wakes up from its 20 second sleep. It prints "Process 2575 is done its Parent pid 1922..."
, and exits.
The child process wakes up from its 20 second sleep. It prints "Process 2576 is done its Parent pid 1..."
, and exits. But why is the parent pid 1, even though it was 2575 earlier? Because the parent process has already exited, which causes the the child process to be orphaned by the init process, which has a pid of 1.
Upvotes: 1
Reputation: 34829
The only thing different between parent and child is the sleep(10)
. So the two printf
s and the sleep(20)
will be executed by both child and parent. But because of the sleep(10)
the printfs from the child will be delayed by 10 seconds after the corresponding printfs from the parent.
Which is to say that the first printf
is misleading. It should say
printf( "my pid is %d and my parent's pid is %d\n", getpid(), getppid() );
In your sample output, the child is pid 2576, the parent is 2575, and the grandparent is pid 1922. The grandparent is the shell process that launched your program.
Upvotes: 0