Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views3 pages

Os Lab 12

Uploaded by

maliklinta0
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)
3 views3 pages

Os Lab 12

Uploaded by

maliklinta0
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/ 3

FATIMA JINNAH WOMEN UNIVERSITY

Department of Software Engineering


Operating system lab
lab 12

NAME: INSHAL NASIR


REG NO: 2023-BSE-038
SUBMITTED TO: Engr. Muhammad Shehzad

Task 1:
Implement banker algorithm.
CODE:
#include <stdio.h>
#include <stdbool.h>
void calculateNeed(int need[][10], int max[][10], int allocation[][10], int p, int r) {
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
bool isSafeState(int processes[], int available[], int max[][10], int allocation[][10], int p, int r) {
int need[10][10];
calculateNeed(need, max, allocation, p, r);
bool finish[10] = {false};
int safeSequence[10];
int work[10];
for (int i = 0; i < r; i++) {
work[i] = available[i];
}
int count = 0;
while (count < p) {
bool found = false;
for (int i = 0; i < p; i++) {
if (!finish[i]) {
bool canAllocate = true;
for (int j = 0; j < r; j++) {
if (need[i][j] > work[j]) {
canAllocate = false;
break;
}
}
if (canAllocate) {
for (int j = 0; j < r; j++) {
work[j] += allocation[i][j];
}
safeSequence[count++] = i;
finish[i] = true;
found = true;
}
}
}
if (!found) {
printf("System is not in a safe state.\n");
return false;
}
}
printf("System is in a safe state.\nSafe Sequence: ");
for (int i = 0; i < p; i++) {
printf("P%d ", safeSequence[i]);
}
printf("\n");
return true;
}
int main() {
int p, r;
printf("Enter the number of processes: ");
scanf("%d", &p);
printf("Enter the number of resource types: ");
scanf("%d", &r);
int processes[10];
for (int i = 0; i < p; i++) {
processes[i] = i;
}
int max[10][10], allocation[10][10], available[10];
printf("Enter the Max Matrix:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
scanf("%d", &max[i][j]:
}
}
printf("Enter the Allocation Matrix:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
scanf("%d", &allocation[i][j]);
}
}
printf("Enter the Available Resources:\n");
for (int i = 0; i < r; i++) {
scanf("%d", &available[i]);
}
isSafeState(processes, available, max, allocation, p, r);
return 0;
}
Output:

You might also like