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 称为描述词组,是用来回传该描述词的读,写或例外的状况。底下的宏提供了处理这三种描述词组的方式:

FD_CLR(inr fd,fd_set* set);用来清除描述词组set中相关fd 的位

FD_ISSET(int fd,fd_set *set);用来测试描述词组set中相关fd 的位是否为真

FD_SET(int fd,fd_set*set);用来设置描述词组set中相关fd的位

FD_ZERO(fd_set *set); 用来清除描述词组set的全部位
参数
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函数用法的更多相关文章

  1. linux c语言 select函数使用方法

    linux c语言 select函数使用方法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<un ...

  2. select()函数用法二

    Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如 connect.accept.recv或recvfrom这样的阻塞程序 ...

  3. linux 下的select函数

    函数原型 /* According to POSIX.1-2001 */ #include <sys/select.h>  //头文件 /* According to earlier st ...

  4. select()函数用法三之poll函数

    poll是Linux中的字符设备驱动中有一个函数,Linux 2.5.44版本后被epoll取代,作用是把当前的文件指针挂到等待队列,和select实现功能差不多. poll()函数:这个函数是某些U ...

  5. select函数用法详解

    1. select函数 select的作用: 轮询的方式,从多个文件描述符中获取状态变化后的情况. 头文件 #include <sys/time.h> //for struct timev ...

  6. 转 linux socket的select函数例子

    使用select函数可以以非阻塞的方式和多个socket通信.程序只是演示select函数的使用,功能非常简单,即使某个连接关闭以后也不会修改当前连接数,连接数达到最大值后会终止程序. 1. 程序使用 ...

  7. Linux编程里getopt_long_only函数用法详解

    在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子.下面介绍使用getopt_long_only和getopt_long(两者用法差不多)解析命令行选项. 程序中主 ...

  8. C语言qsort函数用法

    qsort函数简介 排序方法有很多种:选择排序,冒泡排序,归并排序,快速排序等. 看名字都知道快速排序是目前公认的一种比较好的排序算法.因为他速度很快,所以系统也在库里实现这个算法,便于我们的使用. ...

  9. C语言assert()函数用法总结

    assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> void assert( i ...

随机推荐

  1. CentOS7安装WDCP3

    CentOS7安装WDCP3.2面板教程 到此WDCP安装完毕

  2. Spring源码情操陶冶-AnnotationConfigBeanDefinitionParser注解配置解析器

    本文承接前文Spring源码情操陶冶-自定义节点的解析,分析spring中的context:annotation-config节点如何被解析 源码概览 对BeanDefinitionParser接口的 ...

  3. hdu 4656 Evaluation [任意模数fft trick]

    hdu 4656 Evaluation 题意:给出\(n,b,c,d,f(x) = \sum_{i=1}^{n-1} a_ix^i\),求\(f(b\cdot c^{2k}+d):0\le k < ...

  4. POJ 3537 Crosses and Crosses [Multi-SG]

    传送门 我也不知道为什么枚举vis必须加上一个边界才能A 以后还是都加上吧 #include <iostream> #include <cstdio> #include < ...

  5. tcp_sync_server and tcp_sync_client

    #include <iostream> #include <fstream> #include <sstream> #include <boost/asio. ...

  6. GitHub入门与实践

    基本命令 git status 工作区状态 git add git commint 暂存区 git push gitHub客户端 下载网址:https://desktop.github.com/ 解决 ...

  7. git取消文件跟踪

    在使用git的时候,有些文件是不需要上传的,所以就可以修改   .gitignore 例如: 如果是对所有文件都取消跟踪的话,就是 git rm -r -cached .  //不删除本地文件 git ...

  8. cygwin + git + nat123 30元搭建公网可访问的git服务器

    首先参考上一篇安装服务器上的CYGWIN: http://jingyan.baidu.com/article/7e440953eabd742fc0e2efae.html 上一篇中已经加入了GIT相关的 ...

  9. Hibernate学习(二)保存数据

    package cn.lonecloud.test; import java.util.Date; import org.hibernate.HibernateException; import or ...

  10. 在jdbc中使用properites文件进行使用

    首先先在源代码中创建一个properites文件 db_url=jdbc\:mysql\://localhost\:3306/db_friend db_user=root db_password= d ...