Question

In: Computer Science

Using a pseudocode example, explain the wait and signal operations of a binary semaphore.

Using a pseudocode example, explain the wait and signal operations of a binary semaphore.

Solutions

Expert Solution

*****************************Please give feedback if there is an issue in explanation.Thanks*********************

Abbreviation:

  1. CS Critical Section where shared variables are accessed and no two process can coexist.
  2. S Binary Semaphore

Binary Semaphore can only attain two value 0 or 1. Value 1 indicate that no process is currently in Critical Section.Value 0 indicate that one process is in critical section or may be more process are waiting to enter in CS. Binary Semaphore supports two operation wait(S) and Signal(S). Every process accessing a CS executes wait(S) before entering CS and executes Signal(S) after exiting CS further elaborated.

Terms Used

  1. waiting Queue : Queue that maintain all process that waiting to enter critical section .This queue is First In First Out.
  2. process Queue: Queue that maintain all process that are currently ready to be executed. Similar to ready queue. This queue is priority queue and priority is decided by which type of CPU scheduling algo is used

All process follow the below sequence of code

wait(S)

.....

CS

........

signal(S)

The wait and signal can be implemented in two approached

BLOCKING : CPU cycles are wasted on doing nothing just Looping.Unknown which process will be processed.(mainly depend on CPU scheduler which is selecting the process when signal was called). Can lead to starvation when a process is never get selected after a Signal

NON-BLOCKING : CPU cycles are not wasted but a waiting queue is maintained. Guarantee first process that was put in wait queue to removed first when signal is called . Least waiting time and no starvation.

Below is implementation of above approaches

=============================BLOCKING CALL===============================================

wait(S) {

while(S==0); // Loops forever until S is changed to 1

S=S-1; // The statement is only executed when S=1;

}

Signal(S)

{

if(s>=1)

return;

else

s=s+1;

}

====================NON-BLOCKING-CALL====================================================

wait(S)

{

if(S==1) //No process is waiting of this semaphore

{

S=0; // S is decreased to denote a process is waiting

return;

}

else

{

insert process 'p' executing wait to queue(Q) of waiting process.

Suspend(P); //Move process 'p' out of process queue

}

}

Signal (S)

{

if(S>=1) // A signal is called on no suspended process

return;

else // there is atleast one process suspended

{

P=remove from wait queue // first waited process is choden

resume(p); //Allow it execute by moving to process queue

}

}

Reference : Opearting System by Silberschatz, Galvin et al


Related Solutions

What is semaphore? List the properties of semaphore variables. Define the wait function wait(s).
What is semaphore? List the properties of semaphore variables. Define the wait function wait(s).
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Please describe signal transduction using yeast as an example.
Please describe signal transduction using yeast as an example.
Using pseudocode, write the code that will perform the necessary file operations as described in the...
Using pseudocode, write the code that will perform the necessary file operations as described in the short scenario below: “During registration at a college, student’s details will be captured by a staff member and written to a file called “studentDetails.dat”. The following student details will be captured: name, surname, studentNumber, registeredCourse. The staff member will be prompted for the student details. After every record added to the file, the staff member will be asked whether they would like to add...
Q1. [10 pt] How will you implement a binary semaphore with block wake-up protocol? Give the...
Q1. [10 pt] How will you implement a binary semaphore with block wake-up protocol? Give the code. (Please help, Operating system question, 2011 Spring)
Explain the main purpose of an interrupt signal. Give an example of how the interrupt is...
Explain the main purpose of an interrupt signal. Give an example of how the interrupt is used.
create flowchart using Flowgorithm and Pseudocode for the following program example:   Design a program for Jones...
create flowchart using Flowgorithm and Pseudocode for the following program example:   Design a program for Jones College. The current tuition is $12,000 per year, and tuition is expected to increase by 5 percent each year. Display the tuition amount for each year over the next five years (use a looping structure).
------An analog signal is sampled, quantized, and encoded into a binary PCM wave. The number of...
------An analog signal is sampled, quantized, and encoded into a binary PCM wave. The number of representation levels used is 1024. A synchronizing pulse is added at the end of each code word representing a sample of the analog signal. The resulting PCM wave is transmitted over a channel of bandwidth 24kHz using a 8 PAM system with raised-cosine spectrum. The rolloff factor is 0.5. (a) Find the rate (bit/s) at which information is transmitted through the channel. (b) Find...
Explain what constitutes discontinued operations and provide an example.
Explain what constitutes discontinued operations and provide an example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT