Question: Consider the partial program below which includes a main ( ) and threaded runner function that use a semaphore. This program contains a conceptual problem

Consider the partial program below which includes a main() and threaded runner function that use a semaphore. This program contains a conceptual problem with the way it uses the semaphore, what is it? Identify the problem, and explain why it occurs. Assume all syntax is correct.
Useful Functions:
int sem_init(sem_t *sem, int pshared,
unsigned int count);
int sem_wait(sem_t *sem);
int sem_post(sem_t *sem);
#define NUM_ACTIVE 2/* number of threads allowed in critical section at once */
#define NUM_THREADS 5/* number of threads total */
sem_t sem,
void* thread_job(void){
if (sem_wait(&sem)==-1){
fprintf(stderr, "Failed to lock!
");
} else if (sem_wait(&sem)==0){
//[do some non-critical stuff]
//[critical section goes here]
sem_post(&sem);
//[more non-critical stuff]
}
}
int main(void){
pthread_t tids[NUM_THREADS];
sem_init(&sem, 0, NUM_ACTIVE); // initialize semaphore
for (int i=0; i < NUM_THREADS; i++)
pthread_create(&tids[i], NULL, thread_ job, NULL);
For (int i #l O; i L NUM_ THREADS; i++)
pthread_join(tids[i], NULL);
}

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!