I am currently trying to communicate a parent process which should have multiple children process for each request, and wish to know how do I keep track of the number of children and check if any of the children have terminated?
I have tried using pipes to communicate by using the read() function to send back a message of termination. However, this somehow blocks all concurrency and waits for 1 child to complete and send back the termination message before handling the next process requests.
Is there a way to solve this? Or do I have to use methods like shared memory, sockets, etc…?
I am using C language, and I just need a way tracking how many children I have. I can increment a counter in the parent process, but when a child dies? how do get that message back and use it to decrement the counter in the parent?
On POSIX-compliant platforms, If you just wish to know when the child process exits and nothing else then you can use SIGCHLD signal.
SIGCHLDis the signal sent to a process when a child process terminates, You can handle it by writing your own signal handler. Its return value is the process id of the child process which exits.If you need to communicate between parent and child processes on a regular basis during the course of program execution then you will have to use some sort of Inter Process Mechanism(IPC), selection of which depends upon number of factors like performance,need of synchronization,need of serial communication etc.