a’s device signals · 2002-09-18 · a’s device signals an interrupt b’s device signals an...

17
A’s device A’s ISR Process A Process B B’s ISR B’s device A’s device signals an interrupt B’s device signals an interrupt starts device starts device interrupt time T WAIT/block WAIT/block starts device time-> device busy code executing on a processor

Upload: others

Post on 20-Apr-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

� �� � �� �� � � � � � � �� � �� �

� � �� �� �� � ��

� � �� �� �� ��� � �� � � �� � � � ! � � � � � � �� � �� �� � � � "#� $� � ! � � �� $%� � ! � � � � � ! � � �� � � �� � �� �� � � &� & �'

�( !' & � � �� �� � � � " #

� � &' � �� � � � � � � � � "# �� � $%� � ! � � � � �

� � � �� � � � � � � � � )� �� � �� � � � � � � �� � $ � ! � *,+ - .

� + - . �� � � �� � � �� � & !� & � �

� � � � � � �� � � � � � � � � ) !� � � �� � � � � � � �� � $ � ! � *,+ - .

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ A BC

�� � � � � � � D �E F � G � � D � � H I

A’s device

A’s ISR

Process A

Process B

B’s ISR

B’s device

A’s device signals an interrupt

B’s device signals an interrupt

startsdevice

startsdevice

interrupt

time T

WAIT/block

WAIT/block

startsdevice

time->

device busy

code executing on a processor

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 = 9 :J 48 9K L 3 5K8 9 A BM

Page 2: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

�� � � � � � � D �E F � G � � D � � H N

� �� � � � �O P �� � $%� � ! � � �Q �� � $� � ! � � �R �� � � � � �

)� &� � � P' � *

� $� � ! � � �Q � � � �� � �� � & $� � � �� � �� �� �� � �� S �� �

� � � � ! � P &� P �� � � �T Q + O �� S� $� � ! � � �R � � � � � � � � � � � P �� � + # U

� � �� � $� � � � $� �� � � ! � � � &' �� S � Q V W# O � &� � S � ��

� $%� � � � $� �� � � ! � � � &' �� S � "# ! �� ! � � � � �

� $%� � � � $� �� � � ! � � � &' �� S � Q X � � � � � ! � � � S �� �� � �� � & $�� � �' � Q � � �� �' ' � � � � P &� � �� �� � ! � � � &' � �Y O � �

� �� � � � � � &' � �� � � � �[Z � �\ ��� & $ � � �� �� S^] ) � � � � ! �

� � �� � * � � � Q � �_ � � � � �� �� P' � !\ � � � �� �� S � � � �� �

� � � � ! � X � � � �� � Y

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 = 9 :J 48 9K L 3 5K8 9 A A B

` � � Da �� �,b H� c �a �� � H � � G � � � F H � � F� � I

routine/methodto implementWAIT(anEvent)

ready or blocked

event(s) awaited

OS implementation-of-processes level

continues runningafter possible delay

OS processlevel

.anEvent.WAIT or WAIT(anEvent) (however programmed)

Process A

routine/methodto implement

SIGNAL(anEvent)

from interrupt serviceNOTE: asynchronous/interrupt-driven

Process A’s descriptor

events waiting

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 = 9 :J 48 9K L 3 5K8 9 A A A

Page 3: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

` � � Da �� �,b H� c �a �� � H � � G � � � F H � � F� � N

� � & $ $ � � � $� � ! � � �Q X � ! �' 'd

� �� � �� � � � � T Q + O � � &� �� �� � � � � �Z � � �� � � � � �� �� S] �� �_ � � � � � � �� Y Y Y � � �� � � �� � �� � & $� � � P � ! �' ' � � # + ef Q g

� � � � # + ef Q g � � &� �� �_ � � � $� � ! � � �Q � � � � �� � �� � � � �� � � �� �� Z � � �� � � �� � �� � � ] �� � � � �� �� � � �d

� � �� � � � � � � �� � �� Z � � �� � � � � �� �� S]� �( �� �

� � � � T Q + O � � &� �� � � � � & � � � �� � �� � � � �� � �� � & $�

� $� � ! � � �Q � �� � � � � � � �� � � � �� � �� � �� �� Z � � �� � �

�� � �� � �[]� �� � �� � �� �� & � � � P' � !\ � � ) �� � �� �� S � � �� � *� �� � � ( �� �

� � � � � � �h iQ h g " .j

� ) � � � P � � \ . �� ! & � � �� � # �� � � � � � � � � � � " � " � � ��

�� � � �� � � �� � S � � �� � �� � $� � ! � � � k� � � � � �

� �� � S � � �� � *

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 = 9 :J 48 9K L 3 5K8 9 A Al

` � � Da �� �,b H� c �a �� � H � � G � � � F H � � F� � �

� � � � � � � �� � ( � � $%' � �� � UQ . i . " f h + O + " f

� � � � � �� � � � � � �� � � � � � � � �� � � � �� � �� P

$%� � ! � � � � � k� � � � � � � � ( � ! &� �� S � � � T Q + O �� �

# + ef Q g � � &� �� � �

� � � �� � � � � �� S �� � $ � � � � P' � � � � &' � �� � � � �� P �� � �� �� � �� ' � � � �� S � � � � � �� �� � & !� � �� � � � T Q + O �� �

# + ef Q g P � ! � & � � � � �� � �� � & $� � � � �� �� � � �� �� �� ��

! �� � � � '

� T Q + O �� � # + ef Q g � & �� P � � � � � Q O " V + .

)� �� � �� � �� � & $� � P' � *Y + � � � i# # if O + Q g � � � � �� � �

! � � � � !� � � � � � � �� � � ! �� � � � � � �Y � m "T n

� � � � P � � �� � �� � & $� � n � �� ' � � � \ � �� � &� � $� � ! � � � � �� ! � � $ � � �� � �� �� � & !� � �� � o � � � � $ � � � � � Y Y Y � � � ' �� ��

� � �� � � � ! � � � � � ! � � � � !� � ! ! � � � � � # mQ U ih h Q O Q

P . " f . W U U if O - U " . i# # i# � � � S �� �� �'

$%� � P' � � d� �� � $ �� �� �� S � �� � � � � P � ! � & � � � � � �� �

� &' � �� � � � � � � � �� �� ! �� ! & � � �� � � $ $' � ! �� � �� � � &� � �� S �� & � �� ' � � �'

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 = 9 :J 48 9K L 3 5K8 9 A A 6

Page 4: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

p qsr F � � � � c � � Hsystemcall

systemcall

I/O control

language’s I/O interface

language-independentOS I/O interface

application program

language runtimesystem and

library routines

...operatingsystem

device-independent:selects specific I/O service required

this level must be prevented from programming devices directly. I/O is done by requesting OS service via system calls

I/O request

userlevel

� $%� �� �' � S � � "# $%� � S � � � � � � � � ! � �

� &� $%� �� �' � S � � � $ $%' � ! �� � �� � �\ � � � � t & � �� � � � "# � � �

+ k " � � � � �� � � ! �' ' �

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 = 9 :J 48 9K L 3 5K8 9 A Au

p qsr �� � � � � � � r v wx � � F� �

systemcall

I/O control

operating system

buffer area for a device type

data buffersoperationsto access

data buffers....

device handler

interruptserviceroutine

user level

operating systemhardware

application process request

deviceevent

application

runtime/library

device handler processbuffer access

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 = 9 :J 48 9K L 3 5K8 9 A A y

Page 5: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

�� � � � � � � r v wx � � F� �

� � � $ � �� � o � $ $%' � ! �� � �� � � � �� �� o " # �� � � ! �� � � �

� ! ! � � � � � � �� � P &z �� �

� P �� � � � � & $ o � � � � ! ��� � � �� �� o " # � ( � ! &� � ��

�' � � � ! ! � � � � � � �� � P &z �� �

� � � � � � � � � � � � � � �� � � � & $ $ � � � � � � d

� { |} |~� �� �� |�� �� � � � � � � �� � � � �� �Y

" � ' �� � $� � ! � � � k� � � � � � �� �� ! � � � � ! ! � � �

� � �� � � P' � � �� �Y

V & �� P � � P' � � � � � �� � � �' � �� �� � �� _ � � � � � �Y� � �� �� }� �� � �� ��� �� � � ~ }� �� � � �� �� �� � ) �Y SY

$� � � �� ! � k � P � �� ! � * �� � �� �Y

V & �� P � � P' � � � � � �� &� � �' � �� � � � & � � � P' � � R WO� � & �� � �� �� � $ �� � �� � � � � � � ! ! � � � �� S �� � � �' �

� � �� �� S ) � � �� � � � � � �� P � � P' � � � � � � ! � � � �

�� � �� � � �� �� � *Y

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � ; > 5K� 5J 4 < 3� <� �48 : < L L < L A A �

� � � � H H � H � � D� G � � � D H

one programone process

runtimesystem

one programone process

runtimesystem

address space address space

kernel threads

OS/kernel

a) sequential programming language

one programmany processes

runtimesystem

address space

kernel threads

OS/kernel

b) concurrent programming language no OS support (user threads only)

one programmany processes

runtimesystem

address space

user threads user threads

one programmany processes

runtimesystem

address space

kernel threads

OS/kernel

c) concurrent programming language OS kernel threads for user threads

one programmany processes

runtimesystem

address space

user threads user threads

... ...

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � ; > 5K� 5J 4 < 3� <� �48 : < L L < L A A �

Page 6: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

� � � F� � H � H � � � b H � � � G � � � D Hruntime system

address space of process

one kernel-thread or process

OS/kernel

b) concurrent programming language no OS support (user threads only)

user threads

one programmany processes

runtimesystem

address space

user threads

A B C

user-thread implementation

user threaddata

utid = create ()kill (utid)wait (utid)signal (utid)schedule ()

user thread operations

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � ; > 5K� 5J 4 < 3� <� �48 : < L L < L A AC

� � � F� � H � H � � � b H � � � � D� � � � � � G � � � D H

runtime system

address space of process

one kernel-thread per user-thread

OS/kernel

c) concurrent programming language OS kernel threads for user threads

user threads

one programmany processes

runtimesystem

address space

user threads

A B C

user-thread implementation

user threaddata

ktid = create ()kill (ktid)wait (ktid)signal (ktid)

user thread operations

kernel threads

ktid = create ( ) calls OS: create-thread ( )kill (ktid) calls OS: kill-thread (ktid)wait (ktid) may call OS: block-thread (ktid)signal (ktid) may call OS: unblock-thread (ktid) the OS carries out thread scheduling

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � ; > 5K� 5J 4 < 3� <� �48 : < L L < L A AM

Page 7: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

� H � � � G � � � D H � � �

� ! �� X� � � � $ �� � � � "# � � �� � � P �� �� ! � �� S& � �� � � � � � � � �

! �� X� & � � � � � � � �' � � � � � � $ $%' � ! �� � �� � � � �' � � �

&� P � &� � � �

� � � � � � � ' � $� � ! � � � � � P' � !\ � � �� �� � � � � � � � � \ � � �

P' � !\ �� S � �� � � ! �' '

� � $ $%' � ! �� � �� ! �� X� � ( $%' � �� � � &' � � $� � ! � � � � �

"# \ � �� � �� ' �� � $� � ! � � � k \ �� � �' � � � � � � �

� R WO � �� � ' �� S � � �� � � � �� � � � � � � $%' �� � � & � �� � � � � � � � $� � � � $� � ��

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � ; > 5K� 5J 4 < 3� <� �48 : < L L < L A l B

� � � � � � G � � � D H � � D H � � � G � � � D H

� \ �� � �' � � � � � � � )� � �� �� � � � & � �� � � � � � � � * �� �

� ! � � � &' � � & � �� S � � � "# X � � ! � � � &' �� S �' S � � �� � �

� � � � � $ $%' � ! �� � �� ! �� � � � $ �� � � � "# � � �� � � P �� �� ! � �� S & � �� � � � � � � � � � P &� �� ' �� "# � ! � � � &' �� S� � $� � � � $� �� � �� � $� � � � �� � P � � � �

� � �' � � � � � � � � $ �� � � � � � � �� �� � � � "# � � � $ �� � �� � Y

� & � �� � � � � � � � � ! �� � � \ � P' � !\ �� S "# ! �' ' � � �� � � &�

�z � !� �� S �� � �� & � �� � � � � � � � � � � ! � ! �� ! �� � �� & � � �

� &�� ! �� � ( $' � �� � � &' � � $%� � ! � � � � �

� � � �� � �� � � �z �� �� � � � � � � � $ � !\ � S � �� � � � � X� � � � � �� � \ �� � �' � � � � � � � $ �� & � �� � � � � � � �� � � � P � � \ . �� ! & � � �� � # �� � � �

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 9 5 <4 � 2 48 : < L L78 � � ; 9K : 3 5K8 9 A l A

Page 8: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

� � F � F � � � � F� � H

shareddata

A B processes A and B containcritical regions (CRs)- code which reads or writes this shared data

shareddata

A B

entry protocol--------------

test flagif shared data is busy,test again (busy-wait)if free set to busy

and enter

exit protocol--------------set flag to free

flag:free/busy

entry protocol--------------

test flagif shared data is busy,test again (busy-wait)if free set to busy

and enter

exit protocol--------------set flag to free

CR CR

CR CR note:1. CRs needed only if some process writes2. every CR is associated with some specific shared data

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A l l

p � D FE F H F � � � � H �b � � D b H � �

� �� � � � �� � � $� �� � ! � ' � � �� �� � � �� � & �� P �

�� � � � ! k �� � �� � � � P' � � m "T n

� � � � P � � �� � �� � & $� � n � f " � &� � $� � ! � � � � � � � ' �� �

�� � $ $� � $%� � �� � � � � S �� �� �' & � �Y� � � ! � �� � �� �� � & !� � �� � � i#

� � � � � � � � �� � � � �� � )� � �� � �� � � � �� * ) .+ # . *� � � � � �� � � !' � �� ) U+ # . *

�Y SY � � � � � �� � � !' � ��

� � S�� � k k � � �� � � � �� � � � P & � � � S��   k k � � �� � � � �� � � � � � � � � �� �� � �' � �' & �

�� � � $%� �� � ! � 'd

¡ ¢£¤ ¥£¦ ¤ ¥ §¨ ¢£ ¡ © ¡ ¢ ª�« ¬­ ¢ ¡® £ ª�� � �' & � �� � � S � �� �� � � � � � �� � � � �� � � � � P & � � � � �� � �� � �' & � �� � � S � �� �� � � � �� ¯ �� � � � �� � � � �� � � � � � � � �

�� � � & !' � � � � � �� Y

� ! �� �' � � P � & � � � � � � ! �� � �� � � � � � ! � � � � � � �� � ��

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A l 6

Page 9: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

v � � � ° G� � � H� ± F ²� H �� �� ` w I ³´ µ

� �� � � $%� �� � ! � ' � � P � � � �� � � ' � � P & � � � � �� �� S

� P �� � �� � � P' � !\ � � �� �� S $� � ! � � � � �

h �_ � � � � �� � $ � �� � �� � � P' � � � � � � $ � � � �

" $ �� �� � � � � � � � � � � � $ � �� �d

� � �� ) �# � � * � � �# � � Y � � �T � �� )h � ¶\ �� � � X � - ) �# � � * *d�� �# � � · � � � �� �# � � � �# � � �  

�' � � � & � $ �� � � � � $%� � ! � � � � � �� �� S �� �# � �

� � S � �' ) �# � � * � � �# � � Y � � � # � S�� �' ),¸ ) �# � � * *�� � � �� � �� � � � $%� � ! � � � � � � � �� �� S �� �# � �

� � �� �# � � � �# � � ¹  

�' � � � � � � � � � $%� � ! � � �

� � � ! � ! �� � �� & � � �� � �� �� � � � �� �� �� � & !� � ��

+ � $%' � � �� � �� � � � d �� �� � � S �� �� � � t & � & �

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A l u

� H � H � c H � � � ° G� � � H

� � &� & �' � ( !' & � � �� � S & �� � � � �� � �� � & !� & � �

� �' & � �   )� � � � * �� � � ) P & � *

