473,378 Members | 1,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to wait for *all* children to return ?

Question: write a program which creates 5 processes (in addition to
itself). One of these processes must display 1, another must display
2
.... the last one displays 5. The parent process waits until all other
processes are finished, then returned.

My solution is that

/* Header files omitted */

#define NUMBER_PROCESS 5

void sig_chld(int sig)
{
pid_t pid;
int stat;

while ((pid = waitpid(-1, &stat, WNOHANG)) 0)
{
;
}
signal(SIGCHLD, sig_chld);
}

int main(int argc, char *argv[])
{
pid_t pid = getpid();
int i;

signal(SIGCHLD, sig_chld);

for (i = 0; i < NUMBER_PROCESS && pid 0; i++)
{
pid = fork();
if (pid < 0)
{
perror("Error fork");
exit(-1);
}
else if (pid == 0)
{
printf("%d\n", i+1);
}
}/* for i */

return 0;
}

Should I add a for loop outside the while loop in sig_chld function
to
make sure that exactly 5 child termination are captured?

May 17 '07 #1
1 2277
loudking wrote:
Question: write a program which creates 5 processes (in addition to
itself). One of these processes must display 1, another must display
2
... the last one displays 5. The parent process waits until all other
processes are finished, then returned.
Answer: fork etc are not part of Standard C. Your likely best bet
is comp.unix.programmer for questions about child support.

PS
while ((pid = waitpid(-1, &stat, WNOHANG)) 0)
{
;
}

That's a lot of room for a no-op. What's wrong with `{}` either
after the `)` or underneath the loop test?

PPS

You never use `pid`. So there's no point in assigning to it.
The you can dispose of the redundant brackets round the
condition.

PPS

You never use the value of `stat`. I wonder if there's a way
in which you can avoid having a `stat` variable at all?

PPPS

You're clearly using some extensions to C, but is it really necessary
to use `exit(-1)` rather than `exit(EXIT_FAILURE)` or even -- since
we're conveniently inside `main` -- `return EXIT_FAILURE`?

--
"He's dead, Jim, but not as we know it." Unsaid /Trek/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Naomi | last post by:
hello Could anyone please tell me if there is any way i can have the text "Menu Loading Please Wait..." appear while the js menu loads and then disappear after it loads ? hoping to hear some...
47
by: Jeff Relf | last post by:
Hi All, I plan on using the following C++ code to create nodes with unlimited children: // I would like to declare NodeT like this, // but it won't compile because Lnk_T is not defined yet....
22
by: codefixer | last post by:
Hi, I have a situation where I have to handle the following scenario. The main() must wait for child to complete or the main() must kill the child after 3 seconds and exit. /* Assume...
9
by: Roger Down | last post by:
Lets say I have a method UpdateCache() called from a single thread. I also have a method GetCache() called from multiple threads. When UpdateCache() is called, the cache updating is being...
6
by: Rob | last post by:
Hello, I'm sure this has come up before. I have need for a collection of all elements/objects in an HTML document that have any kind of an attribute (HTML or CSS) that is making use of a URL to...
3
by: loudking | last post by:
Question: write a program which creates 5 processes (in addition to itself). One of these processes must display 1, another must display 2 .... the last one displays 5. The parent process waits...
1
by: loudking | last post by:
Question: write a program which creates 5 processes (in addition to itself). One of these processes must display 1, another must display 2 .... the last one displays 5. The parent process waits...
22
by: Jason Zheng | last post by:
This may be a silly question but is possible for os.wait() to lose track of child processes? I'm running Python 2.4.4 on Linux kernel 2.6.20 (i686), gcc4.1.1, and glibc-2.5. Here's what happened...
3
by: RedWiz | last post by:
Hi i have to develop a multihreaded php application on linux, then through pcntl_fork and wait. I tried it, but there something going wrong, i think. The difference whit other languages like c...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.