I need some way for the parent process to communicate with each child separately.
I have some children that need to communicate with the parent separately from the other children.
Is there any way for a parent to have a private communication channel with each child?
Also can a child for example, send to the parent a struct variable?
I'm new to these kind of things so any help is appreciated. Thank you
(I'll just assume we're talking linux here)
As you probably found out,
fork()
itself will just duplicate the calling process, it does not handle IPC.The most common way to handle IPC once you forked() is to use pipes, especially if you want "a private comunication chanel with each child". Here's a typical and easy example of use, similar to the one you can find in the
pipe
manual (return values are not checked):The code is pretty self-explanatory:
From there you can do anything you want; just remember to check your return values and to read
dup
,pipe
,fork
,wait
... manuals, they will come in handy.There are also a bunch of other ways to share datas between processes, they migh interest you although they do not meet your "private" requirement:
or even a simple file... (I've even used SIGUSR1/2 signals to send binary datas between processes once... But I wouldn't recommend that haha.) And probably some more that I'm not thinking about right now.
Good luck.