12.35

see origin tiny server in section 11.6 on book

the key is close(connfd) in both parent and child process to ensure close fd and reuse it or it’ll failed: Accept error: too many open files

--- 12.tiny.c	2021-02-25 07:26:33.305926066 +0000
+++ 12.35.c	2021-02-25 07:26:33.302592754 +0000
@@ -1,6 +1,8 @@
 /*
- * tiny.c - A simple, iterative HTTP/1.0 Web server that uses the
+ * 12.35.c - A simple, iterative HTTP/1.0 Web server that uses the
  *     GET method to serve static and dynamic content.
+ *
+ *     concurrent server in multi process way.
  */
 #include "csapp.h"
 
@@ -35,8 +37,14 @@
     Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE,
         port, MAXLINE, 0);
     printf("Accepted connection from (%s, %s)\n", hostname, port);
-    doit(connfd);                                             //line:netp:tiny:doit
-    Close(connfd);                                            //line:netp:tiny:close
+
+    if (Fork() == 0) {
+      Close(listenfd);
+      doit(connfd);                                             //line:netp:tiny:doit
+      Close(connfd);                                            //line:netp:tiny:close
+      exit(0);
+    }
+    Close(connfd);
   }
 }
 

run server

(cd ./site/content/chapter12/code; make && ./12.35)

open another terminal and benchmark it

wrk -d4 http://localhost:5000

output

Running 4s test @ http://localhost:5000
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    62.20ms   87.89ms 348.01ms   82.24%
    Req/Sec   397.69    184.95   767.00     58.33%
  2585 requests in 4.00s, 578.09KB read
Requests/sec:    645.64
Transfer/sec:    144.39KB
comments powered by Disqus