tags:

views:

89

answers:

3

why is it ok for a reader to exist when there are no writers but not ok for a writer to exist when there are no readers in pipes?

. Is it because the reader is meant to wait so it's ok if there is no writer whereas a writer is ready with data and it is not known how long it has to wait even though it has data ready.

. Is it because the writer's file descriptor can be misused by readers( I'm not clear how)

+1  A: 

It's because the error condition is triggered by output. So a reader with no writers just sits there, not bothering anything, because there's no output that's trying to go someplace and can't. A writer with no readers tries to send its output, can't, and errors.

chaos
A: 

In the case of a reader, it will immediately block (sleep) because there's nothing to read. If to writer starts the reader continues to sleep, and no harm done.

For a writer, it would fill up the buffers and block. If no reader came along it would be a pure waste of system resources.

FYI, the above is an educated guess.

dwc
+4  A: 

You must be talking about some specific implementation of pipes.

[Proc 1]
$ mkfifo /tmp/mypipe
$ echo "No Boom Here" > /tmp/mypipe
<process blocks>

[Proc 2, later]

$ cat /tmp/mypipe
No Boom Here

So, it works fine on Unix systems, you can read or write a pipe without readers or writers. However your process will block until the companion sigs up.

Perhaps this is a Windows thing?

As an aside, the Unix way is the proper behavior, IMHO. It should just block either way.

Will Hartung