Like Tree1Likes
  • 1 Post By Elkvis

Need help with fork and exit

This is a discussion on Need help with fork and exit within the Linux Programming forums, part of the Platform Specific Boards category; Hello Everyone, I am trying to write a code where parent process will increment a variable and then child will ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Need help with fork and exit

    Hello Everyone,

    I am trying to write a code where parent process will increment a variable and then child will increment the same variable(its own copy). However, I see that after child process returns to parent by calling exit, it also executes rest of the parents code in its context while using parents copy of the variable. The program is given below.

    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/mman.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <semaphore.h>
    sem_t mutex;
    
    int main(void)
    {
    int i, j, fd;
    pid_t pid;
    int count = 0;
    sem_init(&mutex, 1, 1);
    /* fork a new process */
    if (fork() == 0) {
    /* The child will run this section of code */
    for (j=0;j<5;j++)
    {
    sem_wait(&mutex);
    printf("Child count %d %d\n", ++count, getpid());
    sem_post(&mutex);
    }
    /* exit the child process */
    printf("Child PID(%d): exiting...\n", getpid());
    exit;
    }
    
    /* The parent will run this section of code */
    /* give the child a chance to start running */
    sleep(2);
    for (i=0;i<5;i++)
    {
    sem_wait(&mutex);
    printf("Parent count %d %d\n", ++count, getpid());
    sem_post(&mutex);
    }
    /* exit the parent process */
    printf("Parent PID(%d): exiting...\n", getpid());
    return 0;
    }
    Here is the output:

    ./a.out
    Child count 1 29825
    Child count 2 29825
    Child count 3 29825
    Child count 4 29825
    Child count 5 29825
    Child PID(29825): exiting...
    Parent count 1 29824
    Parent count 2 29824
    Parent count 3 29824
    Parent count 4 29824
    Parent count 5 29824
    Parent PID(29824): exiting...
    Parent count 6 29825
    Parent count 7 29825
    Parent count 8 29825
    Parent count 9 29825
    Parent count 10 29825
    Parent PID(29825): exiting...


    I see that if I use 'return' instead of 'exit' then this problem does not appear. If I use an "if-else" to segregate child and parent code then also this problem does not appear.

    Could anyone please help me to understand what is going on here?

    Thanks,
    With Regards,
    Indranil

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Hello,

    When i compile you program I have a warning "statement with no effect" line 27. It goes away if you put exit(1), and then the program works correctly.

    Hope this helps,

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,584
    hint: exit is a function, not a keyword.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    29
    is this an OS class?^^

    ok, so the special thing about fork() is its return value. When in the child process it returns 0 and when in the parent process it doesn't. So this way it can be used to determine in which process we currently are.

    Suppose there is first a parent with PID 29824. It runs until fork(). At this moment an exact same child process is created, which will continue running from that line. Right now you have two processes which run at the same time. However the parent process will skip the if and go directly to sleep(). That's why you see the "Child count" print first. If you remove sleep() they should be in mixed order. (If I'm not mistaken. I can't compile your code...). Then child exits in the if clause, and parent should be done sleeping. Then the parent continues after sleep, printing it's count. Of course, it will also start from 1, since it's variable is independent of that of the child process.
    I don't know where the second parent comes from though, didn't appear in your code.

    I don't know what you mean by the child executing the parents code in its context. After the child exits, it is gone, away. The main thing to realize is that after fork() the child and parent process both exist and execute at the same time.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by naturegirl View Post
    I don't know what you mean by the child executing the parents code in its context.
    Because the "parent" code from the first post is not inside an else block, it executes in both parent and child.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    29
    but I thought the child process stops after "exit" ?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,584
    it would if exit were being called as a function, but in this program it is not. the OP is attempting to use exit in the same way as return is used, and this is incorrect. refer to my previous post above.
    MK27 likes this.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If warnings were enabled, gcc would call this:
    Code:
    exit;
    A "statement with no effect". It can't really call that an error because it is the same as this...
    Code:
    x;
    ...presuming x has already been declared. Which, if you #include stdlib, "exit" has already been declared and its value, could, eg, be assigned to a function pointer:
    Code:
    	void (*func)(int) = exit;
    Another problem with the original post which might not be apparent after correcting that and running the code is that the mutex used is not shared -- at fork(), it is copied, and so each process is using its own, seperate mutex. See these two posts from a recent thread:

    Need help with process synchronization
    Need help with process synchronization

    WRT to difference between sem_init() and sem_open().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use exit in C
    By IndioDoido in forum C Programming
    Replies: 2
    Last Post: 04-05-2007, 05:42 PM
  2. exit(-3)
    By Moony in forum C Programming
    Replies: 6
    Last Post: 07-06-2006, 01:47 PM
  3. exit()
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2005, 08:07 AM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  5. exit() ?
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2002, 05:31 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21