-
Notifications
You must be signed in to change notification settings - Fork 1
/
Memory.cpp
180 lines (149 loc) · 4.04 KB
/
Memory.cpp
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
//
// Memory.cpp
//
#include <iostream>
#include <string>
#include <sstream>
#include <stddef.h>
#include <list>
#include <vector>
#include <algorithm>
#include <fstream>
#include <math.h>
#include "IO.h"
#include "Memory.h"
using namespace std;
//check if there is a hole in memory that is bigger than the size of the new process
Memory::PCB* Memory::findHole(double size){
PCB* current;
current = head;
while(current != tail){
if(current -> next -> begin - current -> end > size)
return current;
else
current = current -> next;
}
return NULL;
}
//find pid in memory
Memory::PCB* Memory::findPidInMem(int wantPid){
PCB* check;
check = head;
while(check != tail){
if(check->pid == wantPid)
return check;
else
check = check -> next;
}
return NULL;
}
//command - A priority size
void Memory::commandAdd(int priority, double size){
PCB* firstNode = NULL;
PCB* newNode;
PCB* nextNode;
PCB* limitNode;
//create first node
if(head == NULL){
firstNode = new PCB;
firstNode -> end = -1;
head = firstNode;
}
newNode = new PCB;
newNode -> pid = ++gpid;
newNode -> priority = priority;
addProcessToReadyQueue(priority, gpid);
if(gpid == 1){
//create first node
newNode -> begin = 0;
newNode -> end = size -1;
newNode -> prev = firstNode;
firstNode -> next = newNode;
//add limit node
limitNode = new PCB;
limitNode -> begin = size_of_memory;
newNode -> next = limitNode;
limitNode -> prev = newNode;
tail = limitNode;
}
else{
PCB* node = findHole(size);
if(node == NULL){ // if there is no hole
cout << "*Error; There is no enough space in Memory!" << endl;
}
else{
nextNode = node -> next;
newNode -> next = nextNode;
newNode -> prev = node;
node -> next = newNode;
nextNode -> prev = newNode;
newNode -> begin = newNode -> prev -> end +1;
newNode -> end = newNode -> begin + size -1;
}
}
}
//command - t
void Memory::commandTerminate(){
//remove the process from memory
removeProcessFromMem();
//remove from CPU
removeProcessfromCPU();
}
//remove process from Mem when terminate
void Memory::removeProcessFromMem(){
PCB* checkPid;
PCB* sucNode;
PCB* preNode;
checkPid = head;
while(checkPid -> pid != current_process_in_CPU_pid){
checkPid = checkPid -> next;
}
sucNode = checkPid -> next;
preNode = checkPid -> prev;
sucNode -> prev = preNode;
if(preNode != 0)
preNode -> next = sucNode;
if(checkPid == head)
head = sucNode;
}
//command - D #
void Memory::commandFinishWorkHardDisk(int num){
PCB* temp;
if(!hardDisks[num-1].empty()){
temp = findPidInMem(hardDisks[num-1].front());
if(temp == NULL){
cout << "*Error; didn't find the pid in Memory" << endl;
}
else{
addProcessToReadyQueue(temp -> pid, hardDisks[num-1].front());
hardDisks[num-1].pop_front();
}
}
if(hardDisks[num-1].empty())
checkIODevices = false;
}
//command - P #
void Memory::commandFinishWorkPrint(int num){
PCB* temp;
if(!printers[num-1].empty()){
temp = findPidInMem(printers[num-1].front());
if(temp == NULL){
cout << "*Error; didn't find the pid in memory" << endl;
}
else{
addProcessToReadyQueue(temp -> pid, printers[num-1].front());
printers[num-1].pop_front();
}
}
if(printers[num-1].empty())
checkIODevices = false;
}
//command - S m
void Memory::commandShowMemory(){
PCB* p;
p = head -> next;
while(p != tail){
cout << "P" << p -> pid << ": " << p -> begin << " - " << p -> end << endl;
p = p -> next;
}
}