-
Notifications
You must be signed in to change notification settings - Fork 0
/
banking.c
202 lines (170 loc) · 4.42 KB
/
banking.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <sys/types.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "structs.h"
#include "queue.h"
#define CLIENT_COUNT 5
#define CALL_COUNT 10
Operation operation;
Answer answers[CLIENT_COUNT];
pthread_mutex_t print_mutex;
int can_perform_op(OpCode code, int client_id, int locked_by) {
// 1 = ok, 0 = later, -1 = error
switch(code) {
case OP_LOOK:
return 1;
case OP_LOCK:
return locked_by == client_id || locked_by == -1 ? 1 : 0;
case OP_UNLOCK:
case OP_INC:
case OP_DEC:
return locked_by == client_id ? 1 : -1;
}
return 0;
}
char *code_to_text(OpCode code) {
switch(code) {
case OP_LOCK: return "LOCK";
case OP_UNLOCK: return "UNLOCK";
case OP_LOOK: return "LOOK";
case OP_INC: return "INC";
case OP_DEC: return "DEC";
}
return "unknown op";
}
void *bank_server(void *args) {
OpQueue queue;
init_opqueue(&queue);
int balance = 0;
int locked_by = -1;
while(1) {
Operation *op = NULL;
if(operation.status == 1) {
pthread_mutex_lock(&operation.mutex);
op = clone_op(&operation);
operation.status = 0;
pthread_mutex_unlock(&operation.mutex);
fprintf(stderr, "[Thread %d], operation %s, new op", op->client_id, code_to_text(op->code));
}
else if(locked_by == -1) {
op = pop_op(&queue);
if(op != NULL)
{
fprintf(stderr, "[Thread %d], operation %s, popped op", op->client_id, code_to_text(op->code));
}
}
else {
continue;
}
if(op == NULL) {
continue;
}
int is_performable = can_perform_op(op->code, op->client_id, locked_by);
if(is_performable != 0) {
pthread_mutex_lock(&op->put_answer_to->mutex);
if(is_performable == 1) {
switch(op->code) {
case OP_LOOK:
op->put_answer_to->comment = "";
break;
case OP_LOCK:
op->put_answer_to->comment = "successfully locked";
locked_by = op->client_id;
break;
case OP_UNLOCK:
op->put_answer_to->comment = "successfully unlocked";
locked_by = -1;
break;
case OP_INC:
balance += op->value;
op->put_answer_to->comment = "successfuly added";
break;
case OP_DEC:
balance -= op->value;
op->put_answer_to->comment = "successfully withdrawed";
break;
}
fprintf(stderr, " - completed\n");
}
else {
op->put_answer_to->comment = "balance not locked";
fprintf(stderr, " - declined\n");
}
op->put_answer_to->status = is_performable;
op->put_answer_to->value = balance;
// free(op);
pthread_cond_signal(&op->put_answer_to->cond);
pthread_mutex_unlock(&op->put_answer_to->mutex);
}
else {
push_op(&queue, op);
fprintf(stderr, " - pushed to queue\n");
}
}
return NULL;
}
void *bank_client(void *args) {
int client_id = *(int*)args;
Answer *answer = &answers[client_id];
for(int i = 0; i < CALL_COUNT; ++i) {
OpCode call_code = i + 1 != CALL_COUNT ?
(OpCode)(rand() % 5) :
OP_UNLOCK;
int value = rand() % 100;
while(operation.status != 0) {
}
pthread_mutex_lock(&operation.mutex);
fill_op(&operation, answer, call_code, client_id, value);
pthread_mutex_unlock(&operation.mutex);
pthread_mutex_lock(&answer->mutex);
while(answer->status == 0) {
pthread_cond_wait(&answer->cond, &answer->mutex);
}
pthread_mutex_unlock(&answer->mutex);
pthread_mutex_lock(&print_mutex);
if(answer->status == 1) {
switch(call_code) {
case OP_LOOK:
printf(
"[Thread %d]%s current balance %d\n",
client_id, answer->comment, answer->value);
break;
case OP_LOCK:
printf("[Thread %d] %s\n", client_id, answer->comment);
break;
case OP_UNLOCK:
printf("[Thread %d] %s\n", client_id, answer->comment);
break;
case OP_INC:
case OP_DEC:
printf(
"[Thread %d] %s %d, current balance %d\n",
client_id, answer->comment, value, answer->value);
break;
}
}
else {
printf("[Thread %d] Error: %s (tried %s)\n", client_id, answer->comment, code_to_text(call_code));
}
fflush(stdout);
pthread_mutex_unlock(&print_mutex);
answer->status = 0;
}
return NULL;
}
int main() {
pthread_t server_th;
pthread_t client_ths[CLIENT_COUNT];
init_op(&operation);
pthread_mutex_init(&print_mutex, NULL);
pthread_create(&server_th, NULL, bank_server, NULL);
for(int i = 0; i < CLIENT_COUNT; ++i) {
int *idx = (int*)malloc(sizeof(int));
*idx = i;
init_answer(&answers[i]);
pthread_create(&client_ths[i], NULL, bank_client, idx);
}
pthread_join(server_th, NULL);
return 0;
}