select()函数要点解析

从中摘到的信息:
1,函数库文件和原型
#include <sys/select.h>

/* According to earlier standards */ 
#include <sys/time.h>
 #include <sys/types.h
#include <unistd.h
 int select(int nfds, fd_set *readfds, fd_set *writefds,
           fd_set *exceptfds, struct timeval *timeout);

 void FD_CLR(int fd, fd_set *set);
int  FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
 void FD_ZERO(fd_set *set);

2,select() may update the timeout argument to indicate how much time was left.
这句是说,调用select()之后,可能会将timeval的值修改为调用后剩余的时间,因此,每次调用之前,timeval的值都要重新设置。
3, On exit, the sets are modified in place to indicate which file descriptors actually changed status. Each of the three file descriptor sets may be specified as NULL if no file descriptors are to be watched for the corresponding class of events.
这句是说,在函数返回时,文件描述符集的内容将会被修改,状态发生变化的文件描述符被置位。(by 杨w:文件描述符相当于一个位图,其中一个bit代表一个文件描述符,如果监视的文件描述符的状态发生了变化,那么它对应的bit会被置1)。
因此,每次调用select()函数之前,fd_set也要重新设置。
4,例子
//变量的声明和定义
int can_fd;
fd_set rfds;
struct timeval tv;
can_fd = open("/dev/can", O_RDWR);
if(can_fd<=0)
{
qDebug()<<"can't open can_dev "<<endl;
cout<<"can't open can_dev"<<endl;
return;
}
else
qDebug()<<"open can_dev successfully!"<<endl;

FD_ZERO(&rfds);//清空串口接收端口集
FD_SET(can_fd, &rfds);//设置串口接收端口集
tv.tv_sec = 0;
tv.tv_usec = 50000;//50ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值