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
Computer Systems: A Programmer's Perspective (3rd Edition)
- design a java program that implements thread priority based on setPriority() method using different level of priority for two threads .Both these threads donot run on preemptive platform in the same way.Thus,prioritize the threads by running each thread for few seconds.After this main thread stops this thread.Moreover, the program should display the no of times each thread made it through the loop is displayedarrow_forwardWhat are the distinctions between a thread operating in user mode and a thread operating in the kernel mode? Is there a preferred species that we may choose from?arrow_forwardWrite an ALP program for ARM Cortex M3/4 to perform the following(1) SVC is to be called from an application task running at Thread unprivileged mode.(2) Two parameters are passed to the handler via R1, R2.(3) If the SVC number is last 3 digits of your Student ID (0 to 255) or last 2 digits of your Student ID (if last 3-digits of Student ID > 255), then the addition of the data in R1, R2 should be performed and after that return and resume application task.(4) If the SVC number is not your Student ID (3 or 2 digits), then return and resume application task.Give suitable screen shots of the KEIL IDE-in debug mode to demonstrate the desired outputs. Ensure that the screenshot captures system time & day.Below Program can be used (basic skeleton)as below to perform the above conditions . AREA RESET, CODE, READONLYin DCD 0x20000100,0x8000005D, 0,0,0,0,0,0,0,0,0,SVC_handler+1,0,0,0,0,0,0,0,0,0,0,0 ENTRY MAIN LDR R1,=0X20000200 MSR PSP,R1 MOV R0,#3…arrow_forward
- Problem 2. In addition to POSIX semaphores, the POSIX threads standard provides another useful synchronization primitives: the muter (type pthread_mutex_t) A mutex is used to wait for it implement mutual exclusion. A thread locks a mutex variable by calling pthread_mutex_lock(), and unlocks it by calling pthread_mutex_unlock(). When a thread calls pthread_mutex_lock() while another thread has previously locked the same mutex but not yet unlocked it, the former thread blocks until the latter thread calls pthread_mutex_unlock() on it. a. Is there any difference between a binary semaphore and a pthread_mutex? Justify your answer. b. Consider a program with multiple threads running this function: struct {...} shared_data; pthread_mutex_t m; void *thread_func(void *arg) { [update shared_data]; // critical section! return NULL; } Assume the mutex is initialized before the threads are created. Add the necessary pthread_mutex_* calls to the above code to ensure that only one thread is in the…arrow_forwardPreviously, we discussed how each JVM thread can only run a single task at a time. For you, particularly, what evidence do you have to support this assumption?arrow_forwardWhat is the difference between a thread running in user mode and a thread running in the kernel? Do any advantages exist that one species may have over another?arrow_forward
- let us suppose that a function f() running on the UI thread must invoke a long-running function called long_op(). Use a short code segment, show how f() can create a worker thread and invoke long_op() in the worker thread. Your code should use general Java concurrency constructs; do not use Android-specific concurrency constructs.arrow_forwardExplore the concept of adaptive thread management in dynamic multithreading. How can adaptive algorithms be used to dynamically adjust the number of threads based on workload and resource availability?arrow_forwardI have a web server program. It works as follows: the parent thread waits for new client connections. Once a new client connection comes in, the parent thread creates a child thread that services the client connection while the parent thread goes back and waits for more connections. Once the client connection finishes, the corresponding servicing child thread exits. When my code has undergone the usual code review process in my company, it has received major criticisms for being gravely inefficient. Name the optimization technique that I I have neglected to use? Clearly explain the technique and how the technique works and how it would improve the performance of my code.arrow_forward
- Write a C program that creates multiple threads with NULL as parameter sent to the thread execution function, runner. Each thread inside the runner function must reads an integer value, n, and then produce sum of values from 1 to n(inclusive) and prints the sum. Here sum is global variable. Ensure that the main () must wait for the termination of these threads and synchronized the problem solution by using semaphore lock.arrow_forwardSuresh and Ramesh started creating an application where it requires to do some mathematical operations.They are going to deploy it in a server. They wanted to do simultaneous execution of multiple parts ofprograms to utilize CPU time. They decided to create 2 separate threads for operations: num**2 andsqrt(num) with sleep of 100ms each.Write a program containing two threads and each thread should have a sleep of 100 ms. One thread is forcalculating the square of the elements in the array and other is for square roots.Trace the output in the threads only. Use Exception handling to handle and trace the interruptions if they occur.Input: An array. Can use static data. Preferably try to use run() and start().Output:"Thread 1"+ square(num)"Thread 2"+ square root(num)likewise to distinguish the thread statusarrow_forwardA thread with priority 30 is currently executing. What would its re-assigned priority be if: (a) it used up all of its time quantum? (b) I/O was blocked?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