当前位置:网站首页>Unix Environment Programming Chapter 14 14.4 I/O Multiplexing
Unix Environment Programming Chapter 14 14.4 I/O Multiplexing
2022-08-09 10:28:00 【Mary Soda Meatloaf】
14.4.1 Function select and pselect
#include int select(int maxfdpl, fd_set *restrict readset, fd_set *restrict writeset, fd_set* restrict exceptset, struct timeval *restrict tvptr); Parameter: struct timeval *restrict tvptr.
It indicates the length of time it is willing to wait, in seconds and microseconds.There are three situations:
- tvptr==NULL.wait forever.This indefinite wait is interrupted if a signal is caught.Returns when one of the specified descriptors is ready or catches a signal.If a signal is caught, select returns -1 and errno is set to EINTR.
- tvptr->tv_sec==0 && tvptr->tv_usec==0.Do not wait at all, test all specified descriptors and return immediately.
- tvptr->tv_sec!=0 || tvptr->tv_usec!=0.Wait the specified number of seconds and microseconds.Returns immediately when one of the specified descriptors is ready, or when the specified time value has passed.If no descriptor is ready by the time the timeout expires, the return value is 0.
The middle three parameters: readfs, writefds, exceptfds are pointers to descriptor sets.
These 3 descriptor sets describe the set of descriptors we care about that are readable, writable, or in exceptional conditions.Each descriptor set is stored in a fd_set data type.
After declaring a descriptor set, we must use FD_ZERO to set the descriptor set to 0, and then set the bits of each descriptor we care about in it.
Any of the three parameters in the middle of the selection can be a null pointer, which means that it does not care about the corresponding conditions.
Select the first parameter maxfdp1 means "the maximum file descriptor number value plus 1".
select has three possible return values
- A return value of -1 indicates an error, in which case none of the descriptor sets are modified.
- A return value of 0 means that no descriptors are ready, and all descriptor sets will have bit 0 set.
- A positive return value indicates the number of descriptors that have been prepared.The value is the sum of the number of descriptors that are ready for the 3 descriptor sets, so if the same descriptor is ready for reading and writing, it will be counted twice in the return value.
Some clarification on what "ready" means:
A descriptor in the read set (readfds) is considered ready if a read operation does not block.
A descriptor in the write set (writefds) is considered ready if the write operation does not block.
If there is a pending exception condition for a descriptor in the exception condition set, the descriptor set is considered to be active.
For read, write and exception conditions, file descriptors for ordinary files always return ready.
14.4.2 Function poll
The poll function is similar to the select function, but the programmer interface is different.The poll function can be used with any type of file descriptor.
#include int poll(struct pollfd fd[], nfds_t nfds, int timeout);//Return value: the number of descriptors that are ready, if it times out, return 0, if there is an error, return -1 Unlike select, poll does not construct a descriptor set for each condition, but an array of pollfd structures, each array element specifying a descriptor number and the condition we are interested in changing the descriptor.
struct pollfd{int fd;short events;short revents;};The elements in the fdarray array are specified by nfds.
poll's events and revents flags:
| LogoName | Enter to events? | get result from revents? | Description |
| POLLIN | yes | yes | Normal or priority with data readable |
| POLLRDNORM | yes | yes | Normal data readable |
| POLLRDBAND | yes | yes | Priority with data readable |
| POLLPRI | yes | yes | High priority data is readable |
| POLLOUT | yes | yes | Normal data can be written |
| POLLWRNORM | yes | yes | Normal data can be written |
| POLLWRBAND | yes | yes | Priority writable with data |
| POLLERR | yes | An error occurred | |
| POLLHUP | yes | Suspend occurred | |
| POLLNVAL | yes | Descriptor is not an open file |
The last parameter to poll specifies how long we are willing to wait.
timeout==-1
Wait forever, returning when one of the specified descriptors is ready, or catches a signal.If a signal is caught, poll returns -1 and errno is set to EINTR.
timeout==0
Don't wait.Test all descriptors and return immediately.
timeout>0
Wait timeout milliseconds.Returns immediately when one of the specified descriptors is ready, or when the timeout expires.If no descriptor is ready when the timeout expires, the return value is 0.
边栏推荐
猜你喜欢

Attentional Feature Fusion

OneNote 教程,如何在 OneNote 中搜索和查找笔记?

使用.NET简单实现一个Redis的高性能克隆版(四、五)
![[项目配置] 配置Qt函数库和ui界面库的封装并调用的项目](/img/e9/d41f144a2f27e76f97cd6401d37578.png)
[项目配置] 配置Qt函数库和ui界面库的封装并调用的项目

Nodejs服务端

深度学习--生成对抗网络(Generative Adversarial Nets)
今天做了手机播放器的均衡器

Demand side power load forecasting (Matlab code implementation)

Electron application development best practices
![[Halcon&定位] 解决Roi区域外的模板匹配成功](/img/ad/549c7e6336ef62469a7c71e6bfcb42.png)
[Halcon&定位] 解决Roi区域外的模板匹配成功
随机推荐
百度云大文件网页直接下载
笔记本电脑使用常见问题,持续更新
深度学习--自编码器(AutoEncoder)
[Halcon&定位] 解决Roi区域外的模板匹配成功
开源SPL,WebService/Restful广泛应用于程序间通讯,如微服务、数据交换、公共或私有的数据服务等。
【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
socket实现TCP/IP通信
[贴装专题] 贴装流程中涉及到的位置关系计算
xmms播放器加了播放列表的管理功能
自定义类型:结构体,枚举,联合
Technology Sharing | How to simulate real usage scenarios?mock technology to help you
1004 成绩排名 (20 分)
mongodb学习笔记
shell脚本实战(第2版)/人民邮电出版社 脚本1 在PATH中查找程序
收到人生第一笔五位数工资
[项目配置] 配置Qt函数库和ui界面库的封装并调用的项目
函数组件和类组件和dva视图更新问题
条件控制语句
工作--今天的学习
Nodejs服务端