0

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

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.