Experiment 8 Write A C Program To Implement First Fit Memory Management
Experiment 8 Write A C Program To Implement First Fit Memory Management
management.
#include<stdio.h>
Void firstFit(int blockSize[], int m, int processSize[], int n)
{
Int I, j;
Int allocation[n];
For(I = 0; I < n; i++) {
Allocation[i] = -1;
}
For (I = 0; I < n; i++)
{
For (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(“ %i\t\t\t”, i+1);
Printf(“%i\t\t\t\t”, processSize[i]);
If (allocation[i] != -1)
Printf(“%i”, allocation[i] + 1);
Else
Printf(“Not Allocated”);
Printf(“\n”);
}
}
Int main()
{
Int m;
Int n;
Int blockSize[] = {100, 500, 200, 300, 600};
Int processSize[] = {212, 417, 112, 426};
M = sizeof(blockSize) / sizeof(blockSize[0]);
N = sizeof(processSize) / sizeof(processSize[0]);
firstFit(blockSize, m, processSize, n);
return 0 ;
}
OUTPUT –
Experiment 9 – Write a C program to implement Best Fit memory
management.
#include <stdio.h>
Void implimentBestFit(int blockSize[], int blocks, int processSize[], int proccesses){
Int allocation[proccesses];
Int occupied[blocks];
For(int I = 0; I < proccesses; i++){
Allocation[i] = -1;
}
For(int I = 0; I < blocks; i++){
Occupied[i] = 0;
}
For (int I = 0; I < proccesses; i++){
Int indexPlaced = -1;
For (int j = 0; j < blocks; j++) {
If (blockSize[j] >= processSize[i] && !occupied[j])
{
If (indexPlaced == -1)
indexPlaced = j;
Else if (blockSize[j] < blockSize[indexPlaced])
indexPlaced = j;
}
}
If (indexPlaced != -1){
Allocation[i] = indexPlaced;
Occupied[indexPlaced] = 1;
}
}
Printf(“\nProcess No.\tProcess Size\tBlock no.\n”);
For (int I = 0; I < proccesses; i++){
Printf(“%d \t\t\t %d \t\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, 50, 30, 120, 35};
Int processSize[] = {40, 10, 30, 60};
Int blocks = sizeof(blockSize)/sizeof(blockSize[0]);
Int proccesses = sizeof(processSize)/sizeof(processSize[0]);
implimentBestFit(blockSize, blocks, processSize, proccesses);
return 0 ;
}
OUTPUT-
.
Int allocation[processes];
Int occupied[blocks];
Occupied[i] = 0;
If (indexPlaced == -1)
indexPlaced = j;
Else if (blockSize[indexPlaced] < blockSize[j])
indexPlaced = j;
If (indexPlaced != -1) {
Allocation[i] = indexPlaced;
Occupied[] = 1;
blockSize[indexPlaced] -= processSize[i];
}
If (allocation[i] != -1)
Printf(“%d\n”,allocation[i] + 1);
Else
Printf(“Not Allocated\n”);
Int main(){
return 0;
OUTPUT-