22
CHAPTER 5 1 Fall 2010 - DE5 Concurrency Concurrency Dr. D. M. Akbar Hussain Department of Electronic Systems DE5 1 Concurrency Concurrency Execution of multiple processes . xecution of multiple processes . Multi-programming: Management of multiple processes within a multiple processes within a uni uni- processor system processor system, every system has this support, whether big, small or complex. Multi-processing: Management of multiple processes within a multi-processor system, servers and works stations Dr. D. M. Akbar Hussain Department of Electronic Systems DE5 2 stations. (Shared Memory) Shared Memory) Distributed Processing: Management of multiple processes executing on number of distributed computer systems, for example clusters. (Do not Share Memory) Do not Share Memory)

Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

1Fall 2010 - DE5

ConcurrencyConcurrency

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 1

ConcurrencyConcurrency

EExecution of multiple processes .xecution of multiple processes .

Multi-programming:Management of multiple processes within a multiple processes within a uniuni--

processor systemprocessor system, every system has this support, whether big, small or complex.

Multi-processing:Management of multiple processes within a multi-processor system, servers and works

stations

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 2

stations. ((Shared Memory)Shared Memory)

Distributed Processing:Management of multiple processes executing on

number of distributed computer systems, for example clusters.

((Do not Share Memory)Do not Share Memory)

Page 2: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

2Fall 2010 - DE5

Interleaving:

Concurrency PrincipleConcurrency Principle

Interleaving:

1. In a single processor case multiple processes areinterleaved in time to provide the illusionillusion ofofsimultaneoussimultaneous executionexecution of these processes.

2. Although, it is not really parallel processing but there arebenefitsbenefits usingusing suchsuch techniquetechnique apart from having

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 3

benefitsbenefits usingusing suchsuch techniquetechnique, apart from havingoverheads involved in switching of these processes.

Concurrency PrinciplesConcurrency Principles

Issues:

1. Basically, it is not possible to predict the speed predict the speed of execution of processes.

2. Optimal allocation Optimal allocation of resources is not possible by the o/s.3. Sharing of global variablesglobal variables.

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 4

Page 3: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

3Fall 2010 - DE5

Pseudo Code for Demonstration:S h t li ti di i t f th

Concurrency PrinciplesConcurrency Principles

Suppose we have two or more applications reading input from the keyboard and putting the result on the screen. It would make sense to have the same procedure for all these applications and obviously it will be loaded into the global address space.

