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

Let P and V semaphore operators be implemented as wait(S) and signal(S), respect

ID: 3810864 • Letter: L

Question

Let P and V semaphore operators be implemented as wait(S) and signal(S), respectively. The semantics are

       wait(S) {                                                                                     signal(S) {

                  while (S < 0);                                                                                    S++;

                  S--;                                                                                         }

            }

Write producer and consumer processes that share an array BUFFER that can contain up to 10 items. State which variables are shared and state their initial values.

Explanation / Answer

Let MUTEX, EMPTY and FULL be three semaphore variables which are used in the following solution.
//Initially MUTEX is free, Number of EMPTY slots is 10, and Number of FULL slots is 0.

semaphore MUTEX = 1; // MUTEX is used to control the access of critical section
semaphore EMPTY = 10;        // EMPTY is used to count the number of empty buffer slots which is 10 initially
semaphore FULL = 0;    // FULL is used to count number of full buffer slots

//Producer produces an item and inserts into the buffer using semaphore variables.
producer()
{
int I;

while (1) { // Looping forever to request producer any time
enter_newitem(I); // It gets new item into I
wait(EMPTY); // decrement the EMPTY semaphore
wait(MUTEX); // Decrement the MUTEX before entering into the critical section
produce_item(I); // It puts an item into buffer
signal(MUTEX); // Increment the MUTEX after exiting from the critical section
signal(FULL); // It increments the FULL to counts the number of full slots in buffer
}
}

//Consumer consumes an item by getting an item from buffer
Consumer()
{
int I;
  
while (1) { // looping forever to request consumer any time
wait(FULL); // It decrement the FULL semaphore
wait(MUTEX); // Decrement the MUTEX before entering into the critical section
getitem_frombuffer(I); // Gets an item from buffer to consume
signal(MUTEX); // Increment the MUTEX after exiting from the critical section
signal(EMPTY); // It increments the EMPTY semaphore to count the number of empty slots in buffer
consume_item(I); // Consumes an item I.
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote