-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhilosophyEating.cpp
166 lines (141 loc) · 4.33 KB
/
PhilosophyEating.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
#include "PhilosophyEating.h"
#include <random>
#include <qpainter.h>
#include <qdebug.h>
#include <qpalette.h>
#define COLOR_AVAI QColor(0, 0, 0)
#define COLOR_1 QColor(255, 0, 0)
#define COLOR_2 QColor(0, 255, 0)
#define COLOR_3 QColor(255, 165, 0)
#define COLOR_4 QColor(128, 0, 128)
#define COLOR_5 QColor(0, 0, 255)
#define COLOR_T_1 QColor(255, 0, 0, 100)
#define COLOR_T_2 QColor(0, 255, 0, 100)
#define COLOR_T_3 QColor(255, 165, 0, 100)
#define COLOR_T_4 QColor(128, 0, 128, 100)
#define COLOR_T_5 QColor(0, 0, 255, 100)
HANDLE philosophers[PHI_NUM];
int chopsticksStatus[CHOPS_NUM];
int philosophyStatus[PHI_NUM][2];
CRITICAL_SECTION mutex[CHOPS_NUM];
PhilosophyEating* pe;
void beginEating(PhilosophyEating* pe);
//deadLockSolution
void thinking(int philosopherID);
void eating(int philosopherID);
void getChopsticksDeadLock(int philosopherID, int chopstickID);
void putChopsticksDeadLock(int philosopherID, int chopstickID);
void setChopstickToPhi(QLabel* label, QColor c);
DWORD WINAPI philosopherDeadLock(LPVOID lpParameter);
PhilosophyEating::PhilosophyEating(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
chops[0] = ui.chop1;
chops[1] = ui.chop2;
chops[2] = ui.chop3;
chops[3] = ui.chop4;
chops[4] = ui.chop5;
phis[0] = ui.phi1;
phis[1] = ui.phi2;
phis[2] = ui.phi3;
phis[3] = ui.phi4;
phis[4] = ui.phi5;
color[0] = COLOR_1;
color[1] = COLOR_2;
color[2] = COLOR_3;
color[3] = COLOR_4;
color[4] = COLOR_5;
color[5] = COLOR_AVAI;
pe = this;
connect(this, SIGNAL(sendSignal(QLabel * label, QColor c)), this, SLOT(setChopstickToPhi(QLabel * label, QColor c)));
for (int i(0); i < 5; i++)
{
setChopstickToPhi(chops[i], color[i]);
setChopstickToPhi(phis[i], color[i]);
}
int i = 0;
beginEating(this);
}
DWORD WINAPI philosopherDeadLock(LPVOID lpParameter)
{
int id = (int)lpParameter;
//pInfo pinfo = (pInfo)lpParameter;
//int id = pinfo->id;
//qDebug() << "in:" << pinfo->id;
PhilosophyEating* handle = pe;
int leftID = id;
int rightID = (id + 1) % PHI_NUM;
while (true)
{
emit(handle->sendSignal(handle->phis[id], handle->color[5]));
thinking(id);
emit(handle->sendSignal(handle->phis[id], handle->color[id]));
getChopsticksDeadLock(id, leftID);
emit(handle->sendSignal(handle->chops[leftID], handle->color[id]));
getChopsticksDeadLock(id, rightID);
emit(handle->sendSignal(handle->chops[rightID], handle->color[id]));
eating(id);
putChopsticksDeadLock(id, leftID);
emit handle->sendSignal(handle->chops[leftID], handle->color[5]);
putChopsticksDeadLock(id, rightID);
emit handle->sendSignal(handle->chops[rightID], handle->color[5]);
}
}
void thinking(int philosopherID)
{
qDebug() << "**************************Thinking**********************\n";
Sleep((rand() % 4 + 1) * 100);
qDebug() << "**************************Finish Thinking**********************\n";
}
void eating(int philosopherID)
{
qDebug() << "*************************Eating**************************\n";
Sleep((rand() % 4 + 1) * 100);
qDebug() << "**************************Finish Eating************************\n";
}
void getChopsticksDeadLock(int philosopherID, int chopstickID)
{
EnterCriticalSection(&(mutex[chopstickID]));
chopsticksStatus[chopstickID] = philosopherID;
qDebug() << chopstickID << "->" << philosopherID << "\n";
}
void putChopsticksDeadLock(int philosopherID, int chopstickID)
{
LeaveCriticalSection(&(mutex[chopstickID]));
chopsticksStatus[chopstickID] = CHOPS_AVAIl;
qDebug() << chopstickID << "----Empty\n";
}
void beginEating(PhilosophyEating* pe)
{
int i;
Info info[5];
for (i = 0; i < CHOPS_NUM; i++)
{
chopsticksStatus[i] = CHOPS_AVAIl;
InitializeCriticalSection(&(mutex[i]));
}
for (i = 0; i < PHI_NUM; i++)
{
philosophyStatus[i][0] = PHI_EMPTY;
philosophyStatus[i][1] = PHI_EMPTY;
}
DWORD ThreadID;
for (i = 0; i < PHI_NUM; i++)
{
info[i].id = i;
info[i].point = pe;
qDebug() << "out:"<<info[i].id;
philosophers[i] = CreateThread(NULL, 0, philosopherDeadLock, (LPVOID)(i), 0, &ThreadID);
}
//WaitForMultipleObjects(5, philosophers, false, INFINITE);
}
/*****************************************************************************/
void PhilosophyEating::setChopstickToPhi(QLabel* label, QColor c)
{
qDebug() << "yes";
QPalette label_palette;
label_palette.setColor(QPalette::Background, c);
label->setAutoFillBackground(true);
label->setPalette(label_palette);
}