Files
Driver-tutorial/select_poll/poll1.c

46 lines
1020 B
C

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
int main(){
int fd1, fd2;
struct pollfd pollarray[10];
char buf[10];
int n;
fd1 = open("./pone", O_RDWR);
fd2 = open("./ptwo", O_RDWR);
pollarray[0].fd = fd1;
pollarray[1].fd = fd2;
pollarray[0].events = POLLIN;
pollarray[1].events = POLLIN;
n = poll(pollarray, 2, 10000);
if(n < 0){
perror("poll:");
exit(1);
}
//test whether fd1 is ready or not
if( (pollarray[0].revents & POLLIN)){
printf(" reading from fd1 (pone)\n");
n = read(fd1, buf, 10);
printf(" read data = %s from pone\n", buf);
}
//test whether fd2 is ready or not
if( (pollarray[1].revents & POLLIN)){
printf(" reading from fd2 (ptwo)\n");
n = read(fd2, buf, 10);
printf(" read data = %s from ptwo\n", buf);
}
// close(fd1);
// close(fd2);
return 0;
}