I'm trying to make a program that does the following:
./run cmd1 arg1 : cmd2 arg2 : cmd3 arg3
allows me to run three commands for example in parallel using fork()
and execvp
and connecting the output of the cmd 1 to the input of the cmd 2 using socketpair
only.
My question is:
do i need to create multiple socketpairs for each command that i use or i can use the same socketpair, i just use it again and again ?
Thanks
You should create a separate socket-pair for each parent-and-child communications-link you need to use.
For example, your program might be structured something like this:
Process #1 creates socketpair(A,B)
Process #1 calls fork(), creating Process #2
Process #1 uses socket A to communicate with Process #2
Process #2 uses socket B to communicate with Process #1
Process #2 creates socketpair(C,D)
Process #2 calls fork(), creating Process #3
Process #2 uses socket C to communicate with Process #3
Process #3 uses socket D to communicate with Process #2
Note that in the above sample, Process #2 can't re-use socket B to communicate with Process #3, since it needs socket B to communicate with Process #1. Process #2 can't use socket A for anything, since socket A is reserved for Process #1 to use (if Process #2 tried to use it socket A, Process #2 would just be sending bytes to itself on socket B, which isn't a useful thing to do)