Monday, May 14, 2012

Uneven tree of processes using fork()

I have to construct a tree of processes using fork() in C. I get a sequence of numbers from standard input (for example: 1 5 0 3) and those numbers tell me how many children each node has. If we take the example then the root process creates 1 child, then this one child creates 5 children of its own, then from those 5 children the first one doesn't create any children, the second one creates 3 of them and then we're done. After this is complete the root process calls pstree that draws out the tree.



Here is a picture of the example:





My question is how can I make new children from a specific node? One needs to create 0 new processes and the next one needs to create 3 of them. I don't know how to distinguish so that only that specific child makes new children and not all of them. Also I'm not sure how to use pstree, because the tree is normally already gone when pstree gets called. I know I can wait() for children to execute first but last ones do not have any children to wait for so they end too fast.





No comments:

Post a Comment