Fork() - help me understand

Discussion in 'Unix' started by neo_vi, Jan 7, 2010.

  1. neo_vi

    neo_vi Member

    Joined:
    Feb 1, 2008
    Messages:
    720
    Likes Received:
    16
    Trophy Points:
    18
    Occupation:
    Software engineer
    Location:
    Earth
    Home Page:
    http://computertipaday.blogspot.com
    Fork
    Definition of wikipedia says that

    "The fork operation creates a separate address space for the child. The child process has an exact copy of all the memory segments of the parent process, though if copy-on-write semantics are implemented actual physical memory may not be assigned (i.e., both processes may share the same physical memory segments for a while). Both the parent and child processes possess the same code segments, but execute independently of each other."

    And also in Let us C book, by Yashvant kanetkar it is mentioned as
    "Fork doesn't mean that the child process contains the data and code below the fork() call."

    Then why does this code prints only the statements below the fork twice compared to printing all the statements twice.
    Code:
    #include<stdio.h>
    #include<sys/types.h>
    
    int main()
    {
    printf("Before forking\n");
    fork();
    printf("After forking\n");
    }
     
  2. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    What is the compiler that you used and in which Operating System did you run this program?
     
  3. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    According to my understanding, this is what might have happened. Since the fork()ed child process shares the same memory segment, standard file streams(STDIN, STDERR, STDOUT) and the printf() writes the content to the STDOUT buffer. The child process again prints it since it has the copy of the content written by its parent in the STDOUT buffer.

    Could you let me know what happens when running the program by issuing flush() of STDOUT after every printf() statements.
     
  4. neo_vi

    neo_vi Member

    Joined:
    Feb 1, 2008
    Messages:
    720
    Likes Received:
    16
    Trophy Points:
    18
    Occupation:
    Software engineer
    Location:
    Earth
    Home Page:
    http://computertipaday.blogspot.com
    Ubuntu 9.04, GCC
     
  5. neo_vi

    neo_vi Member

    Joined:
    Feb 1, 2008
    Messages:
    720
    Likes Received:
    16
    Trophy Points:
    18
    Occupation:
    Software engineer
    Location:
    Earth
    Home Page:
    http://computertipaday.blogspot.com
    So the code will become like this.
    Code:
      
    #include<stdio.h>
    #include<sys/types.h>
    
    int main()
    {
    printf("Before forking\n");
    fflush(stdout);
    fork();
    printf("After forking\n");
    fflush(stdout);
    }
    
    Again i got the same output
    Code:
    Before forking
    After forking
    After forking
    
     
  6. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    When the code is executed, the first printf() is executed once before another process is created using fork(). After fork(), the new process gets the copy of the code part after fork(). So each process(parent and child) executes the second printf() once each.

    This is how a fork()-ed process works.
     
  7. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Previously I thought the reason as something else and posted. But after seeing this link, I came to know the behaviour of fork(). Hope this might help you :).
     
  8. shankar.489

    shankar.489 New Member

    Joined:
    Aug 19, 2010
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    System Software Engineer
    Location:
    Anantapur-India
    #include<stdio.h>
    #include<sys/types.h>

    int main()
    {
    printf("Before forking\n");
    if(fork()==0)
    printf("After forking\n");
    }
    try this for ur required output.
    note:If successful, fork() returns 0 in the child process, and returns the process ID of the child process to the parent process.
    and the remaining analyzy i left to you...good luck
     
  9. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    http://www.crazylearner.in
    yes it is correct that fork() makes the image of parent process but it is also correct that the execution of child process starts after the fork call....
    fork() returns two values 0 in the child process and pid in the parent process..
    Code:
    1.int main(){
    2.printf("before fork");
    3.fork();
    4.printf("after fork");
    5.}
    
    upto line 3 only parent process was executing
    after line 3 both parent and child process are executing
    line 1 to 3 executed only by parent
    line 3 to 5 executed by both child and parent.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice