函数原型

  /* According to POSIX.1-2001 */
#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);//函数原型

描写叙述

       Three independent sets of file descriptors are watched.  Those listed in readfds will be watched to see if characters  become  avail‐
able for reading (more precisely, to see if a read will not block; in particular, a file descriptor is also ready on end-of-file),
those in writefds will be watched to see if a write will not block, and those in exceptfds will be watched for exceptions. 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.
//该函数同意进程指示等待多个事件的不论什么一个发生。而且仅仅在有一个或多个事件发生或者经历一段指定的时间后才唤醒它
它仅仅有在例如以下四种情况下返回
1. 集合中的不论什么描写叙述符准备好读
2. 集合中的不论什么描写叙述符准备好谢
3. 集合中的不论什么描写叙述符有异常等待处理
4. 等待事件到达
       The time structures involved are defined in <sys/time.h> and look like

           struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};

等待事件这个參数有三种可能

1.永远灯下去。仅在有一个描写叙述符准备好才返回。为此。我们把该參数设置为空指针

2.等待一段固定时间。在有一个描写叙述符准好好I/O才返回,可是不能超时;

3. 不等待,检查描写叙述符后马上返回,这时定时器的时间必须设置为0

返回值:

(1)正常情况下返回满足要求的文件描写叙述符个数。

(2)经过了timeout等待后仍无文件满足要求。返回0。

(3)假设select被某个信号中断,将返回-1并设置errno为EINTR;

(4)若出错。返回-1并设置对应的errno。

select的用法:

(1)将要监控的文件加入到文件描写叙述符集。

(2)调用select開始监控;

(3)推断文件是否发生变化;

系统提供四个宏对描写叙述符集进行操作:

void FD_SET(int fd, fd_set *fdset); //将文件描写叙述符fd加入到文件描写叙述符集fdset中。
void FD_CLR(int fd, fd_set *fdset); //从文件描写叙述符集fdset中清除文件描写叙述符fd;
void FD_ISSET(int fd, fd_set *fdset); //在调用select后使用FD_ISSET来检測文件描写叙述符集中的文件fd发生了变化
void FD_ZERO(fd_set *fdset);//清空文件描写叙述符集

以下看个样例。用select来监视stdin看何时有输入

root@wl-Lenovo-B590:/myworkspace/anvancdedprogramminginunix/mysourcecode/chapter11#   cat -n select.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6
7 int
8 main(void)
9 {
10 fd_set rfds;
11 struct timeval tv;
12 int retval;
13
14 /* Watch stdin (fd 0) to see when it has input. */
15 FD_ZERO(&rfds);
16 FD_SET(0, &rfds);
17
18 /* Wait up to five seconds. */
19 tv.tv_sec = 5;
20 tv.tv_usec = 0;
21
22 retval = select(1, &rfds, NULL, NULL, &tv);
23 /* Don't rely on the value of tv now! */
24
25 if (retval == -1)
26 perror("select()");
27 else if (retval)
28 printf("Data is available now.\n");
29 /* FD_ISSET(0, &rfds) will be true. */
30 else
31 printf("No data within five seconds.\n");
32
33 exit(EXIT_SUCCESS);
34 }

编译后执行结果如图:

能够看到,五秒不输入则到达定时时间。有输入则会捕捉到

參考: UNIX网络编程

Linux Programmer's Manual

linux 下的select函数的更多相关文章

  1. linux c语言 select函数用法

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

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

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

  3. Linux下利用ioctl函数获取网卡信息

    linux下的ioctl函数原型如下: #include <sys/ioctl.h> int ioctl(int handle, int cmd, [int *argc, int argv ...

  4. 【C/C++】Linux下使用system()函数一定要谨慎

    [C/C++]Linux下使用system()函数一定要谨慎 http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为 ...

  5. Linux下系统时间函数、DST等相关问题总结(转)

    Linux下系统时间函数.DST等相关问题总结 下面这个结构体存储了跟时区相关的位移量(offset)以及是否存在DST等信息,根据所在的时区信息,很容易找到系统时间与UTC时间之间的时区偏移,另外根 ...

  6. 【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

    下面程序演示了在嵌入式Linux和PC机Linux下使用popen函数时,程序的运行结果是有差异的. 两个程序 atest.c 和 btest.c,atest 检查是否有 btest 进程运行,如果没 ...

  7. Linux下的getline函数

    最近在做国嵌的mp3项目,在mp3主控程序中用到了这个函数,挺好使的,在这里记录一下.注意是linux下的,不是C++中的. 函数原型 ssize_t getline(char **lineptr, ...

  8. linux下实现rm()函数删除文件或目录

    转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600866.html 在linux下有两个函数可以用来删除文件: #include <unistd.h ...

  9. linux下实现ls()函数遍历目录

    转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html 需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有 ...

随机推荐

  1. SASS 使用(vs code)

    二.在vs code中编译sass 1.在拓展商店里搜索“easy sass”,并安装,安装成功后点重新加载. 2.接下来进行配置: 在 vs code 菜单栏依次点击“文件 首选项 设置”,打开 s ...

  2. HDU-5693 D Game 动态规划 两次动规

    题目链接:https://cn.vjudge.net/problem/HDU-5693 题意 中文题 这个游戏是这样的,首先度度熊拥有一个公差集合{D},然后它依次写下N个数字排成一行.游戏规则很简单 ...

  3. 紫书 习题8-8 UVa 1612 (贪心+精度)

    这道题我很快就写出来了, 但是一直WA, 然后发现是精度, 这坑了我一个小时-- (1)贪心.每次就尽量分数高, 可以保证最后分数最高 (2)神tm精度问题.记住判断大于小于和等于的时候要用EPS(1 ...

  4. 常用Java开源库(新手必看)

    Jakarta common: Commons LoggingJakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具 ...

  5. HDU 1026 Ignatius and the Princess I(BFS+记录路径)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. Linux之我最常用的20条命令

    Linux之我最常用的20条命令 玩过 Linux的人都会知道, Linux中的命令的确是非常多,但是玩过 Linux的人也从来不会因为 Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的 ...

  7. Pleasant sheep and big big wolf

    pid=3046">点击打开链接 题目:在一个N * M 的矩阵草原上,分布着羊和狼.每一个格子仅仅能存在0或1仅仅动物.如今要用栅栏将全部的狼和羊分开.问怎么放,栅栏数放的最少,求出 ...

  8. .NET 框架简单介绍

    初学.NET肯定会有一系列的疑问,比方(下面为自己的疑问): 1) 何为. NET框架.它都包括哪些东西? 2) 程序集是什么.它是怎样在CLR(通用语言执行时)中执行的? 3) C#与VB.NET同 ...

  9. jsoncpp-src-0.5.0.tar.gz 源码错误!!!!

    近期在做毕设,使用到了JsonCpp0.5.0版本号的源码! 依照网上的安装配置教程,搭建好环境后就能够使用了! 在这里就不浪费空间去将怎样搭建开发环境了!请大家去google一下就好了! 在解析一个 ...

  10. Leaflet--建设移动设备友好的互动地图

    Leaflet 是一个为建设移动设备友好的互动地图,而开发的现代的.开源的 JavaScript 库.它是由 Vladimir Agafonkin 带领一个专业贡献者团队开发,尽管代码仅有 33 KB ...