-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_fit_ll.c
174 lines (155 loc) · 3.6 KB
/
first_fit_ll.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
// C implementation of the First
// Fit memory management algorithm
// using a linked list. This implementation
// is exactly the same as the ordinary First Fit
// memory management discusesd in that file
// except we use a linked
// list instead of an array. Some important
// and common C features are used. We have stucture
// pointers as well as several pointers to structures.
// But now we can delete elements as well as add them.
#include <stdio.h>
#include <stdlib.h>
// Two global counters
int g = 0, k = 0;
// Structure for free list
struct free {
int tag;
int size;
struct free* next;
} *free_head = NULL, *prev_free = NULL;
// Structure for allocated list
struct alloc {
int block_id;
int tag;
int size;
struct alloc* next;
} *alloc_head = NULL, *prev_alloc = NULL;
// Function to create free
// list with given sizes
void create_free(int c)
{
struct free *p
= (struct free*)malloc(sizeof(struct free));
p->size = c;
p->tag = g;
p->next = NULL;
if (free_head == NULL)
free_head = p;
else
prev_free->next = p;
prev_free = p;
g++;
}
// Function to print free list which
// prints free blocks of given sizes
void print_free()
{
struct free *p = free_head;
printf("Tag\tSize\n");
while (p != NULL) {
printf("%d", p->tag);
printf("%d",p->size);
printf("\n");
p = p->next;
}
}
// Function to print allocated list which
// prints allocated blocks and their block ids
void print_alloc()
{
struct alloc *p = alloc_head;
printf("Tag\tBlock ID\tSize\n");
while (p != NULL) {
printf("%d\t", p->tag);
printf("%d\t",p->block_id);
printf("%d\t\t", p->size);
printf("\n");
p = p->next;
}
}
// Function to allocate memory to
// blocks as per First fit algorithm
void create_alloc(int c)
{
// create node for process of given size
struct alloc* q
= (struct alloc*)malloc(sizeof(struct alloc));
q->size = c;
q->tag = k;
q->next = NULL;
struct free* p = free_head;
// Iterate to find first memory
// block with appropriate size
while (p != NULL) {
if (q->size <= p->size)
break;
p = p->next;
}
// Node found to allocate
if (p != NULL) {
// Adding node to allocated list
q->block_id = p->tag;
p->size -= q->size;
if (alloc_head == NULL)
alloc_head = q;
else {
prev_alloc = alloc_head;
while (prev_alloc->next != NULL)
prev_alloc = prev_alloc->next;
prev_alloc->next = q;
}
k++;
}
else // Node found to allocate space from
printf("Block of size %d cannot be allocated\n", c);
}
// Function to delete node from
// allocated list to free some space
void delete_alloc(int t)
{
// Standard delete function
// of a linked list node
struct alloc *p = alloc_head, *q = NULL;
// First, find the node according
// to given tag id
while (p != NULL) {
if (p->tag == t)
break;
q = p;
p = p->next;
}
if (p == NULL)
printf("Tag ID doesn't exist\n");
else if (p == alloc_head)
alloc_head = alloc_head->next;
else
q->next = p->next;
struct free *temp = free_head;
while (temp != NULL) {
if (temp->tag == p->block_id) {
temp->size += p->size;
break;
}
temp = temp->next;
}
}
// Driver Code
int main()
{
int blockSize[] = { 100, 500, 200 };
int processSize[] = { 417, 112, 426, 95 };
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
for (int i = 0; i < m; i++)
create_free(blockSize[i]);
for (int i = 0; i < n; i++)
create_alloc(processSize[i]);
print_alloc();
// Block of tag id 0 deleted
// to free space for block of size 426
delete_alloc(0);
create_alloc(426);
printf("After deleting block with tag id 0.\n");
print_alloc();
}