(I just suppose we are talking about Linux here)
As you probably learned, fork() itself will simply duplicate the calling process, it does not handle IPC .
From the fork manual:
fork () creates a new process, duplicating the calling process. The new process, called the child, is an exact duplication of the calling process, called the parent.
The most common way to handle IPC when you fork () is to use channels, especially if you want "private communication with every child." Here's a typical and simple usage example, similar to the one you can find in the pipe manual (return values ββare not checked):
#include <sys/wait.h>
The code is pretty clear:
- Parent Forks ()
- The child reads () from the pipe until the EOF
- Parent writes () to the phone, then closes () he
- Dates were shared, cheers!
From there, you can do whatever you want; just remember to check your return values ββand read the dup , pipe , fork , wait ... instructions, they come in handy.
There are also many other ways to exchange data between processes, they are of interest to you, although they do not meet your "private" requirement:
or even a simple file ... (I even used SIGUSR1 / 2 signals to send binary data between processes once ... But I would not recommend this haha.) And probably something else I'm talking about now I donβt think so.
Good luck.
source share