1
CSE 513 I ntroduction to Operating Systems Class 3 - I nterprocesses Communication & Synchronization
Jonathan Walpole
- Dept. of Comp. Sci. and Eng.
CSE 513 I ntroduction to Operating Systems Class 3 - I - - PowerPoint PPT Presentation
CSE 513 I ntroduction to Operating Systems Class 3 - I nterprocesses Communication & Synchronization Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and Science University 1 Race conditions What is a race condition?
1
2
t wo or more processes have an inconsist ent view of a
values of memory locat ions replicat ed in regist ers during
cont ext swit ches at arbit rary t imes during execut ion processes can see “st ale” memory values in regist ers
prevent cont ext swit ches by prevent ing int errupt s? make processes coordinat e wit h each ot her t o ensure
3
4
5
6
What about using a binary “lock” variable in
Many computers have some limited hardware
“At omic” Test and Set Lock inst r uct ion “At omic” compar e and swap oper at ion
Solves the problem of
Expr essing int ent ion t o ent er C.S. Act ually set t ing a lock t o pr event concur r ent access
7
Test- and- set does two things atomically:
Test a lock (whose value is r et ur ned) Set t he lock
Lock obtained when the return value is FALSE I f TRUE, someone already had the lock (and
8
9
10
11
12
13
14
15
16
17
18
19
20
21
a process consumes CP
it would be bet t er t o block inst ead of busy wait (on a
sleep – allows a process t o sleep on a condit ion wakeup – allows a process t o signal anot her process
but how can t hese be implement ed?
22
OS can implement t hem by managing a dat a st ruct ure t hat
but how can t he OS access t hese dat a st ruct ures
t he OS can arrange not t o perf orm a cont ext swit ch while
but what about int errupt s? what if int errupt handlers manipulat e t he sleep and wakeup
how can t he OS synchronize access t o it s own crit ical
23
why not allow user processes t o disable int errupt s? is it ok t o disable int errupt s in t he OS? what precaut ions should you t ake?
24
25
26
27
28
29
An abstract data type that can be used f or
I nteger variable with two operations:
down (sema_var )
up(sema_var )
Both up() and down() are assumed to be atomic
made t o be at omic by OS implement at ion
30
There are multiple names f or semaphores
Down(S), wait (S), P(S) Up(S), signal(S), V(S)
Semaphore implementations
Binar y semaphor es (mut ex)
Count ing semaphor es
31
32
33
34
35
36
I mplement producer consumer solution:
37
38
Generally, the hardware has some simple
Cont r ol over int er r upt s (almost all) Special at omic inst r uct ions in I SA
Spin- Locks vs. Blocking
Spin-locks (busy wait ing)
Blocking
39
Blocking
40
User- accessible semaphores in UNI X are
each up and down oper at ion is done at omically on an
*********WORDS OF WARNI NG *********
Semaphor es ar e allocat ed by (and in) t he oper at ing
Semaphor es in UNI X ARE A SHARED RESOURCE
REMOVE your semaphor es af t er you ar e done wit h
41
42
Listing currently allocated ipc resources
Removing semaphores
43
There are a number of “classic” I PC problems
Pr oducer / Consumer synchr onizat ion The dining philosopher s pr oblem The sleeping bar ber pr oblem The r eader s and wr it er s pr oblem Count ing semaphor es out of binar y semaphor es
44
45
Why doesn’t this work?
46
47
48
I s this correct? What does it mean f or it to be correct? I s there an easier way?
49
50
if t here are people wait ing f or a hair cut bring t hem t o
else go t o sleep
if t he wait ing chairs are all f ull, t hen leave st ore. if someone is get t ing a haircut , t hen wait f or t he barber
if t he barber is sleeping, t hen wake him up and get a
51
52
Readers and writers want to access a database Multiple readers can proceed concurrently Writers must synchronize with readers and
Maximize concurrency Prevent starvation
53
54
A binary semaphore can only take on the values
Class exercise: create a counting semaphore
55