18
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

A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

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

� � �� �� �� � ��

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

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

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

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

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

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

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

�� � � � � � � C �D E � F � � C � � G H

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 :I 48 9J K 3 5J8 9 A A 6

Page 2: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

�� � � � � � � C �D E � F � � C � � G L

� �� � � � �M N �� � $%� � ! � � �O �� � $� � ! � � �P �� � � � � �

)� &� � � N' � *

� $� � ! � � �O � � � �� � �� � & $� � � �� � �� �� �� � �� Q �� �

� � � � ! � N &� N �� � � �R O + M �� Q� $� � ! � � �P � � � � � � � � � � � N �� � + # S

� � �� � $� � � � $� �� � � ! � � � &' �� Q � O T U# M � &� � Q � ��

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

� $%� � � � $� �� � � ! � � � &' �� Q � O V � � � � � ! � � � Q �� �� � �� � & $�� � �' � O � � �� �' ' � � � � N &� � �� �� � ! � � � &' � �W M � �

� �� � � � � � &' � �� � � � �YX � �Z ��� & $ � � �� �� Q\[ ) � � � � ! �

� � �� � * � � � O � �] � � � � �� �� N' � !Z � � � �� �� Q � � � �� �

� � � � ! � V � � � �� � W

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

_ � � C` �� �,a G� b �` �� � G � � F � � � E G � � E� � H

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 :I 48 9J K 3 5J8 9 A A c

Page 3: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

_ � � C` �� �,a G� b �` �� � G � � F � � � E G � � E� � L

� � & $ $ � � � $� � ! � � �O V � ! �' 'd

� �� � �� � � � � R O + M � � &� �� �� � � � � �X � � �� � � � � �� �� Q[ �� �] � � � � � � �� W W W � � �� � � �� � �� � & $� � � N � ! �' ' � � # + ef O g

� � � � # + ef O g � � &� �� �] � � � $� � ! � � �O � � � � �� � �� � � � �� � � �� �� X � � �� � � �� � �� � � [ �� � � � �� �� � � �d

� � �� � � � � � � �� � �� X � � �� � � � � �� �� Q[� �( �� �

� � � � R O + M � � &� �� � � � � & � � � �� � �� � � � �� � �� � & $�

� $� � ! � � �O � �� � � � � � � �� � � � �� � �� � �� �� X � � �� � �

�� � �� � �Y[� �� � �� � �� �� & � � � N' � !Z � � ) �� � �� �� Q � � �� � *� �� � � ( �� �

� � � � � � �h iO h g " .j

� ) � � � N � � Z . �� ! & � � �� � # �� � � � � � � � � � � " � " � � ��

�� � � �� � � �� � Q � � �� � �� � $� � ! � � � k� � � � � �

� �� � Q � � �� � *

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

_ � � C` �� �,a G� b �` �� � G � � F � � � E G � � E� � �

� � � � � � � �� � ( � � $%' � �� � SO . i . " f h + M + " f

� � � � � �� � � � � � �� � � � � � � � �� � � � �� � �� N

$%� � ! � � � � � k� � � � � � � � ( � ! &� �� Q � � � R O + M �� �

# + ef O g � � &� �� � �

� � � �� � � � � �� Q �� � $ � � � � N' � � � � &' � �� � � � �� N �� � �� �� � �� ' � � � �� Q � � � � � �� �� � & !� � �� � � � R O + M �� �

# + ef O g N � ! � & � � � � �� � �� � & $� � � � �� �� � � �� �� �� ��

! �� � � � '

� R O + M �� � # + ef O g � & �� N � � � � � O M " T + .

)� �� � �� � �� � & $� � N' � *W + � � � i# # if M + O g � � � � �� � �

! � � � � !� � � � � � � �� � � ! �� � � � � � �W � m "R n

� � � � N � � �� � �� � & $� � n � �� ' � � � Z � �� � &� � $� � ! � � � � �� ! � � $ � � �� � �� �� � & !� � �� � o � � � � $ � � � � � W W W � � � ' �� ��

� � �� � � � ! � � � � � ! � � � � !� � ! ! � � � � � # mO S ih h O M O

N . " f . U S S if M - S " . i# # i# � � � Q �� �� �'

$%� � N' � � d� �� � $ �� �� �� Q � �� � � � � N � ! � & � � � � � �� �

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

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

Page 4: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

q rts E � � � � b � � Gsystemcall

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

� $%� �� �' � Q � � "# $%� � Q � � � � � � � � ! � �

� &� $%� �� �' � Q � � � $ $%' � ! �� � �� � �Z � � � � u & � �� � � � "# � � �

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

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

q rts �� � � � � � � s w xy � � E� �

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 :I 48 9J K 3 5J8 9 A A z

Page 5: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

�� � � � � � � s w xy � � E� �

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

� ! ! � � � � � � �� � N &{ �� �

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

�' � � � ! ! � � � � � � �� � N &{ �� �

� � � � �| }~ | �� � �~ | � | } ~ �� }� o � & $ $ � � � � � � d

� � � � �� � �� | � ��� } ~ � � � � � � �� � � � �� �W

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

