Epoll + multi-process for simple server

Source: Internet
Author: User

Recently, a colleague in the project team used epoll + multithreading to implement a simple server. However, after stress testing, it was found that if the edge trigger mode was used, packet loss would occur, the wait time for the horizontal Triggering setting does solve the packet loss problem, but it affects the server performance. So I wrote an epoll + multi-process model. The Code is as follows:

# Include <sys/socket. h> # include <sys/epoll. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <fcntl. h> # include <unistd. h> # include <stdio. h> # include <errno. h> # include <sys/types. h> # include <sys/Mman. h ># include <iostream> using namespace STD; # define max_events 10000 void process (INT listenfd); struct shmstruct {int count;}; int main (INT argc, char ** argv) {short Port = 6666; // default port if (argc = 2) {Port = atoi (argv [1]);} shm_unlink ("test"); int fd1 = shm_open ("test", o_rdwr | o_creat | o_excl, 666); struct shmstruct * PTR; ftruncate (fd1, sizeof (struct shmstruct); PTR = (struct shmstruct *) MMAP (null, sizeof (struct shmstruct), prot_read | prot_write, map_shared, fd1, 0); close (fd1 ); int listenfd = socket (af_inet, sock_stream, 0); fcntl (listenfd, f_setfl, o_nonblock); // set the non-blocking mode sockaddr_in sin; bzero (& sin, sizeof (SIN); sin. sin_family = af_inet; sin. sin_addr.s_addr = inaddr_any; sin. sin_port = htons (port); BIND (listenfd, (const sockaddr *) & sin, sizeof (SIN); Listen (listenfd, 5); For (INT I = 0; I <5; I ++) {pid_t pid = fork (); If (pid = 0) // sub-process {process (listenfd ); // working process return 0;} while (true) {sleep (10);} return 0;} void process (INT listenfd) {int fd1 = shm_open ("test ", o_rdwr, 666); struct shmstruct * PTR; PTR = (struct shmstruct *) MMAP (null, sizeof (struct shmstruct), prot_read | prot_write, map_shared, fd1, 0 ); close (fd1); struct epoll_event eV, events [max_events]; // generate a file descriptor int epfd = epoll_create (max_events) dedicated to processing the epoll of accept ); // set the file descriptor EV associated with the event to be processed. data. FD = listenfd; // set the event type EV to be processed. events = epollin | epollet; // register the epoll event if (epoll_ctl (epfd, epoll_ctl_add, listenfd, & eV) <0) {printf ("worker epoll_ctl error = % S. ", strerror (errno); exit (1) ;}while (true) {// wait for the occurrence of the epoll event int NFDs = epoll_wait (epfd, events, max_events, -1); // process all events that occur for (INT I = 0; I <NFDs; ++ I) // In the for loop, you can change it to the thread pool to process epoll events {If (events [I]. data. FD = listenfd) {socklen_t clilen; struct sockaddr_in clientaddr; int sockfd = accept (listenfd, (sockaddr *) & clientaddr, & clilen); If (sockfd <0) {continue;} // set non-blocking if (fcntl (sockfd, f_setfl, fcntl (sockfd, f_getfd, 0) | o_nonblock) =-1) {continue ;} // sets the file descriptor eV for read operations. data. FD = sockfd; // set the read operation event eV for the injection test. events = epollin | epollet; // register evepoll_ctl (epfd, epoll_ctl_add, sockfd, & eV);} else if (events [I]. events & epollin) {int sockfd = events [I]. data. FD; If (sockfd <0) {continue;} Char Buf [1024] = {0}; // start to process the data sending and receiving bzero (BUF, sizeof (BUF); int Len = read (sockfd, Buf, 1023); If (LEN <0) {If (errno = econnreset) {close (sockfd ); events [I]. data. FD =-1;} else {printf ("worker read data error = % S. ", strerror (errno) ;}} else if (LEN = 0) {events [I]. data. FD =-1;} else {If (send (sockfd, "ASDF", 4, 0) <0) {printf ("Send MSG error: % s (errno: % d) \ n ", strerror (errno), errno); exit (0);} printf (" count: % d \ n ", + PTR-> count );}}}}}

  

Note: the code can be compiled normally under RedHat. If other platforms are using this function, you may need to modify the code in the shared memory section, because the absolute path is required for creating shared memory on some Linux platforms.

Through the test, the results of the stress test are linearly related to the number of sub-processes, and also to the server configuration. The machine I use is under five sub-processes, up to 5000 of the concurrency is allowed without packet loss. In fact, if the thread pool is used in the for loop for business processing, the number of sub-processes that may be enabled can reach a large number of concurrency without needing to be too many, this epoll + multi-process + thread pool is also the epoll model currently used by most servers. When the time is up, write a model that is added to the thread pool and test it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.