Pseudo Code:function_test ( string charact){

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 5

{readin (input_variable, keyboard);charact = input_variable;readout (charact, display);exit;}

Where is the problem

P1

Process P1 has called function_testand have just read (X) (X) the input and j ( )( ) p

being interrupted,

so so charactcharact = X;= X;

P2

Process P2 has called function_test and allowed to run till end with input “Y”“Y”,

e.g., e.g., charactcharact = Y.= Y.function_test ( string charact){readin (input_variable, keyboard);h t i t i bl

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 6

P1When P1 resume it will display

what ?

charact = input_variable;readout (charact, display);exit;}

Page 4: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

4Fall 2010 - DE5

How to Achieve Mutual ExclusionHow to Achieve Mutual Exclusion

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 7

Basics of Mutual ExclusionBasics of Mutual Exclusion

1. Only oneone processprocess is allowed into its critical section.

2 A process which may stop execution in non critical section mustmust notnot2. A process which may stop execution in non critical section mustmust notnotaffect/interfereaffect/interfere withwith othersothers..

3. A process should notnot bebe delayeddelayed indefinitelyindefinitely requiring access to criticalsection.

4. Any process mustmust bebe allowedallowed toto enterenter intointo criticalcritical sectionsection withoutwithout delaydelay, ifno process is in critical section.

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 8

5. Noo assumptionsassumptions aboutabout speedspeed oror numbersnumbers of CPUs to be considered.

6. A process should remain in the critical section forfor aa finitefinite timetime onlyonly..

Page 5: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

5Fall 2010 - DE5

Operating System ConcernsOperating System Concerns

What are the issues o/s has to consider for concurrency:

1 O/S t k t k k t k f ll th ( ll it i d th h PCB’ )1. O/S must keep track keep track of all the processes (normally it is done through PCB’s).

2. O/S must allocate and deallocate and de--allocate allocate the resources for the processes. Which includes processor time, memory, files and i/o devicesprocessor time, memory, files and i/o devices.

3.3. Protection of resources Protection of resources against unintended interference from other processes.

4 Result of a process must be independent of the speed independent of the speed at which the

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 9

4. Result of a process must be independent of the speed independent of the speed at which the execution is taking place relative to the speed of other concurrent processes.

Operating System ConcernsOperating System Concerns

Degree of Awareness Relationship Influence on others ProblemsUnaware of others Competition 1. Result is independent of others

2. Timing may be affected1. Mutual Exclusion2. Deadlock3. Starvation

Indirectly aware of others Cooperation by sharing 1. Result is not independent of others2. Timing may be affected

1. Mutual Exclusion2. Deadlock3. Starvation4. Data Coherence

Directly aware of others Cooperation by communication 1. Result is not independent of others2. Timing may be affected

1. Deadlock2. Starvation

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 10

Page 6: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

6Fall 2010 - DE5

Mutual Exclusion has cost ?Mutual Exclusion has cost ?

Mutual ExclusionMutual Exclusion

DeadlockDeadlock StarvationStarvation

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 11

DeadlockDeadlock

Suppose two processes P1 P1 and P2 P2 are to accomplish a task by using two resources R1 R1 and R2R2As shown here, What happened next ?

P1P1 P2P2

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 12

R1R1 R2R2Waiting (for ever)Waiting (for ever)

Page 7: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

7Fall 2010 - DE5

P1P1 P3P3P2P2

P2 is StarvedP2 is Starved

StarvationStarvation

RR

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 13

P1P1 P3P3 P1P1 P3P3 P1P1 P3 ……………………………….P3 ……………………………….

In addition to deadlock and starvation another requirement another requirement may also be necessary, for example, suppose an application needs that a must condition should always be in placed say: a = b. a = b. In other words any process updating aa must update b b or vice versa,

Data CoherencyData Coherency

say for example a = b = 20;say for example a = b = 20;P1: P1: a = a a = a -- 10;10;

b = b b = b -- 10;10;P2:P2: b = b * 2;b = b * 2;

a = a * 2;a = a * 2;

Which is fine if the consistency is achieved, but consider the following situation and both processes impose mutual exclusion;

11 1010

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 14

P1: P1: a = a a = a -- 10;10;P2:P2: b = b * 2;b = b * 2;P1:P1: b = b b = b -- 10;10;P2:P2: a = a * 2;a = a * 2;

Which clearly demonstrate that a = b does not hold any more.

Page 8: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

8Fall 2010 - DE5

DekkersDekkers Algorithm for MEAlgorithm for ME

Process 0 Process 1

Critical Section

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 15

Critical Section

Turn 0/1

DekkersDekkers algorithmalgorithm

IgloIglo ((eskimoiskeskimoisk snehyttesnehytte))

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 16

Page 9: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

9Fall 2010 - DE5

There is a strict alternation ruleThere is a strict alternation rule

DekkersDekkers algorithmalgorithm

There is a strict alternation rule.There is a strict alternation rule.

ME is guaranteedME is guaranteed

ConsequencesConsequences::

1.1. Execution is dictated by the slowest of the two.Execution is dictated by the slowest of the two.

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 17

yy

2.2. If one is lost the other is blocked for ever.If one is lost the other is blocked for ever.

Modification 1Modification 1

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 18

Page 10: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

10Fall 2010 - DE5

Modification 1Modification 1

Dictated by the slowestslowest ofof thethe twotwo..

NoNo guaranteeguarantee ofof MutualMutual ExclusionExclusion:: Because both can setthere flags at the same time to truetrue after one processreached at the end and setting others flag to false, so at thatpoint both flags becomes false so now there is a chance to setboth flags to true (meaning both can enter in their critical

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 19

g ( gsection).

Modification 2Modification 2

Own Flag is set outside the while loop & then checking others FlagOwn Flag is set outside the while loop & then checking others Flag

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 20

Dictated by the slowest of the two.Dictated by the slowest of the two.Guarantee Mutual Exclusion.Guarantee Mutual Exclusion.Deadlock,Deadlock, if both has set the Flag to True at the same instantif both has set the Flag to True at the same instant

Page 11: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

11Fall 2010 - DE5

Modification 3Modification 3Own Flag is set outside the while loop, then checking others Flag & Own Flag is set outside the while loop, then checking others Flag &

resetting resetting its own flag & its own flag & introducing Delayintroducing Delay

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 21

Dictated by the slowest of the twoDictated by the slowest of the twoGuarantee Mutual ExclusionGuarantee Mutual ExclusionDeadlock ?Deadlock ?Almost thereAlmost there

Correct SolutionCorrect Solution

AsAs wewe havehave observedobserved thatthat knowingknowing thethe statestate ofof otherotherprocessesprocesses isis notnot enoughenough toto makemake ourour problemproblemworkable,workable, basicallybasically oneone needsneeds anan orderorder thethe waywayprocessesprocesses proceedsproceeds..

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 22

Page 12: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

12Fall 2010 - DE5

Correct SolutionCorrect Solution

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 23

Correct SolutionCorrect Solution

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 24

Dictated by the slowest of the two.Dictated by the slowest of the two.Guarantee Mutual Exclusion.Guarantee Mutual Exclusion.No Deadlock.No Deadlock.

Page 13: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

13Fall 2010 - DE5

Peterson’s solution is similar but more elegant as the text says:

Peterson’s SolutionPeterson’s Solution

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 25

Mutual exclusion can also be implemented in Hardware by using eithereitherinterruptinterrupt handlinghandling oror throughthrough specialspecial machinemachine instructionsinstructions::

Hardware Mutual ExclusionHardware Mutual Exclusion

interruptinterrupt handlinghandling oror throughthrough specialspecial machinemachine instructionsinstructions::

InterruptInterrupt DisablingDisabling::Just before entering the criticalcritical region,region, forcing interrupt disabling willensure ME:

Potentially system performance is degradeddegraded..NoNo supportsupport forfor multimulti--processorprocessor system architecture.

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 26

Page 14: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

14Fall 2010 - DE5

Machine InstructionMachine Instruction

1. Basically twotwo actionsactions areare performedperformed inin oneone cyclecycle forthese type of instruction atomically, for example, readingreadingandand writingwriting.

2. Therefore, one can use them as they cannotcannot bebe interruptedinterruptedduring these actions.

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 27

Machine InstructionMachine Instruction

Test and Set InstructionTest and Set Instruction

Boolean testsettestset (int a){

if (a == 0) a = 1; return true;else return false;

}

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 28

Page 15: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

15Fall 2010 - DE5

Exchange InstructionExchange Instruction

Machine InstructionMachine Instruction

Exchanges Exchanges the contents of a register with a memory location, obviously during this, access to the memory location is blocked.access to the memory location is blocked.

void exchangeexchange (int register, memory){ int temp;

temp = memory; memory = register;

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 29

register = temp;}

n n number of processesbolt bolt Shared variable (int)Function_MEFunction_ME (process)(process){while true

TestsetTestset for MEfor ME

while true {while { (!testset (boltbolt))

be happy and do nothingbe happy and do nothing}

Enter Critical SectionEnter Critical Sectionset bolt to 0;set bolt to 0;do remaining part}

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 30

}}

InitiallyInitially boltbolt isis setset toto 00,, anyany processprocess findingfinding itit zerozero willwill enterenter intointo thethe criticalcriticalsection,section, obviouslyobviously allall othersothers willwill gogo intointo ““bebe happyhappy looploop””.. OnceOnce thatthat processprocessleavesleaves itsits criticalcritical sectionsection anyany otherother processprocess findingfinding boltbolt zerozero willwillimmediatelyimmediately gogo intointo itsits criticalcritical sectionsection andand soso onon..

Page 16: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

16Fall 2010 - DE5

n n number of processesboltbolt Shared variable (int)Function_MEFunction_ME (process)(process){keykey local variable (int)

Exchange for MEExchange for ME

while true {key = 1;while { (keykey != 0)

exchangeexchange (key, boltkey, bolt); ”be happy until you get bolt zerobe happy until you get bolt zero”}

Enter Critical SectionEnter Critical Sectionexchange (key, bolt);exchange (key, bolt);do remaining part

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 31

g p}

}

InitiallyInitially boltbolt isis setset toto 00,, anyany processprocess findingfinding itit zerozero willwill enterenter intointo thethe criticalcritical section,section,butbut thisthis timetime itit willwill bebe throughthrough thethe locallocal variablevariable “key”“key” andand thethe boltbolt exchangeexchangemechanismmechanism ofof thethe exchangeexchange instructioninstruction.. AfterAfter completingcompleting itsits job,job, boltbolt isis againagain setset toto 00throughthrough thethe exchangeexchange instructioninstruction..

Plus/Minus of Machine Plus/Minus of Machine InstructionInstruction

1.1. Easy to implementEasy to implement2.2. Support for any number of processorsSupport for any number of processors3.3. Multiple Critical section support (By using different variables)Multiple Critical section support (By using different variables)

1.1. Busy waiting (performance degradation)Busy waiting (performance degradation)2.2. Starvation is possibleStarvation is possible33 Deadlock is possibleDeadlock is possible

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 32

3.3. Deadlock is possibleDeadlock is possible

Page 17: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

17Fall 2010 - DE5

Design of an o/s as sequential processes with a reliable mechanism of support

SemaphoresSemaphores

g q p ppfor cooperation.Dijkstra proposed a solutionsolution basedbased onon thethe fundamentalfundamental principleprinciple ofofcooperationcooperation basedbased onon signalssignals, so that a process can be forcedforced toto stopstop at arequired place and later starts when instructed through a signal.

These signalssignals areare calledcalled semaphoressemaphores, to transmit a signal the processexecutes signal (s) and to receive a signal it executes wait (s). Therefore, ifthe corresponding signals are not transmitted the process is suspended till

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 33

the corresponding signals are not transmitted the process is suspended tillthe transmission takes place.

It is initialized to a non negative valuenon negative value

Properties of SemaphoresProperties of Semaphores

It is initialized to a non negative value.non negative value.

Wait operation decrements semaphore valueWait operation decrements semaphore value, if it goes negative the corresponding process is blocked.blocked.

Signal operation Signal operation increments semaphore value, if the value is not positiveif the value is not positive, process can be unblocked unblocked from the suspended list.

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 34

Signal and Wait are atomicatomic..

Page 18: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

18Fall 2010 - DE5

Semaphore could be declared as a simple structure having a variable declaration, may be an integer for count and another variable/structure for a process queue.

it ( ) i l ( )

Semaphore Semaphore PrimitivesPrimitives

Critical Section (if count is zero)

wait (s)count = count - 1;

if count < 0put the process in queueblock the process

signal (s)count = count + 1;if count ≤ 0

get a process from the queueunblock the process and put it in ready queue

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 35

The order of removal from the queue is not defined, FIFO etc., the only requirement is that no process should wait indefinitely in the queue.

Binary SemaphoresBinary Semaphores

waitBwaitB (s)(s)if count == 1if count == 1

count = 0;count = 0;elseelse

put the process in queueput the process in queueblock the processblock the process

signalBsignalB (s)(s)if queue is emptyif queue is empty

count = 1;count = 1;elseelse

get a process from the queueget a process from the queueput it in ready queueput it in ready queue

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 36

Page 19: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

19Fall 2010 - DE5

Process A

s = 1 C D B

Ready ListSuspended ListProcess C

s = 0 D B A

Ready ListSuspended List

Semaphore Mechanism (Strong s)Semaphore Mechanism (Strong s)

Semaphore Semaphore

Process B

s = 0 A C D

Ready ListSuspended List

Semaphore

Process D

s = -3 B A C

Ready ListSuspended List

Semaphore

Process D

s = -1 A C B

Ready ListSuspended List

S h

Process D

s = -2 C B A

Ready ListSuspended List

S h

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 37

Semaphore

Process D

s = 0 B A C

Ready ListSuspended List

Semaphore

Semaphore

The following pseudo ops can be used for the mutual exclusion problem:

There could be n processesn processes

Each process executes wait executes wait before entering to its critical section

ME Using SemaphoresME Using Semaphores

Positive value means Positive value means it can enter into critical section

If the value becomes negative, process is suspendedbecomes negative, process is suspended

Semaphore s = 1; n = “number of processes”;

Process ()

loop

it ( )it ( )

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 38

wait (s)wait (s)

critical sectioncritical section

signal (s)signal (s)

remaining executionremaining execution

Page 20: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

20Fall 2010 - DE5

1

1

00

LockLock AA BB CC

Wait (lock) Normal Execution

ME Using SemaphoresME Using Semaphores

••

0

0

0

0

0

0

B

B

C

C

Wait (lock)

Wait (lock)

signal (lock) Blocked

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 39

0

0

1

1

signal (lock)

signal (lock)

Critical Section

ProduceProduce waitB(s)waitB(s) InsetInset n = n + 1n = n + 1 If n == 1If n == 1 signalB (delay)signalB (delay) signalB (s)signalB (s)

Producer Consumer (Binary)Producer Consumer (Binary)

ProduceProduce waitB(s)waitB(s) InsetInset n n + 1n n + 1 If n 1If n 1 signalB (delay)signalB (delay) signalB (s)signalB (s)

Critical SectionCritical Section

First Time only

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 40

waitB(s)waitB(s) TakeTake n = n - 1n = n - 1 consumeconsume waitB (delay)waitB (delay)signalB (s)signalB (s)waitB(delay)waitB(delay) If n == 0If n == 0

Correct Solution ?

Page 21: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

21Fall 2010 - DE5

Producer Consumer s n Delay

1 1 0 0

2 waitB (s) 0 0 0

3 n=n+1 0 1 0

4 If n==1 then signalB (delay) 0 1 1

5 signalB (s) 1 1 1

Possible ScenarioPossible Scenario

5 signalB (s) 1 1 1

6 waitB (delay) 1 1 0

7 waitB (s) 0 1 0

8 n=n-1 0 0 0

9 signalB (s) 1 0 0

10 waitB (s) 0 0 0

11 n=n+1 0 1 0

12 If n==1 then signalB (delay) 0 1 1

13 signalB (s) 1 1 1

14 If n==0 then waitB (delay) 1 1 1

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 41

15 waitB (s) 0 1 1

16 n=n-1 0 0 1

17 signalB (s) 1 0 1

18 If n==0 then waitB (delay) 1 0 0

19 waitB (s) 0 0 0

20 n=n-1 0 -1 0

21 signalB (s) 1 -1 0

ProduceProduce waitB(s)waitB(s) InsetInset n = n + 1n = n + 1 If n = 1If n = 1 SignalB (delay)SignalB (delay) signalB (s)signalB (s)

Producer Consumer (Binary)Producer Consumer (Binary)

ProduceProduce waitB(s)waitB(s) InsetInset n n + 1n n + 1 If n 1If n 1 SignalB (delay)SignalB (delay) signalB (s)signalB (s)

Critical SectionCritical Section

First Time only

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 42

waitB(s)waitB(s) TakeTake

m = nm = n

consumeconsume WaitB (delay)WaitB (delay)signalB (s)signalB (s)waitB(delay)waitB(delay) If m = 0If m = 0

n = n - 1n = n - 1

Correct Solution

Page 22: Concurrency - Aalborg Universitethomes.et.aau.dk/akh/2010/operatingsystem-10_files/LECT-4-CLASS.pdf · CHAPTER 5 Fall 2010 - DE5 5 Operating System Concerns What are the issues o/s

CHAPTER 5

22Fall 2010 - DE5

Previously for this problem an infinite buffer was consider, now for realistic situations one has bounded buffer (BB) some thing like a circular buffer, therefore, it can be implemented in the following way:

Producer: Consumer:

Producer Consumer (Bounded Buffer)Producer Consumer (Bounded Buffer)

Loop Loop

produce if (in == out) {do nothing, remain here}

if ((in + 1)%n) == out {do nothing, remain here} take[out]

put[in] out = (out + 1)%n

in = (in + 1)%n consume

out in

Dr. D. M. Akbar Hussain

Department of Electronic SystemsDE5 43

Filled slots

[1] [2] [3] [4] [5] [6] [n]