四川丹甫制冷压缩机股份有限公司招PPT课件_第1页
四川丹甫制冷压缩机股份有限公司招PPT课件_第2页
四川丹甫制冷压缩机股份有限公司招PPT课件_第3页
四川丹甫制冷压缩机股份有限公司招PPT课件_第4页
四川丹甫制冷压缩机股份有限公司招PPT课件_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Semaphores Well-known synchronization abstraction Defined as a non-negative integer with two atomic operationsP(s) - wait until s 0; s-V(s) - s+ The atomicity and the waiting can be implemented by either busywaiting or blocking solutions.Reminder: notation = atomic第1页/共29页Semaphore Usage Binary sema

2、phores can provide mutual exclusion (solution of critical section problem) Counting semaphores can represent a resource with multiple instances (e.g. solving producer/consumer problem) Signaling events (persistent events that stay relevant even if nobody listening right now)第2页/共29页while (1) .other

3、stuff.critical sectionThe Critical Section ProblemP(mutex)V(mutex)Semaphore:mutex initially 1Fill in the boxes第3页/共29页Producer / ConsumerProducer:while(whatever) locally generate itemfill empty buffer with itemConsumer:while(whatever)get item from full bufferuse item第4页/共29页Producer / ConsumerProduc

4、er:while(whatever) locally generate itemfill empty buffer with itemConsumer:while(whatever)get item from full bufferuse itemP(emptybuf);V(fullbuf);P(fullbuf);V(emptybuf);Semaphores: emptybuf initially N; fullbuf initially 0;第5页/共29页Tweedledum and Tweedledee Separate threads executing their respectiv

5、e procedures. The idea to cause them to forever take turns exchanging insults through the shared variable X in strict alternation. The Sleep() and Wakeup() routines operate as follows: Sleep blocks the calling thread, Wakeup unblocks a specific thread if that thread is blocked, otherwise its behavio

6、r is unpredictable第6页/共29页The code shown above exhibits a well-known synchronization flaw. Outline a scenario in which this code would fail, and the outcome of that scenariovoid Tweedledum() while(1) Sleep(); x = Quarrel(x); Wakeup(Tweedledee); void Tweedledee() while(1) x = Quarrel(x); Wakeup(Tweed

7、ledum); Sleep(); If dee goes first to sleep, the wakeup is lost (since dum isntsleeping yet). Both sleep forever.第7页/共29页Show how to fix the problem by replacing the Sleep and Wakeup calls with semaphore P (down) and V (up) operations. void Tweedledum() while(1) Sleep(); x = Quarrel(x); Wakeup(Tweed

8、ledee); void Tweedledee() while(1) x = Quarrel(x); Wakeup(Tweedledum); Sleep(); P(dum);V(dee);semaphore dee = 0;semaphore dum = 0;V(dum);P(dee):第8页/共29页Monitor Abstraction Encapsulates shared data and operations with mutual exclusive use of the object (an associated lock). Associated Condition Varia

9、bles with operations of Wait and Signal.monitor_lockenQdeQinitshared dataentry queuenotEmptyconditions第9页/共29页Condition Variables We build the monitor abstraction out of a lock (for the mutual exclusion) and a set of associated condition variables.Wait on condition: releases lock held by caller, cal

10、ler goes to sleep on conditions queue. When awakened, it must reacquire lock.Signal condition: wakes up one waiting thread. Broadcast: wakes up all threads waiting on this condition.第10页/共29页Nachos-style Synchronizationsynch.h, cc SemaphoresSemaphore:PSemaphore:V Locks and condition variablesLock:Ac

11、quireLock:ReleaseCondition:Wait (conditionLock)Condition:Signal (conditionLock)Condition:Broadcast (conditionLock)第11页/共29页Monitor Abstractionmonitor_lockenQdeQinitshared dataentry queuenotEmptyconditionsEnQ:Lock-Acquire ( );if (head = null)head = item;notEmpty-Signal (Lock);else tail-next = item;ta

12、il = 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( ); 第12页/共29页Monitor Abstractionmonitor_lockenQdeQinitshared dataentry queuenotEmptyconditionsEnQ:Lock-Acquire ( );if (head = null)head = it

13、em;notEmpty-Signal (Lock);else 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( ); 第13页/共29页EnQ:Lock-Acquire ( );if (head = null)head = item;notEmpty-Signal (Lock);else

14、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( ); Monitor Abstractionmonitor_lockenQdeQinitshared dataentry queuenotEmptyconditions第14页/共29页EnQ:Lock-Acquire ( );if (he

15、ad = null)head = item;notEmpty-Signal (Lock);else 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( ); Monitor Abstractionmonitor_lockenQdeQinitshared dataentry queuenotE

16、mptyconditions第15页/共29页EnQ:Lock-Acquire ( );if (head = null)head = item;notEmpty-Signal (Lock);else 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( ); Monitor Abstracti

17、onmonitor_lockenQdeQinitshared dataentry queuenotEmptyconditions第16页/共29页EnQ:Lock-Acquire ( );if (head = null)head = item;notEmpty-Signal (Lock);else tail-next = item;tail = item; Lock-Release( );deQ:Lock-Acquire (lock);if (head = null)notEmpty-Wait (lock); item = head; if (tail = head) tail = null;

