c - Confusion with dup2(), exec() and pipes -
i have been struggling understand concept involving commands dup2(), exec() , pipes in conjunction.
the thing trying achieve:
pipe output of program x input of program y.
something basic who | sort
with parent , 2 children, children responsible executing programs , parent passing programs children.
here don't understand pipes:
p1) pipes treated files , should unidirectional, prevents me using 1 pipe multiple unidirectional communication channels ? so, let's have pipe1 , 3 processes (p - parent - c1, c2, children) have pipe open forking. of these processes use file descriptors. let's assume doing correctly, closing unused pipe ends, p writes c1. what issue using pipe communication between c1 , c2again? while writing question, idea hit me: there issue reads while many processes may have open simultaneously (two processes blocking read), i.e. system cannot sure wants read buffered data written it? if so, how implemented in system ?
i try understand concept, please bear me.
to apply question real life here pseudocode dealing with:
p:
pcloses unneeded read end ofpipe1psends program argument ('who')c1viapipe1pcloses write endpwaits children exit
c1:
c1reads argument read end ofpipe1c1dup2()s standard out write end ofpipe1c1closes both ends ofpipe1(because duped already)c1execvp()s program ('who')
c2:
c2dup2()s read end ofpipe1stdingets input program executedc2closes both ends ofpipe1c2waits input onstdinofc1dupedpipe1c2execvp()s program ('sort') input
now, if described above, have no luck. if introduced pipe
pipe2 looks this: p:
pcloses both ends of unneeded pipepipe2pcloses unneeded read end ofpipe1psends program argument ('who')c1viapipe1pcloses write endpwaits children exit
c1:
c1closes read end ofpipe2c1reads argument read end ofpipe1c1dup2()s standard out write end ofpipe2c1closes write end ofpipe2c1closes both ends ofpipe1--pipe2,pipe1redundant in childc1execvp()s program ('who')
c2:
c2dup2()s read end ofpipe2stdinc2closes both ends ofpipe1c2waits input onstdinofc1dupedpipe2c2executes programsortinput
is assumption correct pipes should not reused in multiple processes because system may not sure whom "serve" ? or there other reason ?
pipes designed one-to-one communication — 1 writer, 1 reader. while there nothing prevents having many readers , writers want, behavior makes not usable, multiple readers:
- reading pipe destructive operation: each byte sent through pipe read 1 of readers, whoever grabs first. if want broadcast information, need either use different ipc mechanism or explicitly replicate data (like
teecommand). - if there multiple writers, writes interspersed in unpredictable way. guarantee writes of size
pipe_bufor less atomic. - if number of writers drops zero, reader sees end-of-file condition.
in architecture you're describing, have 2 independent communication channels: p sends who c1, , c1 sends output of running who command c2. in shell script, similar to
echo | { read command; exec command; } | sort with echo who executed in original process rather in subshell.
your first proposal doesn't work because there's no way output of p go c1 , output of c1 go c2. it's still same pipe, output of p go c2 , output of c1 go itself, or mixture.
Comments
Post a Comment