FIOASYNC Enables a simple form of
asynchronous I/O notification. This
command causes the kernel to send
SIGIO signal to a process or a process
group when I/O is possible. Only
sockets, ttys, and pseudo-ttys
implement this functionality.
FIONBIO Enables nonblocking I/O. The
effect is similar to setting the
O_NONBLOCK flag with the fcntl
subroutine. The third parameter to the
ioctl subroutine for this command is a
pointer to an integer that indicates
whether nonblocking I/O is being
enabled or disabled. A value of 0
disables non-blocking I/O.
(src)
ioctl和FIOASYNC等价于fcntl和O_ASYNC。
ioctl和FIONBIO等价于fcntl和O_NONBLOCK。
这两个是等价的:
fcntl(socket, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
nb = 1;
ioctl(s, FIONBIO, &nb);
FIOASYNC设置O_ASYNC标记,该标记决定fd可以IO时进程是否会收到SIGIO和SIGPOLL信号。
FIONBIO设置O_NONBLOCK标记,该标记会改变read,write和同类函数的行为,使得在fd还不能IO时立即返回而不是hang住。
后者经常跟select,poll等函数一起使用,使得主程序不会因为个别socket而影响其他。
关于何时需要用FIOASYNC
Although the combination of blocking
and nonblocking operations and the
select method are sufficient for
querying the device most of the time,
some situations aren't efficiently
managed by the techniques we've seen
so far.
Let's imagine a process that executes
a long computational loop at low
priority but needs to process incoming
data as soon as possible. If this
process is responding to new
observations available from some sort
of data acquisition peripheral, it
would like to know immediately when
new data is available. This
application could be written to call
poll regularly to check for data, but,
for many situations, there is a better
way. By enabling asynchronous
notification, this application can
receive a signal whenever data becomes
available and need not concern itself
with polling.
User programs have to execute two
steps to enable asynchronous
notification from an input file.
First, they specify a process as the
"owner" of the file. When a process
invokes the F_SETOWN command using the
fcntl system call, the process ID of
the owner process is saved in
filp->f_owner for later use. This step
is necessary for the kernel to know
just whom to notify. In order to
actually enable asynchronous
notification, the user programs must
set the FASYNC flag in the device by
means of the F_SETFL fcntl command.
After these two calls have been
executed, the input file can request
delivery of a SIGIO signal whenever
new data arrives. The signal is sent
to the process (or process group, if
the value is negative) stored in
filp->f_owner.
For example, the following lines of
code in a user program enable
asynchronous notification to the
current process for the stdin input
file:
signal(SIGIO, &input_handler); /* dummy sample; sigaction( ) is better */
fcntl(STDIN_FILENO, F_SETOWN, getpid( ));
oflags = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, oflags | FASYNC);