unix 套接字中异步和非阻塞有什么区别?
我在 nginx 中看到这样的代码:
if(fcntl(ngx_processes[s].channel[0], F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK) == -1) {
...
if (ioctl(ngx_processes[s].channel[0], FIOASYNC, &on) == -1) {
...
任何人都可以告诉我 fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK)
和 ioctl(s, FIOASYNC, & ;on)
,async
和 nonblocking
不是同一件事吗?
I'm seeing such code in nginx:
if(fcntl(ngx_processes[s].channel[0], F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK) == -1) {
...
if (ioctl(ngx_processes[s].channel[0], FIOASYNC, &on) == -1) {
...
Anyone can tell me what's the difference between fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK)
and ioctl(s, FIOASYNC, &on)
,aren't async
and nonblocking
the same thing??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FIOASYNC
切换O_ASYNC
标志(通常在open(2)
或fcntl(2)
中设置)一个文件描述符,当文件描述符准备好进行 IO 时,它将要求内核向进程发送SIGIO
或SIGPOLL
。O_ASYNC
不经常使用:select(2)
或poll(2) 更高) )
O_NONBLOCK
不会向用户进程提供任何关于 fd 已准备好进行read(2)
或write(2) 的通知)
- 相反,它会更改read(2)
和write(2)
的行为以及类似的调用,以便在文件描述符不存在时立即返回准备好阅读或写作。O_NONBLOCK
通常与select(2)
或poll(2)
或 类似的调用,以保证客户端或服务器的主循环不会在一个特定对等点上阻塞,从而使其所有对等点挨饿。FIOASYNC
toggles theO_ASYNC
flag (which is usually set inopen(2)
orfcntl(2)
) for a file descriptor, which will ask the kernel to sendSIGIO
orSIGPOLL
to the process when the file descriptor is ready for IO.O_ASYNC
is not used often:select(2)
orpoll(2)
The
O_NONBLOCK
doesn't provide any notification to the user process that a fd is ready forread(2)
orwrite(2)
-- instead, it changes the behavior ofread(2)
andwrite(2)
and similar calls to return immediately if the file descriptor isn't ready for reading or writing.O_NONBLOCK
is typically used in conjunction withselect(2)
orpoll(2)
or similar calls to guarantee that the main loop of a client or server won't block on one specific peer, and thus starve all its peers.