# CS604 Assignment No 4 Spring 2012 solution

Question 1:
Suppose we have a system with three processes P3, P4 and P5 and ten resources of the same type (e.g. magnetic
tapes). The three processes have declared maximum simultaneous resources requirements and current allocation
as given in the following table.
Process Max Need Current Usage
P3 8 3
P4 5 1
P5 8 2
Available resources = 4

What is the sequence of allocations to each process (up to their maximum needs) which will allow all processes

to finish in a safe state?

 Process Max Need current Usage P3 8 3 P4 5 1 P5 8 2 Available=4

1st we determine the Need   for each process.

Max need = Need +Allocated

Need  = Max Need – Allocated

 Process Max Need Current usage Need = Max Need – current usage p3 8 3 5 p4 5 1 4 p5 8 2 6

Now our table will become such as

 Process Current usage Need Available P3 3 5 P4 1 4 P5 2 6

In 1ST ITERATION

The need of process P4 is

(Need P4) 4 ≤   4 (Available)

 Process Current usage Need Available P3 3 5 5 P4 1 4 P5 2 6

Safe sequence <P4>

In 2nd iteration

(Need of P3) 5 ≤ 5(Available)

 Process Current usage Need Available P3 3 5 5 P4 1 4 8 P5 2 6

Safe sequence <P4,P3>

In 3rd Iteration

(Need of p5) 6 ≤ 8(Available)

 Process Current usage Need Available P3 3 5 5 P4 1 4 8 P5 2 6 10

Question 2:

Write a code or pseudo code by using monitor
i) To deposit money in a bank account
ii) To withdraw money from a bank account

Solution:

Sample code for monitor class in standard C

monitor class Account {

private int balance = 0
invariant balance >= 0

public method boolean withdraw(int amount)
precondition amount >= 0
{
if balance < amount then return false
else { balance = balance – amount ; return true }
}

public method deposit(int amount)
precondition amount >= 0
{
balance = balance + amount
}
}

.