Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

510 Advanced I/O Chapter 14


14.5.1 SystemVAsynchronous I/O


System V provides a limited form of asynchronous I/O that works only with STREAMS
devices and STREAMS pipes. The System V asynchronous I/O signal isSIGPOLL.
To enable asynchronous I/O for a STREAMS device, we have to callioctlwith a
second argument (request)ofI_SETSIG.The thirdargument is an integer value formed
from one or moreofthe constants in Figure14.18. These constants aredefined in
<stropts.h>.

Interfaces related to the STREAMS mechanism weremarked obsolescent in SUSv4, so we don’t
cover them in any detail. See Rago[ 1993 ]for moreinformation about STREAMS.

Constant Description
S_INPUT We can read data (other than high-priority data) without blocking.
S_RDNORM We can read normal data without blocking.
S_RDBAND We can read priority data without blocking.
S_BANDURG If this constant is specified withS_RDBAND,theSIGURGsignal is generated
instead ofSIGPOLLwhen we can read priority data without blocking.
S_HIPRI We can read high-priority data without blocking.
S_OUTPUT We can write normal data without blocking.
S_WRNORM Same asS_OUTPUT.
S_WRBAND We can write priority data without blocking.
S_MSG TheSIGPOLLsignal message has reached the stream head.
S_ERROR The stream has an error.
S_HANGUP The stream has hung up.

Figure 14.18Conditions for generatingSIGPOLLsignal

In addition to callingioctlto specify the conditions that should generate the
SIGPOLLsignal, we have to establish a signal handler for this signal. Recall from
Figure10.1 that the default action forSIGPOLLis to terminate the process, so we should
establish the signal handler beforecallingioctl.

14.5.2 BSD Asynchronous I/O


Asynchronous I/O in BSD-derived systems is a combination of two signals:SIGIOand
SIGURG.The former is the general asynchronous I/O signal, and the latter is used only
to notify the process that out-of-band data has arrived on a network connection.
To receive theSIGIOsignal, we need to perform three steps.


  1. Establishasignal handler forSIGIO, by calling eithersignalorsigaction.

  2. Set the process ID or process group ID to receive the signal for the descriptor,by
    callingfcntlwith a command ofF_SETOWN(Section 3.14).

Free download pdf