Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

2 child process tree handling

Posted on 2003-11-08
4
Medium Priority
?
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 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