You are on page 1of 5

LAB-9

Atthunuri Rajeshreddy
20BCE1658

Course Code: CSE2005 Course Name: Operating System Lab


Faculty: Dr. Bhanu Chander Balusa Slot: L21+L22

Aim:

Consider the following free memory partitions of size 100 KB, 500 KB, 200 KB, 450 KB and
600 KB in same order. If sequence of requests for blocks of size 212 KB, 417 KB, 112 KB
and 426 KB in same order come. Find out which free memory partition is assigned to the
requests using First fit, Best fit and Worst fit algorithms. Implement the above scenario using
C programming.

Background:

 First fit. Allocate the first hole that is big enough. Searching can start either at the
beginning of the set of holes or at the location where the previous first-fit search ended.
We can stop searching as soon as we find a free hole that is large enough.
 Best fit. Allocate the smallest hole that is big enough. We must search the entire list,
unless the list is ordered by size. This strategy produces the smallest leftover hole.
 Worst fit. Allocate the largest hole. Again, we must search the entire list, unless it is
sorted by size. This strategy produces the largest leftover hole, which may be more useful
than the smaller leftover hole from a best-fit approach.
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int size[5]={100,500,200,450,600};
int process[5]={212,417,112,426};
int a[4];
int m;
printf("enter input: ");
scanf("%d",&m);
switch(m)
{
case 1: printf("\nFirst fit\n");
printf("\n");
for(int j=0;j<4;j++)
{
for(int i=0;i<5;i++)
{
if(process[j]<=size[i])
{
printf("%d allocated at %d ",process[j],size[i]);
size[i]=size[i]-process[j];
printf("(amount of space free %d) \n",size[i]);
break;

}
break;
case 2: printf("\nworst fit\n");
int max;
for(int j=0;j<4;j++){
for(int i=0;i<5;i++){
if(process[j]<=size[i]){
a[i]=size[i]-process[j];

}
else
{
a[i]=0;

}
max=a[0];
for(int i=1;i<5;i++){
if(max<a[i]){
max=a[i];
}

}
for(int i=0;i<5;i++){
if(max==a[i]){
if(size[i]>=process[j]){
printf("%d allocated at %d ",process[j],size[i]);
size[i]=size[i]-process[j];
printf("(amount of space free %d) \n",size[i]);

}
else{
printf("%d not allocated\n",process[j]);
break;

}
break;
case 3:
printf("\nBest fit\n");
int min;
for(int j=0;j<4;j++){
for(int i=0;i<5;i++){
if(process[j]<=size[i]){
a[i]=size[i]-process[j];

}
else{
a[i]=1000;
}

}
min=a[0];
for(int i=0;i<5;i++){
if(min>a[i]){
min=a[i];

}
for(int i=0;i<5;i++){
if(min==a[i]){
if(size[i]>=process[j]){
printf("%d allocated at %d ",process[j],size[i]);
size[i]=size[i]-process[j];
printf("(amount of space free %d)\n",size[i]);
break;

}
else{
printf("%d not allocated\n",process[j]);
break;

}
Output:

You might also like