7 - Semaphores and Monitors

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Monitors in Java

- A lock and condition variable are in every Java object - There are no explicit classes for locks or condition variables - Every object is/has a monitor. At most, one thread can be inside an object's monitor. - The compiler generates code to acquire the object's lock at the start of the method and release it just before returning. - You can use the synchronize keyword to synch multiple methods together. You can also wrap certain sections in methods to synchronize.

Monitor

- A programming language construct that controls access to shared data, synchronization code added by compiler, enforced at runtime. - A monitor encapsulates: shared data structures, procedures, and synchronization - A monitor protects its data from unstructured access - It guarantees that threads accessing its data through its procedures interact only in legitimate ways

Semaphores

- Binary (mutex) and counting. - An abstract data type to provide mutual exclusion to critical sections. - Can be used as an atomic counter - Considered an integer that supports two operations: decrement and increment Decrement blocks threads until semaphore is open and increment allows another thread to enter. - Semaphore safety property: the semaphore value is always greater than or equal to zero

Semaphore Blocking

- Each semaphore is a queue of waiting processes - When P() is called by a thread: If Semaphore is open, thread continues. If semaphore is closed, thread blocks on queue. - Then V() opens the semaphore: If a thread is waiting on the queue, the thread is unblocked. If no threads are waiting on the queue, the signal is remembered for the next thread.

Semaphore Cons

- They are essentially shared global variables which can potentially be accessed anywhere in the program - No connection between the semaphore and the data being controlled by the semaphore - Used both for critical sections (mutual exclusion) and coordination (scheduling) - No control or guarantee of proper usage - Sometimes hard to use and prone to bugs.

Condition Variables vs Semaphores

- They are not the same however, they can each be used to implement the other

Mesa Monitors (Mesa, Java)

- signal() places a waiter on the ready queue, but signaler continues inside monitor - Condition is not necessarily true when waiter runs again

Hoare Monitors

-signal() immediately switches from the caller to a waiting thread -the condition that the waiter was anticipating is guaranteed to hold when waiter executes -signaler must restore monitor invariants before signaling

Bounded Buffer

A bounded buffer lets multiple producers and multiple consumers share a single buffer. Producer and consumer execute at different rates Producer - inserts resources into the buffer set Consumer - removes resources from the buffer set Mode possible with three buffers: - Empty - count of empty buffers - Full - count of full buffers - Mutex - mutual exclusion to shared set of buffers

Monitor Semantics

A monitor guarantees mutual exclusion. Only one thread can execute any monitor procedure at any time. If a second thread invokes a monitor procedure when a first thread is executing one, it blocks. Uses four methods: StartRead, StartWrite, EndRead and EndWrite.

Monitor Invariant

A safety property associated with the monitor, expressed over the monitored variables. It holds whenever a thread enters or exits the monitor.

Condition Variables

Condition variables support three operations: - Wait (release monitor lock, wait for C/V to be signaled. So condition variables have wait queues too) - Signal (wakeup one waiting thread - Broadcast (wakeup all waiting threads) Condition variables are not boolean objects

Semaphore Types

Mutex Semaphore (aka Binary Semaphore) - Represents single access to a resource, guarantees mutual exclusion to a critical section Counting Semaphore (aka General Semaphore) - Represents a resource with many units available, or a resource that allows certain kinds of unsynchronized concurrent access. Multiple threads can pass the semaphore. Number of threads determined by the semaphore.

Signal Semantics

There are two flavors of monitors that differ in the scheduling semantics of signal() Hoare Monitors and Mesa Monitors

Condition Variable

Associated with a condition needed for a thread to make progress once it is in the monitor

Where would you use Semaphores?

If you wanted to control the ability to read write a specific object. i.e. allow multiple reader processes but only allow one writer process.


Ensembles d'études connexes

Pathophysiology: Fluid & Hemodynamic Disorders

View Set

Mental Health Nursing Practice Questions

View Set

NU471 Week 5 EAQ #4 Evolve Elsevier: Disaster Planning - Mastery Level Target: Level 3

View Set

Prep U Ch 39 Fluid, Electrolyte, and Acid Base Balance

View Set

Principles of Management Exam 2 (Chapter 5-8)

View Set

[AP CSP] Encryption, Digital Certificates and Key Exchange Vocabulary

View Set