1,105,594 Community Members

o/p question

Member Avatar
nitin1
Practically a Master Poster
655 posts since Sep 2012
Reputation Points: 15 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 4 [?]
 
0
 
#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.

Member Avatar
Ancient Dragon
Achieved Level 70
27,645 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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

Member Avatar
nitin1
Practically a Master Poster
655 posts since Sep 2012
Reputation Points: 15 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 4 [?]
 
-1
 

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

Member Avatar
Ancient Dragon
Achieved Level 70
27,645 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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.

Member Avatar
sepp2k
Posting Pro in Training
463 posts since Jul 2012
Reputation Points: 300 [?]
Q&As Helped to Solve: 97 [?]
Skill Endorsements: 23 [?]
 
3
 

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

Member Avatar
sepp2k
Posting Pro in Training
463 posts since Jul 2012
Reputation Points: 300 [?]
Q&As Helped to Solve: 97 [?]
Skill Endorsements: 23 [?]
 
0
 

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.

Member Avatar
nitin1
Practically a Master Poster
655 posts since Sep 2012
Reputation Points: 15 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 4 [?]
 
-1
 

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

Member Avatar
sepp2k
Posting Pro in Training
463 posts since Jul 2012
Reputation Points: 300 [?]
Q&As Helped to Solve: 97 [?]
Skill Endorsements: 23 [?]
 
1
 

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

Member Avatar
L7Sqr
Veteran Poster
1,006 posts since Feb 2011
Reputation Points: 179 [?]
Q&As Helped to Solve: 168 [?]
Skill Endorsements: 11 [?]
 
0
 

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

Member Avatar
lalitha2294
Newbie Poster
12 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

ok

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article