� � �� � � N' � � �� �W

T & �� N � � N' � � � � � �� � � �' � �� �� � �� ] � � � � � � ) �� �

� � Q�� �' � *W� | } ~ �� �� }~ � �~ | �� }~ � � � �� }~ � � �� �� �� � ) �W QW

$� � � �� ! � k � N � �� ! � * �� � �� �W

T & �� N � � N' � � � � � �� &� � �' � �� � � � � � Q � �' ' � � � �

& � � � N' � � P UM � � & �� � �� �� � $ �� � �� � � � � �

� ! ! � � � �� Q �� � � �' � � � �� �� Q ) � � �� � � � � � �� N �

� N' � � � � � � ! � � � � �� � �� � � �� �� � *W

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

� � � � G G � G � � C� F � � � C G

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 >?@ � ; > 5J� 5I 4 < 3� <� �48 : < K K < K A B A

Page 6: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

� � � E� � G � G � � � a G � � � F � � � C Gruntime 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 >?@ � ; > 5J� 5I 4 < 3� <� �48 : < K K < K A B B

� � � E� � G � G � � � a G � � � � C� � � � � � F � � � C G

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 >?@ � ; > 5J� 5I 4 < 3� <� �48 : < K K < K A B 6

Page 7: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

� G � � � F � � � C G � � �

� ! �� V� � � � $ �� � � � "# � � �� � � N �� �� ! � �� Q& � �� � � � � � � � �

! �� V� & � � � � � � � �' � � � � � � $ $%' � ! �� � �� � � � �' � � �

&� N � &� � � �

� � � � � � � ' � $� � ! � � � � � N' � !Z � � �� �� � � � � � � � � Z � � �

N' � !Z �� Q � �� � � ! �' '

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

"# Z � �� � �� ' �� � $� � ! � � � k Z �� � �' � � � � � � �

� P UM � �� � ' �� Q � � �� � � � �� � � � � � � $%' �� � � & � �� � � � � � � � $� � � � $� � ��

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

� � � � � � F � � � C G � � C G � � � F � � � C G

� Z �� � �' � � � � � � � )� � �� �� � � � & � �� � � � � � � � * �� �

� ! � � � &' � � & � �� Q � � � "# V � � ! � � � &' �� Q �' Q � � �� � �

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

� � �' � � � � � � � � $ �� � � � � � � �� �� � � � "# � � � $ �� � �� � W

� & � �� � � � � � � � � ! �� � � Z � N' � !Z �� Q "# ! �' ' � � �� � � &�

