0% found this document useful (0 votes)
47 views6 pages

OS Assignement

1. The document describes an algorithm to check if a system is in a safe state using the Banker's algorithm. It involves maintaining matrices to track the maximum, allocated, and remaining need of resources for each process. 2. The algorithm initializes available resources and marks all processes as unfinished. It finds a process where remaining need is less than or equal to available resources. If none exist, the system is unsafe. Otherwise, it allocates resources to the process and marks it as finished. 3. The time complexity of the algorithm is O(n^2m) where n is the number of processes and m is the number of resources. It ensures processes will eventually release all allocated resources when finished.

Uploaded by

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

OS Assignement

1. The document describes an algorithm to check if a system is in a safe state using the Banker's algorithm. It involves maintaining matrices to track the maximum, allocated, and remaining need of resources for each process. 2. The algorithm initializes available resources and marks all processes as unfinished. It finds a process where remaining need is less than or equal to available resources. If none exist, the system is unsafe. Otherwise, it allocates resources to the process and marks it as finished. 3. The time complexity of the algorithm is O(n^2m) where n is the number of processes and m is the number of resources. It ensures processes will eventually release all allocated resources when finished.

Uploaded by

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

Student Name : Sahil Patial

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;

printf("Enter the no of processes : ");


scanf("%d", &p);

for(i = 0; i< p; i++)


completed[i] = 0;

printf("\n\nEnter the no of resources : ");


scanf("%d", &r);

printf("\n\nEnter the Max Matrix for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}

printf("\n\nEnter the allocation for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}

printf("\n\nEnter the Available Resources : ");


for(i = 0; i < r; i++)
scanf("%d", &avail[i]);

for(i = 0; i < p; i++)


for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];

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;

for(i = 0; i < p; i++)


{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}

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();
}

1. Problem in terms of operating system concept.


The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for
safety by simulating the allocation for predetermined maximum possible amounts of all
resources, then makes an “s-state” check to test for possible activities, before deciding whether
allocation should be allowed to continue.

 Question given to me:

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  Also, it is unrealistic to assume that the number of processes is static since in


most systems the number of processes varies dynamically.

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:

Available Processes Allocation Max


R1 R2 R3 R1 R2 R3

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

P2 P4 P3 P1


7 4 3
P4 P2 P1 P3
1 2 2
P4 P2 P3 P1
6 0 0
There are total 4 safe-sequences.
0 1 1

o Case 2:

Available Processes Allocation Max


R1 R2 R3 R4 R1 R2 R3 R4

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

YES , system is in safe state.


2 0 1 1
Safe Sequence:
0 6 5 0
P0 P2P3P4P1
1 1 0 2

1 0 2 0

1 4 4 4

You might also like