Thread: Fork

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2

    Fork


    Hey Guys,

    I was learning about and was executing the below program and got output as

    Before fork 2After fork 2Before fork 2After fork 2

    Code:
    #include<stdio.h>
    int main()
    {
        int var=2;
    
        printf("Before fork %d",var);
        fork();
        printf("After fork %d",var);
    
        return 0;
    }
    I just added a \n in each printf. When I run the program I am getting output as

    Before fork 2
    After fork 2
    After fork 2


    Code:
    #include<stdio.h>
    int main()
    {
        int var=2;
    
        printf("Before fork %d\n",var);
        fork();
        printf("After fork %d\n",var);
    
        return 0;
    }
    I am not able to understand by adding a new line will it make such a difference ?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,395
    Rep Power
    1871
    Compare with
    Code:
    #include<stdio.h>
    int main()
    {
        int var=2;
    
        printf("Before fork %d",var);fflush(stdout);
        fork();
        printf("After fork %d",var);fflush(stdout);
    
        return 0;
    }
    \n also flushes the output stream.

    Until the stream is flushed, the characters you pass to printf are still in a buffer within the memory space of the process - memory which will be copied when you fork()

    Comments on this post

    • shilpac agrees
    • EEmaestro agrees : Very helpful--thanks, salem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2
    Originally Posted by salem
    Compare with
    Code:
    #include<stdio.h>
    int main()
    {
        int var=2;
    
        printf("Before fork %d",var);fflush(stdout);
        fork();
        printf("After fork %d",var);fflush(stdout);
    
        return 0;
    }
    \n also flushes the output stream.

    Until the stream is flushed, the characters you pass to printf are still in a buffer within the memory space of the process - memory which will be copied when you fork()
    - Thanks Salem.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2

    fork returning -1


    Can anyone tell me in what scenario a fork can fail to create child process?
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,395
    Rep Power
    1871
    • Type in 'man fork' at your terminal.
    • Scroll down to where it says "ERRORS"
    • Read
    • ?
    • profit
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2
    Originally Posted by salem
    • Type in 'man fork' at your terminal.
    • Scroll down to where it says "ERRORS"
    • Read
    • ?
    • profit
    Read. Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo