Download presentation
Presentation is loading. Please wait.
Published byRafe Lane Modified over 8 years ago
1
OS Winter’03 Concurrency
2
OS Winter’03 Bakery algorithm of Lamport Critical section algorithm for any n>1 Each time a process is requesting an entry to CS, assign it a ticket which is Unique and monotonically increasing Let the process into CS in the order of their numbers
3
OS Winter’03 Choosing a ticket Does not guarantee uniqueness! Use process Ids: Process need to know that somebody perhaps chose a smaller number:
4
OS Winter’03 Bakery algorithm for n processes
5
OS Winter’03 Correctness Lemma: Mutual exclusion is immediate from this lemma It is easy to show that Progress and Fairness hold as well (recitation)
6
OS Winter’03 Hardware primitives Elementary building blocks capable of performing certain steps atomically Should be universal to allow for solving versatile synchronization problems Numerous such primitives were identified: Test-and-set Fetch-and-add Compare-and-swap
7
OS Winter’03 Test-and-Set (TS) boolean test-and-set(boolean &lock) { temp=lock; lock=TRUE; return temp; } reset(boolean &lock) { lock=FALSE; }
8
OS Winter’03 Critical section using TS Shared boolean lock, initially FALSE do { while(test-and-set(&lock)); critical section; reset(&lock); reminder section; } while(1); Check yourself! Is mutual exclusion satisfied? Is progress satisfied? Is fairness satisfied?
9
OS Winter’03 Discussion Satisfies Mutual Exclusion and Progress Does not satisfies Fairness Provides exclusion among unbounded number of processes Process IDs and number are unknown Busy waiting Burning CPU cycles while being blocked
10
OS Winter’03 Fetch-and-Add (FAA) s: shared, a: local int FAA(int &s, int a) { temp=s; s=s+a; return temp; } FAA can be used as a ticket machine
11
OS Winter’03 Critical section using FAA Shared: int s, turn; Initially: s = 0; turn=0; Process P i code: Entry: me = FAA(s,1); while(turn < me); // busy wait for my turn Critical section Exit: FAA(turn,1); Check yourself! Is mutual exclusion satisfied? Is progress satisfied? Is fairness satisfied?
12
OS Winter’03 Discussion Satisfies all three properties Supports unbounded number of processes Unbounded counter Busy waiting
13
OS Winter’03 Problems with studied synchronization methods Critical section framework is inconvenient for programming Performance penalty Busy waiting Too coarse synchronization Using hardware primitives directly results in non-portable code
14
OS Winter’03 Higher Level Abstractions Higher level software abstractions are represented by Semaphores Monitors
15
OS Winter’03 Semaphores Invented by Edsger Dijkstra in 1968 Interface consists of two primitives: P() and V()
16
OS Winter’03 Notes on the Language Dutch: P: Proberen, V: Verhogen Hebrew: P: פחות, V: ועוד English: P(): wait(), V(): signal()
17
OS Winter’03 Semaphores: initial value Initial value of a semaphore indicates how many identical instances of the critical resource exist A semaphore initialized to 1 is called a mutex (mutual exclusion)
18
OS Winter’03 Programming with semaphores Semaphores is a powerful programming abstraction Define a semaphore for each critical resource E.g., one for each linked list Granularity? Concurrent processes access appropriate semaphores when synchronization is needed
19
OS Winter’03 Some examples … V(synch); … P(synch); …
20
OS Winter’03 Some examples Do { P(mutex); critical section V(mutex); Remainder section; While(1);
21
OS Winter’03 Implementing semaphores Semaphores can be implemented efficiently by the system P() is explicitly telling the system: “ Hey, I cannot proceed, you can preempt me ” V() instructs the system to wake up a waiting process
22
OS Winter’03 Implementing Semaphores type semaphore = record count: integer; queue: list of process end; var S: semaphore; S.count must be initialized to a nonnegative value (depending on application)
23
OS Winter’03 Implementing Semaphores P(S): S.count--; if (S.count<0) { add this process to S.queue block this process; } V(S): S.count++; if (S.count <= 0) { remove a process P from S.queue place this process P on ready queue }
24
OS Winter’03 We ’ re still cheating … P() and V() must be executed atomically In uniprocessor system may disable interrupts In multi-processor system, use hardware synchronization primitives TS, FAA, etc … Involves a some limited amount of busy waiting
25
OS Winter’03 Monitors monitor monitor-name { shared variable declarations procedure P1(…) { … } … procedure Pn() { … } Only a single process at a time can be active within the monitor => other processes calling Pi() are queued Conditional variables for finer grained synchronization x.wait() suspend the execution until another process calls x.signal()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.