0% found this document useful (0 votes)
10 views14 pages

Os 6-9

The document contains multiple C programs demonstrating various algorithms for resource management in operating systems. It includes implementations for calculating safe sequences, detecting deadlocks, and memory allocation strategies (first fit, worst fit, best fit) as well as a page replacement algorithm. Each program outputs relevant results based on the input data provided.

Uploaded by

M
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)
10 views14 pages

Os 6-9

The document contains multiple C programs demonstrating various algorithms for resource management in operating systems. It includes implementations for calculating safe sequences, detecting deadlocks, and memory allocation strategies (first fit, worst fit, best fit) as well as a page replacement algorithm. Each program outputs relevant results based on the input data provided.

Uploaded by

M
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/ 14

PROGRAM:

#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}
};

calculateSafeSequence(processes, avail, max, allot);


return 0;
}

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:

Process No. Process Size Block No.


1 212 2
2 417 5
3 112 3
4 426 4
PROGRAM:
include <stdio.h>
#define MAX_PAGES 4
#define MAX_FRAMES 3
void pageReplacement(int pages[], int n, int frames[], int m) {
int pageFaults = 0, hit = 0;
for (int i = 0; i < n; i++) {
int page = pages[i];
int found = 0;
for (int j = 0; j < m; j++) {
if (frames[j] == page) {
found = 1;
hit = 1;
break;
}
}
if (!found) {
pageFaults++;
frames[i % m] = page; // Replace page using FIFO
}
printf("Page: %d, Frames: ", page);
for (int j = 0; j < m; j++) {
printf("%d ", frames[j]);
}
printf("\n");
}
printf("\nPage Faults: %d\n", pageFaults);
printf("Hit: %d\n", hit);
}
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4};
int frames[MAX_FRAMES] = {-1, -1, -1};
pageReplacement(pages, 8, frames, MAX_FRAMES);
return 0;
}

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

You might also like