多任务I/O之poll函数

#include <stdio.h>
#include <unistd.h>
#include <sys/poll.h>

int main(void){
//要等待的文件的数组
struct pollfd fds[2];
int ret;

fds[0].fd = STDIN_FILENO;//文件符
fds[0].events = POLLIN;//等待的事件类型

fds[1].fd = STDOUT_FILENO;
fds[1].events = POLLOUT;

ret = poll(fds, 1, 5000);
if(ret == -1){
perror("poll");
return 1;
}

if(!ret){
printf("time out\n");
return 0;
}

if(fds[0].revents & POLLIN){
printf("can read\n");
}
if(fds[1].revents & POLLOUT){
printf("can write\n");
}
return 0;
}
原文地址:https://www.cnblogs.com/ggzwtj/p/2204126.html