1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Fork() - help me understand

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

  1. neo_vi

    neo_vi New Member

    Joined:
    Feb 1, 2008
    Messages:
    722
    Likes Received:
    15
    Trophy Points:
    0
    Occupation:
    Software engineer
    Location:
    Earth
    Home Page:
    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:
    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:
    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 New Member

    Joined:
    Feb 1, 2008
    Messages:
    722
    Likes Received:
    15
    Trophy Points:
    0
    Occupation:
    Software engineer
    Location:
    Earth
    Home Page:
    Ubuntu 9.04, GCC
     
  5. neo_vi

    neo_vi New Member

    Joined:
    Feb 1, 2008
    Messages:
    722
    Likes Received:
    15
    Trophy Points:
    0
    Occupation:
    Software engineer
    Location:
    Earth
    Home Page:
    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:
    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:
    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:
    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