Go Back   The UNIX and Linux Forums > Top Forums > Programming
.
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Dec 2006
Posts: 29
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.
Sponsored Links
  #2 (permalink)  
Old 04-23-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,148
Good observation! Either parent or child may run first. In a multiprocessor system, they might even run somewhat simultaneously. Should the child run first, it will quickly try to read from the pipe and block until data is available in the pipe. The parent will write to the pipe, but if the pipe fills, the parent will block until the child reads some data out of it (thus freeing up space in the pipe). So the pipe is syncronizing everything.
  #3 (permalink)  
Old 04-24-2008
Registered User
 

Join Date: Dec 2006
Posts: 29
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.
  #4 (permalink)  
Old 04-24-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 759
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Simple Question aforball UNIX for Dummies Questions & Answers 3 03-04-2008 04:26 PM
pipe | question convenientstore Shell Programming and Scripting 12 06-09-2007 06:49 PM
Simple C question... Hopefully it's simple Xeed Programming 6 12-15-2006 02:29 PM
Question in reference to the pipe | elkoreanopr UNIX for Dummies Questions & Answers 3 10-03-2006 01:39 PM
Ok simple question for simple knowledge... Corrail UNIX for Dummies Questions & Answers 1 11-28-2005 01:03 PM



All times are GMT -4. The time now is 02:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0