Communication between parent process and multiple children

I am currently trying to pass a parent process that needs to have multiple child processes for each request, and want to know how I can keep track of the number of children and check if any of those children have terminated?

I tried to use channels to communicate with a function read()

to send a completion message. However, this somehow blocks all concurrency and waits for 1 child to complete and send a completion message before handling further process requests.

Is there a way to solve this? Or do I need to use methods like shared memory, sockets, etc ??

I am using the C language and I just need to keep track of how many children I have. I can increment the counter in the parent process, but when does the child die? how to return this message and use it to decrement the counter in the parent?

+3


source to share


1 answer


On POSIX compliant platforms. If you just want to know when the child process exits and nothing else, you can use SIGCHLD . is the signal sent to the process when the child process exits, you can handle it by writing your own signal handler. Its return value is the process ID of the child process that is shutting down.
SIGCHLD



If you need to regularly communicate between parent and child processes during program execution, you will have to use some kind of Inter Process (IPC) Mechanism , the choice of which depends on a number of factors such as performance, need for synchronization, need for serial communication, etc.

+2


source







All Articles