Recipe 7.14. Doing Non-Blocking I/O (Perl Cookbook)

Perl Cookbook

Perl CookbookSearch this book
Previous: 7.13. Reading from Many Filehandles Without BlockingChapter 7
File Access
Next: 7.15. Determining the Number of Bytes to Read
 

7.14. Doing Non-Blocking I/O

Problem

You want to read from or write to a filehandle without the system blocking your process until the program, file, socket, or device at the other end is ready. This is desired less often of regular files than of special files.

Solution

Open the file with sysopen, and specify the O_NONBLOCK option:

use Fcntl;

sysopen(MODEM, "/dev/cua0", O_NONBLOCK|O_RDWR)
    or die "Can't open modem: $!\n";

If you already have a filehandle, use fcntl to change the flags:

use Fcntl;

$flags = '';
fcntl(HANDLE, F_GETFL, $flags)
    or die "Couldn't get flags for HANDLE : $!\n";
$flags |= O_NONBLOCK;
fcntl(HANDLE, F_SETFL, $flags)
    or die "Couldn't set flags for HANDLE: $!\n";

Once a filehandle is set for non-blocking I/O, the sysread or syswrite calls that would block will instead return undef and set $! to EAGAIN:

use POSIX qw(:errno_h);

$rv = syswrite(HANDLE, $buffer, length $buffer);
if (!defined($rv) && $! == EAGAIN) {
    # would block
} elsif ($rv != length $buffer) {
    # incomplete write
} else {
    # successfully wrote
}

$rv = sysread(HANDLE, $buffer, $BUFSIZ);
if (!defined($rv) && $! == EAGAIN) {
    # would block
} else {
    # successfully read $rv bytes from HANDLE
}

Discussion

The O_NONBLOCK constant is part of the POSIX standard, so most machines should support it. We use the POSIX module to get the numeric value for the error EAGAIN.

See Also

The sysopen and fcntl functions in perlfunc (1) and in Chapter 3 of Programming Perl; the documentation for the standard POSIX module; your system's open (2) and fcntl (2) manpages; Recipe 7.13; Recipe 7.15


Previous: 7.13. Reading from Many Filehandles Without BlockingPerl CookbookNext: 7.15. Determining the Number of Bytes to Read
7.13. Reading from Many Filehandles Without BlockingBook Index7.15. Determining the Number of Bytes to Read

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.