首页 > 其他分享 >[操作系统]阻塞io 非阻塞io Epoll

[操作系统]阻塞io 非阻塞io Epoll

时间:2024-08-25 17:16:40浏览次数:12  
标签:io Epoll read epoll 阻塞 will call file data

Blocking I/O, Nonblocking I/O, And Epoll
January 10, 2017
In this post I want to explain exactly what happens when you use nonblocking I/O. In particular, I want to explain:

The semantics of setting O_NONBLOCK on a file descriptor using fcntl
How nonblocking I/O is different from asynchronous I/O
Why nonblocking I/O is frequently used in conjunction with I/O multiplexers like select, epoll, and kqueue
How nonblocking mode interacts with edge-triggered polling with epoll
Blocking Mode
By default, all file descriptors on Unix systems start out in "blocking mode". That means that I/O system calls like read, write, or connect can block. A really easy way to understand this is to think about what happens when you read data on stdin from a regular TTY-based program. If you call read on stdin then your program will block until data is actually available, such as when the user actually physically types characters on their keyboard. Specifically, the kernel will put the process into the "sleeping" state until data is available on stdin. This is also the case for other types of file descriptors. For instance, if you try to read from a TCP socket then the read call will block until the other side of the connection actually sends data.

Blocking is a problem for programs that should operate concurrently, since blocked processes are suspended. There are two different, complementary ways to solve this problem. They are:

Nonblocking mode
I/O multiplexing system calls, such as select and epoll
These two solutions are often used together, but they are independent strategies to solving this problem, and often both are used. In a moment we'll see the difference and why they're commonly both used.

Nonblocking Mode (O_NONBLOCK)
A file descriptor is put into "nonblocking mode" by adding O_NONBLOCK to the set of fcntl flags on the file descriptor:

/* set O_NONBLOCK on fd */
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
From this point forward the file descriptor is considered nonblocking. When this happens I/O system calls like read and write that would block will return -1, and errno will be set to EWOULDBLOCK.

This is interesting, but on its own is actually not that useful. With just this primitive there's no efficient way to do I/O on multiple file descriptors. For instance, suppose we have two file descriptors and want to read both of them at once. This could be accomplished by having a loop that checks each file descriptor for data, and then sleeps momentarily before checking again:

struct timespec sleep_interval{.tv_sec = 0, .tv_nsec = 1000};
ssize_t nbytes;
for (;

标签:io,Epoll,read,epoll,阻塞,will,call,file,data
From: https://www.cnblogs.com/DCFV/p/18379160

相关文章

  • SP666 VOCV - Con-Junctions 题解
    注意到这个问题具有最优子结构性,考虑树上dp。记$dp[i][0/1]$表示i号节点不放灯或放灯的最小值,$s[i][0/1]$为对应的方案数。那么我们可以进行如下转移:$dp[u][0]=\sum_{u->v}dp[v][1]$$dp[u][1]=\sum_{u->v}\min(dp[v][0],dp[v][1])$在更新对应的dp数组时,我们用......
  • POLIR-Society-Organization-真实社政: 人性{黑、白、灰}: + 管理Strategy的(整体/组织
    手机实名制+虚拟卡号手机实名制防止电诈减少犯罪发生;虚拟卡号确实有正面意义与负面意义正面意义:"虚拟号"的政策本身是好的没问题的;例如,社会性的研究;即使“不法分子”使用“虚拟号”诈骗犯罪,群众的“警惕性”更高更易察觉;因为:“虚拟号段”已经“预先分类”过,筛选......
  • 【Linux】理解操作系统中的进程状态:阻塞、挂起、运行
    理解操作系统中的进程状态:阻塞、挂起、运行1.进程状态概述2.阻塞(Blocked)3.挂起(Suspended)4.运行(Running)5.状态转换关系6.总结理解操作系统中的进程状态:阻塞、挂起、运行操作系统是管理计算机硬件和软件资源的核心部分,而进程管理则是操作系统中最重要的功能......
  • [C++ Error] f0202.cpp(13): E2268 Call to undefined function 'system'
    system('pause');解决方法,修改代码:system("pause");[C++Error]f0202.cpp(13):E2268Calltoundefinedfunction'system'错误解释:这个错误表明您在C++代码中尝试调用了一个未定义的函数system。system函数是C标准库中的函数,用于执行一个字符串中给出的命令。在C++中,......
  • Neo-GNNs: Neighborhood Overlap-aware Graph Neural Networks for Link Prediction
    目录概符号说明MotivationNeo-GNN代码Neo-GNNs:Neighborhoodoverlap-awaregraphneuralnetworksforlinkprediction.NeurIPS,2021.概一种计算上相对高效的,同时利用结构信息和特征信息的链接预测模型.符号说明\(\mathcal{G}=(\mathcal{V},\mathcal{E})\),gra......
  • ## 已解决:`java.lang.ClassCastException: class java.lang.Integer cannot be cast t
    在Java开发中,类型转换错误是常见的异常之一。java.lang.ClassCastException:classjava.lang.Integercannotbecasttoclassjava.lang.Long表示在尝试将一个Integer类型的对象强制转换为Long类型时出现了错误。这种错误可能会导致程序运行时崩溃,因此需要正确地......
  • IO与进程
    #include<stdio.h>fopen("文件路径","打开方式");//打开文件标准iofclose(文件流);//关闭文件标准iofgetc(文件流);//读一个字符标准iofgutc(‘要写的字符’,文件流);//写一个字符标准iofgets(存放字符串首地址(如buf),大小,文件流);//读一串字符标准iofguts(存放字符......
  • IO进程练习---往文件中录入当前时间
     题目要求编程读写一个文件test.txt,每隔1秒向文件中写入一行录入时间的数据,类似这样:1 2007-7-3015:16:42 2 2007-7-3015:16:43该程序应该无限循环,直到按Ctrl-C中断程序。再次启动程序写文件时可以追加到原文件之后,并且序号能够接续上次的序号,比如:1  2007......
  • [AGC067B] Modifications
    MyBlogs[AGC067B]Modifications谔谔,做过类似的题还是不会啊啊啊。首先考虑给定一个\(a\)序列如何进行判定。倒着做这个覆盖的过程,每次可以看成是,如果\([l_i,r_i]\)剩下的点的颜色都相同,则可以把\([l_i,r_i]\)删掉。如果最后能删空就是合法的。区间DP判定这个过程:\(f......
  • apt update 报错:Could not handshake: Error in the pull function. [IP: 185.199.108
    sudoaptupdate报错:错误:12https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/amd64ReleaseCouldnothandshake:Errorinthepullfunction.[IP:185.199.108.153443]错误:13https://nvidia.github.io/nvidia-docker/ubuntu18.04/amd64Re......