shared buffer

22
slide 1 gaius Shared buffer laboratory 2 implements a shared buffer Read (VAR ch: CHAR) put character into buffer get character wait for keyboard int loop Process end Keyboard port shared by the application process Read (VAR ch: CHAR) ; share d by the dri ver process what are the problems with this? data might be altered by both processes at the same time data (buffer pointers) could become corrupt

Upload: jinal-dhobi

Post on 02-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 1/22

slide 1

gaius

Shared buffer

laboratory 2 implements a shared buffer

Read (VAR ch: CHAR)

put character into bufferget character

wait for keyboard int

loop

Process

end

Ke yboard

port

shared by the application process Read (VAR ch: CHAR) ;

shared by the driver process

what are the problems with this?

data might be altered by both processes at the same timedata (buffer pointers) could become corrupt

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 2/22

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 3/22

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 4/22

slide 4

gaius

Shared buffers

see BufferDevice.mod for a shared buffer data structure

we still have a problem to solve?

what two issues have not been discussed yet?

what happens if there is no data to take out?

there is no room left in the buffer?

if there is no data in the buffer and we attempt to get a datum then we

should wait until data arrives

if there is no space in the buffer and we attempt to put a datum then we

should wait until space available

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 5/22

slide 5

gaius

Shared buffers

we can implement this with two semaphores

ItemAvailable

SpaceAvailable

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 6/22

slide 6

gaius

Shared buffer (continued)

before we place an item into a buffer we must

Wait(SpaceAvailable)

before we extract an item from a buffer we must

Wait(ItemAvailable)

after we place an item into the buffer we must

Signal(ItemAvailable)

after we extract an item from the buffer we must

Signal(SpaceAvailable)

what are their initial values for an empty buffer? (size 3)

ItemAvailable 0SpaceAvailable 3

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 7/22

slide 7

gaius

Shared buffer (continued)

this buffer mechanism is known as Dijkstra’s bounded buffer after its

author E.W. Dijkstra who discovered the algorithm in 1960s

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 8/22

slide 8

gaius

Shared buffer (continued)

Wait(SpaceAvailable) Wait(ItemAvailable)

Signal(ItemAvailable) Signal(SpaceAvailable)

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 9/22

slide 9

gaius

Semaphores

the module Executive.mod in the handbook exports the:

type SEMAPHORE

procedures Wait and Signal

you can use these procedures to implement your bounded buffer for

laboratory 2

gaius

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 10/22

g

Circular buffer details

circular buffer manipulation

need to put a datum in at the front

need to extract a datum from the end

3

2 1

0

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 11/22

slide 10

slide 11

gaius

Circular buffer details

implement this with two indicesin and out

when we add a datum to the buffer we place it at position in. We

then increment in modulo the buffer size.

when we extract a datum from the buffer we extract it fromposition out. We then increment out modulo the buffer size.

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 12/22

slide 10

slide 12

gaius

Circular Buffer Code

when we add a datum to the circular buffer we:Buf[in] := ch ;

in := (in+1) MOD MaxBufferSize ;

when we extract a datum from a circular buffer we:

ch := Buf[out] ;

out := (out+1) MOD MaxBufferSize ;

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 13/22

slide 10

slide 13

gaius

Circular Buffer Code (continued)

in laboratory 2 you have write the procedure InitBuffer. It mustperform 3 operations:

it must create a buffer

initialize the buffer pointers to correct values

initialize the semaphore values

create the buffer.

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 14/22

slide 10

slide 14

gaius

Creating and initializing a buffer

the data structure Buffer is a pointer typeyou must make sure it points to something sensible!

to do this use the pseudo procedure NEW

NEW(b) ;

initialize the buffer pointers in and out

either

WITH bˆ DO

in := 0 ;

out := 0 ;

END

or alternatively

bˆ.in := 0 ;

bˆ.out := 0 ;

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 15/22

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 16/22

slide 10

slide 16

gaius

Circular Buffer Code (continued)

initialize semaphore valuesmust use procedure InitSemaphore from Executive.mod

to initialize semaphores!

example

WITH bˆ DOMutex :=

InitSemaphore(1, ’Mutex’) ;

END

you must initialize the two other semaphores

ItemAvailable

SpaceAvailable

to their respective values 0 and MaxBufferSize

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 17/22

slide 10

slide 17

gaius

Circular Buffer Code (continued)

so the InitBuffer code should look something like:

PROCEDURE InitBuffer () : Buffer ;

VAR

b: Buffer ;

BEGIN

NEW( b ) ;

WITH bˆ DO

ItemAvailable :=

InitSemaphore(0, ’ItemAvailable’) ;

SpaceAvailable :=

InitSemaphore(MaxBufferSize,

’SpaceAvailable’) ;

Mutex :=

InitSemaphore(1, ’Mutex’) ;

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 18/22

InitSemaphore(1, Mutex ) ;

in := 0 ;

out := 0

END ;

RETURN( b )

END InitBuffer ;

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 19/22

slide 10

slide 19

gaius

Device driver (re visited)

recall that the high level description of the device driver was:PROCEDURE DeviceDriver ;

BEGIN

(* mask processor *)

(* ints off *)TurnInterruptsOff ;

SetupDevice ;

EnableDeviceInterrupts ;LOOP

WaitForInterrupt(DeviceInterrupt) ;

ServiceDevice ;

Store or retrieve dataEND

END DeviceDriver

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 20/22

slide 10

slide 20gaius

Device driver (re visited)

the value of DeviceInterrupt is 021H for our microkernel

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 21/22

slide 10

slide 21

gaius

Laboratory 2

once you have completed your laboratory 2 you should be able to typecharacters and see them appear on the screen

two processes active on your microkernel

device driver ReadDriver

user code ass2.mod

remember that your device driver is responding to interrupts andsuccessfully placing characters into the shared buffer

the application process will extract characters from the buffer and

display them to the screen

7/27/2019 Shared Buffer

http://slidepdf.com/reader/full/shared-buffer 22/22

slide 10

slide 22

gaius

Laboratory 2

self check work 

describe major data structures being used by the device drivermodules - and what they provide

provide a commentary on how the microkernel is operating