#include<stdio.h>
#include<conio.h>
int main()
{
int *Frag,*Files,*Block,nFile,nFrag,nBlock,i,j;
int *FFile,*FBlock;
int tmp,greatest;
//Take Input of Block Size Information
printf("\nEnter the Number of Blocks:");
scanf("%d",&nBlock);
Block=(int*)malloc(sizeof(int)*nBlock);
FBlock=(int*)malloc(sizeof(int)*nBlock);
for(i=1;i<=nBlock;i++)
{
printf("Block-%d Size :",i);
scanf("%d",&Block[i]);
}
//Take Input Of File Information
printf("\nEnter the Number of File:");
scanf("%d",&nFile);
nFrag=nFile;
Frag=(int*)malloc(sizeof(int)*nFrag); //Contains the Fragmentation Value
Files=(int*)malloc(sizeof(int)*nFile); //Contains the File size
FFile=(int*)malloc(sizeof(int)*nFile); //Contains the Block Number
for(i=1;i<=nFile;i++)
{
FFile[i]=-1;
}
for(i=1;i<=nFile;i++)
{
printf("File-%d Size :",i);
scanf("%d",&Files[i]);
}
for(i=1;i<=nFile;i++)
{
greatest=0;
for(j=1;j<=nBlock;j++)
{
if(FBlock[j]!=1)
{
tmp=Block[j]-Files[i];
if(tmp>=0)
{
if(greatest<tmp)
{
FFile[i]=j;
Frag[i]=tmp;
greatest=tmp;
}
}
}
}
if(FFile[i]!=-1)
FBlock[FFile[i]]=1;
}
printf("\n\t\t****WorstFit Memory Management****\n");
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nFile;i++)
{
if(FFile[i]!=-1) //Check File is assigned a block
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,Files[i],FFile[i],Block[FFile[i]],Frag[i]);
else
printf("\n%d\t\t%d\t\t%s\t\t%s\t\t%s",i,Files[i],NULL,NULL,NULL);
}
return 0;
}
Best fit
#include<iostream>
using namespace std;
typedef struct Process
{
int process_id;
int processSize;
int allocatedHole;
}Process;
typedef struct Hole
{
int hole_id;
int holeSize;
bool allocated;
}Hole;
int main()
{
int n,m,i,j,k,flag=0,min;
cout<<"Enter the number of holes : ";
cin>>m;
Hole hole[m+1];
for(i=1;i<=m;i++)
{
cout<<"Enter the size of the hole "<<i<<" : ";
cin>>hole[i].holeSize;
hole[i].hole_id = i;
hole[i].allocated = false;
}
cout<<"Enter the number of processes : ";
cin>>n;
Process process[n+1];
for(i=1;i<=n;i++)
{
process[i].process_id = i;
cout<<"Enter the size of process "<<i<<" : ";
cin>>process[i].processSize;
}
min = hole[1].holeSize;
for(i=1;i<=n;i++)
{
flag = 0;
for(j=1;j<=m;j++)
{
if(hole[j].holeSize>=process[i].processSize)
{
flag = 1;
min = j;
for(k=j;k<=m;k++)
{
if((hole[min].holeSize>hole[k].holeSize)&&(process[i].processSize<=hole[k].holeSize)&&(hole[k].allocated == false))
{
min = k;
}
}
if((process[i].processSize <= hole[min].holeSize)&&(hole[min].allocated == false))
{
flag = 1;
hole[min].allocated = true;
process[i].allocatedHole = hole[min].hole_id;
break;
}
}
}
if(!flag)
{
process[i].allocatedHole = (int)NULL;
}
}
cout<<"\n\n################# File Allocation Table ###############\n";
cout<<"\nProcess No.\t\tProcess Size\t\tAllocated Hole\n";
cout<<"-------------------------------------------------------------------\n";
for(i=1;i<=n;i++)
{
cout<<process[i].process_id<<"\t\t\t"<<process[i].processSize<<"\t\t\t"<<process[i].allocatedHole<<endl;
}
return 0;
}
#include<conio.h>
int main()
{
int *Frag,*Files,*Block,nFile,nFrag,nBlock,i,j;
int *FFile,*FBlock;
int tmp,greatest;
//Take Input of Block Size Information
printf("\nEnter the Number of Blocks:");
scanf("%d",&nBlock);
Block=(int*)malloc(sizeof(int)*nBlock);
FBlock=(int*)malloc(sizeof(int)*nBlock);
for(i=1;i<=nBlock;i++)
{
printf("Block-%d Size :",i);
scanf("%d",&Block[i]);
}
//Take Input Of File Information
printf("\nEnter the Number of File:");
scanf("%d",&nFile);
nFrag=nFile;
Frag=(int*)malloc(sizeof(int)*nFrag); //Contains the Fragmentation Value
Files=(int*)malloc(sizeof(int)*nFile); //Contains the File size
FFile=(int*)malloc(sizeof(int)*nFile); //Contains the Block Number
for(i=1;i<=nFile;i++)
{
FFile[i]=-1;
}
for(i=1;i<=nFile;i++)
{
printf("File-%d Size :",i);
scanf("%d",&Files[i]);
}
for(i=1;i<=nFile;i++)
{
greatest=0;
for(j=1;j<=nBlock;j++)
{
if(FBlock[j]!=1)
{
tmp=Block[j]-Files[i];
if(tmp>=0)
{
if(greatest<tmp)
{
FFile[i]=j;
Frag[i]=tmp;
greatest=tmp;
}
}
}
}
if(FFile[i]!=-1)
FBlock[FFile[i]]=1;
}
printf("\n\t\t****WorstFit Memory Management****\n");
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nFile;i++)
{
if(FFile[i]!=-1) //Check File is assigned a block
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,Files[i],FFile[i],Block[FFile[i]],Frag[i]);
else
printf("\n%d\t\t%d\t\t%s\t\t%s\t\t%s",i,Files[i],NULL,NULL,NULL);
}
return 0;
}
Best fit
#include<iostream>
using namespace std;
typedef struct Process
{
int process_id;
int processSize;
int allocatedHole;
}Process;
typedef struct Hole
{
int hole_id;
int holeSize;
bool allocated;
}Hole;
int main()
{
int n,m,i,j,k,flag=0,min;
cout<<"Enter the number of holes : ";
cin>>m;
Hole hole[m+1];
for(i=1;i<=m;i++)
{
cout<<"Enter the size of the hole "<<i<<" : ";
cin>>hole[i].holeSize;
hole[i].hole_id = i;
hole[i].allocated = false;
}
cout<<"Enter the number of processes : ";
cin>>n;
Process process[n+1];
for(i=1;i<=n;i++)
{
process[i].process_id = i;
cout<<"Enter the size of process "<<i<<" : ";
cin>>process[i].processSize;
}
min = hole[1].holeSize;
for(i=1;i<=n;i++)
{
flag = 0;
for(j=1;j<=m;j++)
{
if(hole[j].holeSize>=process[i].processSize)
{
flag = 1;
min = j;
for(k=j;k<=m;k++)
{
if((hole[min].holeSize>hole[k].holeSize)&&(process[i].processSize<=hole[k].holeSize)&&(hole[k].allocated == false))
{
min = k;
}
}
if((process[i].processSize <= hole[min].holeSize)&&(hole[min].allocated == false))
{
flag = 1;
hole[min].allocated = true;
process[i].allocatedHole = hole[min].hole_id;
break;
}
}
}
if(!flag)
{
process[i].allocatedHole = (int)NULL;
}
}
cout<<"\n\n################# File Allocation Table ###############\n";
cout<<"\nProcess No.\t\tProcess Size\t\tAllocated Hole\n";
cout<<"-------------------------------------------------------------------\n";
for(i=1;i<=n;i++)
{
cout<<process[i].process_id<<"\t\t\t"<<process[i].processSize<<"\t\t\t"<<process[i].allocatedHole<<endl;
}
return 0;
}
No comments:
Post a Comment