Repository navigation
Expand file tree
/
Copy pathMemLeak.cpp
More file actions
185 lines (151 loc) · 3.47 KB
/
Copy pathMemLeak.cpp
File metadata and controls
185 lines (151 loc) · 3.47 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
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
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <thread>
using namespace std;
class Process {
private:
int id;
double execution_time;
bool isAlive;
public: void setID(int n){
id = n;
}
public: void setExecution(int secs){
execution_time = secs;
}
public: void setStatus(bool st){
isAlive = st;
}
public: bool getStatus(){
return isAlive;
}
public: int getExecution(){
return execution_time;
}
public: int getID(){
return id;
}
};
class CPU{
public:
Process *p;
CPU(){
p = new Process; //Leak
}
public: void SetProcess(Process process){
*p = process;
}
public: void run(){
for(int sec= p -> getExecution();sec>= 0 ;sec--){
Sleep(1000);
cout << "running" <<sec << endl;
}
}
public: void displayData(){
cout << "P#" << p -> getID() << " Execution Time Left: " << p -> getExecution() << " Running: " << p -> getStatus() << endl;
}
};
class LinkedList{
public:
typedef struct Node{
Process process;
Node* next;
} *ListPtr;
ListPtr header;
ListPtr previous;
int size;
LinkedList(){
header = NULL;
previous = NULL;
size = 0;
}
~LinkedList(){
}
public: void insertProcess(Process p){
ListPtr new_process = new Node;
new_process -> process = p;
new_process -> next = NULL;
if(header == NULL){
header = new_process;
previous = header;
} else {
previous -> next = new_process;
previous = new_process;
}
size ++;
}
public: Process removeProcess(){
Process p;
if(size > 0){
if(header -> next == NULL){
p = header -> process;
header = NULL;
} else {
ListPtr new_header = new Node; // Leak
new_header = header -> next;
header -> next = NULL;
header = new_header;
}
size --;
return p;
}
}
public: int getSize(){
return size;
}
public: void showList(){
ListPtr curr;
curr = header;
while(curr != NULL){
Process p = curr -> process;
cout << "P#" << p.getID() <<endl;
curr = curr -> next;
}
}
};
class Scheduler{
private:
CPU cpu[4];
int id;
LinkedList L;
public:
Scheduler(){
id = 0;
for(int i = 0 ; i < 1000 ; i++){
Process p;
int time = rand() % 10 + 1;
p.setExecution(time);
p.setID(id);
id++;
L.insertProcess(p);
}
}
public: void checkCPU(){
}
public: void createProcess(){
Process p;
int time = rand() % 10 + 1;
p.setExecution(time);
p.setID(id);
id++;
}
public: void displaySimulation(){
while(true){
for(int i = 0; i < 4 ; i++){
cout << "--------------------------------" <<endl;
cout << "CPU: [" << i << "] : ";
cpu[i].displayData();
cout << "--------------------------------" <<endl;
}
cout.flush();
Sleep(1000);
cout << '\r';
}
}
};
int main()
{
Scheduler S;
return 0;
}