-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvirtual_mem.c
More file actions
133 lines (122 loc) · 3.16 KB
/
virtual_mem.c
File metadata and controls
133 lines (122 loc) · 3.16 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
#include "virtual_mem.h"
#include <stdio.h>
#include <stdlib.h>
#include "memory.h"
// Virtual Memory Capacity
static int virtual_memory_capacity = 1024;
virtualMemoryQueue *front, *rear, *temp, *t;
int virtualMemoryEnqueue(int pid, int processSize) {
if (processSize <= virtual_memory_capacity) {
Chunk *processReturn = (Chunk *)malloc(sizeof(Chunk));
virtualMemoryQueue *newnode =
(virtualMemoryQueue *)malloc(sizeof(virtualMemoryQueue));
newnode->next = NULL;
newnode->pid = pid;
newnode->size = processSize;
if (front == NULL) {
front = newnode;
rear = newnode;
} else {
rear->next = newnode;
rear = newnode;
}
virtual_memory_capacity -= processSize;
processReturn->pid = pid;
processReturn->size = processSize;
return 0;
} else {
/*
Error: Process size is too large
*/
return -1;
}
}
void *virtualMemoryDequeue(int freeMemory) {
if (freeMemory > front->size) {
if (front == NULL) {
/*
Error:Virtual Memory is Empty
*/
return NULL;
} else {
Chunk *processReturn = (Chunk *)malloc(sizeof(Chunk));
processReturn->size = front->size;
processReturn->pid = front->pid;
// Note: we cannot simply do processReturn = front; here as it is getting
// cleared when we free(temp)
temp = front;
front = front->next;
free(temp);
virtual_memory_capacity += processReturn->size;
return processReturn;
}
}
}
Chunk *get_process_virt_mem(int pid) {
temp = front;
t = front;
while (temp != NULL && temp->pid != pid) {
t = temp;
temp = temp->next;
}
if (temp == NULL) {
return (Chunk *)-1;
}
if (temp == front) {
front = front->next;
} else if (temp == rear) {
rear = t;
rear->next = NULL;
} else {
t->next = temp->next;
}
virtual_memory_capacity += temp->size;
Chunk *ret = (Chunk *)calloc(1, sizeof(Chunk));
ret->pid = temp->pid;
ret->size = temp->size;
free(temp);
return ret;
}
void display_virtual_memory() {
temp = front;
printf("Printing Status of Virtual memory : ");
if (front != NULL) {
// printf("Memory Free Size=%d\nMemory contains:\n",
// virtual_memory_capacity);
while (temp != NULL) {
printf("%d:%d \t", temp->pid,
temp->size); // Display the variables from top -> bottom
temp = temp->next;
}
} else {
printf("< EMPTY >");
}
printf("\n");
}
void flushQueue() // To Empty the Queue
{
if (front != NULL) {
while (front != NULL) {
temp = front;
front = front->next;
free(temp);
}
}
}
// // For debugging
// void main() {
// void *temp1 = virtualMemoryEnqueue(1, 200);
// display_virtual_memory();
// void *temp2 = virtualMemoryEnqueue(2, 500);
// display_virtual_memory();
// // Temp3 will be null and it wont be pushed to the queue since
// // 1200>(1024-200-500)
// void *temp3 = virtualMemoryEnqueue(3, 1200);
// display_virtual_memory();
// // Free memory > Front->size
// virtualMemoryDequeue(300);
// display_virtual_memory();
// // Free memory < Front->size
// virtualMemoryDequeue(200);
// display_virtual_memory();
// }