当前位置:网站首页>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.
边栏推荐
- 1003 我要通过! (20 分)
- 强化学习 (Reinforcement Learning)
- 源代码阅读器项目
- Throwing a question? The execution speed of the Count operation in the Mysql environment is very slow. You need to manually add an index to the primary key---MySql optimization 001
- shell脚本实战(第2版)/人民邮电出版社 脚本2 验证输入:仅限字母和数字
- ESIM(Enhanced Sequential Inference Model)- 模型详解
- libavcodec.dll导致游戏不能运行及explorer关闭
- 工作--今天的学习
- 排序1:冒泡排序
- [贴装专题] 贴装流程中涉及到的位置关系计算
猜你喜欢
随机推荐
条件控制语句
tuple dictionary collection
StratoVirt 中的虚拟网卡是如何实现的?
函数二
需求侧电力负荷预测(Matlab代码实现)
Redis + NodeJS 实现一个能处理海量数据的异步任务队列系统
公里周日
源代码阅读器项目
好久没更新博客了
Technology Sharing | Sending Requests Using cURL
史上最小白之《Word2vec》详解
使用.NET简单实现一个Redis的高性能克隆版(四、五)
深度学习--神经网络(基础讲解)
[贴装专题] 贴装流程中涉及到的位置关系计算
cannot import name ‘load_offloaded_weights‘ from ‘accelerate.utils‘ (/home/huhao/anaconda3/envs/huha
xmms播放器加了播放列表的管理功能
LeetCode(剑指 Offer)- 25. 合并两个排序的链表
KeyBERT和labse提取字符串中的关键词
MySQL索引的B+树到底有多高?
单元测试2之实际结果检查的引用




![[项目配置] 配置Qt函数库和ui界面库的封装并调用的项目](/img/e9/d41f144a2f27e76f97cd6401d37578.png)



![[贴装专题] 视觉贴装平台与贴装流程介绍](/img/ec/870af3b56a487a5ca3a32a611234ff.png)
