Mutual Exclusion and Synchronizationالاستبعاد المتبادل والتزامن

الشريحة 1 من 14

مقدمة الفصل

ماذا ستتعلم في هذا الفصل

  1. Principles of concurrency: key terms, race conditions, process interaction, the requirements for mutual exclusion.
  2. Mutual exclusion with hardware support: disabling interrupts and special machine instructions.
  3. Semaphores: primitives, binary semaphores, strong/weak semaphores, the producer/consumer problem.
  4. Monitors: characteristics and synchronization with condition variables.
تفاصيل المصدرشرائح الفصل 2b · الشريحة ⁦2⁩
المصدر
شرائح الفصل 2b · الشريحة ⁦2⁩
ملاحظات حول المصدر (1)
  • المخطط يذكر تمرير الرسائل ومشكلة القرّاء والكتّاب، ولا توجد شرائح عنهما في هذا الملف.

Concurrencyالتزامن

  • Managing multiple processes — multiprogramming, multiprocessing, distributed processing — is central to modern OS design; the big issue is concurrency: managing their interaction.
Concurrency arises in (slide 4)

Multiple applications

  • Sharing time

Structured applications

  • Extension of modular design

Operating system structure

  • OS themselves implemented as a set of processes or threads
  • Processes may be interleaved on uniprocessors and overlapped on multiprocessors (slides 6–7).
  • Difficulties: sharing of global resources; optimally managing the allocation of resources; programming errors are hard to locate because results are not deterministic and reproducible (slide 8).
تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦3, 4, 6, 7, 8⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦3, 4, 6, 7, 8⁩

Key terms related to concurrencyالمصطلحات الأساسية

تذكّر التعريف قبل الكشف.

  1. Atomic operation
  2. Critical section
  3. Deadlock
  4. Livelock
  5. Mutual exclusion
  6. Race condition
  7. Starvation

Two processes keep changing their states in response to each other, but neither does any useful work. Which term from Table 5.1 describes this?

تفاصيل المصدرشرائح الفصل 2b · الشريحة ⁦5⁩
المصدر
شرائح الفصل 2b · الشريحة ⁦5⁩

Race conditionحالة السباق

Race condition
Multiple processes or threads read and write data items in a way where the final result depends on the order of execution of the processes. The output depends on who finishes the race last.

Enforcing single access to echo (slide 9)

  1. 1

    P1 and P2 run on separate processors; P1 enters echo first.

  2. 2

    P2 tries to enter but is blocked — P2 suspends.

  3. 3

    P1 completes execution.

  4. 4

    P2 resumes and executes echo.

Two threads both add 1 to a shared counter; sometimes the counter grows by 2, sometimes by 1, depending on timing. What is this an example of?

تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦9–10⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦9–10⁩

Process interactionتفاعل العمليات

Table 5.2 Process Interaction (slide 11, simplified)
Degree of awarenessدرجة الإدراكRelationshipالعلاقةPotential control problemsمشكلات التحكم المحتملة
Processes unaware of each otherCompetition
  • Mutual exclusion
  • deadlock (renewable resource)
  • starvation
Processes indirectly aware of each other (e.g., shared object)Cooperation by sharing
  • Mutual exclusion
  • deadlock (renewable resource)
  • starvation
  • data coherence
Processes directly aware of each other (have communication primitives available to them)Cooperation by communication
  • Deadlock (consumable resource)
  • starvation

Competition among processes for resources — three main control problems (slide 12)

  • 1. Need for mutual exclusion (critical sections)
  • 2. Deadlock
  • 3. Starvation
تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦11–12⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦11–12⁩
ملاحظات حول المصدر (1)
  • الجدول مختصر: حُذف عمود «تأثير العملية في الأخرى» من الشريحة 11 للتبسيط، ونصه: نتائج العملية مستقلة أو قد تعتمد على معلومات من غيرها، وقد يتأثر توقيتها.

Requirements for mutual exclusionمتطلبات الاستبعاد

  1. 1Only one process at a time is allowed in the critical section for a resource
  2. 2A process that halts in its noncritical section must do so without interfering with other processes
  3. 3No deadlock or starvation
  4. 4A process must not be delayed access to a critical section when there is no other process using it
  5. 5No assumptions are made about relative process speeds or number of processes
  6. 6A process remains inside its critical section for a finite time only

Which is one of the requirements for mutual exclusion?

تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦13–14⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦13–14⁩

Disabling interruptsتعطيل المقاطعات

Mutual exclusion: hardware support

  • Uniprocessors only allow interleaving; a process runs until it invokes an OS service or until it is interrupted.
  • Disabling interrupts guarantees mutual exclusion.
  • It will not work in a multiprocessor architecture.

Pseudo-code (slide 17), repeated while (true)

  1. 1

    Disable interrupts

  2. 2

    Critical section

  3. 3

    Enable interrupts

  4. 4

    Remainder

What is the main limitation of disabling interrupts to achieve mutual exclusion?

تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦16–17⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦16–17⁩

Special machine instructionsالتعليمات الخاصة

Mutual exclusion: hardware support

  • Compare&Swap instruction — also called a "compare and exchange instruction".
  • Exchange instruction.

Advantages (slide 19)المزايا

  • Applicable to any number of processes on a single processor or multiple processors sharing main memory
  • Simple and therefore easy to verify
  • Can support multiple critical sections

Disadvantages (slide 20)العيوب

  • Busy-waiting consumes processor time
  • Starvation is possible when a process leaves a critical section and more than one process is waiting
  • Deadlock is possible

Which is a DISADVANTAGE of hardware mutual exclusion with special machine instructions?

تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦18–20⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦18–20⁩

Semaphoresالسيمافورات

