Question: In this project, you will design a multithreaded webserver in C on Unix/Linux Platform. Your server program should run as follows: server [portnum] [threads] [buffers]

![on Unix/Linux Platform. Your server program should run as follows: server [portnum]](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f93d3ab50e6_17066f93d3a32724.jpg)
![[threads] [buffers] [schedalg] The command line arguments to your web server are](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f93d3b9c72f_17166f93d3b068b9.jpg)
In this project, you will design a multithreaded webserver in C on Unix/Linux Platform. Your server program should run as follows: server [portnum] [threads] [buffers] [schedalg] The command line arguments to your web server are to be interpreted as follows. - portnum: the port number that the web server should listen on; the basic web server already handles this argument. - threads: the number of worker threads that should be created within the web server. Must be a positive integer. - buffers: the number of request connections that can be accepted at one time. Must be a positive integer. Note that it is not an error for more or less threads to be created than buffers. - schedalg: the scheduling algorithm to be performed. Must be one of ANY, FIFO, HPSC, or HPDC. For example, if you run your program as server 5003816 FIFO A Brief Overview of HTTTP: Web browsers and web servers interact using a text-based protocol called HTTP (Hypertext Transfer Protocol). A web browser opens an Internet connection to a web server and requests some content with HTTP. The web server responds with the requested content and closes the connection. The browser reads the content and displays it on the screen. Each piece of content on the server is associated with a file. An HTTP request (from the web browser to the server) consists of a request line, followed by zero or more request headers, and finally an empty text line. A request line has the form: method uri version. The method is usually GET (but may be other things, such as POST, OPTIONS, or PUT). The uri is the file name and any optional arguments (for dynamic content). Finally, the version indicates the version of the HTTP protocol that the web client is using (e.g., HTTP/1.0 or HTTP/1.1). An HTTP response (from the server to the browser) is similar; it consists of a response line, zero or more response header lines, an empty text line, and finally the interesting part, the response body. A response line has the form version status message. The status is a three-digit positive integer that indicates the state of the request; some common states and the corresponding messages are 200 for OK, 403 for Forbidden, and 404 for Not found. Two important lines in the response header are ContentType, which tells the client the MIME type of the content in the response body (e.g., html or gif) and Content-Length, which indicates its size in bytes. The server can add any custom header line. You have to design a simple http webserver that would process web request for atleast Get and head (You may choose to include more methods such as PUT, POSTS etc.) Follow http specification from here: Phase 2: Multithreaded Server Single-threaded web servers suffer from a fundamental performance problem in that only a single HTTP request can be serviced at a time. Thus, every other client that is accessing this web server must wait until the current http request has finished; the most important extension that you will be adding is to make the basic web server multi-threaded. The server will consist of 2+n threads. The first two threads will do following jobs: Thread 1: Thread 1 will be responsible for listening to incoming http connection and inserting them into a fixed size queue. This thread is a main thread that also creates a pool of n worker threads. Thread 2: will be responsible for choosing a thread from the ready queue and scheduling it to one of the worker thread. n-worker threads: A pool of n worker threads will serve the incoming requests. The number n is given as a parameter when the webserver starts. Each worker thread is able to handle both static and dynamic requests. A worker thread wakes when there is an http request in the queue; when there are multiple http requests available, which request is handled depends upon the scheduling policy. The worker thread then waits for another http request. Scheduling: Note that when your web server has multiple worker threads running (the number of which is specified on the command line), you will not have any control over which thread is actually scheduled at any given time by the OS. Your role in scheduling is to determine which http request should be handled by each of the waiting worker threads in your web server. The scheduling policy is determined by a command line argument when the web server is started and are as follows: - Any Concurrent Policy (ANY) : When a worker thread wakes, it can handle any request in the buffer. The only requirement is that all threads are handling requests concurrently. (In other words, you can make ANY=FIFO if you have FIFO working.) - First-in-First-out (FIFO) : When a worker thread wakes, it handles the first request (i.e., the oldest request) in the buffer. Note that the http requests will not necessarily finish in FIFO order since multiple threads are running concurrently; the order in which the requests complete will depend upon how the OS schedules the active threads. - Highest Priority to Static content (HPSC) : When a worker thread wakes, it handles the first request that is static content; if there are no requests for static content, it handles the first request for dynamic content. Note that this algorithm can lead to the starvation of requests for dynamic content. - Highest Priority to Dynamic Content (HPDC): When a worker thread wakes, it handles the first request that is dynamic content; if there are no requests for dynamic content, it handles the first request for static content. Note that this algorithm can lead to the starvation of requests for static content. Important notes: - You should investigate how to create and manage posix threads with pthread_create and pthread_detach. - The master threads and the worker threads are in a producer-consumer relationship and require that their accesses to the shared buffer be synchronized. Specifically, the master thread must block and wait if the buffer is full; a worker thread must wait if the buffer is empty. In this project, you are required to use condition variables. If your implementation performs any busy-waiting (or spin-waiting) instead, you will be heavily penalized. - HPSC and SPDC policies require that something be known about each request before the requests can be scheduled. Thus, to support this scheduling policy, you will need to do some initial processing of the request outside of the worker threads; you will want the master thread to perform this work, which requires that it read from the network descriptor
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
