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
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
1. Implement multithreading in Java
Java allows a program to create threads during runtime by extending the Thread class or
implementing the interface Runnable.
Build a class to represent a thread. Each thread should be able to store and will be given a
unique id so that your program will be able to track them (if you extends the class Thread,
you may use the getName() and setName() methods from the Thread class).
Build a Java program which creates 10 threads. Assign a unique id for each thread. You
may use an integer as a counter and increase the counter every time a thread is created. A
thread should print a message when it is created e.g. 'Thread 1 is created' (for thread with
id 1).
arrow_forward
15. It is also known that user credentials may be available in a remote database. The wanted user credentials need to be downloaded from a remote database. While the main thread executing the local processing, because an external database is accessed over the Internet, data accessing is executed in another thread. As the remote data is downloaded in a thread, the data needs to be passed back to the main thread. The following statement ensures that the main thread is updated on the main queue.
DispatchQueue._______._______ {
// do something here
}
arrow_forward
data comunication
A program for communication between server and client will be developed. Multiclients (more than 1 client) can communicate with server simultaneously. While a client communication with the server, another clients should communicate with the server. The communication should be seen in consoles. TCP protocol will be used.. Thread functions should be used for the simultaneous communication
arrow_forward
I have a web server program. It works as follows: the parent thread waits for new client connections. Once a new client connection comes in, the parent thread creates a child thread that services the client connection while the parent thread goes back and waits for more connections. Once the client connection finishes, the corresponding servicing child thread exits. When my code has undergone the usual code review process in my company, it has received major criticisms for being gravely inefficient. Name the optimization technique that I I have neglected to use? Clearly explain the technique and how the technique works and how it would improve the performance of my code.
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
Part 3: Interrupt handlingIn 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…
arrow_forward
doable without textbook
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
1. Consider the track line in railways. If multiple trains trying to access the same line it
should not allow because it will lead to collision. Now write a java program to schedule
the track for the train. Create 3 threads objects for the class Train for 3 trains and
schedule the Line by calling the getLine() (Synchronized method on the same Object but
only one thread will be able to execute it at a time) in the Line class.
arrow_forward
Create two Thread subclasses, one with a run() that starts up, captures the reference of the second Thread object and then calls wait(). The other class run() should call notifyAll() for the first thread after some number ofn seconds have passed, so the first thread can print a message
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
1. Implement mutual exclusion with multithreading in Java
Build a Java program which simulates a communication between a server and clients. You
must use a thread to represent each party (as server or client). (Hint: study about the
Semaphore class to perform mutual exclusion between threads)
Create three threads. One thread will act as a server, which always ready to receive a
message from a client (ping) and then replies the message to the same client (pong). The
other two threads will become the clients, where each client will send a message to the
server (ping) and waits for the reply (pong). Define 2 different classes to represent the
server and the client.
To perform the communication, each thread will refer to a same variable/data structure for
sending and receiving the message. Use mutual exclusion approach to ensure each client
can send and receive the reply without interruption from any other elient(s).
For this simulation, your objective is to ensure that each client must…
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 the four diagrams illustrated below, show which of them result in deadlock? For those situations that will result in deadlock, provide the cycle of resources and threads. For those situations which are not in deadlock, show by illustration the order in which the threads may complete their executions
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
Consider a Semaphore named “mux”,
Semaphore mux = new Semaphore(-2);
mux.acquire();
number++;
mux.release();
If three threads T1,T2 and T3 are trying to access the shared data (number) concurrently,
a. Will any thread be able to go into its critical section? Explain.
b. Will there be a race condition among the threads?
c. What will be the final value of the variable “number” if its initial value is 0?
d. Correct the code, if race condition exists.
arrow_forward
Above is a python script that scans wifi networks. Please explain each line of code. Write an explanatory note about the libraries used and their methods.
arrow_forward
In this particular situation, a solution that just makes use of a single
thread is preferable than one that makes use of several threads.
arrow_forward
Plz solve this question ASAP
C language
arrow_forward
In this particular situation, a solution with a single thread is preferable than a solution with several threads.
arrow_forward
perform several operations at the same time.
* Each Thread that you run works separately from the rest of the commands in the program, and therefore in the event of any error occurring in the Thread, it will not affect the rest of the commands in the program, as it does not affect any other Thread running in the program.
In light of your study of multiple channels,
• Write a program in the Java language that demonstrates this concept - you can choose the nature of the program that you want to achieve
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
Kindly answer this as soon as possible. Urgent and answer all parts thanks.
write a multi-threaded Java program that
(i)
Has a single Producer class object which is a thread and a single Consumer class which is also
a thread.
(ii)
Create a class Packet. This class should contain a simple string.
(ii)
Create a class Buffer that internally uses some data structure to hold incoming packet
objects – it should have methods insertPkt and removePkt and should have methods to tell
the user how many packets are in the Buffer at any time e.g. size.
(iv)
Make the Producer object at random time intervals create Packet objects and place them in
the Buffer object.
(v)
OBJECTIVE : Use a mechanism from the books Concurrency chapter to synchronize the
producer thread pushing packets into the Buffer and the consumer thread removing them
such that the Buffer size always remains between 50-70 packets.
(vi)
Show through any means how your objective is met by running the Producer and Consumer
object.
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_forward1. Implement multithreading in Java Java allows a program to create threads during runtime by extending the Thread class or implementing the interface Runnable. Build a class to represent a thread. Each thread should be able to store and will be given a unique id so that your program will be able to track them (if you extends the class Thread, you may use the getName() and setName() methods from the Thread class). Build a Java program which creates 10 threads. Assign a unique id for each thread. You may use an integer as a counter and increase the counter every time a thread is created. A thread should print a message when it is created e.g. 'Thread 1 is created' (for thread with id 1).arrow_forward15. It is also known that user credentials may be available in a remote database. The wanted user credentials need to be downloaded from a remote database. While the main thread executing the local processing, because an external database is accessed over the Internet, data accessing is executed in another thread. As the remote data is downloaded in a thread, the data needs to be passed back to the main thread. The following statement ensures that the main thread is updated on the main queue. DispatchQueue._______._______ { // do something here }arrow_forward
- data comunication A program for communication between server and client will be developed. Multiclients (more than 1 client) can communicate with server simultaneously. While a client communication with the server, another clients should communicate with the server. The communication should be seen in consoles. TCP protocol will be used.. Thread functions should be used for the simultaneous communicationarrow_forwardI have a web server program. It works as follows: the parent thread waits for new client connections. Once a new client connection comes in, the parent thread creates a child thread that services the client connection while the parent thread goes back and waits for more connections. Once the client connection finishes, the corresponding servicing child thread exits. When my code has undergone the usual code review process in my company, it has received major criticisms for being gravely inefficient. Name the optimization technique that I I have neglected to use? Clearly explain the technique and how the technique works and how it would improve the performance of my code.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_forward
- Part 3: Interrupt handlingIn 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…arrow_forwarddoable without textbookarrow_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_forward1. Consider the track line in railways. If multiple trains trying to access the same line it should not allow because it will lead to collision. Now write a java program to schedule the track for the train. Create 3 threads objects for the class Train for 3 trains and schedule the Line by calling the getLine() (Synchronized method on the same Object but only one thread will be able to execute it at a time) in the Line class.arrow_forwardCreate two Thread subclasses, one with a run() that starts up, captures the reference of the second Thread object and then calls wait(). The other class run() should call notifyAll() for the first thread after some number ofn seconds have passed, so the first thread can print a messagearrow_forward
arrow_back_ios
SEE MORE QUESTIONS
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