2 child process tree handling
Posted on 2003-11-08
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?