� ! �� � �� � � � � � ! � � �� � � �� � ��

� �' & � � � ) � & �� � � �� *   oº o» Y Y Y f ) ! �� $� � ! � � � *

�Y SY � � � � $ � � � � � � � � & � ! � » � � �� �� � �' � � � � � � »

$%� � ! � � �Q d ¡ ¢ ¬¼½ ¡ § ¢ ¾¿ ¬ ¢À Á £ « ­ Âà � �' & �� º

$%� � ! � � �R d ¡ ¢ ¬¼½ ¡ § ¢ ¾¿ ¬ ¢À Á £ « ­ Âà � �' & ��  

$%� � ! � � � .d ¡ ¢ ¬ ¼½ ¡ § ¢ ¾¿ ¬ ¢À Á £ « ­ Âà � �' & �� �

$%� � ! � � �h d ¡ ¢ ¬ ¼½ ¡ § ¢ ¾¿ ¬ ¢À Á £ « ­  à � �' & �� �

�� � $%� � ! � � �h � � P' � !\ � � � � � � � � & � ! � »

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A l y

Page 10: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

Ä � � �x H F� �

A BaSem

1

0

0

B

aSem.semWait

aSem.semSignal

concurrent processes:serialisation of critical regions

aSem.semWait

C

aSem.semWaitB,C

