Find the answer to your Linux question:
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.
  1. #1
    Just 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


    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
    So my question is:
    - 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

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    The problem is that you coded your switch statement incorrectly. It should be:

    Code:
    switch(pid = fork()) {
        case -1:
            ...
            break;
        case 0:
            ...
            break;
        default:
            ...
            break;
    }
    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.

    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.

  3. #3
    Just 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 ?

  4. #4
    Just Joined!
    Join Date
    Jul 2009
    Posts
    70
    nobody ?

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,158
    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!

  6. #6
    Just Joined!
    Join Date
    Jul 2009
    Posts
    70
    Yes I am going to study signal manpage

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •