Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

EOF on pipe
This problem occurs on both Free BSD and Linux.

I have a parent-child situation, where the parent creates two pipes,
and then forks(), then the child exec's a sort.  The parent then reads
the pipe to get the sorted output. SO...

parent:
establishes 2 pipes pfd1 and pfd2 which have both read and
write ends.  Parent writes records to The write end of the
pipe connected to STDIN of the child.

child:
dup2() the pipe ends, the read end of pfd1 to STDIN and the write end
of pfd2 to STDOUT.  The child then execlp() /usr/bin/sort -n.

Sort reads the input, does it's thing, then writes the output.  The
parent reads the pfd2 read end, and manipulates the sorted output.

The problem is, I never see the EOF on the sorted output.

I found that the read loop in the parent could terminate on a
waitpid() or by using a signal handler for SIGCHLD, and a variable
shared between the signal handler and the parent code, I could
terminate the read loop.  But I never saw the read() terminate with a
count of 0 indicating EOF.

Since this situation behaved uniformly over Linux, FreeBSD current,
and dragonfly-BSD, I suspect I am running into a subtlety that is
architected.

Anybody got any clues for me?

--
Derek Tattersall   | Nothing cures insomnia like the realization that
| it's time to get up.
dlt@mebtel.net     |
|
dlt666@yahoo.com   |

Report this thread to moderator Post Follow-up to this message
Old Post
Derek Tattersall
10-19-04 01:56 AM


Re: EOF on pipe
In article <868ya3hjnn.fsf@lapdog.arm.org>,
Derek Tattersall <dlt@mebtel.net> wrote:

> This problem occurs on both Free BSD and Linux.
>
> I have a parent-child situation, where the parent creates two pipes,
> and then forks(), then the child exec's a sort.  The parent then reads
> the pipe to get the sorted output. SO...
>
> parent:
>         establishes 2 pipes pfd1 and pfd2 which have both read and
>         write ends.  Parent writes records to The write end of the
>         pipe connected to STDIN of the child.
>
> child:
> dup2() the pipe ends, the read end of pfd1 to STDIN and the write end
> of pfd2 to STDOUT.  The child then execlp() /usr/bin/sort -n.

> Sort reads the input, does it's thing, then writes the output.  The
> parent reads the pfd2 read end, and manipulates the sorted output.
>
> The problem is, I never see the EOF on the sorted output.
>

You need to make sure the parent and child close the ends of each
pipe they are not using.  The read end of the pipe will not return EOF
until all of the write ends have been closed.

Cheers,
- jonathan

P.S. You should note that this is generally a dangerous construct (since
you can easily deadlock, due to the limited kernel input buffers), but
since sort(1) usually has to read all of its input before having any
output, it's not a problem in this case.

Report this thread to moderator Post Follow-up to this message
Old Post
Jonathan Adams
10-19-04 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:56 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.