Docsity

Sync Building Blocks: Atomic Primitives, Monitors, Serializers, Path Expressions - Prof. A, Study notes of Operating Systems

Partial preview of the text

Download Sync Building Blocks: Atomic Primitives, Monitors, Serializers, Path Expressions - Prof. A and more Study notes Operating Systems in PDF only on Docsity!

Advanced Synchronization

Bloom paper (online)

Chapter 6 from Silberschatz

slides have many illustrative examples

Support for Critical Section

Hardware support:

Some hardware instructions

are provided to support the programmer

(e.g., ‘test&set’, and ‘swap’ instructions)

Operating System Support:

Operating system

supports for the declaration of data structures andalso operations on those data structures

(e.g., semaphores)

High Level Language Support:

Language

provides support for data structures and operationson them to help with synchronization.

(e.g., critical regions, monitors, serializers, etc)

Limits on low-level mechanism

no abstraction and modularity

i.e., a process that uses a semaphore has toknow which other processes use the semaphore,and how these processes use the semaphore

a process cannot be written in isolation

error-prone

change order or omit lock/unlock, signal/wait

difficult to verify correctness

Higher-level constructs

Language Support

monitors, serlializers, path experssions,conditional critical regions, RWlocks…

mainly programming languages targeted forconcurrent programming and objectoriented languages: Concurrent Pascal (PathPascal), Concurrent C, Java, Modula, Ada,Mesa, Eiffel, …

Requirements of Synch Mech

modularity

separation of resource and its access operations, andsynchronizer and its mechanisms

expressive power

specifying the needed constraints (exclusion andpriority constrains in terms of relevant information…)

ease of use

composing a set of constraints (for complex synch.schemes)

modifiability

changing the constraints if needed

correctness

safety net against inadvertent user errors

Basic Mechanisms

mutex lock

condition variables

semaphores

Proposed in 1969 by Dijkstra for process synchronization“Cooperating Sequential Processes”

P(S):

while S <= 0 do nothing; // busy wait S = S - 1;

V(S):

S = S + 1;

Init_Sem(S, Count): S = Count;

enables mutual exclusion, process synch;

struct sem {

value: int; L: list of processes

} S; Init_Sem(S, Count) {

S.value = Count;

OS support

block

wakeup - P and V atomic Semaphore without busy-wait

P(S) {S.value = S.value -1;

if (S.value < 0) {

add T to S.L; block;

V(S) {S.value = S.value + 1;

if (S.value <=0) {

select a T from S.L; wakeup(T);

Mutual exclusion using

semaphores

Use wait and signal like lock and unlock

bracket critical sections in the same manner

Semaphore value should never be greater than 1

This is a

binary

semaphore

Depends on correct order of calls by programmer

All mutex problems apply (deadlock…)

What should the initial value be?

Monitors

Monitors are a synchronization mechanism where the shareddata are encapsulated with the locks required to access them

Variables/data local to a monitor cannot be directly accessed fromoutside.

the data can only be accessed through the locked code

mon_name (ep1, ep2, …, epn) { entry_point ep entry_point ep

all ep’s are mutually exclusive

multiple threads inside the monitor?

yes, but only one active

others? - waiting on an event

Monitors ..Silberschatz textbook 6.7. example of Monitorimplementation with semaphores (structure for each conditionx, actions for x.wait and x.signal…)

Data Procedure i Procedure j Process a Process b ConditionVariable.wait ConditionVariable.signal Only one process at a given time Queue for each conditional variable

Monitor-style programming

With mutexes and condition variables you canimplement any critical section

CS_enter(); [controlled code] CS_exit(); void CS_enter() {

void CS_exit() { Lock(m) { Lock(m) { while (![condition]) [change shared data Wait(c, m) to reflect out_of_CS] [change shared data [broadcast/signal as needed] to reflect in_CS]

[broadcast/signal as needed]

Readers/Writer example structure:

monitor { Condition OKtoread, OKtowrite;

int readercount;

// data decls

void StartRead() { ... } void StartWrite() { ... } void FinishRead() { ... } void FinishWrite() { ... } }

Reader’s Priority: Monitors ... procedure StartWrite; begin if busy

OR

readcount

then OKtowrite.wait; busy

true; end StartWrite; procedure EndWrite; begin busy

false; if OKtoread.queue then OKtoread.signal; else OKtowrite.signal; end EndWrite; begin

initialization readercount

busy

false; end; end readers-writers.

Readers-Writers: Monitors

Reader: StartRead(); ReadFile(); EndRead(); Writer: StartWrite(); WriteFile(); EndWrite();