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

Memory_Allocation_Programs_With_Comments

The document contains three C programs that implement different memory allocation strategies: First Fit, Best Fit, and Worst Fit. Each program prompts the user to input the number of memory blocks and processes, then allocates memory to processes based on the respective strategy. The allocation results are printed, showing which processes were allocated to which blocks or if they were not allocated at all.

Uploaded by

sheikhajalood
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)
6 views3 pages

Memory_Allocation_Programs_With_Comments

The document contains three C programs that implement different memory allocation strategies: First Fit, Best Fit, and Worst Fit. Each program prompts the user to input the number of memory blocks and processes, then allocates memory to processes based on the respective strategy. The allocation results are printed, showing which processes were allocated to which blocks or if they were not allocated at all.

Uploaded by

sheikhajalood
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

// First Fit Program with Comments

#include <stdio.h>

int main() {
int bsize[20], psize[20], allocation[20]; // Arrays to hold block sizes, process sizes, and al
int bno, pno, i, j;

printf("First Fit Memory Allocation\n");

// Input number of memory blocks


printf("Enter number of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\n");
for (i = 0; i < bno; i++)
scanf("%d", &bsize[i]);

// Input number of processes


printf("Enter number of processes: ");
scanf("%d", &pno);
printf("Enter size of each process:\n");
for (i = 0; i < pno; i++)
scanf("%d", &psize[i]);

// Initially mark all processes as not allocated


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

// Try to allocate each process to the first suitable block


for (i = 0; i < pno; i++) {
for (j = 0; j < bno; j++) {
if (bsize[j] >= psize[i]) { // If block can hold process
allocation[i] = j; // Allocate block j to process i
bsize[j] -= psize[i]; // Reduce available size in block
break; // Exit loop after first fit found
}
}
}

// Output allocation results


printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < pno; i++) {
printf("%d\t\t%d\t\t", i + 1, psize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1); // +1 for block numbering starting at 1
else
printf("Not Allocated\n");
}

return 0;
}
// Best Fit Program with Comments
#include <stdio.h>

int main() {
int bsize[20], psize[20], allocation[20];
int bno, pno, i, j, best;

printf("Best Fit Memory Allocation\n");

// Input memory blocks


printf("Enter number of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\n");
for (i = 0; i < bno; i++)
scanf("%d", &bsize[i]);

// Input processes
printf("Enter number of processes: ");
scanf("%d", &pno);
printf("Enter size of each process:\n");
for (i = 0; i < pno; i++)
scanf("%d", &psize[i]);

// Mark all processes as not allocated


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

// Best fit logic


for (i = 0; i < pno; i++) {
best = -1;
for (j = 0; j < bno; j++) {
if (bsize[j] >= psize[i]) { // Block can hold process
if (best == -1 || bsize[j] < bsize[best]) // Find block with smallest sufficient size
best = j;
}
}
if (best != -1) {
allocation[i] = best;
bsize[best] -= psize[i]; // Reduce block size
}
}

// Print result
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < pno; i++) {
printf("%d\t\t%d\t\t", i + 1, psize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}

return 0;
}
// Worst Fit Program with Comments
#include <stdio.h>

int main() {
int bsize[20], psize[20], allocation[20];
int bno, pno, i, j, worst;

printf("Worst Fit Memory Allocation\n");

// Input block details


printf("Enter number of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\n");
for (i = 0; i < bno; i++)
scanf("%d", &bsize[i]);

// Input process details


printf("Enter number of processes: ");
scanf("%d", &pno);
printf("Enter size of each process:\n");
for (i = 0; i < pno; i++)
scanf("%d", &psize[i]);

// Initialize allocations to -1
for (i = 0; i < pno; i++)
allocation[i] = -1;

// Worst fit logic


for (i = 0; i < pno; i++) {
worst = -1;
for (j = 0; j < bno; j++) {
if (bsize[j] >= psize[i]) {
if (worst == -1 || bsize[j] > bsize[worst]) // Find block with largest size
worst = j;
}
}
if (worst != -1) {
allocation[i] = worst;
bsize[worst] -= psize[i];
}
}

// Show final allocations


printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < pno; i++) {
printf("%d\t\t%d\t\t", i + 1, psize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy