[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

2 child process tree handling

Hi,

I've created a tree where each process spawns 2 children using fork and recursive function calls.  I assigned a node number to each process and displayed it to screen (which I've done).  The goal is to print the node number from highest to lowest, meaning that each process with a lower number must wait for the process that is one number higher to print (and exit) before it can print.  The problem is that the process are all finishing in random order meaning that the nodes displayed are pretty random.  I've used wait() to let the parent process wait for the child process to finish before displaying its node number, but wait doesn't work for adjacent (same depth) processes (or does it?).  What technique can I use to control the sequence of nodes displaying to the screen?  I've tried using a global variable to keep track to the current node to display (by decrementing it after each process finishes and sent to display), but the problem is that I can't have each process loop continuously in order to compare its node number against the global variable, without hanging the program entirely.  Any suggestions?

Thanks.

Ken
0
kenpong1
Asked:
kenpong1
  • 3
1 Solution
 
sunnycoderCommented:
>wait doesn't work for adjacent (same depth) processes (or does it?).  
No it doesn't and there is no way to tell the system to do that.... there can be work arounds though

one way that I can think of is that
pids are alloted in increasing order
maintain pids of all children in a sorted array which is accessible to all children
a child should exit only if all the higher pid number children have exited

so declare a a struct like

struct mystruct {
    int exited;
    pid_t proc_pid;
};

struct mystruct array[MAX_CHILDREN];

this array is sorted on proc_pid field ... if corresponding exited value is 0 then process has not been created
if 1, then it is active
if 2 then it has exited

each child would make its corresponding entry 1 as soon as it is created and will make it 2 before exiting

one pitfall in this case is when pids wraparound... ie when max pid is reached and system again starts allocating lower pid ... you can write special case code to determine when such event occurs and handle it accordingly
0
 
sunnycoderCommented:
since in your case, node numbers are more important, use node numbers in the struct in place of pids and your problem becomes vastly simple and straight forward (no special cases)

Cheers :o)
0
 
kenpong1Author Commented:
If a process with a lower node number waits for a node with a higher number to exit (without using wait command) before it exits, wouldn't that require a continuous loop to monitor the array?  And if it does monitor the array using a loop, wouldn't that cause the program to just hang?

Thanks.

Ken
0
 
sunnycoderCommented:
not necessarily.... depending on how fast your children exit, you can induce sleep

while (there is child for which I should wait)
      sleep(1);

this will check every one second, hence program will not hang
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now