0C

aSem.semSignal

aSem.semSignal

0

1

CR

CR

CR

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A l �

� a � b ° � � � H H H � � G � � � F H � � F� �

A BaSem0

0

0

A

aSem.semWait

aSem.semSignal

wait before signal

signal before wait

A BaSem0

0aSem.semWait

aSem.semSignal1

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A l �

Page 11: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

p � ° � � � � � � � F� � � c H � � � ° G� � � H I

runtime system

address space of process

one kernel-thread per user-thread

OS/kernel

user threadsA B C

user-thread implementation

user threaddata implementation of wait and signal

on semaphore objects

ktid = create ( ) calls OS: create-thread ( )kill (ktid) calls OS: kill-thread (ktid)xSem.semWait by Atid may call OS: block-thread (Atid)xSem.semSignal by Btid may call OS: unblock-thread (Atid)

the OS carries out thread scheduling

xsem.semWaitxsem.semSignal

xsem.semWait

xsem.semWait

xsem.semSignal

xsem

0A,C

Atid Btid Ctiduser space

application

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A l C

p � ° � � � � � � � F� � � c H � � � ° G� � � H N

� � � � & � �� � � � � � � � �� ' ) "# � � � � �� ' �� � $� � ! � � � *� � � � &� � � � � � �� � � � � � � �' ' � � � � $ � � � � �� � & � ��

� � � � � � � �� � S � � �� �

� � � �� & � �� � � � � � � � �� � � � $ $ � � � � \ �� � �' � � � � � � �

� � �T � �� �� � � � � # � S � �' � & �� � � � � � �' � � � P �

~ } � {� � � Š�� ~ }� �� �

) � &' � � $� � ! � � � � � � � $%� � � � $� �� � � ! � � � &' �� S *� � � � ! � �� � � � � S � �� � � � ! � � � � � $ � � � � � P ¶ � !��� � & � � � ! � � $ � � �� � �� �� � & !� � �� � & ! � � �

¡ ¢£¤ ¥£ ¦ ¤ ¥ §¨ ¢£ ¡

� !� & ! � �' �� � � �Æ ! &' � �� � � �� � �� � � � � � � S �

� � � � � �' � � � $ $%' � � � � � \ �� � �' � � � � � � � � ( � ! &� �� S � � � "#

�� � & � �� S "# � � � � $ � � � � � � � � � &� & �' � ( !' & � � �� �� �

! �� � �� � � � � � ! � � �� � � �� � ��

� � � � � � � � � � � ! �� ! & � � �� ! ! �� � � � ' _ � �� �� � � � � �� � ��

