process.h

  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
#include <string>
#include <vector>
using namespace std;
const int MEM_SIZE = 32;
struct Segment
{
int size;
bool status;
};
class Process
{
protected:
int PID;
int MEMORY;
string Name;
int nSegments;
public:
Process();
void SetPID(int x);
int GetPID();
void SetMEMORY(int x);
int GetMEMORY();
void SetName(string x);
string GetName();
int GetNSegments();
void SetNSegments(int x);
Segment segments[5];
int begining_of_segments[5];
};
class Dispatcher
{
public:
vector <Process> proc;
bool ram[MEM_SIZE];
vector <bool> hard;
int freeMem();
Dispatcher();
int Ask(int size);
void Swap(int size, bool insert, Process seg);
void AntiSwap(int size, Process seg);
};
int Dispatcher::Ask(int size){
int fst = -1;
for(int i = 0; i < MEM_SIZE;){
int k = 0;
while(k + i < MEM_SIZE && !ram[k + i])
k++;
if (k >= size){
fst = i;
break;
}else
i = i + k + 1;
}
return fst;
}
void Dispatcher::Swap(int size, bool insert, Process seg)
{
int fst;
if(fst = Dispatcher::Ask(size) != -1)
if(insert)
{
for(int j = 0; j < 5; j++)
if(seg.segments[j].status)
{
seg.segments[j].status = false; //При привязке сегментов к процессу не забудь статус сделать true
break;
}
for(int j = 0; j <= size; j++)
hard.push_back(true);
}
else
{
Dispatcher::Swap(size/2, false, seg);
Dispatcher::Swap(size/2, true, seg);
}
}
void Dispatcher::AntiSwap(int size, Process seg)
{
for(int i = 0; i < 5; i++)
seg.segments[i].status = true;
}
Dispatcher::Dispatcher()
{
for (int i = 0; i < MEM_SIZE; i++)
ram[i] = false;
}
Process::Process()
{
for(int i = 0; i < 5; i++)
segments[i].status = false;
}
int Dispatcher::freeMem()
{
int n = 0;
for (int i = 0; i < MEM_SIZE; i++)
if (!ram[i]) n++;
return n;
}
int Process::GetNSegments()
{
return nSegments;
}
void Process::SetNSegments(int x)
{
nSegments = x;
}
void Process::SetPID(int x)
{
PID = x;
}
int Process::GetPID()
{
return PID;
}
void Process::SetMEMORY(int x)
{
MEMORY = x;
}
int Process::GetMEMORY()
{
return MEMORY;
}
void Process::SetName(string x)
{
Name = x;
}
string Process::GetName()
{
return Name;
}