当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
小程序员的发展计划
libavcodec.dll导致游戏不能运行及explorer关闭
抛出一个问题? Mysql环境下进行Count操作执行的时候速度很慢_需手动给主键添加索引---MySql优化001
编解码(seq2seq)+注意机制(attention) 详细讲解
[相机配置] 海康相机丢包配置环境
基于信号量与环形队列实现读写异步缓存队列
ESIM(Enhanced Sequential Inference Model)- 模型详解
深度学习--生成对抗网络(Generative Adversarial Nets)
【原创】JPA中@PrePersist和@PreUpdate的用法
认识
markdown转ipynb--利用包notedown
借问变量何处存,牧童笑称用指针,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang类型指针(Pointer)的使用EP05
GeoScene Pro 2.1下载地址与安装基本要求
Win32控件------------显示系统使用的控件版本
京东物流与五菱将开发联名版定制产品
xmms的歌词显示及音量控制OK
MySQL全文索引
主从postition变化无法锁定_Slave_IO_Running显示No_Slave_Sql_Running显示No---Mysql主从复制同步002
xmms已经发布到v1.3了,好久没写博客了
1002 写出这个数 (20 分)