Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ASSIGNMENT 2

CSE 323.1

TAZWAR NOOR ADIB BHUIYAN


193 1835 642

1. Write and Explain the producer-consumer Problem in your own words including the
algorithms. Provide a Sample Simulation.

Answer : Solution to producer consumer problem for the algorithm, we will take a buffer of
size, n, e.g. n=10. We will use a binary semaphore, bs-1 (initial). There will be two counting
semaphores, emptyslots and fullslots. Since the buffer is empty so it sets the value for emoty
slots =10 and fullslots =0. The codes for producer and consumer are shown below :

1.Producer(items_produced); 1.Consumer(items_consumed);
2.down(emptyslots); 2.down(fullslots);
3.down(bs); 3.down(bs);
4. buffer[in] = items_produced; 4. item_consumed=buffer[out];
5. in = (in+1)%n; 5. out=(out+1)%n;
6.up(bs); 6. up(bs);
7.up(fullslots); 7.up(empty);

Critical section : line 4 and 5 both parts

In this problem it is producer produces a item and a consumer picks the item and consume it.
Both of this things cannot take place simultaneously, since they are using the same fixed
sized buffer. When a producer is producing an item the consumer can not take that on the
same time. So this algorithm is developed to solve the problem. Starting with the producer,
line 2 it reduces the emptyslots’ initial value to 9 line 3 makes bs = 0, then enters critical
section, ‘in’ the empty slot where item can be stored, initially 0, it becomes 1. While the
producer is in the critical section lets see the consumer’s code, line 2, fullslots becomes -1 by
value from 0, line 3 is not executable since a binary semaphore, it is zero already we cannot
apply down function to it. So here we can see the producer is in the critical section, consumer
cannot enter it. Then coming back to producer, bs becomes 1, and count of fullslots increases
meaning 1 slot of buffer is accommodated. Coming back to consumer now, down of the full
slots sets the value from 1 to 0. Then down(bs) meaning bs = 0 so it can now enter the cirtical
section. ‘Out’ is the slot to be emptied, which is taken by the consumer. While consumer is in
critical section producer cannot enter, same reason bs =0 and it can’t be decreased any
further.The code then ends by increasing bs to 1 again that means the producer has a free
space in the critical section and it can be accessed.
2. Write and Explain Readers-Writers problems in your own words including the algorithms.
Explain each of the use cases.

Answer : In the readers writers problem the highlighted issues are the reader and writer are
sharing a database. So firstly, when a reader is trying to access the same data when a writer
is writing in that database. Secondly when a reader is reading and some writer is trying to
access that database thirdly when a writer is already writing and writer wants to access the
database. Fourth and last when a reader is reading another reader is reading the database.
Here since the writer and the reader are sharing the same database there are some problems,
for the first three cases they are contradicting. For the first and second cases either the reader
is reading or a writer is writing, at the same time if a writer or a reader wants to access the
database it is not possible, for the third case when a writer is writing and another writer wants
to write again in the same database it is not possible as it is contradicting, two writers can not
write in the same database simultaneously. The fourth case is fine because readers are not
making any changes in the database. So to solve this problems, algorithm it setup in the
manner shown below we will take two semaphores one is mutex and other is database
integer variable countreader. Lets take mutex=1, database = 1, countreader=0.

Reader()
{
While(true)
{
Down(mutex);
countreader = countreader +1;
if (countreader == 1)
then down(database);
up (mutex);

Access
Critical Section {database
down(mutex);
countreader = countreader – 1;
if (countreader == 0)
then up(database);
up(mutex);

Section {Process
data

}
}
Writer()
{
While(true)
{
Down(database);
Critical Section { Access database
Up (database);
}
}
1st case (problem) : When a reader is reading the database and a writer arrives to write in the
database

So starting from the first part of the code, initially there is no reader, as soon as a reader
arrives the code for reader() starts executing. Down of mutex changes the value from 1 to 0.
Countreader count increases to 1, since it is equal to 1 the next line of the code is executed
that is down of database changing the value of the semaphore from 1 to 0.Then. the mutex
value rises to 1 and the reader gets access to the database. So keeping the reader in the
database lets see what happens when a writer arrives, the code for writer() starts to execute.
The first line down of database is not possible as it is already 0, so writer is unable to enter
the critical section, that is the database. This shows that when writer arrives while a reader is
reading the database it is inaccessible.

2nd case (problem) : When a writer is writing in the database and a reader arrives to read from
the database

So as soon as the writer arrives the execution of the writer() code starts, here then it causes
down of database changing the value from 1 to 0. Then the writer enters the critical section.
So now we need to check what happens a reader arrives in this time, as soon as the reader
arrives the execution of the writer() code starts. Firstly down of mutex causes the value of
mutex to change from 1 to 0, then the next line countreader count increases from 0 to 1,
Now since the countreader is equal to 1 it will now try to down the database value but it is not
possible since the binary semaphores value is already 0 it can not execute. So the code is
halted and the reader can not access the database.

3rd case (problem) : When a writer is writing in the database and another writer arrives at the
same time

So as first writer arrives the code for witter() start to execute, it causes down of database then
get access to the data base to write in it. At the same time when another writer arrives the
code writer() has to be executed again then we can see the execution is halted as the database
is a binary semaphore and the value of it is already zero. It can not decrease below that. So
the second writer cannot access the the database while the first writer is writing.

4th case (no problem) : when a reader is reading from the database and another reader arrives.

When first reader arrives the execution of reader() code, so firstly down of mutex changes the
value from 1 to 0, then countreader value becomes 1, then it cause the value of database to
down to 0 from 1, then the mutex value becomes 1 as we reach the database by line by line
execution for the first writer. Then as it is reading from the database we need to see what
happens when another reader arrives, again execution of the code reader(), down mutex
changes value from 1 to 0 then countreader value becomes 2 as initially it was 1. The next
line does not work as countreader is not equal to 1 then next line executes up of mutex from 0
to 1 again and second reader get access to the database without any hesitations.

For cases 1,2 and three let us execute the codes further. Starting with reader() as soon as the
reader finishes reading the datas it executes the next line of the code down of mutex then
countreader becomes 0, it changes the value of database to 1 again then mutex value again
rises and data is processed, meaning the reader left the critical section. Now if the writer
comes it ca execute writer() code without any hesitation. Changing the value of binary
semaphores of database from 1 to 0 and accessing the database. Then again when it finishes
its work in the data base it ups the value of database to 1 again now when a reader arrives it
can execute the reader() code without any hesitations. Hence solving the problem of reader
writer, with an excellent algorithm.

3.Write and Explain Bankers Algorithm with a sample simulation. Please elaborate on why the
banker's algorithm is used. 

Answer : Bankers algorithm is also known as deadlock avoidance algorithm, because to avoid
deadlock we have to tell the system beforehand which processes are coming, what the
processes will request for which resources and for how many time will it will require the
resources etc. So the system works smoothly and the processes are all in safe state. This
algorithm is also used to identify if there is any possibility of a deadlock to take place in the
future since the process along with requirements of the resourses and how much are they
required or for how much time.

Process Allocation Max Available Need


A B C A B C A B C A B C
P1 1 2 1 8 6 4 4 4 3 7 4 3
P2 3 1 1 4 3 3 7 5 4 1 2 2
P3 4 1 3 10 1 3 11 6 7 6 0 0
P4 2 1 1 2 2 2 13 7 8 0 1 1
P5 0 0 2 4 3 3 13 7 10 0 0 1
n.b. This table is taken from quiz question

Allocations tells us the number of the resources are are already allocated for each process,
Max tells us how many of the total resources are required for the process to terminate or to
finish its execution. Available tells us about the total available resources the system has
minus the total allocation of all the processes, the current available resources. Need tells us
the need of resources for each processes that is the value of Max minus the Allocation of a
process individually, this will let us know how much resources each of the processes requires
for the completion of the process.

By doing all the calculations accordingly we can complete the table and while solving then
table we have to identify the sequence that is executable meaning the safe sequence by
comparing the need and available resources. For the table above the Safe
sequence<P2,P3,P4,P5,P1> . We can directly find out since for all the requirement of the
resources it is available in due time or else the sequence could have been changed
accordingly, no deadlock could be seen, but if there were processes that required more
resources the system is able to provide after completion of a certain process then there is no
safe sequence the system will reach deadlock and it will not be able to execute at any cost.

Bankers algorithm is named as such as it used mostly in the banking system to avoid any type
of deadlock, this could be used to identify if a loan should be processed for some one or not.
This algorithm is used to manage resources, identify requirements for the resources and time
period that resource require, so if a bank has sufficient amount available and according to the
need of the customers if they can see that they can manage all the customers the loan is
processed. The process are considered as customers as the table provided above and all the
calculations are same as above. This algorithm gives the safest possibilities to execute
process and helps avoiding the deadlock that can happen in a system.

You might also like