0

I am trying to write a program using pipes and forks in C. I am a bit confused as to how to accomplish this.

I need to create an n number of processes based on user-specified input (that is, if the user enters 2, I will create 2 processses via a fork). Each new process created via fork, then, does some stuff.

However, now pipes come into the picture. I have pipes to get information back from each fork. My question is - how do I create multiple pipes, for each fork? If I have multiple forks, how can I handle multiple pipes from the parents perspective?

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?

Thanks!

TimelordViktorious
  • 336
  • 3
  • 10
  • 20

2 Answers2

3

Use pipe(), no need to have to handle any filesystem naming.

You can find a pipe() example there http://man7.org/linux/man-pages/man2/pipe.2.html for Linux, but it should be almost the same for any *nix.

Basically, you use pipe() to create a pair of connected fd. Then you fork() and each process after the fork, which inherited the pipes pair, closes one (uneeded) side of the pipe. You now have the two processes connected, each having one side of the pipe (the remaining fd).

If you have to handle a lot of them later, the parent "master" process should use something like select() or poll() and wait for any child to have data available (meaning a child wrote something). There's also a select() example.

Keep also in mind that a pipe is unidirectional: if you need to send data to the child after the fork(), you should create two pipe-pairs. You could also change tools and use socketpair(): same general usage, but semantic is closer to network usage, and it's bi-directional.

Didn't see last part about arrays. As is saying an other answer, the forked process will have a copy of the array. If it was used to pass data telling what work to do it's fine, but ff the parent has to give further informations after the fork() this array isn't the parent's anymore.

If you really need to pass large amounts of data between parent and child after the fork(), you can also use some shared memory mechanism. Example here with mmap(): How to share memory between process fork()?

Community
  • 1
  • 1
A.B
  • 376
  • 4
  • 11
1

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 forking. 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.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • It's for an assignment, sadly. My assignment requires me to use pipes and forks. I am using forks to create a new process. Each process will read, from a set of files, some data, process that data, then give it back to the parent process. I'm doing this via conventional pipes. The issue I'm having is I need to know what files to give to the process (and that depends on the number of processes I make, since if I have an array of size N, and I have 2 processes, then each process takes half of the array). – TimelordViktorious Nov 21 '16 at 01:03
  • Ah, I got you. I have a *copy*, so I access the *copy*, not the native data. How is malloc'd data copied? – TimelordViktorious Nov 21 '16 at 01:09
  • it's copied. there's no "how". – Marcus Müller Nov 21 '16 at 01:10
  • OK. I think that is an important distinct. I understood (from my readings) some things that may not have made sense to me at first. I'll go back and revise. – TimelordViktorious Nov 21 '16 at 01:11
  • (in fact, the question of how and if to do memory deduplication is an extremely OS, machine, architecture, security requirement and situation-dependent question, and you simply get something that is in any relevant way an independent copy.) – Marcus Müller Nov 21 '16 at 01:11
  • I guess my confusion is how to read from multiple pipes... – TimelordViktorious Nov 21 '16 at 01:15
  • open them like files. Read from them like from multiple files. – Marcus Müller Nov 21 '16 at 01:17