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 }

Copy the code given below to a file of your own named 'sem.?. You should modify the code ONLY by adding code where you see the comments "// YOUR CODE HERE". Look at the main program and the function 'agent' to see how your semaphore is exercised. Note that the semaphore is being used for mutual exclusion, and that the initial semaphore value is 2 Hint 1: It's possible to solve the problem using just one condition variable, one lock, and one integer-valued state variablee To compile the code do: gcc -pthread -1lm o sem sem.c To test, copy the awk file below to a file of your name named test-sem.awk and use it like this: /sem awk -f test-sem.awk where sem' is the binary you get by compiling your sem.c Look at test-sem.awk to make sure you understand the output. Part of the assignment is to understand what the output should be if your code is correct. Copy the code given below to a file of your own named 'sem.?. You should modify the code ONLY by adding code where you see the comments "// YOUR CODE HERE". Look at the main program and the function 'agent' to see how your semaphore is exercised. Note that the semaphore is being used for mutual exclusion, and that the initial semaphore value is 2 Hint 1: It's possible to solve the problem using just one condition variable, one lock, and one integer-valued state variablee To compile the code do: gcc -pthread -1lm o sem sem.c To test, copy the awk file below to a file of your name named test-sem.awk and use it like this: /sem awk -f test-sem.awk where sem' is the binary you get by compiling your sem.c Look at test-sem.awk to make sure you understand the output. Part of the assignment is to understand what the output should be if your code is correct

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!