"# � � �� � � � � � � ! �� ! & � � �� � $� � S � � � � �� S' �� S & � S � � �� � "# � � & $ $ � � � � &' � �� � � � � � � � �

� $ $%' � ! �� � �� $� � ! � � � � �

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A l M

Page 12: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

H G � � � DÇ b H � � � È � � I

outptr

inptr

producer consumer

produce an itemis there space in the buffer?insert item

is there an item in the buffer?remove itemconsume item

first assume one producer and one consumer

semaphores:items, initially 0 (items in buffer) spaces initially N (spaces in buffer)

.= potential delay

. .

outptr

inptr

producer consumer

produce an itemspaces.semWait()insert itemitems.semSignal()

items.semWait()remove itemspaces.semSignal()consume item

. .

.= potential delay

this is programming condition synchronisation

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A 6 B

H G � � � DÇ b H � � � È � � N

now assume many producers and many consumers

for simplicity, use one semaphore to ensure mutually exclusive access to the buffer by all processes(one of the producers and one of the consumers could access together - we do not show this)

outptr

inptr

producer consumer

produce an itemspaces.semWait()guard.semWait()insert itemguard.semSignal()items.semSignal()

items.semWait()guard.semWait()remove itemguard.semSignal()spaces.semSignal()consume item

. .

.= potential delay

use another semaphore: guard, initially 1 (buffer free)

. .

this is programming: mutual exclusion condition synchronisation

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A 6 A

Page 13: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

v � � � ° G� � � H b D F H H H F� �

� � � � � � ! � �� � � � &� � �� ' �� S ! �� ! & � � �� ! ! �� � � � ' ��

� $ �� �� �� S � �� � � � �� � ! �� ! & � � �� � $%� � S � � � �

� � �Æ ! &' � � � � $%� � S � � � � �� � � � & � � ! � � � � !� '

� ! �� � � � S �� � � � � �� ) ! � � � & $� � �� � *� ! �� � � � S �� � � � � S�� �' ) � � � � ' � !\ � �� � � *� $� � S � � � � �� � ! � � $%' �( � �Y SY � � � � �� � �� � � � �� �� �

� &� ! �� � �� � �� �' ! � � � �� � �� � � � P' � !\

) P &� ! �� � � � \ � �� � � � � � � � � � � ! �� ! & � � �� � � !� �� �� *

� &� P � &� � � � � �' �

� ! �� ! & � � �� � $%� � S � � � � �� S ' �� S & � S � � $%� � � � � �

� � S � �� � ' � � �' ! �� �� � & !� � P &� � � $' � � �� � � � � � & � �� S� � � � $ � � � � �d

! �� � �� � � � �' !� �� � ! �' � � S � �� � o � � � �� � � � o

� � ! � � �� � � � � � �� � � � � �� � ! � � � �� � �� � �� � � P' � � o

� !� �� � � P ¶ � !� � oQ � � � �� � � ¯ � � & � �� � S & �� � � �

! � � � �� � �

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 J 34 <� � � < �8 4 =� 27 A 6 l

p � � � � D H � H � � � H �� � � � I

process’saddress space

process’saddress space

shareddata

OS: in everyprocess

address space(shared-memory

IPC)

cross address-space IPCe.g. pipes message-passing

userspace

userspace

userthread

userthread

kernel threads

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � 27 3 9� L = L 5 < � L 54 ; : 5; 4 < A 6 6

Page 14: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

p � � � � D H � H � � � H �� � � � N

� & � �� � � $ � ! � $%� � ! � � � � � � � � � � � �� � �� � !�

�Y SY !' � �� � � �� � � �� � �� �

� "# � & �� � & $ $ � � � !� � � �� � � � � � � � � $ � ! � + - .

� � � �� � � � � � � � + - . � � & � � �d

� �� & � �� ' � � �' � �� � �� � � &' � �� � � � � � � � � $%� � S � � �

) � �� � �� � � � $ $%' � ! �� � � � *� � �� � �� � � � "# � � � ! � � � �� � �� �� � ' � &' � �� � � � � � � � �d

