简体   繁体   中英

How to wait for ALL child processes to terminate and get each exit status

So I've done a bit of research but not a lot of it seems to fit my situation or I'm just not understanding it.

I basically have a parent process that forks - it makes a child.. now this child will also fork at some point.

I want the parent process.. the main parent to be able to catch the exit status of ANY children (the child of the first fork and ANY of its children when it forks)

I basically have any process exit if something goes wrong.. and I want the main parent to receive this exit status.. and if it exited because of an error i can catch it with like an if statement and print something out.

Currently.. I can catch if the first child exits with and error and get the right exitstatus.. but any of its children it wont work. Not sure if I have to use a weird version of waitpid or what.

How do I do this?

Call wait system call (or waitpid(-1, &status, 0) ). Or wait on some process group ID (since all your indirect children share the same process group, unless they change it.). Perhaps create your own process group with setpgid

And you get the SIGCHLD signal when a child exits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM