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

Os 9

The document contains a C program that implements three memory allocation strategies: First Fit, Best Fit, and Worst Fit. It allows users to input memory block sizes and process sizes, and then choose an allocation strategy to see how processes are allocated to memory blocks. The program outputs the allocation results for each strategy, indicating whether each process was allocated or not.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Os 9

The document contains a C program that implements three memory allocation strategies: First Fit, Best Fit, and Worst Fit. It allows users to input memory block sizes and process sizes, and then choose an allocation strategy to see how processes are allocated to memory blocks. The program outputs the allocation results for each strategy, indicating whether each process was allocated or not.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <limits.h>

void firstFit(int blockSize[], int blocks, int processSize[], int processes) {


int allocation[processes];
for (int i = 0; i < processes; i++) allocation[i] = -1;

for (int i = 0; i < processes; i++) {


for (int j = 0; j < blocks; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}

printf("\nFirst Fit Allocation:\n");


printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < processes; 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");
}
}

void bestFit(int blockSize[], int blocks, int processSize[], int processes) {


int allocation[processes];
for (int i = 0; i < processes; i++) allocation[i] = -1;

for (int i = 0; i < processes; i++) {


int bestIdx = -1;
for (int j = 0; j < blocks; 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("\nBest Fit Allocation:\n");


printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < processes; 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");
}
}

void worstFit(int blockSize[], int blocks, int processSize[], int processes) {


int allocation[processes];
for (int i = 0; i < processes; i++) allocation[i] = -1;

for (int i = 0; i < processes; i++) {


int worstIdx = -1;
for (int j = 0; j < blocks; 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("\nWorst Fit Allocation:\n");


printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < processes; 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[20], processSize[20];
int m, n, choice;

printf("Enter number of memory blocks: ");


scanf("%d", &m);
printf("Enter size of each memory block:\n");
for (int i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}

printf("Enter number of processes: ");


scanf("%d", &n);
printf("Enter size of each process:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
}

do {
printf("\nChoose Allocation Strategy:\n");
printf("1. First Fit\n2. Best Fit\n3. Worst Fit\n4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);

int tempBlockSize[20];
for (int i = 0; i < m; i++) tempBlockSize[i] = blockSize[i];

switch (choice) {
case 1:
firstFit(tempBlockSize, m, processSize, n);
break;
case 2:
for (int i = 0; i < m; i++) tempBlockSize[i] = blockSize[i];
bestFit(tempBlockSize, m, processSize, n);
break;
case 3:
for (int i = 0; i < m; i++) tempBlockSize[i] = blockSize[i];
worstFit(tempBlockSize, m, processSize, n);
break;
case 4:
printf("Exiting.\n");
break;
default:
printf("Invalid Choice!\n");
}
} while (choice != 4);

return 0;
}

You might also like