Appendix A. More HTTP Server Examples
This appendix expands the contents of “Beating the C10k Problem” by providing more examples of HTTP servers. These examples are not essential to understand Chapter 5, but you might find them interesting. Also, some of these examples are included in the benchmarks.
fork() Procedure in C Language
We will try to implement a concurrent HTTP server using C.
If you are familiar with C, you will find the following program fairly straightforward.
Otherwise, do not worry, you are not obligated to understand all of the details, just the overall idea.
Invoking fork() makes a copy of the current process, so that suddenly two processes appear in the operating system: the original one (parent) and a child.
This second process has the exact same variables and state, the only difference is the result value of fork():
#include <signal.h>#include <stdlib.h>#include <string.h>#include <netinet/in.h>#include <unistd.h>#include <stdio.h>intmain(intargc,char*argv[]){signal(SIGCHLD,SIG_IGN);structsockaddr_inserv_addr;bzero((char*)&serv_addr,sizeof(serv_addr));serv_addr.sin_family=AF_INET;serv_addr.sin_addr.s_addr=INADDR_ANY;serv_addr.sin_port=htons(8080);intserver_socket=socket(AF_INET,SOCK_STREAM,0);if(server_socket<0){perror("socket");exit(1);}if(bind(server_socket,(structsockaddr*)&serv_addr,sizeof(serv_addr))<0){perror("bind");exit(1);}listen(server_socket,100);structsockaddr_incli_addr;socklen_t
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access