& � �� � � � � � � � ! �' ' � � � � �� � � ! � �� � � ( � ! &� � � � � "#

� � \ �� � �' � � � � � � �

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � 27É K 5J8 ; 5 LJ 3 4 <� � < �8 4 = A 6 u

� H � � G � � �� H � � H H � � �b ° � H H F � � I

Q � � � � � S � � � � � � �� � � � � $ � ! �_ � � � � � � �� ! � � � � �� �� S� � � � �� � �� �� � � � ! � �� �� + h �Y

O � � � � � � � S � P � � � � � � �� � � � �� &� �� � & !� & � � � P � � �

)� � � � � + - . � �� � � ! � *Y O � � � $ $%' � ! �� � � � � & �� �� � �� $%� �� Y

R &� � $ � � � � � � � S � � ) !� Y �� S & � �� � � � � $%� � ! � � & � �

! �' ' � * �� � P � ! � � �� S � � � � ! � � � �� �� � � �� � � P &� � �

� �� � � �Y

address space

OS/kernel

A B

messSend (B, ) messWait(?, )

routine to implementmessSend

user space

address space

.

routine to implementmessWait

process A’smessage queue

process B’smessage queue

B already waiting

B not waiting

.= potential delay

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � 27É K 5J8 ; 5 LJ 3 4 <� � < �8 4 = A 6 y

Page 15: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

� H � � G � � �� H � � H H � � � ° � H H F � � N

� � �' � �� � � � �T � �� �� � � � �� ! � �� S � � � � � S � � ��

t & � & �

� � � � �' � �� � � � �# �� �d � � � � � S � � � P &z �� � � P � �� � � �� � � ! � �� �� � �� � � �� �� S

� � � � � � � P � � P' � � � � � �� � � � � � � � � S � � � � � Z �� �� �]

� � �� � � � �� � � � � � S � $ � � � �� S � � $' � � �� � �� � � � � � � � �

� � �� � � � � � � � ! � � ! & � � �� ! ! �� � � � '

� !' � �� � � � �� � �� �� � �� � !� � � � d � ( � � $%' � � � P �' ��address space

client process C

messSend (S, )

.= potential delay

messWait(S, ).

address space

server process S

messWait(?, )

messSend(C, )

.cross-addess-spacemessage-passingwith bufferingas required

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � 27É K 5J8 ; 5 LJ 3 4 <� � < �8 4 = A 6 �

� � � H G � H � � D � � °� H F � � � ° � � � � F� � H

� Ê � � � �� � �� � � � � �' & � � � � � �� � � � � � � � ' � �� �� �

$ �� � � �� �� � �� � � � � � � �� Y

� # � � � � $ �� �� � �� � �� � � &' � �� �� � S � �� � � � t & �� �

! � �� S � � � � � �� � �� � & !� & � � � �� P �� � � � �� � � � � � �� � $ �� � � �� �� � � � � � � �Y SY � �' �� �_ ' � Ë � � �� �� �� _ ' � X � � � �\ P' � !\ � � �

� � � ��� P' � !\ ' � �� �� � � � � � � �_ ' � X � �� � � �� � & $ �� � � �

� �� � !� � � Y�Y SY � � �� � �� � P � � � � $ �� �� � � � �Y

� �� � !� � � � � ! ! & � � �� � � � � � � � ' � � � � ! � � $ � � �� �

� $ �� �� � �� � � � $ �� � � �� �� � �� � � � � � ' �� � �� ��

ÌÍÎ Ï ÍÐ ÌÐ ÑÒ Í ÑÐ ÑÓ Ñ Ò Y

� O � � ! �� ! � $� �� Ó Ñ ÏÔ ÌÎ Ï Õ ÒÖ Ó Ñ Ì Ï Í � � ÑÖ Ó Í Ð ÓÎ Ñ Ì Ï Í � �

� �_ � � �d �� � � � �� � � !� � � � ! � � $%' �� � � � & ! ! � � �� &' ' �� �

� � � &' � � $ �� � � �� × �� �� � � � � � �� � � �� � � � � �� � � �� �� �

