- Implement some Memory management schemes like FIRST FIT, BEST FIT & WORST FIT.
FIRST FIT
Aim
To write a program to implement best fit algorithm for memory management.
Algorithm
1. Start the Program
2. Declare the size
3. Get the number of processes to be inserted
4. Allocate the first hole that is big enough searching
5. Start at the beginning of the set of holes
6. If not start at the hole that is sharing the pervious first fit search end
7. Compare the hole
8. If large enough then stop searching in the procedure
9. Display the values
10. Stop the program
Result
The program for first fit was implemented and hence verified
#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m;
printf("Enter no of Blocks.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst Block size:",i);
scanf("%d",&a[i]);
}
printf("Enter no of Process.\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst Process:",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("The Process %d allocated to %d\n",j,a[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
{
if(p[j]!=10000)
{
printf("The Process %d is not allocated\n",j);
}
}
}
Output
Enter no of Blocks.
5
Enter the 0st Block size:500
Enter the 1st Block size:400
Enter the 2st Block size:300
Enter the 3st Block size:200
Enter the 4st Block size:100
Enter no of Process.
5
Enter the size of 0st Process:100
Enter the size of 1st Process:350
Enter the size of 2st Process:400
Enter the size of 3st Process:150
Enter the size of 4st Process:200
The Process 0 allocated to 500
The Process 1 allocated to 400
The Process 3 allocated to 200
The Process 2 is not allocated
The Process 4 is not allocated
BEST FIT
Aim
To write a program to implement best fit algorithm for memory management.
Algorithm
1. Start the program
2. Declare the size
3. Get the number of processes to be inserted
4. Allocate the best hole that is small enough searching
5. Start at the best of the set of holes
6. If not start at the hole that is sharing the pervious best fit search end
7. Compare the hole
8. If small enough then stop searching in the procedure
9. Display the values
10. Stop the program
Result
The program for best fit was implemented and hence verified
#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20];
printf("Enter no of Blocks.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst Block size:",i);
scanf("%d",&a[i]);
b[i]=i;
}
printf("Enter no of Process.\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst Process:",i);
scanf("%d",&p[i]);
c[i]=i;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i]<a[j])
{
temp=a[i];
temp1=b[i];
a[i]=a[j];
b[i]=b[j];
a[j]=temp;
b[j]=temp1;
}
if(p[i]<p[j])
{
temp=p[i];
temp2=c[i];
p[i]=p[j];
c[i]=c[j];
p[j]=temp;
c[j]=temp2;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("The Process %d allocated to Block %d\n",c[j],b[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
{
if(p[j]!=10000)
{
printf("The Process %d is not allocated\n",j);
}
}
}
Output
Enter no of Blocks.
3
Enter the 0st Block size:300
Enter the 1st Block size:200
Enter the 2st Block size:100
Enter no of Process.
3
Enter the size of 0st Process:100
Enter the size of 1st Process:300
Enter the size of 2st Process:200
The Process 0 allocated to Block 2
The Process 2 allocated to Block 1
The Process 1 allocated to Block 0
WORST FIT
Aim
To write a program to implement Worst fit algorithm for memory management.
Algorithm
1. Start the program
2. Get the number of Process
3. For the number of processes do the following
a)Get the name and size of the program
b)Generate the memory size randomly by using read() function
c)Arrange the memory block size in descending order according to their size
d)Allocate the memory block to the process. Whose size is either equal to or less then the memory block size.
e)Repeat step the for all the process.
4.Display the status of each memory block after allocation of the process.
5.Stop the program.
Result
The program for worst fit was implemented and hence verified
#include<stdio.h>
main()
{
int p,n,b[50],i,j,t;
printf(“\n enter the number of unallocated space”);
scnaf(“%d”,&n);
printf(“\n enter the allocated memory space”);
for(i=0;i<n;i++)
{
scanf(“%d”,&b[i]);
}
printf(“\n memory space required for given process”);
scnaf(“%d”,&p);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(b[i]<b[j])
{
t=b[j];
b[j]=b[i];
b[i]=t;
}
}
}
if(b[0]>=p)
{
printf(“Worst fit of the given process is %d”,b[0]);
}
else
printf(“The process does not fit it to any free memory”);
}
OUTPUT
Enter the number of unallocated space 4
Enter the allocated memory space
45 23 10 50
The memory space required for the given process 40
worst fit of the given process is 50
9. Implement some Memory management schemes like Paging and Segmentation
PAGING:
Aim :
To implement some Memory management schemes like Paging
Algorithm
1.Start the program
2.Get the number of page and size
3.Get the frame no for each page
4.Get the content of each page
5.Display the page number, Logical address and frame number of each item
6.Get the Logical address of an item and it corresponding offset.
7.For the total number of pages. Do the following
a)Get the page number.
b)Calculate the frame number
c)Find the date item of the correspond physical address
8.Display the date that is retrieved from physical address
9.Stop the program.
#include<stdio.h>
#include<string.h>
main()
{
int l,g,b,j,s,[10],i,n,f[5]={0,0,0,1,0},st[5]={101,201,301,401,501};
int en[5]={200,300,400,500,600};
char con[10],[20];
printf(“\n Enter the number of pages in Process”);
scanf(“%d”,&n);
printf(“\n Enter the content of each page”);
for(i=0;i<n;i++)
scanf(“%s”,con[i]);
printf(“\n Before the Allocation :”);
for(i=0;i<n;i++)
{
if(f[i]==1)
printf(“\n %d \t Full %d”,st[i],en[i]);
else
printf(“\n %d \t Free %d”,st[i],en[i]);
}
printf(“\n After Allocation : ”);
printf(“\n main memory \t free holes \t\t after allocation”);
for(i=0;i<n;i++)
{
l=0;
l=strlen(con[i]);
g=0;
for(j=0;j<n;j++)
{
if(f[j]!=1)
{
b=s[j]+1;
printf(“\n page %d \t %d %s %d”,i+1,st[i],con[i],b);
printf(“\t\t %d - %d”,b,en[j]);
g=1;
f[j]=1;
break;
}
}
if(g==0)
printf(“\n No Free Frames are available for Page %d”,i+1);
}
for(i=0;i<n;i++)
{
if(f[j]==0)
printf(“ \n \t \t %d - %d “,st[i],en[i]);
}
}
OUTPUT
Enter the Number of Pages in Process : 3
Enter the content of each page
India
Kalam
Assam
Before allocation
101Free200
201Free300
301Free400
After allocation
Main Memory Free HolesAfter allocation
Page1 101India 106106-200
Page2 201Kalam 206206-300
Page3 301assam 306306-400
Segmentation
Aim :
To implement some Memory management schemes like Segmentation.
Algorithm
1.Start the program
2.Get the total number of Segmentation
3.For the number of Segmentation. Do the following
a)Get the segment number base and limit
b)Go to step 4.
4.Get the Logical address
5.If the Logical address is valid,Calculate the offset and physical address
6.If the Logical address is not valid,display it as Page Fault
7.Stop the Program
Result:
The program for Segmentation was implemented and hence verified.
Segmentation
#include<stdio.h>
struct segment
{
int sno,base,length;
}seg[10];
main()
{
int i,n,offset,k,j,laddr,val,segno;
printf(“\n Enter the number of Segments : \n”);
scanf(“%d”,&n);
printf(“\n Enter the segno,Base,Limit : \n”);
printf(“Segno \t Base \tLimit\n”);
for(i=0;i<n;i++)
scanf(“%d%d%d”,&seg[i].sno,&seg[i].base,&seg[i].length);
printf(“Enter the Logical Address”);
scanf(“%d”,&laddr);
offset=0;
i=0;
while(1)
{
if(laddr<10)
{
segno=laddr;
break;
}
val=1;
for(j=0;j<i;j++)
val=val*10;
offset=offset+((laddr%10)*val);
laddr=laddr/10;
i++;
}
k=1;
for(i=0;i<n;i++)
{
if((seg[i].length>offset) & (seg[i].sno==segno ))
k=0;
printf(“The Physical Address is %d \n”,seg[i].base+offset)
break;
}
}
if(k==1)
printf(“Page Fault \n”)
}
OUTPUT
Enter the nimber of segments :
3
Enter the segno,Base,Limit
SegnoBaseLimit
11000500
22000300
33000400
Enter the Logical Address
1450
Enter the physical Address
1450
Enter the nimber of segments :
3
Enter the segno,Base,Limit
SegnoBaseLimit
11000500
22000300
33000400
Enter the Logical Address
2350
Page Fault