In the following code, what happens to the buffer from sysread() when the system call is restarted due to EINTR? I am reading from a pipe and not a file and cant afford to lose a single bit. However, my program complains like it is losing data. Are the contents of $buffer in an undefined state or is it guaranteed that the read will be consistent? That is if I have the following data in the pipe:

"1 2 3 4"

and call sysread but the call is interruped and restarted after reading "1 2" can it be guaranteed that $buffer will have "1 2 3 4" or will it have "3 4" and lose "1 2"? Something else? How can I guarantee that I will read all the data even if the call is interrupted by SIGCHLD?

Furthermore, I must get the return code from the INPIPE process and the only way that seems to work ok is to install a blank signal handler and let close() wait on the child so letting perl reap the process by itself is unacceptable.

I am using AIX 5.3 perl 5.8.8

Apologies if the code is rough, I pulled the relevant bits out of a larger program. I'm not good at low level systems programming/semantics so feel free to challenge any of my assumptions or suggest alternate methods.

my $sigset = POSIX::SigSet->new(SIGCHLD); my $sigaction = POSIX::SigAction->new( sub { }, $sigset, &POSIX::SA_RE +START ); my $old_sigset = POSIX::SigSet->new; sigprocmask(SIG_SETMASK, $sigset, $old_sigset) or die "Could not set signal mask\n"; $reader_pid = open(INPIPE,"cat /tmp/file |") my $buffer; #Pipe buffer on AIX seems to be 32K while($rbytes = sysread(INPIPE,$buffer,32768)){ $wbytes = syswrite(OUTPIPE,$buffer); #print OUTPIPE $buffer; if($wbytes != $rbytes){ die("Error writing to outpipe."); } } if(!defined($rbytes)){ bomb("INPIPE had error: $!\n",2); } close(INPIPE) or die $! ? "Error closing inpipe: $!" : "Exit status $? from inpipe";

In reply to EINTR and sysread() by bot403

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.