Solved

2 child process tree handling

Posted on 2003-11-08
4
278 Views
Last Modified: 2013-12-26
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
Comment
Question by:kenpong1
  • 3
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9707281
>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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9707298
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
 

Author Comment

by:kenpong1
ID: 9707449
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 9709656
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now