Os 6-9
Os 6-9
#include <stdio.h>
#include <stdbool.h>
#define P 5
#define R 3
void calculateSafeSequence(int processes[], int avail[], int max[][R], int allot[][R]) {
int need[P][R], finish[P], safeSequence[P], work[R];
int count = 0;
for (int i = 0; i < P; i++) {
finish[i] = 0;
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allot[i][j];
}
}
for (int i = 0; i < R; i++) {
work[i] = avail[i];
}
while (count < P) {
bool found = false;
for (int p = 0; p < P; p++) {
if (!finish[p]) {
int j;
for (j = 0; j < R; j++) {
if (need[p][j] > work[j]) break;
}
if (j == R) {
for (int k = 0; k < R; k++) {
work[k] += allot[p][k];
}
safeSequence[count++] = p;
finish[p] = 1;
found = true;
break;
}
}
}
if (!found) {
printf("System is not in a safe state\n");
return;
}
}
printf("Safe Sequence: ");
for (int i = 0; i < P; i++) {
printf("%d ", safeSequence[i]);
}
printf("\n");
}
int main() {
int processes[] = {0, 1, 2, 3, 4};
int avail[] = {3, 3, 2}; // Available instances of resources
int max[][3] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3} };
int allot[][3] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};
OUTPUT:
Safe Sequence:
1
3
4
0
2
PROGRAM:
include <stdio.h>
#define P 3
#define R 3
void detectDeadlock(int allocation[][R], int max[][R], int avail[]) {
int need[P][R], work[R], finish[P];
int deadlock = 0;
for (int i = 0; i < P; i++) {
finish[i] = 0;
}
for (int i = 0; i < R; i++) {
work[i] = avail[i];
}
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
for (int i = 0; i < P; i++) {
if (!finish[i]) {
int canAllocate = 1;
for (int j = 0; j < R; j++) {
if (need[i][j] > work[j]) {
canAllocate = 0;
break;
}
}
if (canAllocate) {
for (int j = 0; j < R; j++) {
work[j] += allocation[i][j];
}
finish[i] = 1;
}
else {
deadlock = 1;
}
}
}
if (deadlock) {
printf("Deadlock detected!\n");
} else {
printf("No deadlock detected.\n");
}
}
int main() {
int allocation[][3] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2}
};
int max[][3] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2}
};
int avail[] = {3, 3, 2};
detectDeadlock(allocation, max, avail);
return 0;
}
OUTPUT:
No deadlock detected.
Hence the program is executed
PROGRAM:
#include <stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1);
} else {
printf("Not Allocated\n");
}
}
}
int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
firstFit(blockSize, m, processSize, n);
return 0;
}
OUTPUT:
Process No. Process Size Block No.
1 212 1
2 417 3
3 112 2
4 426 4
PROGRAM:
#include <stdio.h>
void worstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
int worstIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx]) {
worstIdx = j;
}
}
}
if (worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1);
} else {
printf("Not Allocated\n");
}
}
}
int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
worstFit(blockSize, m, processSize, n);
return 0;
}
OUTPUT:
Process No. Process Size Block No.
1 212 2
2 417 5
3 112 3
4 426 4
PROGRAM:
#include <stdio.h>
void bestFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
int bestIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx]) {
bestIdx = j;
}
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1);
} else {
printf("Not Allocated\n");
}
}
}
int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
bestFit(blockSize, m, processSize, n);
return 0;
}
OUTPUT:
OUTPUT:
Page: 7, Frames: 7 -1 -1
Page: 0, Frames: 7 0 -1
Page: 1, Frames: 7 0 1
Page: 2, Frames: 2 0 1
Page: 0, Frames: 2 0 1
Page: 3, Frames: 3 0 1
Page: 0, Frames: 3 0 1
Page: 4, Frames: 4 0 1
Page Faults: 5
Hit: 1