linux c语言 select函数使用方法
linux c语言 select函数使用方法
|
|
|
|
表头文件
|
#i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unistd.h> |
|
定义函数
|
int select(int n,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout); |
|
函数说明
|
select()用来等待文件描写叙述词状态的改变。參数n代表最大的文件描写叙述词加1,參数readfds、writefds 和exceptfds 称为描写叙述词组。是用来回传该描写叙述词的读,写或例外的状况。
底下的宏提供了处理这三种描写叙述词组的方式: |
|
參数
|
timeout为结构timeval,用来设置select()的等待时间,其结构定义例如以下 struct timeval { time_t tv_sec; time_t tv_usec; }; |
|
返回值
|
假设參数timeout设为NULL则表示select()没有timeout。 |
|
错误代码
|
运行成功则返回文件描写叙述词状态已改变的个数。假设返回0代表在描写叙述词状态改变前已超过timeout时间,当有发生错误时则返回-1,错误原因存于errno,此时參数readfds,writefds,exceptfds和timeout的值变成不可预測。 EBADF 文件描写叙述词为无效的或该文件已关闭 EINTR 此调用被信号所中断 EINVAL 參数n 为负值。 ENOMEM 核心内存不足 |
|
范例
|
常见的程序片段:fs_set readset; FD_ZERO(&readset); FD_SET(fd,&readset); select(fd+1,&readset,NULL,NULL,NULL); if(FD_ISSET(fd,readset){……} |
以下是linux环境下select的一个简单使用方法
#i nclude <sys/time.h>
#i nclude <stdio.h>
#i nclude <sys/types.h>
#i nclude <sys/stat.h>
#i nclude <fcntl.h>
#i nclude <assert.h>
int main ()
{
int keyboard;
int ret,i;
char c;
fd_set readfd;
struct timeval timeout;
keyboard = open("/dev/tty",O_RDONLY | O_NONBLOCK);
assert(keyboard>0);
while(1)
{
timeout.tv_sec=1;
timeout.tv_usec=0;
FD_ZERO(&readfd);
FD_SET(keyboard,&readfd);
ret=select(keyboard+1,&readfd,NULL,NULL,&timeout);
if(FD_ISSET(keyboard,&readfd))
{
i=read(keyboard,&c,1);
if('\n'==c)
continue;
printf("hehethe input is %c\n",c);
if ('q'==c)
break;
}
}
}
用来循环读取键盘输入
2007年9月17日,将样例程序作一改动,加上了time out,而且考虑了select得全部的情况:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
int main ()
{
int keyboard;
int ret,i;
char c;
fd_set readfd;
struct timeval timeout;
keyboard = open("/dev/tty",O_RDONLY | O_NONBLOCK);
assert(keyboard>0);
while(1)
{
timeout.tv_sec=5;
timeout.tv_usec=0;
FD_ZERO(&readfd);
FD_SET(keyboard,&readfd);
ret=select(keyboard+1,&readfd,NULL,NULL,&timeout);
//select error when ret = -1
if (ret == -1)
perror("select error");
//data coming when ret>0
else if (ret)
{
if(FD_ISSET(keyboard,&readfd))
{
i=read(keyboard,&c,1);
if('\n'==c)
continue;
printf("hehethe input is %c\n",c);
if ('q'==c)
break;
}
}
//time out when ret = 0
else if (ret == 0)
printf("time out\n");
}
}
linux c语言 select函数使用方法的更多相关文章
- linux c语言 select函数用法
linux c语言 select函数用法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unis ...
- 转 linux socket的select函数例子
使用select函数可以以非阻塞的方式和多个socket通信.程序只是演示select函数的使用,功能非常简单,即使某个连接关闭以后也不会修改当前连接数,连接数达到最大值后会终止程序. 1. 程序使用 ...
- linux 下的select函数
函数原型 /* According to POSIX.1-2001 */ #include <sys/select.h> //头文件 /* According to earlier st ...
- [原创]C 语言select函数
参考链接:http://www.cnblogs.com/GameDeveloper/p/3406565.html 注意点: select() 只是执行一次的超时检测.重新进行select要重新设置“超 ...
- linux C语言getopt()函数的使用
getopt被用来解析命令行选项参数. #include <unistd.h> 函数及参数介绍 extern char *optarg; //选项的参数指针,如果选项字符串里的字母后接着冒 ...
- R 语言 select函数在org.Hs.eg.db上的运用
首先org.Hs.eg.db是一个关于人类的 一,在R中导入包library(org.Hs.eg.db) http://www.bioconductor.org/packages/release/da ...
- select()函数用法二
Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如 connect.accept.recv或recvfrom这样的阻塞程序 ...
- linux select函数:Linux下select函数的使用详解【转】
本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...
- linux c中select使用方法
1.select函数作为定时器使用 it_value.tv_sec = 0; it_value.tv_usec = 100000: select(1,NULL,NULL,NULL,& ...
随机推荐
- client三大家族区别(三大家族总结)
目录 目录 2 今日内容: 4 第1章 第三大家族client 4 1.1 主要成员 4 1.2 三大家族区别(三大家族总结) 5 1.2.1 Width和height 5 1.2.2 top和lef ...
- axis2生成webservice服务端返回String[]和String[][]一维数组和二维数组解析
环境:用axis2生成服务端,用aixs做客户端 1:直接返回String[]: public String[] testArr(String name) { String[] ret=new Str ...
- Android2.2源码属性服务分析
属性服务property service 大家都知道,在windows中有个注册表,里面存储的是一些键值对.注册表的作用就是:系统或者应用程序将自己的一些属性存储在注册表中,即使系统或应用程序重启,它 ...
- centos7配置国内yum源
文章目录 1.什么是yum仓库? 2.yum仓库配置 2.1.阿里镜像仓库配置 2.1.1.配置步骤 2.1.2.epel源 安装和配置 2.1.3.查看yum源 2.2.配置 清华大学镜像仓库 1. ...
- 驱动13.i2c设备驱动程序
1 分析i2c设备的识别过程i2c_add_driver i2c_register_driver driver->driver.bus = &i2c_bus_type ...
- dedecms--二次开发之前后台登录分开
最近在写dedecms系统下会员功能二次开发,然后发现在本地测试的时候每次登录后台,管理员帐号都会在前台页面也显示登录了,但是如果真的是在前台页面用管理员账号登录的话那是登陆不了的,所以我觉得这样的效 ...
- PAT 甲级 1045 Favorite Color Stripe(DP)
题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...
- go 依赖包管理工具gb安装报错
尝试了下gb工具,发现有个问题: [root@etcd1 test]# go get github.com/constabulary/gb/... /home/gopath/src/github.co ...
- 智能手机+DIY红外=万能遥控器
目前好像只有:三星S4.,努比亚大牛,华为荣耀3等几款新机才有红外遥控功能,那我们使用的手机没有这个功能怎么办?不要急我有办法呵呵,本次DIY材料好找又简单,大家都可以亲自试一试! DIY材料:红外二 ...
- Gvim 和 Opencv编译
好奇看了一下Gvim编译器:在官网上也有介绍,github上有源码,安装在电脑上,感觉需求不大,记那些命令也太多了.然后编译opencv过程中cmake成功了,但是VS下编译报了很多错,准备不搞这些了 ...