unbuffered pipe

O

Oliver

I'm using a pipe to talk to a forked child process -

I do not want any data to get lost if the child process exits
unexpectedly -

- so I was hoping to turn off buffering so that there was at most 1
piece of data (i.e. one line) in the pipe.

I thought if I did:

select(PIPEHANDLE);
$| = 1;

then my pipe would not do buffering, and I was expecting that that
would lead to blocking writes - i.e. if the data hadn't been read from
the pipe, then a write to the pipe would block until it was.

however - after doing the above - I find I can still write data to the
pipe - regardless of how much is read out of the other end.

could someone enlighten me as to what's going on here please?
thanks muchly,
Oliver.
 
B

Ben Morrow

Quoth (e-mail address removed) (Oliver):
I'm using a pipe to talk to a forked child process -

I do not want any data to get lost if the child process exits
unexpectedly -

- so I was hoping to turn off buffering so that there was at most 1
piece of data (i.e. one line) in the pipe.

I thought if I did:

select(PIPEHANDLE);
$| = 1;

then my pipe would not do buffering, and I was expecting that that
would lead to blocking writes - i.e. if the data hadn't been read from
the pipe, then a write to the pipe would block until it was.

however - after doing the above - I find I can still write data to the
pipe - regardless of how much is read out of the other end.

could someone enlighten me as to what's going on here please?

$| turns off perl's buffering of the data before it gets sent down the
pipe. The data is still buffered inside the pipe: AFAIK, there is no way
to prevent this.

Ben
 
B

Ben Morrow

Quoth Ben Morrow said:
Quoth (e-mail address removed) (Oliver):

$| turns off perl's buffering of the data before it gets sent down the
pipe. The data is still buffered inside the pipe: AFAIK, there is no way
to prevent this.

....however, having thought some more, you could use a socketpair instead
and set the socketopts SO_SNDBUF and SO_RCVBUF to 1 byte.

Ben
 
C

ctcgag

I'm using a pipe to talk to a forked child process -

I do not want any data to get lost if the child process exits
unexpectedly -

- so I was hoping to turn off buffering so that there was at most 1
piece of data (i.e. one line) in the pipe.

I thought if I did:

select(PIPEHANDLE);
$| = 1;

then my pipe would not do buffering, and I was expecting that that
would lead to blocking writes - i.e. if the data hadn't been read from
the pipe, then a write to the pipe would block until it was.

however - after doing the above - I find I can still write data to the
pipe - regardless of how much is read out of the other end.

You have prevented Perl from buffing up it's output before handing it to
the OS, but the OS still has it's own pipe buffer.

If you want the two processes to proceed in lock-stop, you could require
an acknowledgement from the child before the parent attempts the next
print.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top