-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemorymanagement.c
212 lines (163 loc) · 4.45 KB
/
memorymanagement.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*Simulation of first-fit, best-fit and worst-fit allocations.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the node structure for memory blocks
struct Node {
int memorySize;
char processID[3];
struct Node *next;
};
struct Node *freeHead, *allocatedHead;
// Function prototypes
void insertNode(struct Node **head, struct Node *newNode);
void displayList(struct Node *head);
void firstFit(char id[3], int memSize);
void worstFit(char id[3], int memSize);
void bestFit(char id[3], int memSize);
void allocateMemory();
int main() {
int continueInput;
struct Node *newNode;
// Initialize the free and allocated memory lists
freeHead = (struct Node *) malloc(sizeof(struct Node));
freeHead->next = NULL;
allocatedHead = (struct Node *) malloc(sizeof(struct Node));
allocatedHead->next = NULL;
// Input free memory blocks
do {
newNode = (struct Node *) malloc(sizeof(struct Node));
printf("\nEnter free memory size: ");
scanf("%d", &newNode->memorySize);
strcpy(newNode->processID, "f");
newNode->next = NULL;
insertNode(&freeHead, newNode); // Insert into the free list
displayList(freeHead);
printf("\nEnter 1 to add more memory blocks, 0 to stop: ");
scanf("%d", &continueInput);
} while (continueInput != 0);
printf("\nFree List\n_____________\n");
displayList(freeHead);
allocateMemory(); // Call function to allocate memory using different strategies
return 0;
}
// Insert a new node into the linked list
void insertNode(struct Node **head, struct Node *newNode) {
struct Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
// Display the linked list
void displayList(struct Node *head) {
struct Node *current = head->next;
while (current != NULL) {
printf("(%s : %d)\t", current->processID, current->memorySize);
current = current->next;
}
printf("\n");
}
// Memory allocation strategies
void allocateMemory() {
int choice, memSize;
char processID[3];
do {
printf(" \n1. First Fit\n2. Worst Fit\n3. Best Fit\n4. Exit\nYour Choice: ");
scanf("%d", &choice);
if (choice == 4) {
break;
}
printf("\nEnter Process ID: ");
scanf("%s", processID);
printf("Memory size required: ");
scanf("%d", &memSize);
switch (choice) {
case 1:
firstFit(processID, memSize);
break;
case 2:
worstFit(processID, memSize);
break;
case 3:
bestFit(processID, memSize);
break;
default:
printf("Invalid choice! Please select a valid option.\n");
break;
}
printf("\nAllocated List\n_____________\n");
displayList(allocatedHead);
printf("\nFree List\n_____________\n");
displayList(freeHead);
} while (choice != 4);
}
// Placeholder functions for memory allocation strategies
void firstFit(char id[3], int memSize) {
// Implementation of First Fit allocation strategy
}
void worstFit(char id[3], int memSize) {
// Implementation of Worst Fit allocation strategy
}
void bestFit(char id[3], int memSize) {
// Implementation of Best Fit allocation strategy
}
//SAMPLE OUTPUT
/*
Enter free memory size: 100
( f : 100)
Enter 1 to add more memory blocks, 0 to stop: 1
Enter free memory size: 200
( f : 100) ( f : 200)
Enter 1 to add more memory blocks, 0 to stop: 1
Enter free memory size: 300
( f : 100) ( f : 200) ( f : 300)
Enter 1 to add more memory blocks, 0 to stop: 0
Free List
_____________
( f : 100) ( f : 200) ( f : 300)
1. First Fit
2. Worst Fit
3. Best Fit
4. Exit
Your Choice: 1
Enter Process ID: A
Memory size required: 150
Allocated List
_____________
( f : 150)
Free List
_____________
( f : 100) ( f : 50) ( f : 300)
1. First Fit
2. Worst Fit
3. Best Fit
4. Exit
Your Choice: 2
Enter Process ID: B
Memory size required: 400
Allocated List
_____________
( f : 150) ( f : 400)
Free List
_____________
( f : 100) ( f : 50) ( f : 300)
1. First Fit
2. Worst Fit
3. Best Fit
4. Exit
Your Choice: 3
Enter Process ID: C
Memory size required: 50
Allocated List
_____________
( f : 150) ( f : 400) ( f : 50)
Free List
_____________
( f : 100) ( f : 300)
1. First Fit
2. Worst Fit
3. Best Fit
4. Exit
Your Choice: 4
*/