23,223
社区成员
发帖
与我相关
我的任务
分享
#include<sys/stat.h>
#include<sys/types.h>
#include<poll.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
char buf[20]={0};
int f(const char* fifo){
unlink(fifo);
mkfifo(fifo,0666);
return open(fifo,O_RDONLY);
}
void g(int fd){
read(fd,buf,sizeof(buf));
printf("read done:%s\n",buf);
}
int main(void){
int fd1=f("myfifo1");
int fd2=f("myfifo2");
if(!(fd1||fd2))return 1;
struct pollfd myfifo[2];
myfifo[0].fd=fd1;
myfifo[0].events=POLLIN;
myfifo[1].fd=fd2;
myfifo[1].events=POLLIN;
int ret;
while(ret=poll(myfifo,fd2+1,-1)){
if(ret<=0){
perror("poll error\n");
return 1;
}else{
if( myfifo[0].revents & POLLIN){g(fd1);}
else if(myfifo[0].revents & POLLIN){g(fd2);}
}
}
close(fd1);
close(fd2);
return 0;
}