-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstFitAllocation.c
97 lines (88 loc) · 1.78 KB
/
FirstFitAllocation.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
#include <stdio.h>
#include <stdlib.h>
struct memory
{
int blockSize;
int procSize;
int freeSize;
struct memory *next;
};
struct memory *head = NULL;
struct memory *createMem(int size)
{
struct memory *newMem = (struct memory *)malloc(sizeof(struct memory));
newMem->blockSize = size;
newMem->freeSize = size;
newMem->procSize = 0;
if (head == NULL)
{
newMem->next = NULL;
head = newMem;
}
else
{
newMem->next = head;
head = newMem;
}
}
int allocMem(int size)
{
struct memory *ptr = head;
while (ptr)
{
if (size <= (ptr->freeSize))
{
ptr->procSize = ptr->procSize + size;
ptr->freeSize = ptr->freeSize - size;
return 1;
}
ptr = ptr->next;
}
return 0;
}
void printMemoryStatus()
{
struct memory *current_block = head;
while (current_block)
{
printf("Block Size: %d Process Size:%d, Free Space: %d\n", current_block->blockSize, current_block->procSize, current_block->freeSize);
current_block = current_block->next;
}
}
int main()
{
createMem(200);
createMem(100);
createMem(300);
createMem(500);
printMemoryStatus();
int ch, size;
do
{
printf("\nMenu\n----\n1.Alllocate\n2.Display\n3.Exit\n");
printf("\nEnter the choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter the size of the process..:");
scanf("%d", &size);
if (allocMem(size))
printf("Memory allocated successfully.\n");
else
printf("Insufficient memory.\n");
printf("Memory Status After Allocation:\n");
printMemoryStatus();
break;
case 2:
printf("Memory Status ........:\n");
printMemoryStatus();
break;
case 3:
exit(0);
default:
printf("Invalid Choice....");
}
} while (1);
return 0;
}