版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Bring
forks
today!Outline
for
Today1Objective:To
continue
talking
about
the
critical
section
proband
get
more
practice
thinking
about
possibleinterleavings.Start
talking
about
synchronization
primitives.Introduce
other
“classic”
concurrency
problemsAdministrative
details:Look
on
the
web
for
TAs’
office
hours
or
checknewsgroup
for
UTAs’
office
hours
for
assignment
1.On
collecting
problem
sets…SemaphoresWell-known
synchronization
abstraction
Defined
as
a
non-negative
integer
with
twoatomic
operationsP(s)
-
[wait
until
s
>
0;
s--]
Reminder:
notation
[
]
=
atomicV(s)
-
[s++] The
atomicity
and
the
waiting
can
beimplemented
by
either
busywaiting
orblocking
solutions.11Semaphore
Usage
Binary
semaphores
can
provide
mutualexclusion
(solution
of
critical
section
problem
Counting
semaphores
can
represent
aresource
with
multiple
instances
(e.g.
solvproducer/consumer
problem)Signaling
events(persistent
events
that
stayrelevant
even
if
nobody
listening
right
now)while
(1){
...other
stuff...critical
section}The
Critical
Section
ProblemP(mutex)1V(mutex)Semaphore:mutex
initially
1Fill
in
the
boxesProducer
/
ConsumerProducer:while(whatever){}Consumer:while(whatever)locally
generate
item
{fill
empty
buffer
with
itemget
item
from
full
bufferuse
item}1Producer
/
ConsumerProducer:while(whatever){}Consumer:while(whatever)get
item
from
full
bufferlocally
generate
item
{fill
empty
buffer
with
itemP(emptybuf);V(emptybuf);use
item1V(fullbuf);}Semaphores:
emptybuf
initially
N;
fullbuf
initiP(fullbuf);1Tweedledum
and
Tweedledee
Separate
threads
executing
their
respectiveprocedures.
The
idea
to
cause
them
toforever
take
turns
exchanging
insults
throughthe
shared
variable
X
in
strict
alternation.
The
Sleep()
andWakeup()routines
operateas
follows:Sleep
blocks
the
calling
thread,Wakeup
unblocks
a
specific
thread
if
that
threadis
blocked,
otherwise
its
behavior
is
unpredictable1The
code
shown
above
exhibits
a
well-knownsynchronization
flaw.
Outline
a
scenario
in
which
thiscode
would
fail,
and
the
outcome
of
that
scenariovoid
Tweedledum()void
Tweedledee(){{while(1)
{while(1)
{Sleep();x
=
Quarrel(x);x
=
Quarrel(x);Wakeup(Tweedledum);Wakeup(Tweedledee);Sleep();}}}}If
dee
goes
first
to
sleep,
the
wakeup
is
lost
(since
dumsleeping
yet).
Both
sleep
forever.Show
how
to
fix
the
problem
by
replacing
the
Sleep
andWakeup
calls
with
semaphore
P
(down)
and
V
(up)operations.void
Tweedledum(){while(1)
{Sleep();x
=
Quarrel(x);Wakeup(Tweedledee);}}void
Tweedledee(){while(1)
{x
=
Quarrel(x);Wakeup(Tweedledum);Sleep();}}P(dum);V(dee);semaphore
dee
=
0;semaphore
dum
=
0;V(dum);P(dee):1Monitor
Abstraction
Associated
ConditionVariables
withoperations
of
Wait
andSignal.enQ
deQ
Encapsulates
shareddata
and
operationswith
mutual
exclusiveentry
queuemonitor_lock
notEmptyuse
of
the
object
(anassociated
lock).initshared
dataconditions11Condition
Variables
We
build
the
monitor
abstraction
out
of
a
lock(for
the
mutual
exclusion)
and
a
set
ofassociated
condition
variables.
Wait
on
condition:
releases
lock
held
bycaller,
caller
goes
to
sleep
on
condition’squeue.When
awakened,
it
must
reacquire
lock.
Signal
condition:
wakes
up
one
waitingthread.
Broadcast:
wakes
up
all
threads
waiting
onthis
condition.1Nachos-style
Synchronizationsynch.h,
ccSemaphoresSemaphore::PSemaphore::VLocks
and
condition
variablesLock::AcquireLock::ReleaseCondition::Wait
(conditionLock)Condition::Signal
(conditionLock)Condition::Broadcast
(conditionLock)Monitor
AbstractionenQ
deQinitshared
dataEnQ:{Lock->Acquire
(
);if
(head
==
null){head
=
item;notEmpty->Signal(Lock)e;n}try
queuemonitor_lock
notEmptyelse
tail->next
=
item;tail
=
item;Lock->Release(
);}conditionsdeQ:{Lock->Acquire
(lock);if
(head
==
null)notEmpty->Wait
(lock);item
=
head;if
(tail
==
head)
tail
=
null;head=item->next;Lock->Release(
);}1Monitor
AbstractionenQ
deQinitshared
dataentry
queuemonitor_locknotEmptyconditionsEnQ:{Lock->Acquire
(
);if
(head
==
null){head
=
item;notEmpty->Signal
(Lock);}1else
tail->next
=
item;tail
=
item;Lock->Release(
);}deQ:{Lock->Acquire
(lock);if
(head
==
null)notEmpty->Wait
(lock);item
=
head;if
(tail
==
head)
tail
=
null;head=item->next;Lock->Release(
);}deQ:{Lock->Acquire
(lock);–
if
(head
==
null)notEmpty->Wait
(lock);–
item
=
head;Monitor
AbstractionenQ
deQinitshared
dataEnQ:{Lock->Acquire
(
);–
if
(head
==
null){head
=
item;notEmpty->Signal
(Lock);}else
tail->next
=
item;
entry
queuemonitor_locktail
=
item;Lock->Release(
);}notEmpty––––if
(tail
==
head)
tail
=
null;head=item->next;Lock->Release(
);}conditions1else
tail->next
=
item;tail
=
item;Lock->Release(
);}deQ:{Lock->Acquire
(lock);if
(head
==
null)notEmpty->Wait
(lock);–
item
=
head;EnQ:{Lock->Acquire
(
);–
if
(head
==
null){head
=
item;notEmpty->Signal(Lock)e;n}try
queuemonitor_lock
notEmptyMonitor
AbstractionenQ
deQinitshared
data––––if
(tail
==
head)
tail
=
null;head=item->next;Lock->Release(
);}conditions1else
tail->next
=
item;tail
=
item;Lock->Release(
);}deQ:{Lock->Acquire
(lock);if
(head
==
null)notEmpty->Wait
(lock);–
item
=
head;EnQ:{Lock->Acquire
(
);–
if
(head
==
null){head
=
item;notEmpty->Signal(Lock)e;n}try
queuemonitor_lock
notEmptyMonitor
AbstractionenQ
deQinitshared
data––––if
(tail
==
head)
tail
=
null;head=item->next;Lock->Release(
);}conditions1else
tail->next
=
item;tail
=
item;Lock->Release(
);}deQ:{Lock->Acquire
(lock);if
(head
==
null)notEmpty->Wait
(lock);–
item
=
head;EnQ:{Lock->Acquire
(
);–
if
(head
==
null){head
=
item;notEmpty->Signal(Lock)e;n}try
queuemonitor_lock
notEmptyMonitor
AbstractionenQ
deQinitshared
data––––if
(tail
==
head)
tail
=
null;head=item->next;Lock->Release(
);}conditions11Classic
ProblemsThere
are
a
number
of
“classic”
problemsthat
represent
a
class
of
synchronizationsituations√Critical
Section
problem√Producer/Consumer
problemReader/Writer
problem5
Dining
Philosophers5
Dining
PhilosophersPhilosopher
0Philosopher
1PhilosopherPhilosopher
3Philosopher
4while(food
available){pick
up
2
adj.
forks;eat;put
down
forks;think
awhile;}21Template
for
Philosopherwhile
(food
available){forks*//*pick
upeat;/*put
down
forks*/think
awhile;}1Naive
Solution/*pick
up/*put
down
forks*/}while
(food
available){forPk(sf*o/rk[left(me)]);P(fork[right(me)]);eat;V(fork[left(me)]);V(fork[right(me)]);think
awhile;1Does
this
work?Simplest
Example
of
DeadlockThread
0InterleavingThread
1P(R1)P(R1)P(R2)P(R2)P(R2)P(R1)V(R1)P(R1)
waitsV(R2)V(R2)P(R2)
waitsV(R1)R1
and
R2
initially
1
(binary
semaphore)11Conditions
for
DeadlockMutually
exclusive
use
of
resourcesBinary
semaphores
R1
and
R2Circular
waiting
Thread
0
waits
for
Thread
1
to
V(R2)
andThread
1
waits
for
Thread
0
to
V(R1)Hold
and
waitHolding
either
R1
or
R2
while
waiting
on
otherNo
pre-emption
Neither
R1
nor
R2
are
removed
from
their
respective
holdingThreads.1Philosophy
101(or
why
5DP
is
interesting)
How
to
eat
with
your
Fellows
withoutcausing
Deadlock.Circular
arguments
(the
circular
wait
conditionNot
giving
up
on
firmly
held
things
(nopreemption)Infinite
patience
with
Half-baked
schemes (hold
some
&
wait
for
more)
Why
Starvation
exists
and
what
we
cando
about
it.1Dealing
with
DeadlockIt
can
be
prevented
by
breaking
one
ofthe
prerequisite
conditions:Mutually
exclusive
use
of
resou
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026数字人民币运营管理中心有限公司招聘考试备考题库及答案解析
- 2026四川成都市市场监督管理局所属事业单位考试招聘20人考试备考试题及答案解析
- 2026年会展维护软件开发协议
- 2026中智西安经济技术合作有限公司招聘60人考试模拟试题及答案解析
- 2026年环保培训物业服务协议
- 2026年嘉峪关市金川区卫生健康系统人员招聘笔试备考试题及答案解析
- 2026湖南长沙浏阳市招聘事业单位工作人员66人考试备考试题及答案解析
- 乌当区2025贵州贵阳市乌当区农业农村局招聘驻嘉旺屠宰场动物检疫协检人员笔试历年参考题库典型考点附带答案详解
- 上海市2025上海市闵行区吴泾镇储备人才招录笔试历年参考题库典型考点附带答案详解
- 上海上海电机学院2025年招聘11人(第三批)笔试历年备考题库附带答案详解
- 2026年喀什地区“才聚喀什·智惠丝路”春季招才引智(824人)考试模拟试题及答案解析
- 2026教科版(新教材)小学科学三年级下册期中复习检测试卷及答案(共三套)
- AAV血友病基因治疗应用
- 超市消防安全培训材料课件
- 2026年考研数学一真题
- 2025年生理知识竞赛复习题库及答案(共100题)
- 电梯使用单位电梯安全总监和安全员考试题库及答案
- 学习习近平总书记五四重要回信精神
- 软件性能测试指南
- 心脏骤停后恢复过程护理查房
- 生成式AI与高中英语写作教学的有效融合
评论
0/150
提交评论