/
Banker_System.cpp
146 lines (121 loc) · 2.95 KB
/
Banker_System.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
#include "Banker_System.h"
#include "Banker_Process.h"
#include <iostream>
#include <string>
#include <Windows.h>
void System::setMaxAvailable(unsigned int max)
{
maxAvailable = max > 0 ? max : 0;
}
void System::setAvailable(unsigned int num)
{
available = num > 0 ? num : 0;
}
unsigned int System::getMaxAvailable(void) const
{
return maxAvailable;
}
unsigned int System::getAvailable(void) const
{
return available;
}
void System::wait(void)
{
WaitForSingleObject(hMutex,INFINITE);
}
void System::release(void)
{
ReleaseSemaphore(hMutex, 1, NULL);
}
void System::printProcessList(std::ostream &os)
{
for(auto first = processUid.begin(); first != processUid.end(); ++first)
{
os << *first << " ";
}
os << "\r\n";
}
unsigned int System::attainResource(Process *process)
{
wait();
int rtn = 0;
unsigned int requestNum = process->getRequestNeed();
unsigned int available_bk = getAvailable();
unsigned int ownNeed_bk = process->getOwnNeed();
std::string prompt;
if(requestNum <= getAvailable())
{
setAvailable(getAvailable() - requestNum);
process->setOwnNeed(process->getOwnNeed() + requestNum);
prompt = prompt + "\nsystem" + "\t" + std::to_string(available_bk) + '\n';
std::cout << prompt;
prompt.clear();
if(banker(process))
{
processList.insert(process);
prompt = prompt + "Thread" + "\t" + std::to_string(process->getUid()) + "\t" + std::to_string(process->getMaxNeed()) + "\t"
+ std::to_string(process->getOwnNeed()) + "\t" + std::to_string(process->getRequestNeed()) + "\t" + "Success\n";
std::cout << prompt;
rtn = 0;
}
else
{
setAvailable(available_bk);
process->setOwnNeed(ownNeed_bk);
prompt = prompt + "Thread" + "\t" + std::to_string(process->getUid()) + "\t" + std::to_string(process->getMaxNeed()) + "\t"
+ std::to_string(process->getOwnNeed()) + "\t" + std::to_string(process->getRequestNeed()) + "\t" + "Failure\n";
std::cout << prompt;
rtn = 1;
}
printProcessList(std::cout);
}
else
{
rtn = 1;
}
release();
return rtn;
}
bool System::banker(Process *process)
{
bool rtn;
unsigned int available_bk = getAvailable();
unsigned int num = process->getRequestNeed();
processUid.clear();
for(auto i = processList.begin(); i != processList.end(); )
{
if(false == (*i)->isFinish() && (*i)->getMaxNeed() - (*i)->getOwnNeed() <= getAvailable())
{
(*i)->setFinish(true);
setAvailable(getAvailable()+(*i)->getOwnNeed());
processUid.push_back((*i)->getUid());
i = processList.begin();
}
else
{
++i;
}
}
rtn = true;
for(auto i = processList.begin(); i != processList.end(); ++i)
{
if((*i)->isFinish() == false)
rtn = false;
(*i)->setFinish(false);
}
setAvailable(available_bk);
return rtn;
}
bool System::freeResource(Process *process)
{
wait();
bool rtn;
if(processList.find(process) == processList.end())
rtn = false;
else
rtn = true;
setAvailable(getAvailable() + process->getOwnNeed());
processList.erase(process);
release();
return rtn;
}