-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOS Task BankersAlgorithm.cpp
More file actions
158 lines (134 loc) · 3.46 KB
/
OS Task BankersAlgorithm.cpp
File metadata and controls
158 lines (134 loc) · 3.46 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
#define PTW32_NO_TIMESPEC
#define _TIMESPEC_DEFINED
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <windows.h>
#include <thread>
using namespace std;
#define NUM 5
int Resource[NUM];
int Claim[NUM][NUM];
int Allocation[NUM][NUM];
int C_sub_A[NUM][NUM];
pthread_mutex_t m;
pthread_cond_t c;
int isSafe() {
int Work[NUM];
int Finish[NUM] = { 0 };
for (int i = 0; i < NUM; i++) {
Work[i] = Resource[i];
}
int cnt = 0;
while (cnt < NUM) {
int i;
for (i = 0; i < NUM; i++) {
if (!Finish[i]) {
int j;
for (j = 0; j < NUM; j++) {
if (C_sub_A[i][j] > Work[j]) break;
}
if (j == NUM) {
for (int k = 0; k < NUM; k++) Work[k] += Allocation[i][k];
Finish[i] = 1;
cnt++;
break;
}
}
}
if (i == NUM) break;
}
return (cnt == NUM);
}
int Pick(int PhilNum) {
int Req[NUM] = { 0 };
Req[PhilNum] = 1;
Req[(PhilNum + 1) % NUM] = 1;
for (int i = 0; i < NUM; i++) {
if (Req[i] > C_sub_A[PhilNum][i] || Req[i] > Resource[i])
return 0;
}
for (int i = 0; i < NUM; i++) {
Resource[i] -= Req[i];
Allocation[PhilNum][i] += Req[i];
C_sub_A[PhilNum][i] -= Req[i];
}
if (isSafe()) {
return 1;
}
else {
for (int i = 0; i < NUM; i++) {
Resource[i] += Req[i];
Allocation[PhilNum][i] -= Req[i];
C_sub_A[PhilNum][i] += Req[i];
}
return 0;
}
}
void Put(int PhilNum) {
for (int i = 0; i < NUM; i++) {
if (Allocation[PhilNum][i] > 0) {
Resource[i] += Allocation[PhilNum][i];
C_sub_A[PhilNum][i] += Allocation[PhilNum][i];
Allocation[PhilNum][i] = 0;
printf("philosopher %d puts down fork %d.\n", PhilNum, i);
}
}
pthread_cond_broadcast(&c);
}
void Think(int PhilNum) {
printf("philosopher %d is Thinking\n", PhilNum);
return;
}
void Eat(int PhilNum) {
printf("philosopher %d is Eating\n", PhilNum);
return;
}
void* Phil(void* num) {
int PhilNum = (int)(long)num;
while (1) {
Think(PhilNum);
pthread_mutex_lock(&m);
while (!Pick(PhilNum)) {
pthread_cond_wait(&c, &m);
}
printf("philosopher %d picks up forks %d and %d.\n", PhilNum, PhilNum, (PhilNum + 1) % NUM);
pthread_mutex_unlock(&m);
Eat(PhilNum);
pthread_mutex_lock(&m);
Put(PhilNum);
pthread_mutex_unlock(&m);
}
return NULL;
}
int main() {
pthread_t Thrds[NUM];
for (int i = 0; i < NUM; i++) {
Resource[i] = 1;
for (int j = 0; j < NUM; j++) {
Allocation[i][j] = 0;
Claim[i][j] = 0;
}
}
for (int i = 0; i < NUM; i++) {
Claim[i][i] = 1;
Claim[i][(i + 1) % NUM] = 1;
for (int j = 0; j < NUM; j++) {
C_sub_A[i][j] = Claim[i][j] - Allocation[i][j];
}
}
pthread_mutex_init(&m, NULL);
pthread_cond_init(&c, NULL);
for (long i = 0; i < NUM; i++) {
pthread_create(&Thrds[i], NULL, Phil, (void*)i);
}
for (int i = 0; i < NUM; i++) {
pthread_join(Thrds[i], NULL);
}
pthread_mutex_destroy(&m);
pthread_cond_destroy(&c);
return 0;
}