Process tree using fork()
7,405
OK I understand your question now. Your answer is correct. All I will say is that you can just do
fork()
for the final fork
s instead of
if (fork()) {}
else {}
Related videos on Youtube
Author by
Altair64
Updated on September 18, 2022Comments
-
Altair64 over 1 year
I am given the task of creating this process tree in C using
fork
,if
andelse
:|_____1___ |___2__ | |_4_ | |_3_ | | | | |
Or as a
pstree
like drawing:p(0)─┬─p(1)───p(3) ├─p(2) └─p(4)
Is this code correct?
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { if(fork()){ // 1 if(fork()){ // 2 if(fork()){ // 4 } else{} } else{} } else{ if(fork()){ // 3} else{} } pause(); return 0; }
-
gardenhead over 8 yearsThe question is unclear. This is clearly a homework problem: what were you give, and what do you have to find?
-
Altair64 over 8 yearsHmm - i need to programme this process tree using only fork()
-
joepd over 8 yearsHi. It does not help if you use a non standard way to make a graph. Have a look at the output of
pstree -ap
to see how PIDs and relations are normaslly displayed. Also: Can't you just print out the pid that is returned byfork
? You could usepstree
to check your assumptions. -
Altair64 over 8 yearsOk thank you. I think that our lecturer need to specify what he wants from us :) I have to create a process tree using fork() and if, else in C. The proc tree have to look like is shown above.
-
Altair64 over 8 yearsHere is similar problem but different process tree. stackoverflow.com/questions/24082775/…
-
Runium over 8 yearsAdded a
pstree
like drawing. Is it correct? If not edit question and remove it.
-
-
gardenhead over 8 yearsCould you elaborate The order of what?
-
gardenhead over 8 yearsNo it can't. Besides the numbers don't matter -- only the structure of the tree.
-
Runium over 8 yearsGuess we're talking past each other. The point is that there is no guarantee 3 is forked before 4.
-
gardenhead over 8 yearsI don't think that diagram is meant to have a timeline to it. Whether 3 or 4 is forked first, the tree structure will be the same.
-
Runium over 8 yearsYes, guess not. But then there is those dangling lines. Are those the forks and the
pstree
drawing wrong? (I added that one but perhaps I should not have. Did it as comments does not support that kind of drawings ...) -
gardenhead over 8 yearsThe only way to know what exactly the original drawing was intended to represent is to ask the professor :D I think the lines are confusing and your
pstree
drawing is better. The best way to visualize a process tree is as, well, a tree! -
Runium over 8 yearsAch. The update seems to never arrive. Was looking forward to this ...