聘我网

新概念招聘3.0

FIOASYNC和FIONBIO的区别是什么?

vote up0vote downstar

nginx源码中看到这样的代码:

return ioctl(s, FIONBIO, &nb);
...
if (ioctl(ngx_processes[s].channel[0], FIOASYNC, &on) == -1) {
...

不是很明白FIONBIOFIOASYNC的区别?

字面上非阻塞不就是异步吗。。

 

2 个答复

vote up0vote downcheck

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)

ioctlFIOASYNC等价于fcntlO_ASYNC

ioctlFIONBIO等价于fcntlO_NONBLOCK

这两个是等价的:

fcntl(socket, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);

nb = 1;
ioctl(s, FIONBIO, &nb);

FIOASYNC设置O_ASYNC标记,该标记决定fd可以IO时进程是否会收到SIGIOSIGPOLL信号。

FIONBIO设置O_NONBLOCK标记,该标记会改变readwrite和同类函数的行为,使得在fd还不能IO时立即返回而不是hang住。

后者经常跟selectpoll等函数一起使用,使得主程序不会因为个别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);
链接
vote up0vote down

这个跟windows socket一样的

3种模式: 阻塞非阻塞重叠Overlapped,其实就是asynchronous

其中只有重叠模式支持回调。

非阻塞在操作不能马上完成时会立刻返回,但不支持回调。

阻塞就是最普通的模式了。

阻塞和非阻塞都属于同步(synchronous)。

参考:

套接字重叠 I/O 模式与阻塞/非阻止模式

Windows Socket 速查筆記

链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.