Solved

2 child process tree handling

Posted on 2003-11-08
4
279 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 75
Named range not carried over 10 62
ShiftLeft challenge 21 76
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 250
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

16 Experts available now in Live!

Get 1:1 Help Now