MatthewCS / CS444-webserver

Final project for CS444 @ Clarkson University, Spring 2022
1 stars 0 forks source link

Project Requirement 2: Multi-threading #2

Open JeffResc opened 2 years ago

JeffResc commented 2 years ago

Part 2: Multi-threading

As you may have already noticed, the server now can serve only one browser. Obviously, this does not make much sense for a web server at all. Meanwhile, the browser cannot receive and show any incoming data unless you first input something, which is inconvenient. Imagine when you are opening the Clarkson email page in the browser, it cannot receive any incoming emails unless you send one first. Therefore, we need to make our application able to work on different stuff in parallel.

Detailed Tasks:

  1. Implement multithreading in start_server() in server.c so that our server can handle more than one browser at the same time. More specifically, in start_server(), create a thread to run browser_handler().
  2. Identify the critical sections where different threads may read from/write to the same shared static array browser_list and session_list at the same time in browser_handler(), which therefore need to be protected by the locking mechanism. Note that we already initialized browser_list_mutex and session_list_mutex for you to use. You only need to place the lock and unlock code around the critical sections identified in browser_handler(). Try to make your critical sections as small as possible, which means you should make sure every line of code that needs to be locked is really critical; including uncritical code may cause you to lose points.
  3. Implement multithreading in start_browser() in browser.c so that all incoming messages can be received and outputted automatically. More specifically, in start_browser(), move server_listener() to a proper place, create a thread to run it. Meanwhile, in server_listener(), uncomment the loop code that was temporarily commented out so that it works for multithreading.

Questions:

  1. Why do we need locks for browser_list and session_list in browser_handler()? What could happen if there is no lock?
  2. Can we use multiprocessing instead of multithreading here? What are the differences between multiprocessing and multithreading?

Put your answers to the above two questions in your report.

JeffResc commented 2 years ago

See #7.