in fact, using pipes is but one of many ways to exchange data between processes. I say processes, because fork()
is a system call that "splits off" a new process of your current process. Calling that new process "a fork" is thus a bit confusing, to me.
How to create a pipe will depend on your OS, but on unixoids/POSIX, see man 3 mkfifo
on how to create a named pipe. You can then open and read and write to that like to e.g. a character device, essentially.
Note that using fifos to communicate with multiple processes (like in your problem statement) can be especially inefficient – a read/write call on a FIFO blocks until both ends have opened the file, and thus, things might get a bit complicated at startup.
For this kind of communication between processes, other IPC (inter-process communication) mechanisms are far better suited – for example, System-V IPC (see man ipc
), or sockets, or things like zeroMQ.
I think you might be reading books/tutorials from the eighties – that might really not be the best thing to start with when learning multiprocessing on modern machines and OSes.
Furthermore, suppose I had an array of character pointers in the parent process, when a new process has spawned via fork, can this process access the array, or will it not exist?
Read the documentation of fork()
. The daughter process is an exact copy of the parent process (aside from the PID) at the time of fork
ing. Thus, yes, your forked processes will have a copy of the same data.
Notice how you misuse "access". Your daughter processes can't access any data of the parent process - that is the whole point of process segmentation; processes can't read or write other processes' memory.
If you had the misconception that the forked process could access the data of the parent process, you must revisit your material on which you base your learning of multiprocessing.