Advanced Programming in UNIX Environment Episode 78

System V Asynchronous 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 is SIGPOLL.

Interfaces related to the STREAMS mechanism were marked obsolescent in SUSv4, so we don’t cover them in any detail.

BSD Asynchronous I/O

Asynchronous I/O in BSD-derived systems is a combination of two signals: SIGIO and 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 the SIGIO signal, we need to perform three steps.

1.Establish a signal handler for SIGIO, by calling either signal or sigaction.
2.Set the process ID or process group ID to receive the signal for the descriptor, by calling fcntl with a command of F_SETOWN.
3.Enable asynchronous I/O on the descriptor by calling fcntl with a command of F_SETFL to set the O_ASYNC file status flag.

Step 3 can be performed only on descriptors that refer to terminals or networks, which is a fundamental limitation of the BSD asynchronous I/O facility.

For the SIGURG signal, we need perform only steps 1 and 2.

POSIX Asynchronous I/O

The POSIX asynchronous I/O interfaces give us a consistent way to perform asynchronous I/O, regardless of the type of file. These interfaces were adopted from the real-time draft standard, which themselves were an option in the Single UNIX Specification. In Version 4, the Single UNIX Specification moved these interfaces to the base, so they are now required to be supported by all platforms.
The asynchronous I/O interfaces use AIO control blocks to describe I/O operations.

The aiocb structure defines an AIO control block.

struct aiocb {
	int aio_fildes; /* file descriptor */
	off_t aio_offset; /* file offset for I/O */
	volatile void *aio_buf; /* buffer for I/O */
	size_t aio_nbytes; /* number of bytes to transfer */
	int aio_reqprio; /* priority */
	struct sigevent aio_sigevent; /* signal information */
	int aio_lio_opcode; /* operation for list I/O */
};

The aio_sigevent field controls how the application is notified about the completion of the I/O event. It is described by a sigevent structure.

struct sigevent {
	int sigev_notify; /* notify type */
	int sigev_signo; /* signal number */
	union sigval sigev_value; /* notify argument */
	void (*sigev_notify_function)(union sigval); /* notify function */
	pthread_attr_t *sigev_notify_attributes; /* notify attrs */
};

The sigev_notify field controls the type of notification. It can take on one of three values.

SIGEV_NONE

The process is not notified when the asynchronous I/O request completes.

SIGEV_SIGNAL

The signal specified by the sigev_signo field is generated when the asynchronous I/O request completes. If the application has elected to catch the signal and has specified the SA_SIGINFO flag when establishing the signal handler, the signal is queued (if the implementation supports queued signals). The signal handler is passed a siginfo structure whose si_value field is set to sigev_value (again, if SA_SIGINFO is used).

SIGEV_THREAD

The function specified by the sigev_notify_function field is called when the asynchronous I/O request completes. It is passed the sigev_value field as its only argument. The function is executed in a separate thread in a detached state, unless the sigev_notify_attributes field is set to the address of a pthread attribute structure specifying alternative attributes for the thread.

To perform asynchronous I/O, we need to initialize an AIO control block and call either the aio_read function to make an asynchronous read or the aio_write function to make an asynchronous write.

#include <aio.h>
int aio_read(struct aiocb *aiocb);
int aio_write(struct aiocb *aiocb);

To force all pending asynchronous writes to persistent storage without waiting, we can set up an AIO control block and call the aio_fsync function.

#include <aio.h>
int aio_fsync(int op, struct aiocb *aiocb);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值