-
Notifications
You must be signed in to change notification settings - Fork 2
/
MEMORYAL.C
96 lines (95 loc) · 2.19 KB
/
MEMORYAL.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<stdio.h>
#include<conio.h>
int noofprocess,noofhole;
int holesize[10],processsize[10],holenumber[10];
//First fit way of allocating the memory to the process
void firstfit(){
int i,j;
printf("\n\t\tFirst Fit");
printf("\nProcess Psize Allocatedhole");
for(i=0;i<noofprocess;i++){
int c=0;
for(j=0;j<noofhole;j++){
if(processsize[i]<=holesize[j]){
printf("\nP%d\t%d\tH%d",i,processsize[i],j);
c=1;
holesize[j]-=processsize[i];
break;
}
}
if(c==0){
printf("\nP%d\t%d\tNot allocated",i,processsize[i]);
}
}
}
//Best fit way of allocating memory to the process
void bestfit(){
int i,j;
int temp;
for(i=0;i<noofhole-1;i++){
for(j=i+1;j<noofhole;j++){
if(holesize[i]>holesize[j]){
//holes are sorted
temp=holesize[i];
holesize[i]=holesize[j];
holesize[j]=temp;
//hole number is sorted according to the size
temp=holenumber[i];
holenumber[i]=holenumber[j];
holenumber[j]=temp;
}
}
}
printf("\n\t\tBest Fit\n");
printf("\nProcess Psize Allocated hole");
for(i=0;i<noofprocess;i++){
int c=0;
for(j=0;j<noofhole;j++){
if(processsize[i]<=holesize[j]){
printf("\nP%d\t%d\tH%d",i,processsize[i],holenumber[j]);
c=1;
holesize[j]-=processsize[i];
break;
}
}
if(c==0)
printf("\nP%d\t%d\tNot allocated",i,processsize[i]);
}
}
void main(){
int i,j;
int ch;
clrscr();
printf("\t\t\tMemory Allocation\n");
printf("Enter the number of the memory holes: ");
scanf("%d",&noofhole);
printf("Enter the eack block hole size: \n");
for(i=0;i<noofhole;i++){
printf("Hole %d: ",i);
scanf("%d",&holesize[i]);
holenumber[i]=i;
}
printf("Enter the number of process: ");
scanf("%d",&noofprocess);
printf("Enter the size of each process:\n");
for(i=0;i<noofprocess;i++){
printf("Process %d: ",i);
scanf("%d",&processsize[i]);
}
printf("\nMenu for allocation:\n");
printf("\n1.First Fit \n2.Best Fit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
firstfit();
break;
case 2:
bestfit();
break;
default:
printf("Wrong choice!!");
}
printf("\n\t\tThank you!!");
getch();
}