bot403 has asked for the wisdom of the Perl Monks concerning the following question:
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: EINTR and sysread()
by pc88mxer (Vicar) on Apr 03, 2008 at 15:57 UTC | |
by bot403 (Beadle) on Apr 03, 2008 at 17:00 UTC | |
|
Re: EINTR and sysread()
by pc88mxer (Vicar) on Apr 03, 2008 at 19:43 UTC | |
by bot403 (Beadle) on Apr 05, 2008 at 00:33 UTC |