Operating system notes-Chapter 10 semaphores and monitors

background

Concurrency problems in the operating system: race conditions, that is, there is a big problem with the concurrency of multiple programs

Operating system synchronization refers to the coordinated execution of multiple threads sharing common resources, including mutual exclusion and conditional synchronization. Mutual exclusion means that only one thread can execute in the critical section at a time.

In actual conditions, it is difficult to ensure correct synchronization. Need high-level programming abstraction (such as: lock) and low-level hardware to support compilation.

Semaphore and monitor are more advanced solutions than lock.

signal

Semaphore (sem) is an abstract data type:

  • One integer (sem), two atomic operations (P[decrease], V[increase], Dutch)
  • P(): sem minus 1, if sem<0, wait, otherwise continue
  • V(): sem plus 1, if sem<=0, wake up a waiting IP

The use of semaphores

  • The semaphore is an integer, and the initial value is generally set to a number greater than 0, which will not block during P operation
  • Semaphore is a protected variable
    • After initialization, the only way to change the value of a semaphore is through P( )/V()
    • Operation must be atomic
  • P() can make the process block/suspend, V() can wake up the process
  • Assume that the semaphore is "fair"
    • No thread is blocked in P() and still blocked if V() is called infinitely frequently (on the same semaphore)
    • In practice, FIFO is often used
  • There are two types of semaphores
    • Binary semaphore: 0/1, achieve the same function as lock
    • General/counting semaphore: any non-negative value can be taken
    • The two behave each other (given one can realize the other)
  • Semaphore can be used in two ways
    • Mutually exclusive
    • Conditional synchronization (scheduling constraint-one thread waits for something to happen in another thread)
  • Use semaphores to achieve mutual exclusion

Semaphore realization

Monitor

Purpose: To separate the concerns of mutual exclusion and conditional synchronization

Pipeline:

  • A lock: specify the critical section
  • 0 or more condition variables: wait/notify semaphore is used to manage concurrent access to shared data

General method:

  • Related shared data collected in objects/modules
  • Define methods to access shared data

Condition Variable:

  • Allow the wait state to enter the critical section
    • Allow waiting (sleeping) threads to enter the critical section
    • Atomic releases the lock at a certain moment and enters sleep
  • wait() operation
    • Release the lock, sleep, regain the lock and return
  • signal() operation(or broadcast() operation)
    • Wake up the waiter (or all waiters), if any

achieve:

  • Thread enters the entry queue in a mutually exclusive way (lock)
  • The thread that enters the monitor starts to execute the function maintained by the monitor
  • During the execution of the function, when it is necessary to wait for the shared resource, the current thread is suspended on the corresponding condition variable, and the lock is released (under the condition variable is the suspended queue, when the condition variable is met, the thread is awakened. For the condition Variables have two operations: wait x/signal x)

If thread A sleeps in the monitor, another thread B will be awakened to enter the monitor. Then, before the lock release of A, the execution order of the threads is generally from A to lock release and then B (Hansen-style).

Classic synchronization problem

  • Reader and writer question
  • Philosopher's meal problem

Summary of semaphores and monitoring

Insert picture description here
Synchronization structure:

  • Lock: Mutex
  • Condition variable: conditional synchronization
  • Other primitives: semaphore

Guess you like

Origin blog.csdn.net/MaoziYa/article/details/106649014