epoll_wait()事件仅在gnome终端上的enter键按下事件时触发。无需使用Enter键如何进行这项工作?下面的代码演示了这种行为。
#include <sys/epoll.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
char buffer[10];
int stdinfd = 0;
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = 0;
int epollfd = epoll_create(1);
if (epollfd == -1) {
printf("Failure to create epoll object");
exit(-1);
}
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, stdinfd, &event) == -1) {
printf("unable to epoll_ctrl\n");
exit(-2);
}
printf("Entering for loop\n");
for (;;) {
int epoll_return = epoll_wait(epollfd, &event, 1, 1000/* 1 sec timout */);
printf("epoll_wait() = %d\n", epoll_return);
if (epoll_return < 1) {
continue;
}
printf("event->events: %d\n", event.events);
int read_bytes = read(stdinfd, buffer, sizeof(buffer));
printf("read bytes: %d\n", read_bytes);
printf("first character: %c\n", buffer[0]);
}
}