0% found this document useful (0 votes)
80 views8 pages

Producer-Consumer ,: The Reader-Writer Problems and Solving Them by Semaphore

The document discusses two classic synchronization problems - the producer-consumer problem and the reader-writer problem - and their solutions using semaphores. For the producer-consumer problem, it describes a producer that generates data for a fixed-size buffer and a consumer that takes data from the buffer. The key is ensuring the producer only adds data when space is available and the consumer only removes data when data exists. This is achieved using semaphores for empty space, full space, and mutual exclusion. For the reader-writer problem, it allows multiple readers or a single writer but not a reader and writer concurrently. Semaphores for mutual exclusion on writing and a count of readers are used to control access for readers

Uploaded by

UIoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views8 pages

Producer-Consumer ,: The Reader-Writer Problems and Solving Them by Semaphore

The document discusses two classic synchronization problems - the producer-consumer problem and the reader-writer problem - and their solutions using semaphores. For the producer-consumer problem, it describes a producer that generates data for a fixed-size buffer and a consumer that takes data from the buffer. The key is ensuring the producer only adds data when space is available and the consumer only removes data when data exists. This is achieved using semaphores for empty space, full space, and mutual exclusion. For the reader-writer problem, it allows multiple readers or a single writer but not a reader and writer concurrently. Semaphores for mutual exclusion on writing and a count of readers are used to control access for readers

Uploaded by

UIoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Producer-Consumer , The Reader-Writer Problems

And solving them by semaphore

: ‫إعداد‬
‫محمد محمد جمعة محمد شبانة‬
) 3 ‫( سكشن‬

‫ صالح شعبان‬/ ‫ د‬: ‫إشراف‬


Producer-Consumer problem

It’s a is used for multi-process synchronization between more than


one processes.
there is one Producer that is producing something and there is one
Consumer that is consuming the products produced by the Producer.
The producers and consumers share the same memory buffer that is
of fixed size.
The Producer function is to generate the data and putting it into the
buffer then again start generating data. While the job of the
Consumer is to consume the data from the buffer.
So what’s the problem?
• Producer should produce data only when the buffer is not full. If
the buffer is full, then the producer shouldn't be allowed to put
any data into the buffer.
• The consumer should consume data only when the buffer is not
empty. If the buffer is empty, then the consumer shouldn't be
allowed to take any data from the buffer.
• Producer and consumer shouldn’t access the buffer at the same
time.

The Solutions
we use three semaphore variables

1. Semaphore S: is used to achieve mutual exclusion between


processes by using this variable, either Producer or Consumer
will be allowed to use or access the shared buffer at a particular
time. This variable is set to 1 initially.
2. Semaphore E: 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.

3. Semaphore F: is used to define the space that is filled by the


producer. Initially, it is set to "0" because there is no space filled
by the producer initially.

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.

Procedure code :

void producer() {
while(T) {
produce()
wait(E)
wait(S)
append()
signal(S)
signal(F)
}
}

wait(E) —› reduce the value of the semaphore "E" by one.


wait(S) —› set the semaphore variable "S" to "0" so that no other
process can enter into the critical section.

signal(s) —› setting the semaphore variable "S" to "1”.

signal(F) —› increase the semaphore variable "F" by 1.

This is how we solve the produce part of the producer-consumer


problem.

Consumer Code :

void consumer() {
while(T) {
wait(F)
wait(S)
take()
signal(S)
signal(E)
use()
}
}
signal(E) —› increase the semaphore variable "E" by one.

Use() is a function that is used to use the data taken from the buffer
by the process to do some operation.

This is how we can solve the producer-consumer problem.


The Reader-Writer Problem

we deal with various processes and these processes may use files that
are present in the system. Basically, we perform two operations on a
file “read and write”. All these processes can perform these two
operations. But the problem that arises here is that:

▪ If a process is writing something on a file and another process


also starts writing on the same file at the same time, then the
system will go into the inconsistent state. Only one process
should be allowed to change the value of the data present in the
file at a particular instant of time.
▪ Another problem is that if a process is reading the file and
another process is writing on the same file at the same time ,
this may lead to dirty-read because the process writing on the
file will change the value of the file, but the process reading that
file will read the old value present in the file.

The Solutions
By using semaphore, we can solve these problems but

➢ If a process is performing some write operation, then no


other process should be allowed to perform the read or the
write operation.

➢ If a process is performing some read operation only, then


another process that is demanding for reading operation
should be allowed to read the file and get into the critical
section
➢ But if a process is reading a file and another process is
demanding for the write operation, then it should not be
allowed.

Using semaphore “writer” and semaphore “mutex” to solve the


Problems:

• Semaphore "writer": is used to achieve the mutual


exclusion property. It is used by the process that is writing in
the file and it ensures that no other process should enter the
critical section at that instant of time. Initially, it will be set to
"1".

• Semaphore "mutex": is used to achieve mutual exclusion


during changing the variable that is storing the count of the
processes that are reading a particular file. Initially, it will be set
to "1".

we have one variable readerCount that will have the count of the
processes that are reading a particular file. The readerCount variable
is initially initialized to 0.

Also we going to use two function wait () and signal ().

The wait () function is used to reduce the value of a semaphore


variable by one.

The signal () function is used to increase the value of a semaphore


variable by one.

wait(writer)
...
write operation
...

signal(writer)
• The wait(writer) function is called so that it achieves the mutual
exclusion. The wait () function will reduce the writer value to "0"
and this will block other processes to enter into the critical section.
• The write operation will be carried and finally,
the signal(writer) function will be called, and the value of
the writer will be again set to "1" and now other processes will be
allowed to enter into the critical section.

Ex:

wait(mutex) |
readerCount++ |
if(readerCount == 1) |--- changing the readerCount
wait(writer) |
signal(mutex)

...
read operation

wait(mutex)
readerCount-- |
if(readerCount == 0) |--- changing the readerCount
signal(writer) |
signal(mutex)

• mutex variable used to change something in


the readerCount variable and to achieve mutual exclusion.
• Initially, we are calling the wait(mutex) function, and this will
reduce the value of the mutex by one. After that,
the readerCount value will be increased by one.
• If the readerCount variable is equal to "1" i.e. the reader process is
the first process, in this case, no other process demanding for write
operation will be allowed to enter into the critical section. So,
the wait(writer) will be called and the value of the writer variable
will be decreased to "0" and no other process demanding for write
operation will be allowed.
• After changing the readerCount variable, the value of
the mutex variable will be increased by one, so that other processes
should be allowed to change the value of the readerCount value.
• The read operation by various processes will be continued and
after that when the read operation is done, then again, we have to
change the count the value of the readerCount and decrease the
value by one.
• If the readerCount becomes "0", then we have to increase the
value of the writer variable by one by calling
the signal(writer) function. This is done because if
the readerCount is "0" then other writer processes should be
allowed to enter into the critical section to write the data in the
file.

You might also like