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<unis ...

  2. 转 linux socket的select函数例子

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

  3. linux 下的select函数

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

  4. [原创]C 语言select函数

    参考链接:http://www.cnblogs.com/GameDeveloper/p/3406565.html 注意点: select() 只是执行一次的超时检测.重新进行select要重新设置“超 ...

  5. linux C语言getopt()函数的使用

    getopt被用来解析命令行选项参数. #include <unistd.h> 函数及参数介绍 extern char *optarg; //选项的参数指针,如果选项字符串里的字母后接着冒 ...

  6. R 语言 select函数在org.Hs.eg.db上的运用

    首先org.Hs.eg.db是一个关于人类的 一,在R中导入包library(org.Hs.eg.db) http://www.bioconductor.org/packages/release/da ...

  7. select()函数用法二

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

  8. linux select函数:Linux下select函数的使用详解【转】

    本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...

  9. linux c中select使用方法

    1.select函数作为定时器使用    it_value.tv_sec = 0;    it_value.tv_usec = 100000:    select(1,NULL,NULL,NULL,& ...

随机推荐

  1. How to use MJRefresh

    Installation with CocoaPods:pod 'MJRefresh' Manual import: Drag All files in the MJRefresh folder to ...

  2. iOS学习笔记47-Swift(七)泛型

    一.Swift泛型介绍 泛型是为Swift编程灵活性的一种语法,在函数.枚举.结构体.类中都得到充分的应用,它的引入可以起到占位符的作用,当类型暂时不确定的,只有等到调用函数时才能确定具体类型的时候可 ...

  3. FZU——2111Min Number(多次交换得到最小数,水题)

    Problem 2111 Min Number Accept: 760    Submit: 1516 Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  4. BZOJ3209 花神的数论题 【组合数 + 按位计数】

    题目 背景 众所周知,花神多年来凭借无边的神力狂虐各大 OJ.OI.CF.TC -- 当然也包括 CH 啦. 描述 话说花神这天又来讲课了.课后照例有超级难的神题啦-- 我等蒟蒻又遭殃了. 花神的题目 ...

  5. 【CF1023C】Bracket Subsequence(模拟)

    题意:给定一个正则括号序列 s ,让你在当中选择一个长度正好为 t 的子串,使得 t 恰好也是一个正则括号序列 思路:用栈模拟 #include<cstdio> #include<c ...

  6. 【CF675C】Money Transfers(离散化,贪心)

    http://www.cnblogs.com/chengsheng/p/5535316.html 题目大意:给你n个银行中的存款(负值表示借贷),是成环的,1跟n相接,这n个数的和为0.可以从i向i的 ...

  7. linux 中信号量

    ctrl-c 发送 SIGINT 信号给前台进程组中的所有进程.常用于终止正在运行的程序.ctrl-z 发送 SIGTSTP 信号给前台进程组中的所有进程,常用于挂起一个进程.ctrl-d 不是发送信 ...

  8. lunix cat tail more等用法

    cat主要有三大功能:  1.一次显示整个文件.  $ cat filename  2.从键盘创建一个文件.  $ cat > filename  只能创建新文件,不能编辑已有文件.  3.将几 ...

  9. AC日记——[USACO07DEC]手链Charm Bracelet 洛谷 P2871

    题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like t ...

  10. linux grep 查找文件内容

    自试: wang@wang:~$ grep -i "*args*" ~/IGV01-SW/src/bzrobot_diagnostics/bzrobot_lightbelt_man ...