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

09 - Memory Management

The document contains three C programs that implement memory allocation strategies: First Fit, Best Fit, and Worst Fit. Each program prompts the user to input block sizes and process sizes, then allocates memory based on the chosen strategy and displays the allocation results. The programs utilize arrays to manage block and process sizes, and print the allocation status for each process.

Uploaded by

aasiyahafis2
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)
2 views3 pages

09 - Memory Management

The document contains three C programs that implement memory allocation strategies: First Fit, Best Fit, and Worst Fit. Each program prompts the user to input block sizes and process sizes, then allocates memory based on the chosen strategy and displays the allocation results. The programs utilize arrays to manage block and process sizes, and print the allocation status for each process.

Uploaded by

aasiyahafis2
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

#include <stdio.h>
#define MAX 10
void main()
{
int bsize[MAX], psize[MAX], bno, pno, flag[MAX], allocation[MAX];
for (int i = 0; i < MAX; i++)
{
flag[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\t");
for (int i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
printf("Enter no. of process: ");
scanf("%d", &pno);
printf("Enter size of each process:\t");
for (int i = 0; i < pno; i++)
scanf("%d", &psize[i]);

for (int i = 0; i < pno; i++)


for (int j = 0; j < bno; j++)
if (flag[j] == 0 && bsize[j] >= psize[i])
{
allocation[j] = i;
flag[j] = 1;
break;
}
printf("\nBlock No\tSize\tProcess No.\t\tSize\t\tRemaining");
for (int i = 0; i < bno; i++)
{
printf("\n%d\t\t%d\t\t", i + 1, bsize[i]);
if (flag[i] == 1)
printf("%d\t\t%d\t\t%d", allocation[i] + 1, psize[allocation[i]], bsize[i] -
psize[allocation[i]]);
else
printf("Not Allocated");
}
}
//Best Fit
#include <stdio.h>
#define MAX 10
int main()
{
int bsize[MAX], psize[MAX], pno, bno, temp, low;
static int flag[MAX], allocation[MAX];
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\t");
for (int i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
printf("Enter no. of process: ");
scanf("%d", &pno);
printf("Enter size of each process:\t");
for (int i = 0; i < pno; i++)
scanf("%d", &psize[i]);
printf("\nProcess No.\tSize\t\tBlock No\tSize\t\tRemaining\n");
for (int i = 0; i < pno; i++)
{
low = 10000;
allocation[i] = -1;
for (int j = 0; j < bno; j++)
if (flag[j] != 1)
{
temp = bsize[j] - psize[i];
if (temp >= 0 && temp < low)
{
allocation[i] = j;
low = temp;
}
}
if (allocation[i] != -1)
{
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, psize[i], allocation[i] + 1,
bsize[allocation[i]], low);
flag[allocation[i]] = 1;
}
}
return 0;
}
//Worst Fit
#include <stdio.h>
#define MAX 10
int main()
{
int bsize[MAX], psize[MAX], pno, bno, temp, high;
static int flag[MAX], allocation[MAX];
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\t");
for (int i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
printf("Enter no. of process: ");
scanf("%d", &pno);
printf("Enter size of each process:\t");
for (int i = 0; i < pno; i++)
scanf("%d", &psize[i]);
for (int i = 0; i < pno; i++)
{
high = -1;
for (int j = 0; j < bno; j++)
if (flag[j] == 0 && bsize[j] >= psize[i])
if (high == -1 || bsize[j] > bsize[high])
high = j;

if (high != -1)
{
allocation[i] = high;
flag[high] = 1;
}
else
allocation[i] = -1;
}
printf("\nProcess No.\tSize\t\tBlock No\tSize\t\tRemaining\n");
for (int i = 0; i < pno; i++)
{
if (allocation[i] == -1)
printf("%d\t\t%d\t\tNot Allocated\n", i + 1, psize[i]);
else
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, psize[i], allocation[i] + 1,
bsize[allocation[i]], bsize[allocation[i]] - psize[i]);
}
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