int ret;

how many times sun will be printed and how many times solaris will be printed ? and reason also. i know the fundamentals of fork() but still not able to catch the o/p here. thanks.

Votes + Comments
nice question it made revisit a lot of concepts , i thinks its fully answered now
3 Years
Discussion Span
Last Post by lalitha2294

but if i want to know without running it ? then what will be my approach to it ?


I think the key is to read about the return value of fork() in the man pages\

Return Value
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

I'm no *nix expert, or even novice, if I were to guess I'd say Sun will get printed 4 times and Solaris one time.


but if i want to know without running it ?

Then you run it in your head.

First note that the return values of the first three forks do not matter - you assign each of them to ret, but then you overwrite them right afterwards without using them. So you can simplify the code by writing it as:

ret = fork();

Now if you understand the fundamentals of fork, you know that each fork creates a new process and the control flow in that process starts right after that call to fork. So after the first fork you have two processes and each of them will then execute the second call to fork. Both of those calls will create another process. So you now have two new processes, making a total of four. Now those four processes all execute the third fork and so on...


Oh and also note that since there's no newline at the end of your strings and you don't flush stdout before forking, the output may actually behave unpredicatably.


@sepp2k you have made it so simple. i got it. i am explaining, tell me i am right or wrong. at first fork(), one process--> this make it 2 processes. now next statement is food for 2 processes now. now that 2 will be divided into 4. so like this last statement will be executed by 8 process which will make them 16 processes. so that if-else statements will be executed 16 times. out of which 8 are parent and 8 are children. so 8 are non-zero, 8 are zeros. so 8 times solaris will be printed.

am i right 100% ? if i am wrong at any point, please correct me. secodnly, explain me why is it neccasary to fflush the streams ? thanks.

thanks to all for your valuable responses.

Edited by nitin1


am i right 100% ?


explain me why is it neccasary to fflush the streams ?

When I run your code without flushing or adding newlines, I get a different amount of output every time I run it.

To be honest I'm not sure why anymore. I wanted to say that forking while there's still output in the buffer, will give the forked process a copy of the buffer, leading to duplicated output. That's true, but your code only prints after forking, so that shouldn't be a problem here (also the problem is that I (sometimes) get too little output when I run your code - not too much).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.