hw5
.pdf
keyboard_arrow_up
School
University of Illinois, Chicago *
*We aren’t endorsed by this school
Course
362
Subject
Computer Science
Date
Apr 3, 2024
Type
Pages
5
Uploaded by KaruleanZ
Homework 5 - Concurrent Server with Threads
CS 361 - Fall 2023 - Maratos
Description
In this assignment we will be implementing a server that handles client requests concurrently.
You will use your solution to HW4 as the starting point.
1
Threading Models
Your solution will implement two models for managing threads to handle client requests.
1. One Thread Per Connection - In this mode, each client request will be handled by a
single thread that was created for this task. The peer thread will read the request from
the socket and handle it accordingly.
After the transaction is done the peer thread
should terminate.
2. Thread Pool - In this mode, the server is initialized with a main thread and a pool of N
peer threads. The main thread accepts connection requests and makes the connection
available for the peer threads. The peer threads run the following loop: 1. wait for a
new client, 2. handle the client, 3. close the connection to the client, and 4. loop back
and wait for another client. We will discuss the producer-consumer design pattern in
class and you will use that approach in your implementation for this homework.
2
Concurrency Requirements
If any client accesses the following URL’s they should be handled concurrently and without
interfering with each other:
/ping
,
/echo
, and generic GET requests for files. For example, if
Client 1 requests
/ping
and another requests
/echo
then they will be handled concurrently.
3
Implementation Guide
•
Start by implementing the
thread per connection
model for your concurrent server. It
is only tests 9 and 10 that are relevant to the
thread pool
model.
•
Figure out what data is shared amongst all the threads.
–
try to minimize the number of shared data regions by making variables private
–
some data will have to be shared and in those circumstances you should be iden-
tifying critical regions in your code
•
Minimize the number of critical regions in your code and keep them small.
•
There are two drop in replacements for
recv
and
send
and you must use them in your
code.
1
–
int recv_http_request(int connfd, char * buffer, int max_size, int opts)
–
int send_fully(int connfd, const char * data, int size, int opts)
4
Safety Requirements
4.1
Safe Read/Write
Reading or writing data should be atomic which means that the operation should either
happen completely or not at all. Consider the following scenarios.
Scenario 1 - Good Write
1. Client 1 executes in parallel with Client 2
2. Client 1 performs a
/write
with data
aaaaaaaaaaa
3. Client 2 performs a
/write
with data
bbbbbbbbbbb
4. Client 3 performs a
/read
5. Client 3 receives either
aaaaaaaaaaa
or
bbbbbbbbbbb
Scenario 2 - Bad Write
(this should not happen)
1. Setup is the same as Scenario 1 through Step 4
2. Client 3 reads a combination of
a
and
b
. For example
aaabbbb...
Scenario 3 - Good Read
1. Client 1 performs a
/write
with data
aaaaaaaaaaa
2. Client 2 and 3 execute in parallel
3. Client 2 performs a
/write
with data
bbbbbbbbbbb
4. Client 3 performs a
/read
5. Client 3 receives either
aaaaaaaaaaa
or
bbbbbbbbbbb
Scenario 4 - Bad Read
(this should not happen)
1. Setup is the same as Scenario 3 through Step 4
2. Client 3 reads a combination of
a
and
b
. For example
aaabbbb...
2
4.2
Safe stats
You can assume that
/stats
is only accessed by a single client without any other concurrent
clients. When a client requests
/stats
your server should account for all previous requests
accurately. Consider the following scenarios below.
Scenario 1 - Good stats
1. There are 100 clients handled concurrently. 50 of the resulting transactions transfer 10
header bytes and 10 body bytes. 50 transactions are errors resulting in transferring 10
bytes.
2. After all 100 clients finish another client accesses
/stats
.
3. The server responds that there were 50 requests, 50 errors, 500 header bytes, 500 body
bytes, and 500 error bytes.
Scenario 2 - Bad stats
1. Same as Scenario 1 from Steps 1 and 2.
2. Any of the statistics are reported differently than Scenario 1 Step 3.
5
Quick Explanations of Test Cases
Many of these tests are checking for the same functionality that was described in HW 4.
While functionality is the same for many scenarios, the server is implemented in a different
way by handling clients with threads instead of I/O multiplexing. You can use HW 4 as a
reference for what many of the HTTP responses would look like.
5.1
One
These test cases just make sure your implementation can handle the following with a single
thread.
1. accepting connection requests
2. ping requests
This will require adapting some of our solution from HW 4.
See the introduction video
for HW 5 which solves this test case. Remember for future tests that you should initialize
global variables and create threads in
create_server_socket
. Remember that you should
use the utility methods for reading and writing HTTP responses:
recv_http_request
and
send_fully
instead of calling
send
and
recv
directly.
5.2
Two, Three, Four, and Five
These tests will check if your server supports most of the features you had implemented for
HW4:
/ping
,
/echo
, generic file with
GET
, and simply accepting connections.
3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
5.3
Six, Seven, and Eight
These tests will check your server’s ability to handle reads and writes and report statistics.
Make sure you are following the advice we gave in Section 3 on handling shared data and
critical regions. Furthermore, tests seven and eight can pass with an incorrect implementation
so to get credit for these you need to answer two questions which are described in Section 6
and submit your answers to Gradescope.
Test eight checks your implementation for
/stats
. It is worth mentioning that you could
be failing tests because of a mismatch in versions of
curl
on your local machine.
In that
situation make sure to double check with GitHub to see if you are actually failing the test or
simply have this discrepancy.
5.4
Nine and Ten
These tests work a little differently than the previous. Make sure you read through Section 7
to understand how they work.
6
Questions
Tests 7 and 8 are non-deterministic and they can pass with incorrect solutions depending on
thread scheduling. To get credit for these tests you need to answer the following questions
and submit them to Gradescope.
1. What measures does your solution take to ensure safe read/write (Described in Section
4.1) while maintaining the requirements for concurrency?
2. What measures does your solution take to ensure safe stats (Described in Section 4.2)
while maintaining the requirements for concurrency?
7
Important Comments on Thread Pool
Once you have implemented and passed the first 8 tests you can work on your thread pool
implementation.
This is done by modifying the
makefile
and changing the first line from
FLAG=T910=0
to
FLAG=T910=1
. This will change the test environment so that
threads can only
be created at the start in
create_server_socket
.
Furthermore, any thread that exits will
cause the server to terminate. In the Thread Pool model, threads are supposed to be long
lived and exist for the lifetime of the server. With this flag set your implementation will most
likely fail previous test cases and you will need to fix it. Test
09
corresponds to tests 1-4 and
Test
10
corresponds to tests 5-8.
4
8
Entry Point
You will do your work in
hw5.c
and make a small change to
makefile
(see Section 7) when
you start working on the thread pool model for tests 09 and 10.
9
Due Date and Resubmission Policy
•
The deadline for this homework is 11/22 11:59 PM. There is no late policy.
There will
be no extensions granted for this assignment.
•
Your submission is the last commit you made in your repository.
We will use the
timestamp of this commit to verify the assignment was completed on time.
•
Assuming you have attended
Labs 09, 10, and 11
you can resubmit your work up to
one week after the deadline, 11/29 11:59 PM. You must also submit a written document
that describes the changes you made since the deadline.
The written document is
how you can demonstrate your understanding of what was missing in your original
submission.
10
Bonus Points
•
You can earn up to 10% bonus points from endorsed posts you make on Piazza. Re-
member to claim those points by filling out the form mentioned in the Syllabus -
https://forms.gle/DgCzbVFp6jZYS6UQ8
•
You can earn 15% bonus points for completing Labs 09, 10, and 11.
11
Hardcoding and Academic Integrity
Just as a reminder, the syllabus policy on academic integrity will be enforced.
Also, if we
discover that you are subverting the goal of the assignment and gaming the autograder to
simply earn points is a zero on the assignment. If your code is trying to detect that a certain
input is given (because you know it is associated with a specific test) and simply output an
expected result (without doing the reasonable work required by the assignment) then that is
gaming the autograder. If you have further questions on what would be considered ”gaming”
then post them on Piazza as soon as you have them.
5
Related Questions
THREADS in C PROGRAM HELP!
Using the thread3.c program provided below, itt will require some code modification. Those modifications include:
- instead of 10,000 threads, create 40,000 threads in total
- instead of creating just one kind of thread, create 4 kinds of threads (each with 10,000 instances)
o group 1: adds 1 to counter
o group 2: adds 5 to counter
o group 3: subtracts 2 from counter
o group 4: subtracts 10 from counter
Run the program 5 times and take a screen shot showing the output of the run. Add comments throughout the code please!
thread3.c given code:
// Compile with://// clang -lpthread thread3.c -o thread3// or// gcc -lpthread thread3.c -o thread3//#include <stdio.h>#include <stdlib.h>#include <pthread.h>
#define NTHREADS 10000
// shared variableint counter = 0;
// thread to be executed - unspecified variable argumentsvoid *thread (void *vargp) {counter = counter +1;return NULL;}
int main() {// array to keep Pthread IDs of created threadspthread_t…
arrow_forward
THREADS in C PROGRAM HELP PLEASE!!
Using the thread3.c program provided below, itt will require some code modification. Those modifications include:
- instead of 10,000 threads, create 40,000 threads in total
- instead of creating just one kind of thread, create 4 kinds of threads (each with 10,000 instances)
o group 1: adds 1 to counter
o group 2: adds 5 to counter
o group 3: subtracts 2 from counter
o group 4: subtracts 10 from counter
Run the program 5 times and take a screen shot showing the output of the run. Add comments throughout the code please!
thread3.c given code:
// Compile with://// clang -lpthread thread3.c -o thread3// or// gcc -lpthread thread3.c -o thread3//#include <stdio.h>#include <stdlib.h>#include <pthread.h>
#define NTHREADS 10000
// shared variableint counter = 0;
// thread to be executed - unspecified variable argumentsvoid *thread (void *vargp) {counter = counter +1;return NULL;}
int main() {// array to keep Pthread IDs of created…
arrow_forward
Write a Java program using Thread with single Server and Multiple Clients
NOTE: Client must be running until Bye message is sent. Server should never stop
arrow_forward
THREAD -"C LANGUAGE" ONLY
Write a program that uses 5 threads. Initialize a shared variable with a value of 0.
Each thread must add its Thread ID (tid) to the shared variable.
Once a thread has done the addition, print the ID of the thread.
It is important to make use of mutexes so that only one thread is incrementing the shared variable at a time.
Output the value of the shared variable once all threads have finished incrementing it.
arrow_forward
Problem 0. Write a version of hello.c that creates and reaps (joins) n threads, each of which
prints "Hello, world" and its thread id, where n is a command-line argument, and the thread function
is passed a pointer to its thread ID as its argument.
arrow_forward
Socket programming
The application consists of two parts: Server and Client. Each part can run independently on separate computers.
Server manages the chat session. It maintains a list of the active clients and forwards incoming messages. The Server is multithreaded - communication with each client is through a separate thread. When Server is started, it starts to wait for client connections.
This thread continues forever to listen for clients. When a connection request is received, the server starts a thread to service the client. This is necessary for each client to have its own socket. When the thread is started, a Client object is created containing all the relevant info about each client and saved in a list.
The ServiceClient() thread is where all the work is done. ServiceClient() defines the implementation of the server side of the application protocol
arrow_forward
Here, a solution that uses a single thread is preferable than one that uses several threads.
arrow_forward
Modify the above thread program so that you implement the interface Runnable. Make themain thread waiting till all other threads finish execution.
arrow_forward
In computer graphics when looking at synchronizations when building a renderer in engine development, what are moments where locks and unlocks from mutex's would not always work when looking at synchronization for a renderer in an engine?What I mean is when developing a game engine and you'll want there to be synchronization from a renderer thread and a main thread. Where renderer thread handles flushing and executing all of the concurrent tasks, whereas main thread would handle more of the querying tasks to a render command queue, and handling how these operations are operated within the engine.Can you give me some ideas on when mutex's may not be enough in a more complex? Where you may have multiple scenes, where animation physics are applied, and various vast ideas in this perspective?On another can you give me in great details what are uses of ref counters and different variations of implementations (in C++), and why reference counters are used? And what purpose do they have, as…
arrow_forward
Write a Java program using Thread with single Server and Multiple Clients.
NOTE: Client must be running until Bye message is sent. Server should never stop.
I need answer question pls
arrow_forward
In Java multithreading environment, one thread can send an interrupt to another by calling theinterrupt() method on the Thread object for the target thread (i.e., the thread to beinterrupted). To handle interrupts in a target thread, Java allows two approaches. One is performedby writing an exception handler for InterruptedException (only applicable if the targetthread is invoking methods which throw that exception such as sleep). The other approach isperformed by periodically checking the interrupt status flag Thread.interrupted andperforming the handling routine when that flag is set to true.Write a Java program that illustrates the use of the two approaches described above. Your programshould start by creating two threads, each thread should use different interrupt handling approach.Then, the program needs to send interrupts to each one of the created threads such that a threadneeds to return (i.e., stop execution) after receiving an interrupt from the main thread. Make sureto…
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Related Questions
- THREADS in C PROGRAM HELP! Using the thread3.c program provided below, itt will require some code modification. Those modifications include: - instead of 10,000 threads, create 40,000 threads in total - instead of creating just one kind of thread, create 4 kinds of threads (each with 10,000 instances) o group 1: adds 1 to counter o group 2: adds 5 to counter o group 3: subtracts 2 from counter o group 4: subtracts 10 from counter Run the program 5 times and take a screen shot showing the output of the run. Add comments throughout the code please! thread3.c given code: // Compile with://// clang -lpthread thread3.c -o thread3// or// gcc -lpthread thread3.c -o thread3//#include <stdio.h>#include <stdlib.h>#include <pthread.h> #define NTHREADS 10000 // shared variableint counter = 0; // thread to be executed - unspecified variable argumentsvoid *thread (void *vargp) {counter = counter +1;return NULL;} int main() {// array to keep Pthread IDs of created threadspthread_t…arrow_forwardTHREADS in C PROGRAM HELP PLEASE!! Using the thread3.c program provided below, itt will require some code modification. Those modifications include: - instead of 10,000 threads, create 40,000 threads in total - instead of creating just one kind of thread, create 4 kinds of threads (each with 10,000 instances) o group 1: adds 1 to counter o group 2: adds 5 to counter o group 3: subtracts 2 from counter o group 4: subtracts 10 from counter Run the program 5 times and take a screen shot showing the output of the run. Add comments throughout the code please! thread3.c given code: // Compile with://// clang -lpthread thread3.c -o thread3// or// gcc -lpthread thread3.c -o thread3//#include <stdio.h>#include <stdlib.h>#include <pthread.h> #define NTHREADS 10000 // shared variableint counter = 0; // thread to be executed - unspecified variable argumentsvoid *thread (void *vargp) {counter = counter +1;return NULL;} int main() {// array to keep Pthread IDs of created…arrow_forwardWrite a Java program using Thread with single Server and Multiple Clients NOTE: Client must be running until Bye message is sent. Server should never stoparrow_forward
- THREAD -"C LANGUAGE" ONLY Write a program that uses 5 threads. Initialize a shared variable with a value of 0. Each thread must add its Thread ID (tid) to the shared variable. Once a thread has done the addition, print the ID of the thread. It is important to make use of mutexes so that only one thread is incrementing the shared variable at a time. Output the value of the shared variable once all threads have finished incrementing it.arrow_forwardProblem 0. Write a version of hello.c that creates and reaps (joins) n threads, each of which prints "Hello, world" and its thread id, where n is a command-line argument, and the thread function is passed a pointer to its thread ID as its argument.arrow_forwardSocket programming The application consists of two parts: Server and Client. Each part can run independently on separate computers. Server manages the chat session. It maintains a list of the active clients and forwards incoming messages. The Server is multithreaded - communication with each client is through a separate thread. When Server is started, it starts to wait for client connections. This thread continues forever to listen for clients. When a connection request is received, the server starts a thread to service the client. This is necessary for each client to have its own socket. When the thread is started, a Client object is created containing all the relevant info about each client and saved in a list. The ServiceClient() thread is where all the work is done. ServiceClient() defines the implementation of the server side of the application protocolarrow_forward
- Here, a solution that uses a single thread is preferable than one that uses several threads.arrow_forwardModify the above thread program so that you implement the interface Runnable. Make themain thread waiting till all other threads finish execution.arrow_forwardIn computer graphics when looking at synchronizations when building a renderer in engine development, what are moments where locks and unlocks from mutex's would not always work when looking at synchronization for a renderer in an engine?What I mean is when developing a game engine and you'll want there to be synchronization from a renderer thread and a main thread. Where renderer thread handles flushing and executing all of the concurrent tasks, whereas main thread would handle more of the querying tasks to a render command queue, and handling how these operations are operated within the engine.Can you give me some ideas on when mutex's may not be enough in a more complex? Where you may have multiple scenes, where animation physics are applied, and various vast ideas in this perspective?On another can you give me in great details what are uses of ref counters and different variations of implementations (in C++), and why reference counters are used? And what purpose do they have, as…arrow_forward
- Write a Java program using Thread with single Server and Multiple Clients. NOTE: Client must be running until Bye message is sent. Server should never stop. I need answer question plsarrow_forwardIn Java multithreading environment, one thread can send an interrupt to another by calling theinterrupt() method on the Thread object for the target thread (i.e., the thread to beinterrupted). To handle interrupts in a target thread, Java allows two approaches. One is performedby writing an exception handler for InterruptedException (only applicable if the targetthread is invoking methods which throw that exception such as sleep). The other approach isperformed by periodically checking the interrupt status flag Thread.interrupted andperforming the handling routine when that flag is set to true.Write a Java program that illustrates the use of the two approaches described above. Your programshould start by creating two threads, each thread should use different interrupt handling approach.Then, the program needs to send interrupts to each one of the created threads such that a threadneeds to return (i.e., stop execution) after receiving an interrupt from the main thread. Make sureto…arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education