title: Concurrency in C
body: <p>I want to make a simple web server in C that handles requests concurrently. So far I got the following code that forks the process and executes the request_handle in the child process. (That is at least what I aim to have) but still, executing a request blocks the cycle and until the first request is completed, the second request is not accepted. What should I fix to allow the web server to handle requests concurrently?</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &quot;request.h&quot;
  #include &quot;io_helper.h&quot;
  
  char default_root[] = &quot;.&quot;;
  
  //
  // ./wserver [-p &lt;portnum&gt;] [-t threads] [-b buffers] 
  // 
  // e.g.
  // ./wserver -p 2022 -t 5 -b 10
  // 
  int main(int argc, char *argv[]) {
      int c;
      char *root_dir = default_root;
      int port = 10000;
      int threads = 2;
      int buffer_size = 5;
  
      while ((c = getopt(argc, argv, &quot;p:t:b:&quot;)) != -1)
          switch (c) {
              case 'p':
                  port = atoi(optarg);
                  break;
              case 't':
                  threads = atoi(optarg);
                  break;
              case 'b':
                  buffer_size = atoi(optarg);
                  break;
              default:
                  fprintf(stderr, &quot;usage: ./wserver [-p &lt;portnum&gt;] [-t threads] [-b buffers] \n&quot;);
                  exit(1);
              }
  
  
  
      printf(&quot;Server running on port: %d, threads: %d, buffer: %d\n&quot;, port, threads, buffer_size);
  
      // run out of this directory
      chdir_or_die(root_dir);
  
      // now, get to work
      int listen_fd = open_listen_fd_or_die(port);
      while (1) {
          struct sockaddr_in client_addr;
          int client_len = sizeof(client_addr);
          int conn_fd = accept(listen_fd, (sockaddr_t *) &amp;client_addr, (socklen_t *) &amp;client_len);
  
          pid_t child_pid;
          child_pid = fork();
  
          if (child_pid == 0) {
              close(listen_fd);
              request_handle(conn_fd);
              close(conn_fd);
              exit(0);
          } else {
              close(conn_fd);
          }
      }
      close(listen_fd);
      return 0;
  }
  </code></pre>