In the readers-writers problem, we use three semaphore
variables:
- Semaphore S: This semaphore variable is used to achieve mutual
exclusion between processes. By using this variable, either readers
or writers will be allowed to use or access the shared buffer at a
particular time. This variable is set to 1 initially.
- Semaphore E: This semaphore variable is used to define the
empty space in the buffer. Initially, it is set to the whole space
of the buffer i.e. "n" because the buffer is initially empty.
- Semaphore F: This semaphore variable is used to define the
space that is filled by the readers. Initially, it is set to "0"
because there is no space filled by the readers initially.
By using the above three semaphore variables and by using the
wait() and signal() function, we can solve our problem(the wait()
function decreases the semaphore variable by 1 and the signal()
function increases the semaphore variable by 1). So. let's see
how.
The following is the pseudo-code for the readers:
void readers() {
while(T) {
produce()
wait(E)
wait(S)
append()
signal(S)
signal(F)
}
}
The above code can be summarized as:
- while() is used to produce data, again and again, if it wishes
to produce, again and again.
- produce() function is called to produce data by the
readers.
- wait(E) will reduce the value of the semaphore variable "E" by
one i.e. when the readers produces something then there is a
decrease in the value of the empty space in the buffer. If the
buffer is full i.e. the vale of the semaphore variable "E" is "0",
then the program will stop its execution and no production will be
done.
- wait(S) is used to set the semaphore variable "S" to "0" so
that no other process can enter into the critical section.
- append() function is used to append the newly produced data in
the buffer.
- signal(s) is used to set the semaphore variable "S" to "1" so
that other processes can come into the critical section now because
the production is done and the append operation is also done.
- signal(F) is used to increase the semaphore variable "F" by one
because after adding the data into the buffer, one space is filled
in the buffer and the variable "F" must be updated.
This is how we solve the produce part of the readers-writers
problem. Now, let's see the writers solution. The following is the
code for the writers:
void writers() {
while(T) {
wait(F)
wait(S)
take()
signal(S)
signal(E)
use()
}
}
The above code can be summarized as:
- while() is used to consume data, again and again, if it wishes
to consume, again and again.
- wait(F) is used to decrease the semaphore variable "F" by one
because if some data is consumed by the writers then the variable
"F" must be decreased by one.
- wait(S) is used to set the semaphore variable "S" to "0" so
that no other process can enter into the critical section.
- take() function is used to take the data from the buffer by the
writers.
- signal(S) is used to set the semaphore variable "S" to "1" so
that other processes can come into the critical section now because
the consumption is done and the take operation is also done.
- signal(E) is used to increase the semaphore variable "E" by one
because after taking the data from the buffer, one space is freed
from the buffer and the variable "E" must be increased.
- use() is a function that is used to use the data taken from the
buffer by the process to do some operation.