So, today we were being taught about forking (with respect to Operating Systems :P), and some of my mates seem confused.
The question that was asked in the class was what would be a tree structure of processes when the following code is run:
(You could assume first process to be P1, and then P2, and so on..)
The tree structure that is formed looks like this :
Now, imagine this situation :
I am a robber, and I know how to break their inner locks and security and want to rob a bank. Now, I can’t do this alone, because there’s a high probability I’ll get caught, right? So, I enter the bank, and while entering, I befriend the watchman. And, I take his phone number.
Then, I get inside, and normal banking stuff is going on, people roaming here and there and bank employees doing their boring job. Now, I wanna get into the locks, but I obviously don’t have access. So, I befriend the key-manager of the bank (telling him I’ll give him his share), and take his phone number(you know? Just in case, I want some help). All right. Now, I’m alone and reached to the innermost core area. Phew! The major part is done.
But, Oh No! I find another robber there trying to rob the bank. And, instead of one of us fighting and taking the whole portion, I decide to do partnership and hence befriend him (and, whenever I befriend someone, I take their number), and we rob it together.
This is exactly what’s happening in above code.
I’m the first process – P1.
Watchman – P2
Key-manager – P3
Other robber – P4
Instead of creating child, I’m befriending.
And, instead of taking their pid’s, I’m taking their phone numbers.
Now, here’s a short explanation :
P1 gets created due to the program. Then, it goes inside the main function. The first condition of first if creates P2, and now since forking is done, a value is returned to both process P1 and P2. P1 gets value [pid of P2] (which is non-zero obviously), and P2 gets a value 0. [Note: this is just a return value and not their pids] Since, the value P1 gets is non-zero, it gets to go inside the first if, however P2 is debarred.
P1 gets inside the first if and again goes through a similar road, wherein it creates another child process, and say we call it P3. [Note here, that P2 is still standing outside. He wasn’t allowed in.] So, P1 now has two children, one is P2 and another P3. P3 gets a return value 0, and P1 gets a return value as [pid of P3]. [pid of P3] is non-zero, so, P1 passes the second if condition as well.(However, P3 doesn’t pass it)
P1 gets to the inside, and we see another fork. So, it creates another child – P4.
And, then, we come out of it. Hence, we have the tree structure.
So, now you have understood this much. Good. Let’s check how much you have learnt.
Let me add some printf statements in between.
Now, here’s the output for this (that I get on my machine) :
after first fork. pid : 6726
after second fork, pid: 6726
getting out of if, pid: 6728
outermost, pid: 6728
after third fork, pid: 6726
getting out of if, pid: 6726
outermost, pid: 6726
outermost, pid: 6727
after third fork, pid: 6729
getting out of if, pid: 6729
outermost, pid: 6729
Now, what I want you do to is, try to see if you can explain the output according to the program….without scrolling down. I’ve put the explanation in the end, obviously, but don’t see it until you’re having difficulties explaining the above output.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Explanation:
Think in a different way. Divide it as 5 printf statements. We have 5 printf statements above. Lets see which process can execute which ones.[conceptually think.]
– [after first fork] : printf statement is inside first if. So, only P1 gets inside here. ANd no other process has been created but P1 and P2. So, only once it should occur in the output.
– [after second fork] : this printf statement is inside the second if. Till this point, P1, P2, P3 have been created, but only P1 got the passes to get in. So, only that’ll execute. Hence we should see this once only.
– [after third fork] : printf statement is inside the second if but after a fork. Now, P1 and all of its three childs have been created. But only P1, and P4 can execute this. So, we should see this twice in the output.
NOTE at this point, that P4 would also be going out of the loop and executing the instructions after the innermost if scope ends…till the program ends.
– [getting out of if] : We’re out of innermost if. P1 and P4 have come out, so they’ll execute it. And, The current place is inside the first if, so only P2 was not allowed here. Even P3 will execute this, even though it wasn’t able to get inside the innermost loop. => 3 executions we should observe in the output.
– [outermost] : If you understood all of the above, you should know that there’s no barrier on this one, and it’s like a public method which everyone can execute. 🙂
Note : order in which they’ll appear is something you should not worry about. At least not till the time you wanna change your kernel. So, just see the number of times a printf should appear is correct or not.
Which, if you observe above, is correct above.
……………………………………….
If you found the post useful, share it on your social networks and spread the knowledge. Also, if you find something wrong in the above post, let me know in the comments, so we don’t spread any misconceptions.
Also subscribe to the blog through your email from the subscribe link on the right panel if you want to follow all updates from my blog.
Oh yes, credits to Stuti Rastogi for the code above (with printfs) and thanks to Lucy ma’am without which I wouldn’t have understood anything, and this post hadn’t been possible.
September 22, 2014 at 11:12 pm
awesome post..bhavul
https://www.youtube.com/watch?v=I_UzTTULNxE
check this out too