The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Dec 2006
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-23-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,207
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 04-24-2008
Registered User
 

Join Date: Dec 2006
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #4 (permalink)  
Old 04-24-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 356
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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



All times are GMT -7. The time now is 06:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102