CS 433 Operating Systems
Lab4
: Interprocess Synchronization by Semaphores
Out date :
In date :
1. Procedure: In this assignment you
will design and implement the following:
a) semaphore
object : It has allocated and value fields
(int) and a process queue
b) Semaphore table that has N_SEM
elements

b) Operations on the semaphore
object :
-int make_sem(int
count) : Allocates space for a semaphore in the semaphore table and initialises a semaphore to the count value. Returns the id of the semaphore made or error.
-int wait_sem(int sid): Decrements semaphore
count and blocks the caller if value < 0
-int signal_sem(int sid):
Increments semaphore count and unblocks the caller if value >= 0
-int del_sem(int sid) : deletes a semaphore
from the semaphore table
2. Test :
i.
Initialization : Make a pcb_table of 10 entries.
Make 2 system
processes called Producer and Consumer. Make a semaphore table of
10 semaphores. Make three semaphores called empty,
full, cs initialized to N_BUF, 0 and 1
respectively.
ii.
This
is the example in the
text book for producer/consumer problem. Producer process reads from a file
(sample.txt on the course homepage) and writes its data into a bounded buffer
of size 10 (N_BUF) , one character at a time,until
the end of the file.The Producer starts by checking the empty
semaphore to see if there is any space in the circular buffer. It then has to
perform writing to the buffer in a critical section guarded by the cs semaphore. Finally
it signals the Consumer process.
iii.
The
Consumer process does the reverse
things. It waits on the full semaphore to check if there is any available data.
It then reads from the buffer in a critical section. Normally, reading would
not require critical section but we are manipulating pointers (read and write).
It prints whatever it has read to the screen and then signals the Producer by the empty semaphore meaning
there is one more free buffer (the one just read).