Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!csn!ccncsu!purdue!bu.edu!snorkelwacker.mit.edu!apple!mips!swrinde!elroy.jpl.nasa.gov!usc!wuarchive!udel!princeton!phoenix.Princeton.EDU!tty!keyboard!subbarao!kartik From: subbarao@SunOS.Princeton.EDU (Kartik Subbarao) Newsgroups: comp.unix.questions Subject: Re: pipes in unix Message-ID: Date: 5 May 91 01:53:05 GMT References: <37580001@hpopd.pwd.hp.com> Sender: news@idunno.Princeton.EDU Reply-To: subbarao@phoenix.Princeton.EDU (Kartik Subbarao) Lines: 56 In article <37580001@hpopd.pwd.hp.com> dcc@hpopd.pwd.hp.com (Daniel Creswell) writes: >I'm trying to suss how pipes between processes work (hey I dont get to play >with UNIX all day so I'm a bit dumb!) and came across the following exmaple: > >The following example uses pipe to implement the command >string "ls | sort": > > #include > pid_t pid; > int pipefd[2]; > > /* Assumes file descriptor 0 and 1 are open */ > pipe (pipefd); > > if ((pid = fork()) == (pid_t)0) > { > close(1); /* close stdout */ > dup (pipefd[1]); > execlp ("ls", "ls", (char *)0); > } > else > if (pid > (pid_t)0) > { > close(0); /* close stdin */ > dup (pipefd[0]); > execlp ("sort", "sort", (char *)0); > } > >What I wanna know is how does this work. I've looked up 'dup' and discovered >that it duplicates a descriptor which will be carried across an 'exec' but >dont understand how the recipient program knows its got a pipe. Does it know? No, it doesn't know. Rather, it doesn't care. The typical unix pipe-tools just take standard input, perform some operation on it (sort in this case, and print their results to stdout. So, what's happening here is that you're dup'ing the read end of the pipe to sort's stdin, so when it reads from 0, it reads from the pipe. >Is it simple that dup replaces stdin and stdout if so why? because it doesn't >seem to do that in the above code? > >I know I'm missing something here...would some kind person please tell me what >it is? I hope you have a main() that's surrounding this code. Otherwise, I can see no problem. -Kartik -- internet# rm `df | tail +2 | awk '{ printf "%s/quotas\n",$6}'` subbarao@phoenix.Princeton.EDU -| Internet kartik@silvertone.Princeton.EDU (NeXT mail) SUBBARAO@PUCC.BITNET - Bitnet