�{ � !� �� Q �� � �� & � �� � � � � � � � � � � ! � ! �� ! �� � �� & � � �

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

� � � �� � �� � � �{ �� �� � � � � � � � $ � !Z � Q � �� � � � � V� � � � � �� � Z �� � �' � � � � � � � $ �� & � �� � � � � � � �� � � � N � � Z . �� ! & � � �� � # �� � � �

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ 0 I 34 <� � � < �8 4 =� 9 5 <4 � 2 48 : < K K78 � � ; 9J : 3 5J8 9 A B c

Page 8: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

� � E � E � � � � E� � G

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 I 34 <� � � < �8 4 =� 27 A B l

q � C ED E G E � � � � G �a � � C a G � �

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

�� � � � ! k �� � �� � � � N' � � m "R n

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

�� � $ $� � $%� � �� � � � � Q �� �� �' & � �W� � � ! � �� � �� �� � & !� � �� � � i#

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

�W QW � � � � � �� � � !' � ��

� � Q�� � k k � � �� � � � �� � � � N & � � � Q��   k k � � �� � � � �� � � � � � � � � �� �� � �' � �' & �

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

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

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

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

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

Page 9: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

w � � � ° F� � � G� ± E ²� G �� �� _ x H ³´ µ

� �� � � $%� �� � ! � ' � � N � � � �� � � ' � � N & � � � � �� �� Q

� N �� � �� � � N' � !Z � � �� �� Q $� � ! � � � � �

h �] � � � � �� � $ � �� � �� � � N' � � � � � � $ � � � �

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

� � �� ) �# � � * � � �# � � W � � �R � �� )h � ¶Z �� � � V � - ) �# � � * *d�� �# � � · � � � �� �# � � � �# � � �  

�' � � � & � $ �� � � � � $%� � ! � � � � � �� �� Q �� �# � �

� � Q � �' ) �# � � * � � �# � � W � � � # � Q�� �' ),¸ ) �# � � * *�� � � �� � �� � � � $%� � ! � � � � � � � �� �� Q �� �# � �

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

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

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

+ � $%' � � �� � �� � � � d �� �� � � Q �� �� � � u & � & �

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

� G � G � b G � � � ° F� � � G

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

� �' & � �   )� � � � * �� � � ) N & � *

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

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

�W QW � � � � $ � � � � � � � � & � ! � » � � �� �� � �' � � � � � � »

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

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

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

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

�� � $%� � ! � � �h � � N' � !Z � � � � � � � � & � ! � »

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

Page 10: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

Ä � � �y G E� �

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 I 34 <� � � < �8 4 =� 27 A 6 �

� ` � a ° � � � G G G � � F � � � E G � � E� �

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 I 34 <� � � < �8 4 =� 27 A 6 A

Page 11: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

q � ° � � � � � � � E� � � b G � � � ° F� � � G H

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 I 34 <� � � < �8 4 =� 27 A 6 B

q � ° � � � � � � � E� � � b G � � � ° F� � � G L

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

� � � � � � � �� � Q � � �� �

� � � �� & � �� � � � � � � � �� � � � $ $ � � � � Z �� � �' � � � � � � �

� � �R � �� �� � � � � # � Q � �' � & �� � � � � � �' � � � N �

� � } �� | } Š�� � �� }~ �

) � &' � � $� � ! � � � � � � � $%� � � � $� �� � � ! � � � &' �� Q *� � � � ! � �� � � � � Q � �� � � � ! � � � � � $ � � � � � N ¶ � !��� � & � � � ! � � $ � � �� � �� �� � & !� � �� � & ! � � �

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

� !� & ! � �' �� � � �Æ ! &' � �� � � �� � �� � � � � � � Q �

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

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

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

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

"# � � �� � � � � � � ! �� ! & � � �� � $� � Q � � � � �� Q' �� Q & � Q � � �� � "# � � & $ $ � � � � &' � �� � � � � � � � �

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

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

Page 12: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

G F � � � CÇ a G � � � È � � H

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 I 34 <� � � < �8 4 =� 27 A 6 ^

G F � � � CÇ a G � � � È � � L

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 I 34 <� � � < �8 4 =� 27 A 6 c

Page 13: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

Multiple readers, single writer concurrency control

Many readers may read simultaneously,a writer must have exclusive accessAssume writers have priority

ar is the count of active readersrr is the count of reading readersaw is the count of active writersww is the count of writing writers (but they must take turns to write one-at-a-time

readers’ code writers’ code

become active reader (ar = ar+1)if no active writers then proceed to read (rr = rr+1) else defer to writers (await aw = 0)

become active writer (aw = aw+1)if no reading readers then proceed to write (ww = ww+1) else await no readers (rr = 0)

READ

claim turn to writeWRITE

ar = ar-1rr = rr-1

release claim

if rr = 0 then wake up waiting writersexit

aw = aw-1ww = ww-1if aw = 0 then wake up waiting readersexit

mutual exclusion (to access shared counts)

condition synchronisation

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

w � � � ° F� � � G a C E G G G E� �

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

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

� � �Æ ! &' � � � � $%� � Q � � � � �� � � � & � � ! � � � � !� '

� ! �� � � � Q �� � � � � �� ) ! � � � & $� � �� � *� ! �� � � � Q �� � � � � Q�� �' ) � � � � ' � !Z � �� � � *� $� � Q � � � � �� � ! � � $%' �( � �W QW � � � � �� � �� � � � �� �� �

� &� ! �� � �� � �� �' ! � � � �� � �� � � � N' � !Z

) N &� ! �� � � � Z � �� � � � � � � � � � � ! �� ! & � � �� � � !� �� �� *

� &� N � &� � � � � �' �

� ! �� ! & � � �� � $%� � Q � � � � �� Q ' �� Q & � Q � � $%� � � � � �

� � Q � �� � ' � � �' ! �� �� � & !� � N &� � � $' � � �� � � � � � & � �� Q� � � � $ � � � � �d

! �� � �� � � � �' !� �� � ! �' � � Q � �� � o � � � �� � � � o

� � ! � � �� � � � � � �� � � � � �� � ! � � � �� � �� � �� � � N' � � o

� !� �� � � N ¶ � !� � oO � � � �� � � ¯ � � & � �� � Q & �� � � �

! � � � �� � �

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

Page 14: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

Java concurrency control constructs

SYNCHRONISED methods of an object execute under MUTUAL EXCLUSION with respect to all synchronised methods of the object

mutual exclusion (to access shared counts)condition synchronisation

data

implementation: a semaphore per object

CONDITION SYNCHRONISATION(not very good)WAIT (block, release exclusion on object)NOTIFY (implementation frees an arbitrary thread)NOTIFY ALL (implementation frees all threads first to be scheduled gets in they MUST retest the WAIT condition)

WAIT

NOTIFY (ALL)

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

q � � � � C G � G � � � G �� � � � H

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� K = K 5 < � K 54 ; : 5; 4 < A 6 z

Page 15: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

q � � � � C G � G � � � G �� � � � L

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

�W QW !' � �� � � �� � � �� � �� �

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

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

� �� & � �� ' � � �' � �� � �� � � &' � �� � � � � � � � � $%� � Q � � �

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

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

� � Z �� � �' � � � � � � �

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ � 27É J 5I8 ; 5 KI 3 4 <� � < �8 4 = A ^ �

� G � � F � � �� G � � G G � � �a ° � G G E � � H

O � � � � � Q � � � � � � �� � � � � $ � ! �] � � � � � � �� ! � � � � �� �� Q� � � � �� � �� �� � � � ! � �� �� + h �W

M � � � � � � � Q � N � � � � � � �� � � � �� &� �� � & !� & � � � N � � �

)� � � � � + - . � �� � � ! � *W M � � � $ $%' � ! �� � � � � & �� �� � �� $%� �� W

P &� � $ � � � � � � � Q � � ) !� W �� Q & � �� � � � � $%� � ! � � & � �

! �' ' � * �� � N � ! � � �� Q � � � � ! � � � �� �� � � �� � � N &� � �

� �� � � �W

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É J 5I8 ; 5 KI 3 4 <� � < �8 4 = A ^ A

Page 16: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

� G � � F � � �� G � � G G � � � ° � G G E � � L

� � �' � �� � � � �R � �� �� � � � �� ! � �� Q � � � � � Q � � ��

u & � & �

� � � � �' � �� � � � �# �� �d � � � � � Q � � � N &{ �� � � N � �� � � �� � � ! � �� �� � �� � � �� �� Q

� � � � � � � N � � N' � � � � � �� � � � � � � � � Q � � � � � X �� �� �[

� � �� � � � �� � � � � � Q � $ � � � �� Q � � $' � � �� � �� � � � � � � � �

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

� !' � �� � � � �� � �� �� � �� � !� � � � d � ( � � $%' � � � N �' ��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É J 5I8 ; 5 KI 3 4 <� � < �8 4 = A ^ B

� � � G F � G � � C � � °� G E � � � ° � � � � E� � G

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

$ �� � � �� �� � �� � � � � � � �� W

� # � � � � $ �� �� � �� � �� � � &' � �� �� � Q � �� � � � u & �� �

! � �� Q � � � � � �� � �� � & !� & � � � �� N �� � � � �� � � � � � �� � $ �� � � �� �� � � � � � � �W QW � �' �� �] ' � Ë � � �� �� �� ] ' � V � � � �Z N' � !Z � � �

� � � ��� N' � !Z ' � �� �� � � � � � � �] ' � V � �� � � �� � & $ �� � � �

� �� � !� � � W�W QW � � �� � �� � N � � � � $ �� �� � � � �W

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

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

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

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

� �] � � �d �� � � � �� � � !� � � � ! � � $%' �� � � � & ! ! � � �� &' ' �� �

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

� & �� N � � � �� � � � � � � � � � & Q � �� � � � � � � �� �� �� � � �W

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ Ø 4 3 9 K 3 : 5J8 9 K A ^ 6

Page 17: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

� � � � � � � � � � C � � °� G E � � � ° � � � � E� � G

� R � � � � � � � �� � �� ! �� ! & � � �� ! ! �� � � � ' ! �� N �

� � $%' � � �� � � � � � � � � �� Q ' � � $ �� �� � �� �� � � ��

� � � � � ) �W QW & � �� Q � � � � � $ � � � � *W

� f �� ! �� � � � �� ! � � $ � � �� � � $ �� �� � �� �d� �� � � �� � � � � � � �� � � ' � �� Q N �� � � � �� � � � � � �� � $ �� � � �� �� �

� � � � �

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

�W QW �� � � �� � � � � � d

A B

aSem.semWait

bSem.semWait

bSem.semWait

CR (for bSem)

aSem.semWait

CR (for aSem)

� O � � �� � � � � � �� �' ' � ! �� � � � � � & � ! � � � � � � � ! �' ' �� � � & N ¶ � !� � � � � � � ' � !Z W

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ Ø 4 3 9 K 3 : 5J8 9 K A ^ ^

� F �Ü � q ± ° � � ° � � � E � G � b �� � � G � � E� � G

� O � � � � ! �� d � � �X �' ' � � � �� � �� Q[ $� � $ �� � � �] � � �

� N � � �W

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

� � �� � � !� � �� � � � � � �� � � �� �� � �� ! �� � � �� �� �� �� �W

� + � � ' �� � � � d M � � �{ � !� � �� �� �� ! � � $' �� � � � �� � � !� � � �

�� � � �� � � � � N' � � � �� � �� ) ! �� ! & � � �� � * � � �� � � !� � �� W

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

) ! � � � �� � * �� � �{ � !� � � �' ' $ �� � � �� o � � �� �� � � �� � �� �

� & N � � u & �� � � �� � � � � �' & � � �W

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

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

/01 2 34 5 678 9 :; 4 4 < 9 : =78 9 548 >?@ Ø 4 3 9 K 3 : 5J8 9 K A ^ c

Page 18: A’s device signals · 2003-09-07 · q rts E b G system call system call I/O control language’s I/O interface language-independent OS I/O interface application program language

w � � �� � � b � � � � �

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

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

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

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

� �� ' � � �� �� � ! � � ! & � � �� � $%� � Q � � � �� � � ' � �� Q � � � �� �'

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

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

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

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

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

� � � � � ' � !Z �

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