Shared Buffer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

slide 1 gaius

Shared buffer

laboratory 2 implements a shared buffer

Keyboard port

Process loop wait for keyboard int get character put character into buffer end

Read (VAR ch: CHAR)

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 time data (buffer pointers) could become corrupt

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

slide 5 gaius

Shared buffers

we can implement this with two semaphores ItemAvailable SpaceAvailable

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 0 SpaceAvailable 3

slide 7 gaius

Shared buffer (continued)

this buffer mechanism is known as Dijkstras bounded buffer after its author E.W. Dijkstra who discovered the algorithm in 1960s

slide 8 gaius

Shared buffer (continued)

Wait(SpaceAvailable)

Wait(ItemAvailable)

Signal(ItemAvailable)

Signal(SpaceAvailable)

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

Circular buffer details

circular buffer manipulation need to put a datum in at the front need to extract a datum from the end

slide 10 slide 11 gaius

Circular buffer details

implement this with two indices in 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 from position out. We then increment out modulo the buffer size.

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 ;

slide 10 slide 13 gaius

Circular Buffer Code (continued)

in laboratory 2 you have write the procedure InitBuffer. It must perform 3 operations: it must create a buffer initialize the buffer pointers to correct values initialize the semaphore values create the buffer.

slide 10 slide 14 gaius

Creating and initializing a buffer

the data structure Buffer is a pointer type you 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 ;

slide 10 slide 16 gaius

Circular Buffer Code (continued)

initialize semaphore values must use procedure InitSemaphore from Executive.mod to initialize semaphores! example WITH b DO Mutex := InitSemaphore(1, Mutex) ; END you must initialize the two other semaphores ItemAvailable SpaceAvailable to their respective values 0 and MaxBufferSize

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) ; in := 0 ; out := 0 END ; RETURN( b ) END InitBuffer ;

slide 10 slide 19 gaius

Device driver (revisited)

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 data END

END DeviceDriver

slide 10 slide 20 gaius

Device driver (revisited)

the value of DeviceInterrupt is 021H for our microkernel

slide 10 slide 21 gaius

Laboratory 2

once you have completed your laboratory 2 you should be able to type characters 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 and successfully placing characters into the shared buffer the application process will extract characters from the buffer and display them to the screen

slide 10 slide 22 gaius

Laboratory 2

self check work describe major data structures being used by the device driver modules - and what they provide provide a commentary on how the microkernel is operating

You might also like