#include
main()
{
     int ret;
     ret=fork();ret=fork();ret=fork();ret=fork();
     if(!ret)
         printf("sun");
     else
         printf("solaris");
}

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.

alaa sam commented: nice question it made revisit a lot of concepts , i thinks its fully answered now +3

Recommended Answers

All 9 Replies

The easiest way to answer your question is to just compile and run the program on a *nix computer.

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.
Errors

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:

fork();
fork();
fork();
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.

am i right 100% ?

Yes.

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).

I've answered a similar question before. It contains a diagram that may prove useful for you understanding of control flow.

ok

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.