Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

One has implemented producer and consumer in the following way using Semphores.

ID: 3830202 • Letter: O

Question

One has implemented producer and consumer in the following way using Semphores.

Assume emptyBuffer==2 and fullBuffer==0 initially. Findempty() will return a globally unique pointer to a empty item.

Producer:

sem_wait(&mutex); //p1

sem_wait(&emptyBuffer); //p2

myi = findempty(&buffer); //p3

Fill(&buffer[myi]); //p4

sem_signal(&fullBuffer); //p4

sem_signal(&mutex); //p5

Consumer:

sem_wait(&mutex); //c1

sem_wait(&fullBuffer); //c2

myj = findfull(&buffer); //c3

Use(&buffer[myj]); //c4

sem_signal(&emptyBuffer);//c5

sem_signal(&mutex); //c6

A. Show a schedule that can lead the threads to a deadlock situation by filling out the following table. You don’t have to fill out all blanks if they are not applicable.

B. Can you help fix the problem in an EFFICIENT way? Justify your answer.

Actions Producer State Consumer State empty Buffer value full buffer value

Explanation / Answer

Producer is producing the items and consumer is consuming the items.Buffer is the common data structure used by both the producer and consumer.

Mutex semaphore is used to implement the access to the buffer in a mutual exclusion manner.Since mutex semaphore has only the values 0 and 1,wait operation makes the value 0 and signal operation makes its value 1.Both producer and consumer has to wait for the value 1 to acces the buffer.

The restriction here is the producer can not produce the items if the buffer is full and the consumer can not consume the items when the buffer is empty.To implement this two semaphores are used,full buffer and empty buffer.Producer when producing the items will decrement the empty buffer value and increment the full buffer value and consumer on the other hand while consuming he will decrement the full buffer value and increment the empty buffer value.

The following scenario will lead eventually to a dead lock:

Suppose the buffer is full,Producer will wait(mutex) and then wait(empty buffer) which is not going to happen as the buffer is full,mean while consumer is waiting for mutex,wait(mutex).So,dead lock happens.

producer consumer empty buffer value full buffer value

wait(mutex)

wait(empty buffer)

produce(enter item to buffer)

signal(mutex)

signal(full buffer) 1 1

wait(mutex)

wait(empty buffer)

produce

signal(mutex)

signal(empty buffer) 0 2

wait(mutex)

wait(empty buffer) wait(mutex)

DEAD LOCK occurs

This problem can be fixed by interchanging wait(empty buffer) and wait(mutex).

producer

wait(empty buffer)

wait(mutex)

..............................

so only when the buffer is not full then only the producer can access the buffer.

Hence deadlock can be prevented.

similarly in consumer

wait(full buffer)

wait(mutex)

.........................

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote