Question

In: Computer Science

How is a semaphore initialized when used as a i) a lock() ii) a CV?

  1. How is a semaphore initialized when used as a i) a lock() ii) a CV?

Solutions

Expert Solution

Locks and condition variables are an attempt to implement monitors in languages that do not have them. Normally, monitors are part of the language definition and there needs to be syntax to allow programmers to declare a monitor. The main reason that it has to be part of a language is that normal languages can not enforce the mutual exclusiveness of monitor procedure calls. However, you can attempt to get similar behavior by using semaphores.

To initialize a "lock" with a counting semaphore (so that code segments can be implemented atomically), one sets the initial value of the semaphore to 1. Notice that doing so will allow the first thread that calls P() on the semaphore to proceed but all other calls to P() to block until V() is called.

Lock Program:

Lock::Lock() // Constructor

   {
      value = 1;
      numSleepers = 0; // Number of process blocked on this semaphore
      processID = -1; // No process holds this lock
      mutex = new Semaphore( 1 );  // Initialize semaphore to 1.
      sleep = new Semaphore( 0 );  // Initialize semaphore to 0.
   }
   Lock::Acquire()  // Acquire a lock
   {
      while ( 1 )
      {
         mutex->P();
         if ( value <= 0 )
         {
            numSleepers++;
            mutex->V();
            sleep->P();
         }
         else 
         {
            value--;
            processID = process->getPID(); // Record PID of process
            mutex->V();
            break;  // exit while loop
         }
       }
   }
   Lock::Release()  // Release a lock
   {
      mutex->P();
      value++;
      if ( value > numSleepers ) exit();  // abort if release not used properly
      numSleepers--;
      sleep->V();  // Wake up one sleeper
      mutex->V();
   }

CV program:

procedures to go through.

    CV::CV( Lock *lock1 ) // Constructor
    {
       lock = lock1;  // The associated lock with this condition variable
       numSleepers = 0;  // number of sleepers on this CV.
       CV_sleep = new Semaphore( 0 );
       mutex = new Semaphore( 1 );
    }

    CV::Wait()
    {
       mutex->P();
// quit if process does not have lock
       if ( lock->processID != process->GetPID() ) exit(); 

       numSleepers++;  
       lock->Release(); // Release lock associated with this CV.
       mutex->V();
       CV_sleep->P();  // Sleep
       lock->Acquire(); // When woken up, reacquire lock
    }

    CV::Signal()
    {
       mutex->P();
// quit if process does not have lock
       if ( lock->processID != process->GetPID() ) exit(); 

       if( numSleepers > 0 )
         {
            CV_sleep->V();  // Wake single process up
            numSleepers--;
          }
       mutex->V();
    }

Related Solutions

What is the main difference between semaphore and a CV? A semaphore and a lock?
What is the main difference between semaphore and a CV? A semaphore and a lock?
Under what conditions would the use of a counting semaphore initialized to 5 be used instead...
Under what conditions would the use of a counting semaphore initialized to 5 be used instead of a mutex lock?
Describe the timeline when the ova completes: Prophase II Prophase I Anaphase I Metaphase II Entire...
Describe the timeline when the ova completes: Prophase II Prophase I Anaphase I Metaphase II Entire Meiosis process
describe how phase I and phase II metabolism influence/affect i) renal and ii) hepatic elimination of...
describe how phase I and phase II metabolism influence/affect i) renal and ii) hepatic elimination of a drug? Answers must be detailed and complete sentences.
How to write Best CV for Internship, Please Also include Sample of any information, i already...
How to write Best CV for Internship, Please Also include Sample of any information, i already got many on Google , But i want best, Please Write any Sample CV of random information. i will then change it to mine, Really Thanks
I want answers in 60 minutes Design a CV for the job of a Physiotherapist at...
I want answers in 60 minutes Design a CV for the job of a Physiotherapist at any hospital supposing an individual has completed DPT with an A grade and have 2 years of professional experience in the related field. Your answer Define the terms: Cohesion, Coherence, Skimming, Scanning Your answer You are the CEO of a company write a memorandum to your staff explaining the new software the company has purchased. Your answer What five major details should a person...
Describe a lock-in strategy that might be used by a company to protect the market share...
Describe a lock-in strategy that might be used by a company to protect the market share of its network good. please type the answer. thanks
When a variable is declared, it may also be initialized (given its initial – meaning first...
When a variable is declared, it may also be initialized (given its initial – meaning first – value) using the assignment operator (an equals sign). What is the initial value of cupSize?
What are the differences between type I and type II diabetes? When the body does not...
What are the differences between type I and type II diabetes? When the body does not have enough insulin to convert carbohydrates into energy, what happens to the vascular system? Why are people with diabetes so much more vulnerable to heart disease and some cancers?
8) Errors: Type I and Type II are errors that are possible even when a hypothesis...
8) Errors: Type I and Type II are errors that are possible even when a hypothesis test is done correctly. A hypothesis test is based on probabilities (p-values) This means there is always a probability of drawing the wrong conclusion even when done correctly. Please review the following: a.) What are type I and type II errors? b.) Be able to discuss what a type I or type II error is in a given scenario c.) What is the relationship...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT