0% found this document useful (0 votes)
4 views6 pages

Experiment 8 Write A C Program To Implement First Fit Memory Management

Uploaded by

tomaramit046
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)
4 views6 pages

Experiment 8 Write A C Program To Implement First Fit Memory Management

Uploaded by

tomaramit046
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/ 6

Experiment 8 - Write a C program to implement First Fit memory

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-
.

Experiment 10 – Write a C program to implement Worst Fit memory


management.
#include <stdio.h>

Void implimentWorstFit(int blockSize[], int blocks, int processSize[], int processes){

Int allocation[processes];
Int occupied[blocks];

For(int I = 0; I < processes; i++){


Allocation[i] = -1;

For(int I = 0; I < blocks; i++){

Occupied[i] = 0;

For (int i=0; I < processes; 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[indexPlaced] < blockSize[j])

indexPlaced = j;

If (indexPlaced != -1) {

Allocation[i] = indexPlaced;

Occupied[] = 1;

blockSize[indexPlaced] -= processSize[i];
}

Printf(“\nProcess No.\tProcess Size\tBlock no.\n”);

For (int I = 0; I < processes; 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 processes = sizeof(processSize)/sizeof(processSize[0]);

implimentWorstFit(blockSize, blocks, processSize, processes);

return 0;

OUTPUT-

You might also like