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 solutionChapter 12 Solutions
EBK COMPUTER SYSTEMS
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY