Concept explainers
Explanation of Solution
Implementation of a concurrent prethreaded version of the TINY web server:
Modified code for “sbuf.h” file:
The modified code for “sbuf.h” from section 12.5.4 in book is given below:
#ifndef SBUF_HEADER
#define SBUF_HEADER
#include "csapp.h"
typedef struct
{
int *buf; /* Buffer array */
int n; /* Maximum number of slots */
int front; /* buf[(front+1)%n] is first item */
int rear; /* buf[rear%n] is last item */
sem_t mutex; /* Protects accesses to buf */
sem_t slots; /* Counts available slots */
sem_t items; /* Counts available items */
} sbuf_t;
//Function declaration
void sbuf_init(sbuf_t *sp, int n);
void sbuf_deinit(sbuf_t *sp);
void sbuf_insert(sbuf_t *sp, int item);
int sbuf_remove(sbuf_t *sp);
//Function declaration for sbuf_empty
int sbuf_empty(sbuf_t *sp);
//Function declaration for sbuf_full
int sbuf_full(sbuf_t *sp);
#endif
Modified code for “sbuf.c” file:
The modified code for “sbuf.c” from section 12.5.4 in book is given below:
#include "csapp.h"
#include "sbuf.h"
/* Create an empty, bounded, shared FIFO buffer with n slots */
void sbuf_init(sbuf_t *sp, int n)
{
sp->buf = Calloc(n, sizeof(int));
sp->n = n; /* Buffer holds max of n items */
sp->front = sp->rear = 0; /* Empty buffer if front == rear */
Sem_init(&sp->mutex, 0, 1); /* Binary semaphore for locking */
Sem_init(&sp->slots, 0, n); /* Initially, buf has n empty slots */
Sem_init(&sp->items, 0, 0); /* Initially, buf has zero data items */
}
/* Clean up buffer sp */
void sbuf_deinit(sbuf_t *sp)
{
Free(sp->buf);
}
/* Insert item onto the rear of shared buffer sp */
void sbuf_insert(sbuf_t *sp, int item)
{
P(&sp->slots); /* Wait for available slot */
P(&sp->mutex); /* Lock the buffer */
sp->buf[(++sp->rear)%(sp->n)] = item; /* Insert the item */
V(&sp->mutex); /* Unlock the buffer */
V(&sp->items); /* Announce available item */
}
/* Remove and return the first item from buffer sp */
int sbuf_remove(sbuf_t *sp)
{
int item;
P(&sp->items); /* Wait for available item */
P(&sp->mutex); /* Lock the buffer */
item = sp->buf[(++sp->front)%(sp->n)]; /* Remove the item */
V(&sp->mutex); /* Unlock the buffer */
V(&sp->slots); /* Announce available slot */
return item;
}
//Function definition for empty buffer
int sbuf_empty(sbuf_t *sp)
{
//Declare variable
int ne;
//For lock the buffer
P(&sp->mutex);
ne = sp->front == sp->rear;
//For lock the buffer
V(&sp->mutex);
return ne;
}
//Function definition for full buffer
int sbuf_full(sbuf_t *sp)
{
//Declare variable
int fn;
//For lock the buffer
P(&sp->mutex);
fn = (sp->rear - sp->front) == sp->n;
//For lock the buffer
V(&sp->mutex);
return fn;
}
For code “tiny.c” and “tiny.h”:
Same code as section 11.6 in book.
sample.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Home</title>
</head>
<body>
Tiny server Example
</body>
</html>
main.c:
#include <stdio.h>
#include "csapp.h"
#include "tiny.h"
#include "sbuf...
Want to see the full answer?
Check out a sample textbook solutionChapter 12 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- Write a program using pthreads, which calculates the sum of elements in a hard-coded integer array in parallel using 4 threads. The program must divide the work between 4 threads which run simultaneously. For simplicity, you can assume that the size of the array is 100. Note that the integer array must be declared as a global data structure. Initially code your solution so that the sum of elements is maintained in a global shared variable. Each thread modifies the same shared variable as it sums up elements from the array. Use a suitable synchronization primitive (mutex) to ensure safe access to the global variable. (A sample code of Mutex is attached for your reference)...also put screenshot of output.arrow_forward1. 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_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
- Modify the above thread program so that you implement the interface Runnable. Make themain thread waiting till all other threads finish execution.arrow_forwardcan you do this in Java, please? Thank youarrow_forwardWrite a JAVA program for the following: Create three child threads with the name first, second and third. Change the priority of the threads in the main thread as first with priority 3, second with priority 6 and third with priority 5. Display the name and priority of each thread in the run method. Main thread must wait until the termination of child thread. Output must be in the order of execution threads.arrow_forward
- Write a MultiThreaded ServerSocket Program in C# that can handle multiple clients at the same time. a C# Server Socket use TcpListener Class and listen to PORT 9393. When the C# Server Socket gets a request from Client side, the Server passes the instance of the client request to a separate class handleClient. For each Client request, there is a new thread instant is created in C# Server for separate communication with Client.arrow_forwardWrite a class that extends thread, called clientEng, to implement the following part of client protocol 1- The client thread sends an integer number, let us assume it is N, to the server. 2- The server iterates from 1 to 10 to send 10 values that results from multiplying N by the iteration index. 2- The client gets into a loop that iterates 10 times to receive the 10 values. 3- In each iteration the client prints on the screen a message as "N * M =value ", where M is the number of iteration from 0 to 10 and value is the received value. 4- after the last iteration the client should sends a 'done message from the server. 5- The client receives "OK message from the server and closes the socket. Do not write any other code, other than the thread class, but Add a line that allows for creating the running the code of clientEng.arrow_forward1. Consider two friends Herbert and Schildt are chatting and discussing about next day exam preparation. Create a class chat with two methods Ask and Reply to perform this. Create two threads Herbert and Schidlt using Runnable Interface. Implement inter thread communication for the scenario and exchange the following messages Herbert : Hi Schildt : Hello Herbert : Tomorrow do you have exam Schildt : yes Herbert : have you prepared? Schildt : ya i am preparing Herbert : All the best! Schildt : Thank youarrow_forward
- Write a program that launches 26 threads. Each thread represents an English alphabet, say a, b, c, etc. Each thread should concatenate the string "+ ch " to a string variable that is initially defined as the string " " (i.e. empty string). "ch" represents the character of the current thread being executed (e.g. a, b, c,..) . Print your string variable after all threads have been executed. The targeted correct output may look like "a+b+c+d+e+…..". Run the program with and without synchronization to see its effect. Comment on the non-synchronized output.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 stop. I need answer question plsarrow_forwardWrite a program that simulates a paging system using the aging algorithm. The number of page frames is a parameter. The sequence of page references should be read from a file. For a given input file, plot the number of page faults per 1000 memory references as a function of the number of page frames available. Code Solution: https://github.com/preethikandhalu/C/blob/master/aging.c Task to be done based on the above program: Describe what the code does, explaining how it accomplishes its task of implementing the aging page replacement algorithm. You must create an input file for simulating a sequence of page references. Run the program. (Fix any bugs that it may contain, first). Vary the number of page frames, keeping the sequence of page references constant. What can you infer from the results? Plot the output in an Excel graph. Explain your findings. Adjust your inputs to get meaningful outputs.arrow_forward
- 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