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

Want to see the full answer?
Check out a sample textbook solution
Chapter 12 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengagePrinciples of Information Systems (MindTap Course...Computer ScienceISBN:9781305971776Author:Ralph Stair, George ReynoldsPublisher:Cengage Learning
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Operations Research : Applications and AlgorithmsComputer ScienceISBN:9780534380588Author:Wayne L. WinstonPublisher:Brooks ColeC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning




