Section 16.8. Nonblocking and Asynchronous IO

team bbl


16.8. Nonblocking and Asynchronous I/O

Normally, the recv functions will block when no data is immediately available. Similarly, the send functions will block when there is not enough room in the socket's output queue to send the message. This behavior changes when the socket is in nonblocking mode. In this case, these functions will fail instead of blocking, setting errno to either EWOULDBLOCK or EAGAIN. When this happens, we can use either poll or select to determine when we can receive or transmit data.

The real-time extensions in the Single UNIX Specification include support for a generic asynchronous I/O mechanism. The socket mechanism has its own way of handling asynchronous I/O, but this isn't standardized in the Single UNIX Specification. Some texts refer to the classic socket-based asynchronous I/O mechanism as "signal-based I/O" to distinguish it from the asynchronous I/O mechanism in the real-time extensions.

With socket-based asynchronous I/O, we can arrange to be sent the SIGIO signal when we can read data from a socket or when space becomes available in a socket's write queue. Enabling asynchronous I/O is a two-step process.

  1. Establish socket ownership so signals can be delivered to the proper processes.

  2. Inform the socket that we want it to signal us when I/O operations won't block.

We can accomplish the first step in three ways.

  1. Use the F_SETOWN command with fcntl.

  2. Use the FIOSETOWN command with ioctl.

  3. Use the SIOCSPGRP command with ioctl.

To accomplish the second step, we have two choices.

  1. Use the F_SETFL command with fcntl and enable the O_ASYNC file flag.

  2. Use the FIOASYNC command with ioctl.

We have several options, but they are not universally supported. Figure 16.21 summarizes the support for these options provided by the platforms discussed in this text. We show • where support is provided and where support depends on the particular domain. For example, on Linux, the UNIX domain sockets dont support FIOSETOWN or SIOCSPGRP.

Figure 16.21. Socket asynchronous I/O management commands

Mechanism

POSIX.1

FreeBSD 5.2.1

Linux 2.4.22

Mac OS X 10.3

Solaris 9

fcntl(fd, F_SETOWN, pid)

ioctl(fd, FIOSETOWN, pid)

 

ioctl(fd, SIOCSPGRP, pid)

 

fcntl(fd, F_SETFL, flags|O_ASYNC)

 

 

ioctl(fd, FIOASYNC, &n);

 


    team bbl



    Advanced Programming in the UNIX Environment
    Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
    ISBN: 0321525949
    EAN: 2147483647
    Year: 2005
    Pages: 370

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net