Computer Systems: A Programmer's Perspective (3rd Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
3rd Edition
ISBN: 9780134092669
Author: Bryant, Randal E. Bryant, David R. O'Hallaron, David R., Randal E.; O'Hallaron, Bryant/O'hallaron
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 12, Problem 12.38HW

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...

Blurred answer
Students have asked these similar questions
Write a program that has a counter as a global variable. Spawn 10 threads in theprogram, and let each thread increment the counter 1000 times in a loop. Print the finalvalue of the counter after all the threads finish—the expected value of the counter is10000. Run this program first without using locking across threads and observe theincorrect updating of the counter due to race conditions (the final value will be slightly lessthan 10000). Next, use locks when accessing the shared counter and verify that the counteris now updated correctly
3. 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 client(s). For this simulation, your objective is to ensure that each client must…
Hello, the code task below has to be written in python 3 and the HTTP must show the html file from the command line. Please help. Also below is the skeleton python below which needs to be in python 3 Currently, the web server handles only one HTTP request at a time. Implement a multithreaded server that is capable of serving multiple requests simultaneously. Using threading, first create a main thread in which your modified server listens for clients at a fixed port. When it receives a TCP connection request from a client, it will set up the TCP connection through another port and services the client request in a separate thread. There will be a separate TCP connection in a separate thread for each request/response pair.     Skeleton    Python    Code    for    the    Web    Server #import socket modulefrom socket import *import sys # In order to terminate the programserverSocket = socket(AF_INET, SOCK_STREAM)#Prepare a sever socket#Fill in start#Fill in endwhile True: #Establish the…
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education