18、 head=item-next;Lock-Release( ); Monitor Abstractionmonitor_lockenQdeQinitshared dataentry queuenotEmptyconditions第17页/共29页Classic ProblemsThere are a number of “classic” problems that represent a class of synchronization situations Critical Section problem Producer/Consumer problem Reader/Writer pr

19、oblem 5 Dining Philosophers第18页/共29页5 Dining PhilosophersPhilosopher 0Philosopher 1Philosopher 2Philosopher 3Philosopher 4while(food available)pick up 2 adj. forks; eat; put down forks; think awhile;第19页/共29页Template for Philosopherwhile (food available) /*pick up forks*/eat; /*put down forks*/think

20、 awhile;第20页/共29页Naive Solutionwhile (food available) /*pick up forks*/eat; /*put down forks*/think awhile;P(forkleft(me);P(forkright(me);V(forkleft(me);V(forkright(me);Does this work?第21页/共29页Simplest Example of DeadlockThread 0P(R1)P(R2)V(R1)V(R2)Thread 1P(R2)P(R1)V(R2)V(R1)InterleavingP(R1)P(R2)P

21、(R1) waitsP(R2) waitsR1 and R2 initially 1 (binary semaphore)第22页/共29页Conditions for Deadlock Mutually exclusive use of resources Binary semaphores R1 and R2 Circular waiting Thread 0 waits for Thread 1 to V(R2) and Thread 1 waits for Thread 0 to V(R1) Hold and wait Holding either R1 or R2 while wai

22、ting on other No pre-emption Neither R1 nor R2 are removed from their respective holding Threads.第23页/共29页Philosophy 101(or why 5DP is interesting) How to eat with your Fellows without causing Deadlock. Circular arguments (the circular wait condition) Not giving up on firmly held things (no preempti

23、on) Infinite patience with Half-baked schemes (hold some & wait for more) Why Starvation exists and what we can do about it.第24页/共29页Dealing with DeadlockIt can be prevented by breaking one of the prerequisite conditions: Mutually exclusive use of resources Example: Allowing shared access to rea

24、d-only files (readers/writers problem) circular waiting Example: Define an ordering on resources and acquire them in order hold and wait no pre-emption第25页/共29页while (food available) if (me 0) P(forkleft(me); P(forkright(me); else (P(forkright(me); P(forkleft(me); eat; V(forkleft(me); V(forkright(me

25、); think awhile;Circular Wait Condition第26页/共29页Hold and Wait Conditionwhile (food available)P(mutex); while (forks me != 2) blockingme = true; V(mutex); P(sleepyme); P(mutex);forks leftneighbor(me) -; forks rightneighbor(me)-;V(mutex):eat;P(mutex); forks leftneighbor(me) +; forks rightneighbor(me)+

26、;if (blockingleftneighbor(me) blocking leftneighbor(me) = false; V(sleepyleftneighbor(me); if (blockingrightneighbor(me) blockingrightneighbor(me) = false; V(sleepyrightneighbor(me); V(mutex); think awhile; 第27页/共29页$rZoWkThQeMbJ7G4D1z-w*t!qYmVjSgOdLaI6F3B0y)v%s#pXlUiQfNcK8H5E2A+x*u$rZnWkThPeMaJ7G4C

27、1z-w&t!pYmVjRgOdL9I6E3B0y(v%s#oXlUiQfNbK8H5D2A+x*u$qZnWkShPeMaJ7F4C1z)w&t!pYmUjRgOcL9I6E3B+y(v%r#oXlTiQeNbK8G5D2A-x*t$qZnVkShPdMaJ7F4C0z)w&s!pYmUjRfOcL9H6E3B+y(u%r#oWlTiQeNbJ8G5D1A-x*t$qYnVkSgPdMaI7F3C0z)v&s!pXmUiRfOcK9H6E2B+y(u%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x*u$rZnWkShPeMaJ7G4C1z)w&t!pYmVjRgOcL9I6E3B0y(v%s#oXlTiQfNbK8H5D2A-x*u$qZnWkShPdMaJ7F4C1z)w&s!pYmUjRgOcL9H6E3B+y(v%r#oWlTiQeNbK8G5D1A-x*t$qZ

39、nVkShPdMaI7F4C0z)w&s!pXmUjRfOcL9H6E2B+y(u%r#oWlThQeNbJ8G5D1A-w*t$qYnVkSgPdLaI7F3C0z)v&s#pXmUiRfOcK9H5E2B+x(u%rZoWlThQeMbJ8G4D1A-w*t!qYnVjSgPdLaI6F3C0y)v&s#pXlUiRfNcK9H5E2A+x(u$rZoWkThPeMbJ7G4D1z-w&t!mUiRfOcK9H6E2B+x(u%rZoWlThQeMbJ8G4D1A-w*t!qYnVjSgPdLaI6F3C0y)v&s#pXlUiRfNcK9H5E2A+x(u$rZoWkThQeMbJ7G

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论