Presentation is loading. Please wait.

Presentation is loading. Please wait.

Day 14 Concurrency. Software approaches Programs must be written such that they ensure mutual exclusion. Petersons and Dekkers (Appendix B)

Similar presentations


Presentation on theme: "Day 14 Concurrency. Software approaches Programs must be written such that they ensure mutual exclusion. Petersons and Dekkers (Appendix B)"— Presentation transcript:

1 Day 14 Concurrency

2 Software approaches Programs must be written such that they ensure mutual exclusion. Petersons and Dekkers (Appendix B)

3 Operating system support – Semaphores A semaphore “s” is an integer variable that has a queue associated with it. If the queue is a FIFO queue, then the semaphore is called a strong semaphore, which prevents starvation. A process that blocks on semaphore s, is placed in its queue. A semaphore can be used to enforce mutual exclusion and to synchronize processes.

4 Semaphores Only three operations: A semaphore s can be initialized to a non-negative integer. The semwait(s) procedure or P(s) can be used by a process to wait for a signal from another process. The semsignal(s) or V(s) procedure can be used by a process to signal another (blocked) process. semwait() can block a process and semsignal() can unblock a process. semwait(s) and semsignal(s) are atomic operations.

5 General or Counting semaphore - used to synchronize processes semaphore s.count = n; semwait(s){ s.count--; if(s.count < 0){ add this process to s.queue; block process; } semsignal(s){ s.count++; if(s.count <= 0){ remove a process from s.queue; move the process to the ready queue; } }

6 s.count >= 0 - # of processes that will not block on semwait(s) and will be able to enter the CS. s.count < 0 - # of processes that are blocked on s.

7 Binary semaphore or mutex(used for mutual exclusion) Binary semaphore s = 0 or 1; semwaitB(s){ If(s.value==1){ s.value=0; } else{ place this process in s.queue; block this process; } semsignalB(s){ if(s.queue is empty) s.value=1; else{ remove a process from the s.queue; move the process to the ready queue; } }

8 Mutual exclusion with semaphores semaphore s = 1; int n = num_of_processes; void Q(int i){ while(true){ semwait(s); semsigna(s); } void main(){ parbegin(Q(1),Q(2),…,Q(n)); }

9 Implementing semaphores – using testset() s.flag = 0; semWait(s){ while(!testset(s.flag)){ } s.count--; if (s.count < 0){ place this process in the s.queue and block this process } s.flag = 0; } semsignal(s){ while(!testset(s.flag)){ } s.count++; if(s.count < 0){ remove a process P from s.queue and place the process P on ready list } s.flag = 0; } boolean testset ( i){ If(i == 0){ i = 1; return true; } else{ return false; }

10 Semaphores Is there busy-waiting? Hardware-software solution Hardware support needed for the OS to support a semaphore Must share a common variable (like “bolt” in testset instruction) Processes must share memory

11 Producer-consumer problem n – items in the buffer infinite size buffer Producer(){ while(true){ produce(); append(); n++; } Consumer(){ while(true){ if(n != 0){ take(); n--; consume(); }

12 Consumer-Producer One or many producers One or more consumers Infinite buffer – no overflow Finite buffer - overflow Empty buffer – consumer must be stopped Mutual exclusion and synchronization

13 Possible solution1 (infinite buffer) binary semaphore s = 1; int n = 0; binary semaphore delay = 0; Producer(){ while(true){ produce(); semwaitB(s); append(); n++; if(n==1) semsignalB(delay); semsignalB(s); } Consumer(){ semwaitB(delay); while(true){ semwaitB(s); take(); n--; semsignalB(s); consume(); if(n==0) semwaitB(delay); }

14 Using the code in the previous slide, complete the following table by determining the values of semaphores “s” and “delay” and the count “n” for each of the statements listed.

15 Correction on page 224, figure 5.3 Column “Consumer” If(n == 0) NOT if (n == 1)

16 Possible solution 2 binary semaphore s = 1; int n = 0; binary semaphore delay = 0; Producer(){ while(true){ produce(); semwaitB(s); append(); n++; if(n==1) semsignalB(delay); semsignalB(s); } Consumer(){ semwaitB(delay); while(true){ semwaitB(s); take(); n--; consume(); if(n==0) semwaitB(delay); semsignalB(s); }

17 Another possible solution (3) semaphore s = 1; semaphore n = 0; Producer(){ while(true){ produce(); semwait(s); append(); semsignal(s); semsignal(n); } Consumer(){ while(true){ semwait(s); semwait(n); take(); semsignal(s); consume(); }

18 A correct solution semaphore s = 1; semaphore n = 0; Producer(){ while(true){ produce(); semwait(s); append(); semsignal(s); semsignal(n); } Consumer(){ while(true){ semwait(n); semwait(s); take(); semsignal(s); consume(); }


Download ppt "Day 14 Concurrency. Software approaches Programs must be written such that they ensure mutual exclusion. Petersons and Dekkers (Appendix B)"

Similar presentations


Ads by Google