概述
#include <sys/wait.h>
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <sys/times.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
/**
* https://man7.org/linux/man-pages/man7/epoll.7.html
The following system calls are provided to create and manage an
epoll instance:
• epoll_create(2) creates a new epoll instance and returns a file
descriptor referring to that instance. (The more recent
epoll_create1(2) extends the functionality of epoll_create(2).)
• Interest in particular file descriptors is then registered via
epoll_ctl(2), which adds items to the interest list of the
epoll instance.
• epoll_wait(2) waits for I/O events, blocking the calling thread
if no events are currently available. (This system call can be
thought of as fetching items from the ready list of the epoll
instance.)
*/
#define MAX_EVENTS 10
int setnonblocking(int fd) {
int flags;
if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
flags = 0;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
void do_use_fd(int fd, char *buffer) {
printf("do_use_fd n");
while (1) {
int length = read(fd, buffer, 1023);
if (length <= 0) {
printf("read error n");
perror("read error");
break;
}
buffer[length] = '