[ C Programming ] Please help me complete this C code where we are implementing
ID: 3712681 • Letter: #
Question
[ C Programming ] Please help me complete this C code where we are implementing a semaphore using condition variables and locks.
sem.c code:
Awk script to test code - test-sem.awk:
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.Explanation / Answer
SourceCode:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define NUMOPS 10000
/////////////////// Optional ///// may or may not need as per demands ////////////////
#define SEM_INIT(n)
{
.cond = PTHREAD_COND_INITIALIZER,
.mtx = PTHREAD_MUTEX_INITIALIZER,
.N = n
};
////////////////////////////////////////
typedef struct
{
// Defined the struct with 2 pthread variables and an integer
pthread_cond_t cond;
pthread_mutex_t mtx;
volatile unsigned N; // it can be applied as int N as well but both will have same effect
}SEM;
// contructor of semaphore
// parameter a is the initial value of the semaphore
SEM *sem_create(int a)
{
// YOUR CODE HERE (initialize state and synchronization variables)
SEM *sem;
sem = check_malloc(sizeof(SEM));
assert(sem != NULL);
// if(sem != NULL) // put this condition if required
// {
pthread_mutex_init(&sem->mtx,NULL);
pthread_cond_init(&sem->cond,NULL);
sem->N = a;
// }
return(sem);
}
void sam_destroy(SEM * sem) // if you want to kill process call this else not required
{
if(sem != NULL)
{
pthread_cond_destroy(&sem->cond);
pthread_mutex_destroy(&sem->mtx);
}
}
// wait operation on semaphore
void sem_wait(SEM *sem)
{
// YOUR CODE HERE
pthread_mutex_lock(&sem->mtx);
while (sem->N == 0)
{
pthread_cond_wait(&sem->cond, &sem->mtx); /*unlock mutex, wait, relock mutex*/
}
sem->N--;
pthread_mutex_unlock(&sem->mtx);
}
// signal operation on semaphore
void sem_signal(SEM *sem)
{
// YOUR CODE HERE
if(sem != NULL)
{
pthread_mutex_lock(&sem->mtx);
(sem->N)++;
pthread_mutex_unlock(&sem->mtx);
pthread_cond_signal(&sem->cond);
}
}
/*
* 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 < NUMOPS; i++) {
sem_wait(sem);
// beginning of critical section
printf("in ");
int j; for (j = 0; j < 5000; j++); // wait so others can enter critical section
printf("out ");
// end of critical section
sem_signal(sem);
}
pthread_exit(NULL);
}
// test the semaphore
int main(int argc, char *argv[])
{
pthread_t t1,t2,t3,t4;
SEM *sem = sem_create(2);
pthread_create(&t1, NULL, agent, (void *)sem);
pthread_create(&t2, NULL, agent, (void *)sem);
pthread_create(&t3, NULL, agent, (void *)sem);
pthread_create(&t4, NULL, agent, (void *)sem);
pthread_exit(NULL);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.