� & �� P � � � �� � � � � � � � � � & S � �� � � � � � � �� �� �� � � �Y

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ Ø 4 3 9 L 3 : 5K8 9 L A 6 �

Page 16: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

� � � � � � � � � � D � � °� H F � � � ° � � � � F� � H

� T � � � � � � � �� � �� ! �� ! & � � �� ! ! �� � � � ' ! �� P �

� � $%' � � �� � � � � � � � � �� S ' � � $ �� �� � �� �� � � ��

� � � � � ) �Y SY & � �� S � � � � � $ � � � � *Y

� f �� ! �� � � � �� ! � � $ � � �� � � $ �� �� � �� �d� �� � � �� � � � � � � �� � � ' � �� S P �� � � � �� � � � � � �� � $ �� � � �� �� �

� � � � �

� . � � $ � � �� � � $ �� �� � �� � �� � � & P ¶ � !� � �Ù Ò ÓÙ Ú ÏÎ Û

�Y SY �� � � �� � � � � � d

A B

aSem.semWait

bSem.semWait

bSem.semWait

CR (for bSem)

aSem.semWait

CR (for aSem)

� Q � � �� � � � � � �� �' ' � ! �� � � � � � & � ! � � � � � � � ! �' ' �� � � & P ¶ � !� � � � � � � ' � !\ Y

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ Ø 4 3 9 L 3 : 5K8 9 L A 6C

� G �Ü � p ± ° � � ° � � � F � H � c �� � � H � � F� � H

� Q � � � � ! �� d � � �Z �' ' � � � �� � �� S] $� � $ �� � � �_ � � �

� P � � �Y

� . �� � � �� �� ! d �� � � � � �� � � � � �� � ! �� � � �� �� � �� �� � �

� � �� � � !� � �� � � � � � �� � � �� �� � �� ! �� � � �� �� �� �� �Y

� + � � ' �� � � � d O � � �z � !� � �� �� �� ! � � $' �� � � � �� � � !� � � �

�� � � �� � � � � P' � � � �� � �� ) ! �� ! & � � �� � * � � �� � � !� � �� Y

� h & � � P �' �� d " � ! � � � � �� � � !� � �� ! � � $%' �� � �

) ! � � � �� � * �� � �z � !� � � �' ' $ �� � � �� o � � �� �� � � �� � �� �

� & P � � t & �� � � �� � � � � �' & � � �Y

+ � � � �� � � � & $ $ � � � � � � �� � � !� � �� � �� � & �� �� � & � � � � �

Q .+ h $%� � $ �� � � � � � � ' � &� � �� Î Ï ÍÎ ÝÖ Ö Ò Í Î Þ �� � Î Ö ÓÐ ß ÒÐ Y

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ Ø 4 3 9 L 3 : 5K8 9 L A 6M

Page 17: A’s device signals · 2002-09-18 · A’s device signals an interrupt B’s device signals an interrupt starts device starts device time T WAIT/block WAIT/block starts device time->

v � � �� � � c � � � � �

� � & � � � &' � � �� &� � �� �� �� �d

� � � "# � � & �� � & $ $ � � � ! �� ! & � � �� ! ! � � � � � '� � � � �� � �� � �' " # � ( � ! &� � ��� � � � & $ $ � � � � &' � �� � � � � � � � � $%� � ! � � � � �

� � &� & �' � ( !' & � � �� �� � ! �� � �� � �� � � ! � � �� � � �� � ��

� � � � � $ � � � � �d & � � � �� � � � $' � � �� � �� � ��

� �� ' � � �� �� � ! � � ! & � � �� � $%� � S � � � �� � � ' � �� S � � � �� �'

� � � � $ � � � � � ) �� $%� � � & ! �� � �� � ! � � � & � �� � � � � � � o� � � � � � �� � �� � � � �� �� � *

� �� � �� $� � ! � � � ! � � � &� � ! �� � �� )+ - . * � �� � �� �� �

� !� � � � � � � � � � � � $ � ! � �

�� � �� ' � � �� � � � � � � �� � � P � &� d

� � � �� � � !� � �� �

� � � � � ' � !\ �

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 ; � � 34 =8à 2 34 5 6 A u B