1. 统一事件源:
1 #include <sys/types.h> 2 #include <sys/socket.h> 3 #include <netinet/in.h> 4 #include <arpa/inet.h> 5 #include <assert.h> 6 #include <stdio.h> 7 #include <signal.h> 8 #include <unistd.h> 9 #include <errno.h> 10 #include <string.h> 11 #include <fcntl.h> 12 #include <stdlib.h> 13 #include <sys/epoll.h> 14 #include <pthread.h> 15 16 #define MAX_EVENT_NUMBER 1024 17 static int pipefd[2]; 18 19 int setnonblocking( int fd ) 20 { 21 int old_option = fcntl( fd, F_GETFL ); 22 int new_option = old_option | O_NONBLOCK; 23 fcntl( fd, F_SETFL, new_option ); 24 return old_option; 25 } 26 27 void addfd( int epollfd, int fd ) 28 { 29 epoll_event event; 30 event.data.fd = fd; 31 event.events = EPOLLIN | EPOLLET; 32 epoll_ctl( epollfd, EPOLL_CTL_ADD, fd, &event ); 33 setnonblocking( fd ); 34 } 35 36 void sig_handler( int sig ) 37 { 38 int save_errno = errno; 39 int msg = sig; 40 send( pipefd[1], ( char* )&msg, 1, 0 ); 41 errno = save_errno; 42 } 43 44 void addsig( int sig ) 45 { 46 struct sigaction sa; 47 memset( &sa, '