How to send a message from parent process to multiple fork children?
I have a server that allows clients to connect and I am using select()
to manage new connections or incoming messages from already connected clients. For every 2 clients connected I create a child using fork()
, remembering their file descriptors.
I want the child processes to wait for parent to send information along with the file descriptor of the socket associated with the client that sent that information. All the children read this and only the child associated with that client performs a certain task with that information while the rest of them go back to waiting to read a new message from parent (the other child does this as well after finishing that task).
My idea would be to write the message in a fifo file, copied on newlines by
nr_of_children
times (otherwise only one child could read as the information is consumed) and to have the child processes read from it, using a lock on the fifo so only one process reads a line at a time.
Is there a better way to do this, and if not, is my idea going to do the job?
Thank you.