-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll_test.c
More file actions
50 lines (39 loc) · 1.01 KB
/
poll_test.c
File metadata and controls
50 lines (39 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define device "/dev/char_device"
int main(int argc, char **argv) {
char buffer[256];
int file_desc, i, n;
short revents;
struct pollfd poll_fd;
/* Open '/dev/char_device' */
file_desc = open(device, O_RDONLY);
/* If file can not be opened, throw error */
if (file_desc == -1) {
perror("open");
exit(EXIT_FAILURE);
}
/* Set poll fd and events */
/* !!! events: requested events, revents: returned events !!!*/
poll_fd.fd = file_desc;
poll_fd.events = POLLIN;
while (1) {
printf("Poll going to sleep...\n");
i = poll(&poll_fd, 1, -1); // (fd, # of fds, timeout)
if (i == -1) {
perror("error @ poll");
exit(EXIT_FAILURE);
}
revents = poll_fd.revents;
/* If there is anything to read (POLLIN event), read the buffer and print */
if (revents & POLLIN) {
printf("Poll woke up...\n");
n = read(poll_fd.fd, buffer, 1);
printf("POLLIN - Char = %d,%c \n", n, buffer[0]);
}
}
return 0;
}