![]() |
|
|
|||||||
| Home | Forums | Register | Rules & FAQ | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
Other UNIX.COM Threads You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple Question | aforball | UNIX for Dummies Questions & Answers | 3 | 03-04-2008 12:26 PM |
| pipe | question | convenientstore | Shell Programming and Scripting | 12 | 06-09-2007 02:49 PM |
| Simple C question... Hopefully it's simple | Xeed | High Level Programming | 6 | 12-15-2006 10:29 AM |
| Question in reference to the pipe | | elkoreanopr | UNIX for Dummies Questions & Answers | 3 | 10-03-2006 09:39 AM |
| Ok simple question for simple knowledge... | Corrail | UNIX for Dummies Questions & Answers | 1 | 11-28-2005 09:03 AM |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
simple (probably) pipe question
I'm new to programming with pipes, sockets, etc., and after seeing a particular code example that uses a pipe, I have a question. Here's the code example (which does the same as typing "ls | wc -l" at the command line):
int main(void) { int pfds[2]; pipe(pfds); if (!fork()) { close(1); /* close normal stdout */ dup(pfds[1]); /* make stdout same as pfds[1] */ close(pfds[0]); /* we don't need this */ execlp("ls", "ls", NULL); } else { close(0); /* close normal stdin */ dup(pfds[0]); /* make stdin same as pfds[0] */ close(pfds[1]); /* we don't need this */ execlp("wc", "wc", "-l", NULL); } return 0; } At first I was wondering what the file descriptor was after the 'dup' calls, but I guess that with the child, it would be stdout since 1 was just closed and 1 would be the next valid file descriptor in line (and similar with the parent). What I'm wondering about is that with the 'fork', there are now two processes running at the same time, and it seems like the output of this program wouldn't always be the same, that the child lines of code would have to run before the parent lines of code. Maybe someone could set me straight. Thank you. |
| Forum Sponsor | ||
|
|
|
|||
|
One other thing I'd like to ask about concerning this example is about how each process writes/reads to its appropriate end of the pipe (since no 'write' or 'read' is specifically being called). With the parent, I guess since 1, stdin, was closed, its 'execlp' call will go to stdin and with the child, since it closed stdout, its 'execlp' will go to stdout. But wouldn't this also depend on time-slicing, since what if the child closed 0, then the processor switched to the parent and it closed 1 and its 'dup2' call would find 0 to be the lowest file descriptor available.
|
|
|||
|
If you read Perderabo's post carefully you would not be asking this question. The scenario you are describing cannot happen since dup2 will do what you tell it to instead of picking the lowest file descriptor available and assigning it to pfds. The notion of parent and child process is relative as this is decided only by the return value of the fork() function as to which process becomes the parent and which the child. In your case the "ls" process is the child process (though a small code change can swap this around) while the "wc -l" is the parent process.
|
|||
| Google UNIX.COM |