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

Explanation of Solution

Given C code:

//Include libraries

#include "csapp.h"

#include "sbuf.h"

#include<pthread.h>

#define NTHREADS 4

#define SBUFSIZE 16

//Declare variable

static int byte_cnt;

//Declare variable

static sem_t mutex;

//Define method init_echo_cnt

static void init_echo_cnt(void)

{

//Call method

Sem_init(&mutex, 0, 1);

//Assign value

byte_cnt = 0;

}

//Define method echo_cnt

void echo_cnt(int connfd)

{

//Declare variable

int n;

//Declare array

char buf[MAXLINE];

//Create instance

rio_t rio;

//Assign value

static pthread_once_t once = PTHREAD_ONCE_INIT;

//Call method

pthread_once(&once, init_echo_cnt);

//Call method

Rio_readinitb(&rio, connfd);

//Loop

while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0)

{

//Decrement

P(&mutex);

//Update value

byte_cnt += n;

//Display message

printf("server received %d (%d total) bytes on fd %d\n", n, byte_cnt, connfd...

Blurred answer
Students have asked these similar questions
At the moment, work is being done by a thread with the priority 30. If (a) it used up all of its time quantum, what would be its re-assigned priority? If b is correct, then I/O has been blocked.
What are the main advantages of using thread pools in high-concurrency applications?
In this particular situation, a solution with a single thread is preferable than a solution with several threads.
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