您的位置:首页 > 其它

多路转接之poll和select

2015-03-29 21:03 211 查看
先看poll():

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <poll.h>

#define oops(x, n) { perror(x); exit(n); }

int main()
{
mkfifo("fifo1", 0644);
mkfifo("fifo2", 0644);

int fd1 = open("fifo1", O_RDWR);
int fd2 = open("fifo2", O_RDWR);
char buf[1000];
for(;;)
{
struct pollfd pfd[] = {{0, POLLIN}, {fd1, POLLIN}, {fd2, POLLIN}};
int ret = poll(pfd, 3, 1);
if(ret > 0)
{
if(pfd[0].revents&POLLIN)
{
scanf(" %[^\n]", buf);
printf("keypad:%s\n", buf);
if(strcmp(buf, "quit") == 0) break;
}
if(pfd[1].revents&POLLIN)
{
int n = read(fd1, buf, sizeof(buf));
buf
= '\0';
printf("fifo1:%s", buf);
}
if(pfd[2].revents&POLLIN)
{
int n = read(fd2, buf, sizeof(buf));
buf
= '\0';
printf("fifo2:%s", buf);
}
}

}
close(fd1);
close(fd2);
unlink("fifo1");
unlink("fifo2");

return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: