Code path: https://github.com/prophetss/epoll-event
Before implementing a simple and efficient hashtable (point here), and then always want to use it to extend some of the features, and then accidentally see a Hashtable and Epoll event of the combination of feeling very efficient so that they try to achieve the next. The general idea is to store every service request Epoll received in the Hashtable to be managed, each request can be set independent callback function. You can see the code first, the comments have been written in detail. The code implements a simple instance, as the client side I fork a lot of sub-processes to mimic high concurrent requests due to limited conditions. Performance testing I have time to do this a few days, probably tried to 1s hundreds of thousands of times the request should be possible. Temporarily first write here, the following first put a client source code to fill the size.
1#include <unistd.h>2#include <sys/mman.h>3#include <netinet/inch.h>4#include <semaphore.h>5#include <sys/types.h>6#include <sys/socket.h>7#include <arpa/inet.h>8#include <fcntl.h>9#include <wait.h>Ten#include <string.h> One#include <netdb.h> A#include <stdlib.h> -#include"Error.h" - the - /*process shared files are used to count the number of creation processes*/ - #definePfile_name "Count" - + - structShared { +Sem_t Mutex;/*signal volume for lock*/ A intCount/*Number of processes*/ at } gkfx; - - - voidRequestConst Char*SERVER_IP,intserver_port) - { - structsockaddr_in client_addr; inBzero (&CLIENT_ADDR,sizeof(CLIENT_ADDR)); -client_addr.sin_family =af_inet; toCLIENT_ADDR.SIN_ADDR.S_ADDR =Inaddr_any; +Client_addr.sin_port = htons (0); - the intClient_socket = socket (af_inet, Sock_stream,0); * if(Client_socket <0) Sys_exit_throw ("Create client socket fail"); $ Panax Notoginseng structsockaddr_in server_addr; -Bzero ((Char*) &server_addr,sizeof(SERVER_ADDR)); the +server_addr.sin_family =af_inet; A the structHostent *server =gethostbyname (server_ip); + if(!server) Sys_exit_throw ("fail to get host name"); - $Bcopy ((Char*) Server->h_addr, (Char*) &server_addr.sin_addr.s_addr, server->h_length); $ -Server_addr.sin_port =htons (server_port); -socklen_t Server_addr_len =sizeof(SERVER_ADDR); the - if(Connect (Client_socket,structsockaddr*) &server_addr, server_addr_len) = =-1 ) {WuyiSys_exit_throw ("connent to server fail"); the } - Wu intPID =getpid (); - About Charcontent[ -] = {0}; $sprintf (Content,"%s, pid:%d","I am client! ", PID); - -Send (Client_socket, content, strlen (content),0); - A Close (client_socket); + } the - /* $ parameter 1 is serverip, parameter 2 is the server port number the */ the intMainintargcChar*argv[]) the { the if(ARGC! =3) Exit_throw ("parameter error!\n"); - in Char*SERVER_IP = argv[1]; the intServer_port = Atoi (argv[2]); the About structShared *PSH; the the /*Create a shared file*/ the intFD = open (Pfile_name, O_RDWR | O_creat,0666); + /*Initialize*/ -Write (FD, &shared,sizeof(structshared)); the /*Mapping Memory*/BayiPSH = Mmap (NULL,sizeof(structShared), Prot_read | Prot_write, map_shared, FD,0); the Close (FD); the -Sem_init (&psh->mutex,1,1); - the /*Initial process +1*/ thepsh->count++; the the inti, status; - for(i =0; I <Ten; i++) { thepid_t Fpid =fork (); the if(0==fpid) { theSem_wait (&psh->mutex);94psh->count++; theprintf"%d processes have created!\n", psh->count); theSem_post (&psh->mutex); the request (SERVER_IP, server_port);98 } About Else if(Fpid >0) { - request (SERVER_IP, server_port);101Wait (&status);102 }103 Else104Sys_exit_throw ("Fork error!"); the }106 107 return 0;108}
A high concurrency service/client (C language Implementation, server-side storage based on Hashtable) implemented by a Epoll event