mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-24 02:35:58 +01:00
poll sketch
This commit is contained in:
37
kernel_module/test/poll.c
Normal file
37
kernel_module/test/poll.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <fcntl.h> /* creat, O_CREAT */
|
||||
#include <poll.h> /* poll */
|
||||
#include <stdio.h> /* printf, puts, snprintf */
|
||||
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
||||
#include <unistd.h> /* read */
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char buf[1024];
|
||||
int fd, n;
|
||||
short revents;
|
||||
struct pollfd pfd;
|
||||
|
||||
fd = open(argv[1], O_RDONLY | O_NONBLOCK);
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLIN;
|
||||
while (1) {
|
||||
puts("loop");
|
||||
poll(&pfd, 1, -1);
|
||||
revents = pfd.revents;
|
||||
if (revents & POLLIN) {
|
||||
n = read(pfd.fd, buf, sizeof(buf));
|
||||
printf("POLLIN n=%d buf=%.*s\n", n, n, buf);
|
||||
}
|
||||
if (revents & POLLHUP) {
|
||||
printf("POLLHUP\n");
|
||||
close(pfd.fd);
|
||||
break;
|
||||
}
|
||||
if (revents & POLLNVAL) {
|
||||
printf("POLLNVAL\n");
|
||||
}
|
||||
if (revents & POLLERR) {
|
||||
printf("POLLERR\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user