-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.c
More file actions
195 lines (189 loc) · 5.23 KB
/
Copy pathtempCodeRunnerFile.c
File metadata and controls
195 lines (189 loc) · 5.23 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include<stdio.h>
#include<stdlib.h>
struct block{
int id;
int start_address;
int size;
int used;
struct block *next;
struct block *prev;
};
typedef struct block block;
block *head=NULL,*end=NULL;
//add memory block
void add_block(int s,int i){
int start_add;
block *new=(block*)malloc(sizeof(block));
printf("Enter the starting Address: ");
scanf("%d",&start_add);
new->next=NULL;
new->prev=NULL;
new->start_address=start_add;
new->size=s;
new->id=i;
if(head==NULL){
head=new;
end=new;}
else{
end->next=new;
new->prev=end;
end=new;
}
}
//first fit
void first_fit(){
int processSize;
printf("Enter process size: ");
scanf("%d",&processSize);
block *ptr=head;
while(ptr!=NULL){
if(ptr->size==processSize){
printf("Perfectly fit on block %d, deleted block %d",ptr->id,ptr->id);
if(ptr->prev==NULL){
head=head->next;
head->prev=NULL;
}
else if(ptr->next==NULL){
(ptr->prev)->next=NULL;
}
else{
(ptr->prev)->next=ptr->next;
(ptr->next)->prev=ptr->prev;
}
free(ptr);
return;
}
else if(ptr->size>processSize){
ptr->start_address+=processSize;
ptr->size-=processSize;
printf("Process allocated to block %d. Remaining size in block %d is %d.",ptr->id,ptr->id,ptr->size);
return;
}
ptr=ptr->next;
}
printf("\nNo No suitable block found for size %d(First Fit).\n", processSize);
}
//best fit
void best_fit(){
int processSize;
printf("Enter process size: ");
scanf("%d",&processSize);
block *ptr=head,*best=NULL;
while(ptr!=NULL){
if(ptr->size>=processSize) {
if(best== NULL||ptr->size<best->size)
best=ptr;
}
ptr=ptr->next;
}
if(best!=NULL){
if(best->size==processSize){
printf("Perfectly fit on block %d, deleted block %d",best->id,best->id);
if(best->prev==NULL){
head=head->next;
head->prev=NULL;
}
else if(best->next==NULL){
(best->prev)->next=NULL;
}
else{
(best->prev)->next=best->next;
(best->next)->prev=best->prev;
}
free(best);
return;
}
else{
best->start_address+=processSize;
best->size-=processSize;
printf("Process allocated to Block %d using Best Fit. \nRemaining size in block %d is %d\n", best->id,best->id,best->size);
}
}else{
printf("No suitable block found for size %d(Best Fit).\n", processSize);
}
}
//worst fit
void worst_fit(){
int processSize;
printf("Enter process size: ");
scanf("%d",&processSize);
block *ptr=head,*worst=NULL;
while(ptr!=NULL){
if(ptr->size>=processSize) {
if(worst== NULL||ptr->size>worst->size)
worst=ptr;
}
ptr=ptr->next;
}
if(worst!=NULL){
if(worst->size==processSize){
printf("Perfectly fit on block %d, deleted block %d",worst->id,worst->id);
if(worst->prev==NULL){
head=head->next;
head->prev=NULL;
}
else if(worst->next==NULL){
(worst->prev)->next=NULL;
}
else{
(worst->prev)->next=worst->next;
(worst->next)->prev=worst->prev;
}
free(worst);
return;
}
else{
worst->start_address+=processSize;
worst->size-=processSize;
printf("Process allocated to Block %d using worst Fit. \nRemaining size in block %d is %d\n", worst->id,worst->id,worst->size);
}
}else{
printf("No suitable block found for size %d(worst Fit).\n", processSize);
}
}
//print memory
void print_memory(){
block *ptr=head;
printf("\nMemory State:\n");
printf("ID\tSize\tStarting Address\n");
while (ptr!=NULL) {
printf("%d\t%d\t%d\n", ptr->id, ptr->size,ptr->start_address);
ptr=ptr->next;
}
}
//main function
void main(){
int n,size,c;
printf("Enter no. of memory blocks: ");
scanf("%d",&n);
printf("Enter sizes of each block:\n");
for(int i=1;i<=n;i++){
scanf("%d",&size);
add_block(size,i);
}
do{
printf("\nMemory Allocator Menu\n---------------------");
printf("\n1. First Fit Allocation\n2. Best Fit Allocation\n3. Worst Fit Allocation\n4. Show Memory State\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &c);
switch(c){
case 1:
first_fit();
break;
case 2:
best_fit();
break;
case 3:
worst_fit();
break;
case 4:
print_memory();
break;
case 5:
printf("Exiting....");
break;
default:
printf("Invalid choice.\n");
}
}while(c!=5);
}