what will be the output of the below code:
thanks in advanceCode:main() { fork(); fork(); fork(); printf("hello"); }
what will be the output of the below code:
thanks in advanceCode:main() { fork(); fork(); fork(); printf("hello"); }
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.
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.
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?
..OK... That was what I was missing....There is no guarantee however, that the parent will wait for all it's children, unless told to do so explicitly by the programmer
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 ?
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).