Linux(服务器编程):百万并发服务器系统参数调优( 二 )

< MAX_EPOLL_EVENTS;i ++) { if (reactor->events[i].status == 0) { break;}} if (i == MAX_EPOLL_EVENTS) { printf("%s: max connect limit[%d]\n", __func__, MAX_EPOLL_EVENTS); break;}int flag = 0; if ((flag = fcntl(clientfd, F_SETFL, O_NONBLOCK)) < 0) { printf("%s: fcntl nonblocking failed, %d\n", __func__, MAX_EPOLL_EVENTS); break;}nty_event_set(nty_event_add(reactor->epfd, EPOLLIN,} while (0); printf("new connect [%s:%d][time:%ld], pos[%d]\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), reactor->events[i].last_active, i); return 0;} int init_sock(short port) { int fd = socket(AF_INET, SOCK_STREAM, 0); fcntl(fd, F_SETFL, O_NONBLOCK); struct sockaddr_in server_addr; memset( server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(port); bind(fd, (struct sockaddr*) if (listen(fd, 20) < 0) { printf("listen failed : %s\n", strerror(errno)); } return fd;} int ntyreactor_init(struct ntyreactor *reactor) { if (reactor == NULL) return -1; memset(reactor, 0, sizeof(struct ntyreactor)); reactor->epfd = epoll_create(1); if (reactor->epfd <= 0) { printf("create epfd in %s err %s\n", __func__, strerror(errno)); return -2; } reactor->events = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent)); if (reactor->events == NULL) { printf("create epfd in %s err %s\n", __func__, strerror(errno));close(reactor->epfd); return -3; } return 0;} int ntyreactor_destroy(struct ntyreactor *reactor) { close(reactor->epfd); free(reactor->events);} int ntyreactor_addlistener(struct ntyreactor *reactor, int sockfd, NCALLBACK *acceptor) { if (reactor == NULL) return -1; if (reactor->events == NULL) return -1; nty_event_set( nty_event_add(reactor->epfd, EPOLLIN,return 0;} int ntyreactor_run(struct ntyreactor *reactor) { if (reactor == NULL) return -1; if (reactor->epfd < 0) return -1; if (reactor->events == NULL) return -1; struct epoll_event events[MAX_EPOLL_EVENTS+1]; int checkpos = 0, i; while (1) { long now = time(NULL); for (i = 0;i < 100;i ++, checkpos ++) { if (checkpos == MAX_EPOLL_EVENTS) {checkpos = 0;} if (reactor->events[checkpos].status != 1) { continue;} long duration = now - reactor->events[checkpos].last_active; if (duration >= 60) {close(reactor->events[checkpos].fd); printf("[fd=%d] timeout\n", reactor->events[checkpos].fd);nty_event_del(reactor->epfd,}} int nready = epoll_wait(reactor->epfd, events, MAX_EPOLL_EVENTS, 1000); if (nready < 0) { printf("epoll_wait error, exit\n"); continue;} for (i = 0;i < nready;i ++) { struct ntyevent *ev = (struct ntyevent*)events[i].data.ptr; if ((events[i].events} if ((events[i].events}} }} int main(int argc, char *argv[]) { unsigned short port = SERVER_PORT; if (argc == 2) {port = atoi(argv[1]); } int sockfd = init_sock(port); struct ntyreactor *reactor = (struct ntyreactor*)malloc(sizeof(struct ntyreactor)); ntyreactor_init(reactor); ntyreactor_addlistener(reactor, sockfd, accept_cb); ntyreactor_run(reactor); ntyreactor_destroy(reactor); close(sockfd); return 0;}mul_port_client_epoll.c