Semaphore
An integer value used for signalling among processes. Only three operations may be performed on it, all atomic: initialize, decrement (semWait), increment (semSignal).
Semaphore primitives (Figure 5.3, slide 23)

semWait(s)

  • s.count--
  • if (s.count < 0): place this process in s.queue and block this process

semSignal(s)

  • s.count++
  • if (s.count <= 0): remove a process P from s.queue and place P on the ready list

A semaphore s has count = 1. Processes A, B and C call semWait(s) in that order; semWait decrements the count and blocks the caller if the count becomes negative. What is s.count and how many processes are blocked?

تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦22–23⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦22–23⁩

Binary, strong and weak semaphoresأنواع السيمافور

Binary semaphore primitives (Figure 5.4, slide 24) — value is zero or one

semWaitB(s)

  • if (s.value == one): s.value = zero
  • else: place this process in s.queue; block this process

semSignalB(s)

  • if (s.queue is empty()): s.value = one
  • else: remove a process P from s.queue; place P on the ready list
Strong semaphoreسيمافور قوي
Processes waiting on the semaphore are removed from the queue in FIFO order.
Weak semaphoreسيمافور ضعيف
Doesn't specify the order of removal from the queue.
Figure 5.5 — example of the strong semaphore mechanism (slides 26–27). Queues are written as drawn; their right end is the head.
StepProcessorSemaphoreBlocked queueReady queue
1As = 1(empty)C D B
2Bs = 0(empty)A C D
3Ds = −1BA C
4Ds = 0(empty)B A C
5Cs = 0(empty)D B A
6Ds = −3B A C(empty)
7Ds = −2B AC
Figure 5.7 — processes accessing shared data protected by a semaphore (slide 28)
EventQueue for semaphore lockValue of lock
(start)(empty)1
A: semWait(lock) — A enters its critical region(empty)0
B: semWait(lock) — B blocksB−1
C: semWait(lock) — C blocksC B−2
A: semSignal(lock) — B entersC−1
B: semSignal(lock) — C enters(empty)0
C: semSignal(lock)(empty)1
  • Note that normal execution can proceed in parallel but that critical regions are serialized (slide 28).

In what order does a strong semaphore remove waiting processes from its queue?

تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦24–28⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦24–28⁩

Producer/consumer problemالمنتج والمستهلك

General situation

  • One or more producers generate data and place it in a buffer.
  • A single consumer takes items out of the buffer one at a time.
  • Only one producer or consumer may access the buffer at any one time.
The problem
Ensure that the producer can't add data into a full buffer and the consumer can't remove data from an empty buffer.
Infinite buffer (slide 30)

Producer

  • produce item v
  • b[in] = v
  • in++

Consumer

  • while (in <= out) do nothing
  • w = b[out]
  • out++
  • consume item w

What must a solution to the producer/consumer problem ensure?

تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦29–30⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦29–30⁩

Bounded bufferالمخزن المحدود

The producer/consumer problem

Functions in a bounded (circular) buffer of n elements (slide 33)

Producer

  • produce item v
  • while ((in + 1) % n == out) do nothing — buffer full
  • b[in] = v
  • in = (in + 1) % n

Consumer

  • while (in == out) do nothing — buffer empty
  • w = b[out]
  • out = (out + 1) % n
  • consume item w

In the bounded-buffer solution with a circular array b[0..n−1] and the pointers in and out, which condition makes the consumer wait?

تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦32–34⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦32–34⁩
ملاحظات حول المصدر (1)
  • نص كود الشريحة 33 مشوّه في طبقة النص للملف، وأُعيد ترتيبه حسب شكله في الشريحة.

Monitorsالمراقبات

Monitor
A programming-language construct that provides equivalent functionality to that of semaphores and that is easier to control; implemented in Concurrent Pascal, Pascal-Plus, Modula-2, Modula-3, and Java.

Chief characteristics (slide 37)

  • Local data variables are accessible only by the monitor.
  • A process enters the monitor by invoking one of its procedures.
  • Only one process may be executing in the monitor at a time.
cwait(c)
Suspend execution of the calling process on condition c.
csignal(c)
Resume execution of some process blocked after a cwait on the same condition.

How many processes may be executing inside a monitor at the same time?

تفاصيل المصدرشرائح الفصل 2b · الشرائح ⁦36–38⁩
المصدر
شرائح الفصل 2b · الشرائح ⁦36–38⁩

الخلاصة

تفاصيل المصدر
المصدر
شرائح الفصل 2b

خريطة الفصل

    1. 1مقدمة الفصل
  1. Multiple processes and concurrencyالعمليات المتعددة والتزامن

    1. 2Concurrencyالتزامن
    1. 3Key terms related to concurrencyالمصطلحات الأساسية
  2. Race condition and enforcing single accessحالة السباق وفرض الوصول المنفرد

    1. 4Race conditionحالة السباق
  3. Process interaction and competitionتفاعل العمليات والتنافس

    1. 5Process interactionتفاعل العمليات
    1. 6Requirements for mutual exclusionمتطلبات الاستبعاد
  4. Mutual exclusion: hardware supportالاستبعاد المتبادل بدعم العتاد

    1. 7Disabling interruptsتعطيل المقاطعات
    2. 8Special machine instructionsالتعليمات الخاصة
  5. Semaphoresالسيمافورات

    1. 9Semaphoresالسيمافورات
    2. 10Binary, strong and weak semaphoresأنواع السيمافور
  6. The producer/consumer problemمشكلة المنتج والمستهلك

    1. 11Producer/consumer problemالمنتج والمستهلك
    2. 12Bounded bufferالمخزن المحدود
    1. 13Monitorsالمراقبات
    1. 14الخلاصة