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

A.Describe the sequence of event that would lead to the condition where both thr

ID: 3860977 • Letter: A

Question

A.Describe the sequence of event that would lead to the condition where both threads are stuck forever. (7) Semaphores can be used to limit access to resources. The following system uses two threads. Both threads need access to a printer and an SPI (a communication port) port at the same time. The binary semaphore sprint provides mutual exclusive access to the printer, and the binary semaphore sSPI provides mutual exclusive access to the SPI port (the Outs would open a port for communication). The following scenario has a serious flaw: PI function Thread 1 P(&sprint;): P(&sSPI;); printf ece554 bye"); OutSPI(25); V(&sSPI;); V(&sprint;); Thread 2 P(&sSPI;); P(&sprint;); printf("ece554 welcome"); OutSPI(50); V(&sprint;); V(&sSPI;): Describe the sequence of events that would lead to the condition where both threads are stuck forever. a- b- How would you change the above program to prevent the deadlock?

Explanation / Answer

A)

P(&sprint);                           P(&SPI);
P(&SPI);                           P(&sprint);  
printf("ece554 bye");               printf("ece554 welcome");
outSPI(25);                           outSPI(50);
V(&SPI);                           V(&sprint);  
V(&sprint);                           V(&SPI);

Suppose thread 1 run first and execute the P(&sprint), so thread 1 has access to the printer.
Now thread 2 execute the line P(&SPI),so thread 2 has access to the SPI port.

After that there would be deadlock or stuck forever as thread 1 has access to the printer and waiting for P(&SPI) SPI port which is held by thread 2.
and thread 2 have access to SPI port and waiting for P(&sprint) printer which is held by thread 1.

b: Below is the change sequence to avoid the deadlock or stuck forever

P(&sprint);                           P(&sprint);
P(&SPI);                           P(&SPI);  
printf("ece554 bye");               printf("ece554 welcome");
outSPI(25);                           outSPI(50);
V(&SPI);                           V(&SPI);  
V(&sprint);                           V(&sprint);

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