📄 test_select.c
字号:
// 示例函数select()的使用
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
void display_time(const char *string)
{
int seconds;
seconds = time((time_t*)NULL);
printf("%s, %d\n", string, seconds);
}
int main(void)
{
fd_set readfds;
struct timeval timeout;
int ret;
/*监视文件描述符0是否有数据输入,即文件描述符0表示标准输入即键盘输入*/
FD_ZERO(&readfds); // 开始使用一个描述符集合前一般要将其清空
FD_SET(0, &readfds);
/*设置阻塞时间为10秒*/
timeout.tv_sec = 10;
timeout.tv_usec = 0;
while (1) {
display_time("before select");
ret = select(1, &readfds, NULL, NULL, &timeout);
display_time("after select");
switch (ret) {
case 0:
printf("No data in ten seconds.\n");
exit(0);
break;
case -1:
perror("select");
exit(1);
break;
default:
getchar(); // 将数据读入,否则标准输入上将一直为读就绪
printf("Data is available now.\n");
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -