Solved

2 child process tree handling

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries DB2 Query 2 97
substring method in java 1 129
sum28 challenge 31 115
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 826
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 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