Friday, 8 May 2015

Memory Management Schemes-First Fit

#include<stdio.h>
struct Memory
{
int start;
int end;
int size;
int occupy;
}M[100];
struct Process
{
int pid;
int size;
int occupy;
}Pro[100];

void main()
{
int fb=0,i=0,noofpro,j=0;
clrscr();
printf("Enter No of free block");
scanf("%d",&fb);
for(i=0;i<fb;i++)
{
printf("Enter Memory Starting Address   ");
scanf("%d",&M[i].start);
printf("Enter Memory Ending Address   ");
scanf("%d",&M[i].end);
M[i].size=(M[i].end-M[i].start)+1;
M[i].occupy=0;
}

for(i=0;i<fb;i++)
printf("\n Allocated Memory Space  %d  Size  %d   ",i+1,M[i].size);

printf("Enter No of Process \n");
scanf("%d",&noofpro);

for(i=0;i<noofpro;i++)
{
printf("Enter Process Id ");
scanf("%d",&Pro[i].pid);
printf("Enter Process Size ");
scanf("%d",&Pro[i].size);
Pro[i].occupy=0;
}
for(i=0;i<noofpro;i++)
printf("\n Process %d Size %d   ",i+1,Pro[i].size);

for(j=0;j<noofpro;j++)
{
for(i=0;i<fb;i++)
{
    if(Pro[j].size<=M[i].size)
    {
                if(M[i].occupy!=1)
                {
                                printf("\nProcess  %d  Occupied the space from %d  to  %d   ",Pro[j].pid,M[i].start,(M[i].start+Pro[j].size)-1);
                                printf("\nFree Space in that block is          %d          ",(M[i].end+1)-(M[i].start+Pro[j].size));
                                M[i].occupy=1;
                                Pro[j].occupy=1;
                                break;
                }
     }
}
}
for(i=0;i<noofpro;i++)
{
if(Pro[i].occupy==0)
printf("\n No Space Allocated For Process  %d",Pro[i].pid);
}
getch();

}



Output:

 Enter No of free block                  3
Enter Memory Starting Address   100
Enter Memory Ending Address   250
Enter Memory Starting Address   251
Enter Memory Ending Address   500
Enter Memory Starting Address   501
Enter Memory Ending Address   650

 Allocated Memory Space  1  Size  151
 Allocated Memory Space  2  Size  250
 Allocated Memory Space  3  Size  150 

 Enter No of Process   3
Enter Process Id     100
Enter Process Size 150
Enter Process Id     101
Enter Process Size 500
Enter Process Id   102
Enter Process Size 50

 Process 1 Size 150
 Process 2 Size 500
 Process 3 Size 50

Process  100  Occupied the space from 100  to  249
Free Space in that block is          1
Process  102  Occupied the space from 251  to  300
Free Space in that block is          200
No Space Allocated For Process  101

No comments:

Post a Comment