Like Tree1Likes
  • 1 Post By grumpy

output??

This is a discussion on output?? within the C Programming forums, part of the General Programming Boards category; what will be the output of the below code: Code: main() { fork(); fork(); fork(); printf("hello"); } thanks in advance...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    output??

    what will be the output of the below code:

    Code:
    main()
    
    { fork();
    fork();
    fork();
    printf("hello");
    }
    thanks in advance

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    It doesn't work that way here. Try to reason out the answer for yourself. If you think you know, and have the courage to accept criticism, post your answer here and you'll certainly receive opinions on whether you are right or wrong.

    Note that not all (non-unix) operating systems support fork(). On operating systems that do not support fork(), your program will not be buildable.

    If your C compiler is actually a C++ compiler, your code will not compile.

    As to what will happen on systems that support fork() AND if you compile with an actual C compiler (not a C++ compiler) I'll be interested to see if you can reason out a correct answer for yourself. If you try to run the program, it might pay you to run it a few times to see if the output is actually repeatable ......
    Last edited by grumpy; 07-28-2011 at 07:12 AM.
    AndrewHunter likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    It is also written to presume all fork() calls are successful (I guess).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Hey everyone..
    I tried it using 2 forks.
    Sometimes there are 4 Hello's , that I can understand why.
    But occasionally, there are 2 before and 2 after the control returns to the native shell...Why is that?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    Quote Originally Posted by manasij7479 View Post
    Hey everyone..
    I tried it using 2 forks.
    Sometimes there are 4 Hello's , that I can understand why.
    But occasionally, there are 2 before and 2 after the control returns to the native shell...Why is that?
    When you fork, you create more processes. The OS switches very rapidly between these processes, executing a handful of instructions from each one, before moving on to the next. There is no guarantee however, that the parent will wait for all it's children, unless told to do so explicitly by the programmer (with wait() or waitpid() functions). What happens if the original (parent) process gets control back and finishes before all of it's children have completed their forking and printing?

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    There is no guarantee however, that the parent will wait for all it's children, unless told to do so explicitly by the programmer
    ..OK... That was what I was missing....

    But I find it difficult to believe that more than one process(here the shell and the prodigal child process) would be allowed to write to the same output file without crippling things seriously. Is that simply undefined behaviour that it was lucky and worked without trashing the shell ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    Read the man page for fork, it describes what it inherits (stdin, stdout and stderr amongst other things), and what it doesn't inherit. This sharing is quite normal and often useful. If you run jobs in the background, they may need to communicate problems to you via stdout/err, and must share it with your shell and any other commands you're currently running. You do have to realize though, that there is no guarantee to the order in which output is printed. If you change the printf to something like printf("hello from pid = %d\n", getpid()), you will see that the output is usually mixed up (the pids should be assigned roughly in order).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  2. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 09:46 PM
  4. output a string to a standard output
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 05:59 AM
  5. Replies: 3
    Last Post: 02-19-2003, 07:34 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