Do I have to make a new pipe for every pair of processes in C?

泪湿孤枕 提交于 2021-01-29 01:40:12

问题


If I have 4 processes that I want to pipe:

process1  |  process2  |  process3  |  process4

do I have to make 3 individual pipes likes this

int pipe1[2];
int pipe2[2];
int pipe3[2];

or can I somehow recycle pipe names like in this pseudocode:

int pipe1[2];             // we use ONLY two pipe names: pipe1
int pipe2[2];             //                         and pipe2

pipe(pipe1);              // getting 2 file descriptors here
pipe(pipe2);              // and 2 here

for process=1 to 4
  if (process==3)         // getting 2 new file descriptors for
     pipe(pipe1);         // process3|process4 (reusing pipe1)

  fork()                  // forking here
  if (child 1) then
    use pipe1
  if (child 2) then
    use pipe1
    use pipe2
  if (child 3) then
    use pipe2
    use pipe1             //the pipe1 that we re-pipe()ed
  if (child 3) then
    use pipe1             //the pipe1 that we re-pipe()ed

Would this work? I am not sure if repiping pipe1 will have an impact on the previous forked processes that used pipe1.


回答1:


Short answer: no, "repiping" pipe1 will not have an impact on the previous forked processes that used pipe1, but you're better off declaring 3 pipes and pipe()'ing before fork()'ing.

Long answer: To understand why, let's first see what happens when you create a "pipe", and then what happens when you "fork" a process.

When you call pipe(), it "creates a pipe (an object that allows unidirectional data flow) and allocates a pair of file descriptors. The first descriptor connects to the read end of the pipe; the second connects to the write end." (This is from the man pipe page)

These file descriptors are stored into the int array you passed into it.

When you call fork(), "The new process (child process) shall be an exact copy of the calling process" (This is from the man fork() page)

In other words, the parent process will create a child process, and that child process will have it's own copy of the data.

So when child 3 calls pipe(pipe1), it will be creating a new pipe, and storing the new file descriptors in it's own copy of the pipe1 variable, without modifying any other process's pipe1.

Even though you can get away with only declaring two pipe variables and just calling pipe() in child 3, it's not very easy to read, and other's (including yourself) will be confused later on when they have to look at your code.

For more on fork()'s and pipe()'s, take a look at http://beej.us/guide/bgipc/output/html/multipage/index.html




回答2:


The way I've done it in the past, and the way I would do it again, was to not reuse pipes and end up with N-1 pipes. It would also depend on whether or not you want to have more than two process running at the same time communicating, if so then you'd obviously have problems with reusing 2 pipes.




回答3:


You need one pipe, and thus one call to pipe(), for each | character in your command.

You do not need to use three separate int [2] arrays to store the pipe file descriptors, though. The system does not care what variable you store the pipe file descriptors in - they are just ints.



来源:https://stackoverflow.com/questions/7870006/do-i-have-to-make-a-new-pipe-for-every-pair-of-processes-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!