unix 套接字中异步和非阻塞有什么区别?

发布于 2024-11-14 08:00:22 字数 396 浏览 4 评论 0原文

我在 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)asyncnonblocking 不是同一件事吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如歌彻婉言 2024-11-21 08:00:22

FIOASYNC 切换 O_ASYNC 标志(通常在 open(2)fcntl(2) 中设置)一个文件描述符,当文件描述符准备好进行 IO 时,它将要求内核向进程发送 SIGIOSIGPOLL

O_ASYNC 不经常使用:

  • 在信号处理程序中正确处理 IO 极其困难;它们最好尽可能小,
  • 因为信号会中断程序的控制流,它们的运行成本比标准系统调用(例如 select(2)poll(2) 更高) )
  • 信号提供的信息比其他调用少:它们只报告一个 fd 就绪,而不是报告多个可能就绪的 fd。

O_NONBLOCK 不会向用户进程提供任何关于 fd 已准备好进行 read(2)write(2) 的通知) - 相反,它会更改 read(2)write(2) 的行为以及类似的调用,以便在文件描述符不存在时立即返回准备好阅读或写作。 O_NONBLOCK 通常与 select(2)poll(2)类似的调用,以保证客户端或服务器的主循环不会在一个特定对等点上阻塞,从而使其所有对等点挨饿。

FIOASYNC toggles the O_ASYNC flag (which is usually set in open(2) or fcntl(2)) for a file descriptor, which will ask the kernel to send SIGIO or SIGPOLL to the process when the file descriptor is ready for IO.

O_ASYNC is not used often:

  • it is extremely difficult to properly handle IO in signal handlers; they are best left as tiny as possible
  • because signals interrupt the control flow of the program, they 'cost more' to run than standard system calls, such as select(2) or poll(2)
  • signals provide less information than other calls: they only report one fd ready vs many fds that might be ready.

The O_NONBLOCK doesn't provide any notification to the user process that a fd is ready for read(2) or write(2) -- instead, it changes the behavior of read(2) and write(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 with select(2) or poll(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文