Results 1 to 6 of 6
Hi gurus can you explain following lines of code ?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int rv;
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-22-2011 #1Just Joined!
- Join Date
- Jul 2009
- Posts
- 70
parent called more times - fork - C language
Hi gurus can you explain following lines of code ?
Code:#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int rv; switch(pid = fork()) { case -1: perror("fork"); /* something went wrong */ exit(1); /* parent exits */ case 0: printf(" CHILD: This is the child process!\n"); printf(" CHILD: My PID is %d\n", getpid()); printf(" CHILD: My parent's PID is %d\n", getppid()); printf(" CHILD: I am sleeping for 15 seconds\n"); sleep(15); // exits as a second printf(" CHILD: Sleeping is over\n"); default: printf("PARENT: This is the parent process!\n"); printf("PARENT: My PID is %d\n", getpid()); printf("PARENT: My child's PID is %d\n", pid); printf("PARENT: I am sleeping for 10 seconds\n"); sleep(10); // exits first printf("PARENT: Sleeping is over\n"); } return 0; }
As far as I know:
- if fork() function is called - code is executing in child and parent section (if fork() did not exits with -1 of course)
- after calling fork() function all code after fork() is executed sequentially (but due to sheduller is unable to predict if will be executed parent or child part of code)
- if parent code exits before child code (without waiting or hadnling signal), childs new parent will be init proces (PPID 1)
I get the following output
So my question is:Code:bash-3.2$ ./a2.out PARENT: This is the parent process! CHILD: This is the child process! PARENT: My PID is 15852 PARENT: My child's PID is 15853 PARENT: I am sleeping for 10 seconds CHILD: My PID is 15853 CHILD: My parent's PID is 15852 CHILD: I am sleeping for 15 seconds PARENT: Sleeping is over bash-3.2$ CHILD: Sleeping is over PARENT: This is the parent process! PARENT: My PID is 15853 PARENT: My child's PID is 0 PARENT: I am sleeping for 10 seconds PARENT: Sleeping is over
- why is not child mark as defunct when parent exits first ? (I tried ps -elf | grep defunct during execution of code)
- Why is parent code executed second time (but now with the pid of child ?)
Thanks a lot
- 02-22-2011 #2
The problem is that you coded your switch statement incorrectly. It should be:
Without the breaks, the correct case will be executed, and then the next one in line will be executed, and so on. This can sometimes be intentional, but is usually not wanted, so remember to always end your cases with a break.Code:switch(pid = fork()) { case -1: ... break; case 0: ... break; default: ... break; }
As for why your child is not marked as a zombie: zombie processes are processes that have died but not yet been reaped by their parents. This usually happens when a parent process fails to call wait() (or waitpid() or something else in that family) for whatever reason, and therefore is never notified of the child's death.
When the parent dies first, as you said, init becomes the parent of the child. init always reaps its child processes, so when the child dies, it does not become a zombie.
- 02-25-2011 #3Just Joined!
- Join Date
- Jul 2009
- Posts
- 70
Hi thanks for explanation.
But AFAIK process becomes zombie when parent process did not wait for child die or done or exit code or what is the correct name - simply parent overlive his child.
And this is done by default - if parent did not wait for SIGCHLD or am I wrong ?
- 02-26-2011 #4Just Joined!
- Join Date
- Jul 2009
- Posts
- 70
nobody
?
- 02-27-2011 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,233
The answer from Cabhan was correct as far as the bogus PARENT messages are concerned. As for zombie process, the answer is that the child will become a zombie when it exits unless one of these things happen:
1. The parent process terminates - all attached child processes will be terminated and child zombies will be dispatched as well.
2. The parent catches the SIGCHLD/SIGCLD signal issued by the termination of the child process.
3. It waits for the death of children with one of the wait() functions which will clean up the process info that would otherwise make the child a zombie.
See the sigaction manpage for more information about how to deal with death-of-child processes.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 02-28-2011 #6Just Joined!
- Join Date
- Jul 2009
- Posts
- 70
Yes I am going to study signal manpage


Reply With Quote
