Question: [ C Programming ] Please help me complete this C code where we are implementing a semaphore using condition variables and locks. I had posted

[ C Programming ] Please help me complete this C code where we are implementing a semaphore using condition variables and locks. I had posted this question before, but got a wrong answer. Please post the correctly modified code and screenshots of the output after testing the modified code with the awk script.

[ C Programming ] Please help me complete this C code where

sem.c code:

/* * Implementation of a semaphore using locks and condition * variables. * * Complete the code below by adding code where you see * YOUR CODE HERE. Do not modify the code in any other way. * * Follow the Anderson/Dahlin method in designing the semaphore * object. * */ #include  #include  #include  #include  #define NUMOPS 10000 typedef struct { // YOUR CODE HERE (add state and synchronization variables to struct) } SEM; // contructor of semaphore // parameter a is the initial value of the semaphore SEM *sem_create(int a) { SEM *sem; sem = malloc(sizeof(SEM)); assert(sem != NULL); // YOUR CODE HERE (initialize state and synchronization variables) return(sem); } // wait operation on semaphore void sem_wait(SEM *sem) { // YOUR CODE HERE } // signal operation on semaphore void sem_signal(SEM *sem) { // YOUR CODE HERE } /* * The following code tests the semaphore implementation. * Four threads all try to access a critical section; the semaphore * should allow at most 2 threads in the critical section at once. */ // repeatedly enter and exit critical section void *agent(void *arg) { SEM *sem = (SEM *)arg; int i; for (i = 0; i  

Awk script to test code - test-sem.awk:

# find max count of threads in critical section { if ($1 == "in") x++ if ($1 == "out") x-- if (x > max) max = x } END { print "max in CS = "max }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!