Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are designing a web server that has one server thread reading requests from

ID: 2247520 • Letter: Y

Question

You are designing a web server that has one server thread reading requests from n-number of users that stream in at a given rate. The requests are put in a buffer of constant size that are services by a thread pool of m-threads.

a) Break this problem into 1 or more producer-consumer problems, describing the producer and the consumer at each sub-problem.

Additionally, you decide to forgo the use of lock primitives such as pthread_lock and pthread_unlock to reduce latency on running threads.

b) Describe how compare-and-swap operations can be used to ensure mutual exclusion during produce and consume operations on the buffer.

Explanation / Answer

For the server that is handling thread ,we create a specified number of threads at start and then assign task to them. So we dont need to implement threads every now and then

Use these threads as a buffer for queueing processes
Threads are constantly looking for queue and when a request come one of the thread dispatch the request and perform action
Here are some queue methods

add(); //add a socket at the end
removeFront();//remove socket from front
isEmpty();//True if queue is empty
size(); //return size of queue
getMaxSize();//get maximum size for queue
Your Request processing runnable

public class Processor implements Runnable {

private Queue<Socket> requests;
private boolean stops;

Processor(Queue<Socket> req) {
this.requests = req;
stops = false;
}

@Override
public void run() {
while(!stops) {
if(requests.isEmpty()) {
try{
Thread.sleep(#rendomeTimemill);
} catch(InterruptedException e){}
}else {
Socket st = Queue.removeFront();
try {
//System.out.println("processing request from " + socket.getInetAddress().getHostName());
//do you want
} catch (Exception e) {
} finally {
if(st != null) {
try{ st.close(); st = null; } catch(IOException ex){}

}
}
}
}
}
public void stopNow() {
stops = true;
Thread.interrupt();
}
}
in your main thread create a queue to put requests

//start your server socket
Queue<Socket> requests = new Queue<Socket>();
Start worker thread pool

Precessor []workers = new Processor[NUM_WORKER];
for(int i=0;i<NUM_WORKER; i++) {
worker[i] = new Processor(requests);
Thread th = new Thread(worker[i]);
th.strat();
}
in request listening

//while loope that run forever
// accept socket
if(requests.size() == requests.getMaxSize()) {
socket.getOutputStream().write("HTTP/1.0 505 Error ".getBytes());
socket.getOutputStream().write("<html><body>Try again</body></html> ".getBytes());
socket.close();
} else {
requests.add(socket);
}
when you want to shout down server

for(int i=0;i<NUM_WORKER; i++) {
worker[i].stopNow();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote