Question

In: Computer Science

Compare and contrast the three techniques of dealing with critical sections of code in (1) busy-wait,...

Compare and contrast the three techniques of dealing with critical sections of code in (1) busy-wait, (2) mutex, and (3) semaphore.

Solutions

Expert Solution

QUESTION

Compare and contrast the three techniques of dealing with critical sections of code in (1) busy-wait, (2) mutex, and (3) semaphore.

SOLUTION

Critical Section

A section of code in which only 1 process can be executed at any given time because it uses shared variables. We can use different techniques to solve this problem.

  • · Lock the process before entering the critical section
  • · Unlock the process when you are done executing the critical section
  • · Wait to use the critical section of code if it is already locked
  • · Never have to wait ‘forever’ when trying to get to your critical section

(1) Busy Wait

The repeated execution of a loop of code while waiting for an event to occur is called busy waiting. The CPU is not engaged in any real productive activity during this period, and the process does not progress toward completion.In view of critical section, busy waiting means while it is waiting to enter its critical section it does nothing but continue to test its entry condition.If a low priority process is in its critical section, and a high priority process goes into busy waiting, it may never get to its critical section.

(2) Mutex

Mutex is a mutual exclusion object that synchronizes access to a resource. It is created with a unique name at the start of a program. The Mutex is a locking mechanism that makes sure only one thread can acquire the Mutex at a time and enter the critical section.

Example:

wait (mutex);
…..
Critical Section
…..
signal (mutex);

(3)Semaphore

A semaphore is a signalling mechanism and a thread that is waiting on a semaphore can be signaled by another thread. A semaphore uses two atomic operations, wait and signal for process synchronization.

The wait operation decrements the value of its argument S, if it is positive. If S is negative or zero, then no operation is performed.

Example:

wait(S)
{
while (S<=0);
S--;
}


The signal operation increments the value of its argument S.

Example:

signal(S)
{
S++;
}

There are mainly two types of semaphores i.e. counting semaphores and binary semaphores.

Counting Semaphores are integer value semaphores and have an unrestricted value domain. These semaphores are used to coordinate the resource access, where the semaphore count is the number of available resources.

The binary semaphores are like counting semaphores but their value is restricted to 0 and 1. The wait operation only works when the semaphore is 1 and the signal operation succeeds when semaphore is 0.

Conclusion

Difference between Mutex and Semaphore

Both busy waiting and blocking methods can be used as means to address critical section problems and process synchronization.On a single CPU, busy waiting becomes a circular issue, so blocking would make more sense.

Mutex and Semaphores are examples of blocking methods.

A Mutex is different than a semaphore as it is a locking mechanism while a semaphore is a signalling mechanism. Semaphore is different from mutex as the mutex can be signaled only by the thread that called the wait function.Also binary semaphore can be used as a Mutex but a Mutex can never be used as a semaphore.


Related Solutions

Compare and contrast the Ni-NTA chromatography experiment and the HIC technique . Name three general techniques...
Compare and contrast the Ni-NTA chromatography experiment and the HIC technique . Name three general techniques or tools used in the Ni-NTA chromatography What is histidine? Why is His6 such a popular protein tag for purification? Summarize how the scientists produced the different variants of His6-GFP protein samples for purification. How do we use His6/Ni-NTA interactions in experiments?
Compare and contrast web builders and lie-n-wait spiders with respect to costs and benefits of their...
Compare and contrast web builders and lie-n-wait spiders with respect to costs and benefits of their mode of prey capture. please answer the question completely, but in easily in a simple way. Thank you very much in advance for your help. Please give a chance to other expert to answer this if you are not sure what the question really asks for. Thank you :)
Question 1: Compare and contrast the project evaluation and review technique (PERT) with the critical path...
Question 1: Compare and contrast the project evaluation and review technique (PERT) with the critical path method (CPM). Question 2: Explain in detail Eliyahu Goldratt’s Theory of Constraints with a brief note on its application in Project Management.
ANA code of ethics similarities and differences to ICN code of ethics Compare and contrast the...
ANA code of ethics similarities and differences to ICN code of ethics Compare and contrast the responsibilities you think a nurse has with those that the ANA Code discusses. What are the similarities and what are the differences? Discuss what you found to be new that a nurse does, as defined by the code. Compare and contrast the ANA and ICN Codes. Which one do you like better and explain why?
I was given an assignment to write three sections of Code. 1. Look up password 2....
I was given an assignment to write three sections of Code. 1. Look up password 2. Decrypt a password. 3. What website is this password for? Here's the base code: import csv import sys #The password list - We start with it populated for testing purposes passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] #The password file name to store the passwords to passwordFileName = "samplePasswordFile" #The encryption key for the caesar cypher encryptionKey=16 #Caesar Cypher Encryption def passwordEncrypt (unencryptedMessage, key): #We will start with...
Compare and contrast "The Goal" and "Critical Chain" novels by Goldratt as it relates to Project...
Compare and contrast "The Goal" and "Critical Chain" novels by Goldratt as it relates to Project Management
Compare and contrast the techniques used to determine the economic life of an asset whose lifespan...
Compare and contrast the techniques used to determine the economic life of an asset whose lifespan is (1) longer and (2) shorter than the planning horizon.
Compare and contrast sterilization and sanitization; give a detailed descriptions of ALL techniques discussed in the...
Compare and contrast sterilization and sanitization; give a detailed descriptions of ALL techniques discussed in the text and notes used for both. Paragraph form
Compare and contrast the different techniques to analyze total project risk. What are the strengths and...
Compare and contrast the different techniques to analyze total project risk. What are the strengths and weaknesses of each technique?
A) Discuss plant strategies to avoid herbivory B) Compare and contrast sit-and-wait foragers to active foragers
A) Discuss plant strategies to avoid herbivory B) Compare and contrast sit-and-wait foragers to active foragers
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT