OS Assignement
OS Assignement
Student ID : 11808797
Email Address : [email protected]
Code : C program for finding if the system is safe or not.
#include <stdio.h>
#include <conio.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
getch();
}
o Solution:
2. The algorithm for proposed solution of the assigned problem.
Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i = 1,2,3,4…n
Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step(4)
Work = Work + Allocation[i]
Finish[i] = true
goto step(2)
if Finish [i] = true for all i
then the system is in a safe state
3. Complexity of implemented algorithm.
Complexity for finding whether the system is safe state or not is = O(n*n*m), where n = number
of processes and m = number of resources.
4. Constraints Used:
Allocation: indicates where process you have received a resource.
Need : Express how many more resources can be allocated in future.
Available : Indicates which resource is available.
Max :Expression of the maximum number of resources.
Boundary Conditions:
o Like the other algorithms, the Banker's algorithm has some limitations when
implemented.
o Specifically, it needs to know how much of each resource a process could possibly
request. In most systems, this information is unavailable, making it impossible to
implement the Banker's algorithm.
o Moreover, the requirement that a process will eventually release all its resources
(when the process terminates) is sufficient for the correctness of the algorithm,
however it is not sufficient for a practical system. Waiting for hours (or even
days) for resources to be released is usually not acceptable.
Test Cases:
o Case 1:
R1 R2 R3 P1 0 1 0 7 5 3
10 5 7 P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
Need
R1 R2 R3
Safe sequences are: P2 P4 P1 P3
o Case 2:
R1 R2 R3 R4 P1 4 0 0 1 6 0 1 2
3 2 1 1 P2 1 1 0 0 2 7 5 0
P3 1 2 5 4 2 3 5 6
P4 0 6 3 3 1 6 5 3
P5 0 2 1 2 1 6 5 6
Need
R1 R2 R3 R4
1 0 2 0
1 4 4 4