jekyll-theme-gaeblogx 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +9 -0
- data/_includes/algolia.html +75 -0
- data/_includes/backToTop.html +5 -0
- data/_includes/category.html +19 -0
- data/_includes/comments.html +46 -0
- data/_includes/footer.html +33 -0
- data/_includes/head.html +58 -0
- data/_includes/header.html +34 -0
- data/_includes/previousAndNext.html +13 -0
- data/_includes/sidebar-search.html +25 -0
- data/_includes/tag.html +16 -0
- data/_layouts/default.html +26 -0
- data/_layouts/demo.html +10 -0
- data/_layouts/page.html +31 -0
- data/_layouts/post.html +107 -0
- data/_sass/_backToTop.scss +49 -0
- data/_sass/_demo.scss +65 -0
- data/_sass/_footer.scss +63 -0
- data/_sass/_header.scss +174 -0
- data/_sass/_index.scss +174 -0
- data/_sass/_layout.scss +242 -0
- data/_sass/_page.scss +238 -0
- data/_sass/_post-old.scss +109 -0
- data/_sass/_post.scss +83 -0
- data/_sass/_reset.scss +119 -0
- data/_sass/_scrollbar.scss +35 -0
- data/_sass/_syntax-highlighting.scss +99 -0
- data/assets/10.jpg +0 -0
- data/assets/AWS-Introduction-ec2.png +0 -0
- data/assets/Cache-Oblivious-Algorithms-FunnelSort.jpg +0 -0
- data/assets/Condition-Variables-fig1.png +0 -0
- data/assets/Dynamic-Compilation-adaptJVM.pdf +0 -0
- data/assets/Dynamic-Compilation-dyncomp.pdf +0 -0
- data/assets/Fibonacci-Heap-Fig3.png +0 -0
- data/assets/Fibonacci-Heap-Fig5.png +0 -0
- data/assets/Fibonacci-Heap-Fig6.png +0 -0
- data/assets/Fibonacci-Heap-Fig7.png +0 -0
- data/assets/File-System-bc.jpg +0 -0
- data/assets/File-System-filetable.rich.jpg +0 -0
- data/assets/GIL-Battle.png +0 -0
- data/assets/GIL-CPU.png +0 -0
- data/assets/GIL-Check.png +0 -0
- data/assets/GIL-IO.png +0 -0
- data/assets/GIL-Measure-1.png +0 -0
- data/assets/GIL-Measure-2.png +0 -0
- data/assets/GIL-Signal.png +0 -0
- data/assets/GIL-Tick.png +0 -0
- data/assets/Garbage-Collection-Slides-gc.pdf +0 -0
- data/assets/Garbage-Collection-copying-1.png +0 -0
- data/assets/Garbage-Collection-copying-2.png +0 -0
- data/assets/Garbage-Collection-free-list.png +0 -0
- data/assets/Garbage-Collection-generation-1.png +0 -0
- data/assets/Garbage-Collection-mark-sweep.png +0 -0
- data/assets/Garbage-Collection-nursery-1.png +0 -0
- data/assets/Garbage-Collection-nursery-2.png +0 -0
- data/assets/Garbage-Collection-nursery-3.png +0 -0
- data/assets/Garbage-Collection-nursery-4.png +0 -0
- data/assets/Garbage-Collection-nursery-5.png +0 -0
- data/assets/Garbage-Collection-process.png +0 -0
- data/assets/Garbage-Collection-ref-counting.png +0 -0
- data/assets/Interpreter-Optimization-interp.pdf +0 -0
- data/assets/Memory-Management-fig1.png +0 -0
- data/assets/Memory-Management-fig2.png +0 -0
- data/assets/Memory-Management-fig3.png +0 -0
- data/assets/Memory-Management-fig4.png +0 -0
- data/assets/Memory-Management-fig5.png +0 -0
- data/assets/Memory-Management-fig6.png +0 -0
- data/assets/Memory-Management-fig7.png +0 -0
- data/assets/Memory-Management-fig8.png +0 -0
- data/assets/Multicore-GIL-1.png +0 -0
- data/assets/Multicore-GIL-2.png +0 -0
- data/assets/My-Photo.JPG +0 -0
- data/assets/Operating-System-Virtualization-ept.png +0 -0
- data/assets/Operating-System-Virtualization-guest-page.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-arch.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-process.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-qemu.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-state.png +0 -0
- data/assets/Operating-System-Virtualization-vtx.png +0 -0
- data/assets/Operating-System-Virtualization-xen-arch.png +0 -0
- data/assets/Program-Profiling-profiling.pdf +0 -0
- data/assets/Programming-Language-Virtual-Machine-vm.pdf +0 -0
- data/assets/Remote-Shell-Session-Setup-1.png +0 -0
- data/assets/Signals-1.png +0 -0
- data/assets/Signals-2.png +0 -0
- data/assets/Signals-3.png +0 -0
- data/assets/Some-Slides-about-Containers-Kubernetes.pdf +0 -0
- data/assets/Some-Slides-about-Containers-UCSB-nurmi.pdf +0 -0
- data/assets/Some-Slides-about-PaaS-CS293B_CloudPlatforms.pdf +0 -0
- data/assets/avg-1thread.c +172 -0
- data/assets/avg-manythread.c +284 -0
- data/assets/avg-nothread.c +71 -0
- data/assets/file-create1.c +75 -0
- data/assets/file-create2.c +120 -0
- data/assets/file-fd1.c +56 -0
- data/assets/file-fd2.c +25 -0
- data/assets/file-fd3.c +23 -0
- data/assets/file-read1.c +62 -0
- data/assets/file-read2.c +66 -0
- data/assets/file-seek1.c +87 -0
- data/assets/fork-1.c +35 -0
- data/assets/fork-2.c +44 -0
- data/assets/fork-3.c +60 -0
- data/assets/fork-4.c +64 -0
- data/assets/joinall-1.c +163 -0
- data/assets/joinall-2.c +162 -0
- data/assets/market-kthreads.c +465 -0
- data/assets/market-semaphore.c +504 -0
- data/assets/market1.c +478 -0
- data/assets/market2.c +490 -0
- data/assets/market3.c +503 -0
- data/assets/market4.c +509 -0
- data/assets/my-cat.c +35 -0
- data/assets/pipe-1.c +45 -0
- data/assets/pipe-2.c +80 -0
- data/assets/pipe-3.c +83 -0
- data/assets/pipe-4.c +116 -0
- data/assets/race1.c +75 -0
- data/assets/race2.c +77 -0
- data/assets/race3.c +87 -0
- data/assets/race_ABC.c +144 -0
- data/assets/search-by-algolia.svg +1 -0
- data/assets/semaphore.book.ps +8827 -0
- data/assets/sys-call1.c +20 -0
- data/assets/sys-call2.c +16 -0
- data/assets//350/265/253/347/202/2161.PNG +0 -0
- data/feed.xml +30 -0
- data/index.html +164 -0
- data/js/jekyll-search.min.js +1 -0
- data/js/lunr.min.js +2977 -0
- data/js/main.js +57 -0
- data/js/masonry.pkgd.min.js +9 -0
- data/js/pageContent.js +166 -0
- data/js/search.js +17 -0
- data/js/smooth-scroll.min.js +2 -0
- data/js/waterfall.js +214 -0
- data/page/0archives.html +83 -0
- data/page/1category.html +62 -0
- data/page/2tags.html +64 -0
- data/page/3search.html +112 -0
- data/page/4about.md +34 -0
- metadata +270 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* cs170 -- Rich wolski
|
|
3
|
+
* producer-consumer example
|
|
4
|
+
* uses condition variables for fule/empty conditions
|
|
5
|
+
* uses condition variable to fulfill order
|
|
6
|
+
* uses separate lock for each stock
|
|
7
|
+
*/
|
|
8
|
+
#include <unistd.h>
|
|
9
|
+
#include <stdlib.h>
|
|
10
|
+
#include <stdio.h>
|
|
11
|
+
#include <pthread.h>
|
|
12
|
+
#include <string.h>
|
|
13
|
+
|
|
14
|
+
#include "c-timer.h"
|
|
15
|
+
#include "sema.h"
|
|
16
|
+
|
|
17
|
+
#define RAND() (drand48())
|
|
18
|
+
|
|
19
|
+
struct order
|
|
20
|
+
{
|
|
21
|
+
int stock_id;
|
|
22
|
+
int quantity;
|
|
23
|
+
int action; /* buy or sell */
|
|
24
|
+
sema *fulfilled;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
struct order_que
|
|
28
|
+
{
|
|
29
|
+
struct order **orders;
|
|
30
|
+
int size;
|
|
31
|
+
int head;
|
|
32
|
+
int tail;
|
|
33
|
+
sema *mutex;
|
|
34
|
+
sema *full;
|
|
35
|
+
sema *empty;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
struct stock
|
|
39
|
+
{
|
|
40
|
+
sema *mutex;
|
|
41
|
+
int quantity;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
struct market
|
|
45
|
+
{
|
|
46
|
+
struct stock *stocks;
|
|
47
|
+
int count;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
struct order *InitOrder(int id, int quantity, int action)
|
|
51
|
+
{
|
|
52
|
+
struct order *order;
|
|
53
|
+
|
|
54
|
+
order = (struct order *)malloc(sizeof(struct order));
|
|
55
|
+
if(order == NULL) {
|
|
56
|
+
return(NULL);
|
|
57
|
+
}
|
|
58
|
+
order->stock_id = id;
|
|
59
|
+
order->quantity = quantity;
|
|
60
|
+
order->action = action;
|
|
61
|
+
order->fulfilled = InitSem(0);
|
|
62
|
+
return(order);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
void FreeOrder(struct order *order)
|
|
66
|
+
{
|
|
67
|
+
FreeSem(order->fulfilled);
|
|
68
|
+
free(order);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
struct order_que *InitOrderQue(int size)
|
|
72
|
+
{
|
|
73
|
+
struct order_que *oq;
|
|
74
|
+
|
|
75
|
+
oq = (struct order_que *)malloc(sizeof(struct order_que));
|
|
76
|
+
if(oq == NULL) {
|
|
77
|
+
return(NULL);
|
|
78
|
+
}
|
|
79
|
+
memset(oq,0,sizeof(struct order_que));
|
|
80
|
+
|
|
81
|
+
oq->size = size+1; /* empty condition burns a slot */
|
|
82
|
+
oq->orders = (struct order **)malloc(size*sizeof(struct order *));
|
|
83
|
+
if(oq->orders == NULL) {
|
|
84
|
+
free(oq);
|
|
85
|
+
return(NULL);
|
|
86
|
+
}
|
|
87
|
+
memset(oq->orders,0,size*sizeof(struct order *));
|
|
88
|
+
|
|
89
|
+
oq->mutex = InitSem(1);
|
|
90
|
+
oq->full = InitSem(size);
|
|
91
|
+
oq->empty = InitSem(0);
|
|
92
|
+
|
|
93
|
+
return(oq);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
void FreeOrderQue(struct order_que *oq)
|
|
97
|
+
{
|
|
98
|
+
while(oq->head != oq->tail) {
|
|
99
|
+
FreeOrder(oq->orders[oq->tail]);
|
|
100
|
+
oq->tail = (oq->tail + 1) % oq->size;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
FreeSem(oq->mutex);
|
|
104
|
+
FreeSem(oq->full);
|
|
105
|
+
FreeSem(oq->empty);
|
|
106
|
+
free(oq->orders);
|
|
107
|
+
free(oq);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
struct market *InitMarket(int stock_count, int init_quantity)
|
|
112
|
+
{
|
|
113
|
+
struct market *m;
|
|
114
|
+
int i;
|
|
115
|
+
|
|
116
|
+
m = (struct market *)malloc(sizeof(struct market));
|
|
117
|
+
if(m == NULL) {
|
|
118
|
+
return(NULL);
|
|
119
|
+
}
|
|
120
|
+
m->count = stock_count;
|
|
121
|
+
|
|
122
|
+
m->stocks = (struct stock *)malloc(stock_count*sizeof(struct stock));
|
|
123
|
+
if(m->stocks == NULL) {
|
|
124
|
+
free(m);
|
|
125
|
+
return(NULL);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for(i=0; i < stock_count; i++) {
|
|
129
|
+
m->stocks[i].quantity = init_quantity;
|
|
130
|
+
m->stocks[i].mutex = InitSem(1);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return(m);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
void FreeMarket(struct market *m)
|
|
137
|
+
{
|
|
138
|
+
int i;
|
|
139
|
+
for(i=0; i < m->count; i++) {
|
|
140
|
+
FreeSem(m->stocks[i].mutex);
|
|
141
|
+
}
|
|
142
|
+
free(m->stocks);
|
|
143
|
+
free(m);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
void PrintMarket(struct market *m)
|
|
148
|
+
{
|
|
149
|
+
int i;
|
|
150
|
+
for(i=0; i < m->count; i++) {
|
|
151
|
+
printf("stock: %d, quantity: %d\n",
|
|
152
|
+
i,m->stocks[i].quantity);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
struct client_arg
|
|
159
|
+
{
|
|
160
|
+
int id;
|
|
161
|
+
int order_count;
|
|
162
|
+
struct order_que *order_que;
|
|
163
|
+
int max_stock_id;
|
|
164
|
+
int max_quantity;
|
|
165
|
+
int verbose;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
struct trader_arg
|
|
169
|
+
{
|
|
170
|
+
int id;
|
|
171
|
+
struct order_que *order_que;
|
|
172
|
+
struct market *market;
|
|
173
|
+
int *done;
|
|
174
|
+
int verbose;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
void *ClientThread(void *arg)
|
|
178
|
+
{
|
|
179
|
+
struct client_arg *ca = (struct client_arg *)arg;
|
|
180
|
+
int i;
|
|
181
|
+
int next;
|
|
182
|
+
struct order *order;
|
|
183
|
+
int stock_id;
|
|
184
|
+
int quantity;
|
|
185
|
+
int action;
|
|
186
|
+
int queued;
|
|
187
|
+
double now;
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
for(i=0; i < ca->order_count; i++) {
|
|
191
|
+
/*
|
|
192
|
+
* create an order for a random stock
|
|
193
|
+
*/
|
|
194
|
+
stock_id = (int)(RAND() * ca->max_stock_id);
|
|
195
|
+
quantity = (int)(RAND() * ca->max_quantity);
|
|
196
|
+
if(RAND() > 0.5) {
|
|
197
|
+
action = 0; /* 0 => buy */
|
|
198
|
+
} else {
|
|
199
|
+
action = 1; /* 1 => sell */
|
|
200
|
+
}
|
|
201
|
+
order = InitOrder(stock_id,quantity,action);
|
|
202
|
+
if(order == NULL) {
|
|
203
|
+
fprintf(stderr,"no space for order\n");
|
|
204
|
+
exit(1);
|
|
205
|
+
}
|
|
206
|
+
/*
|
|
207
|
+
* queue it for the traders
|
|
208
|
+
*/
|
|
209
|
+
P(ca->order_que->full);
|
|
210
|
+
/*
|
|
211
|
+
* lock the queue
|
|
212
|
+
*/
|
|
213
|
+
P(ca->order_que->mutex);
|
|
214
|
+
next = (ca->order_que->head + 1) % ca->order_que->size;
|
|
215
|
+
/*
|
|
216
|
+
* there is space in the queue, add the order and bump
|
|
217
|
+
* the head
|
|
218
|
+
*/
|
|
219
|
+
if(ca->verbose == 1) {
|
|
220
|
+
now = CTimer();
|
|
221
|
+
printf("%10.0f client %d: ",now,ca->id);
|
|
222
|
+
printf("queued stock %d, for %d, %s\n",
|
|
223
|
+
order->stock_id,
|
|
224
|
+
order->quantity,
|
|
225
|
+
(order->action ? "SELL" : "BUY"));
|
|
226
|
+
}
|
|
227
|
+
ca->order_que->orders[next] = order;
|
|
228
|
+
ca->order_que->head = next;
|
|
229
|
+
/*
|
|
230
|
+
* signal traders that there is another order
|
|
231
|
+
*/
|
|
232
|
+
V(ca->order_que->empty);
|
|
233
|
+
/*
|
|
234
|
+
* unlock the queue
|
|
235
|
+
*/
|
|
236
|
+
V(ca->order_que->mutex);
|
|
237
|
+
/*
|
|
238
|
+
* wait until the order is fulfilled
|
|
239
|
+
*/
|
|
240
|
+
P(order->fulfilled);
|
|
241
|
+
/*
|
|
242
|
+
* done, free the order and repeat
|
|
243
|
+
*/
|
|
244
|
+
FreeOrder(order);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return(NULL);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
void *TraderThread(void *arg)
|
|
251
|
+
{
|
|
252
|
+
struct trader_arg *ta = (struct trader_arg *)arg;
|
|
253
|
+
int dequeued;
|
|
254
|
+
struct order *order;
|
|
255
|
+
int tail;
|
|
256
|
+
double now;
|
|
257
|
+
int next;
|
|
258
|
+
struct stock *stock;
|
|
259
|
+
|
|
260
|
+
while(1) {
|
|
261
|
+
/*
|
|
262
|
+
* wait until there is a new order
|
|
263
|
+
*/
|
|
264
|
+
P(ta->order_que->empty);
|
|
265
|
+
/*
|
|
266
|
+
* are we done? Tell the next trader and exit
|
|
267
|
+
*/
|
|
268
|
+
if(*(ta->done) == 1) {
|
|
269
|
+
V(ta->order_que->empty);
|
|
270
|
+
return(NULL);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/*
|
|
274
|
+
* lock the que
|
|
275
|
+
*/
|
|
276
|
+
P(ta->order_que->mutex);
|
|
277
|
+
/*
|
|
278
|
+
* get the next order
|
|
279
|
+
*/
|
|
280
|
+
next = (ta->order_que->tail + 1) % ta->order_que->size;
|
|
281
|
+
order = ta->order_que->orders[next];
|
|
282
|
+
ta->order_que->tail = next;
|
|
283
|
+
/*
|
|
284
|
+
* tell the clients there is another free slot
|
|
285
|
+
*/
|
|
286
|
+
V(ta->order_que->full);
|
|
287
|
+
/*
|
|
288
|
+
* unlock the que
|
|
289
|
+
*/
|
|
290
|
+
V(ta->order_que->mutex);
|
|
291
|
+
/*
|
|
292
|
+
* have an order to process
|
|
293
|
+
*/
|
|
294
|
+
stock = &(ta->market->stocks[order->stock_id]);
|
|
295
|
+
P(stock->mutex);
|
|
296
|
+
if(order->action == 1) { /* BUY */
|
|
297
|
+
stock->quantity -= order->quantity;
|
|
298
|
+
if(stock->quantity < 0) {
|
|
299
|
+
stock->quantity = 0;
|
|
300
|
+
}
|
|
301
|
+
} else {
|
|
302
|
+
stock->quantity += order->quantity;
|
|
303
|
+
}
|
|
304
|
+
V(stock->mutex);
|
|
305
|
+
if(ta->verbose == 1) {
|
|
306
|
+
now = CTimer();
|
|
307
|
+
printf("%10.0f trader: %d ",now,ta->id);
|
|
308
|
+
printf("fulfilled stock %d for %d\n",
|
|
309
|
+
order->stock_id,
|
|
310
|
+
order->quantity);
|
|
311
|
+
}
|
|
312
|
+
/*
|
|
313
|
+
* tell the client the order is done
|
|
314
|
+
*/
|
|
315
|
+
V(order->fulfilled);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return(NULL);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
#define ARGS "c:t:o:q:s:V"
|
|
322
|
+
char *Usage = "market1 -c clients -t traders -o orders -q queue-size -s stock-count -V <verbose on>\n";
|
|
323
|
+
|
|
324
|
+
#define INIT_COUNT 5000
|
|
325
|
+
|
|
326
|
+
int main(int argc, char **argv)
|
|
327
|
+
{
|
|
328
|
+
int c;
|
|
329
|
+
int client_threads;
|
|
330
|
+
int trader_threads;
|
|
331
|
+
int orders_per_client;
|
|
332
|
+
int que_size;
|
|
333
|
+
int max_stock;
|
|
334
|
+
int verbose;
|
|
335
|
+
struct client_arg *ca;
|
|
336
|
+
struct trader_arg *ta;
|
|
337
|
+
pthread_t *client_ids;
|
|
338
|
+
pthread_t *trader_ids;
|
|
339
|
+
struct order_que *order_que;
|
|
340
|
+
struct market *market;
|
|
341
|
+
int i;
|
|
342
|
+
int done;
|
|
343
|
+
int err;
|
|
344
|
+
double start;
|
|
345
|
+
double end;
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
/*
|
|
349
|
+
* defaults
|
|
350
|
+
*/
|
|
351
|
+
client_threads = 1;
|
|
352
|
+
trader_threads = 1;
|
|
353
|
+
orders_per_client = 1;
|
|
354
|
+
verbose = 0;
|
|
355
|
+
que_size = 1;
|
|
356
|
+
max_stock = 1;
|
|
357
|
+
|
|
358
|
+
while((c = getopt(argc,argv,ARGS)) != EOF) {
|
|
359
|
+
switch(c) {
|
|
360
|
+
case 'c':
|
|
361
|
+
client_threads = atoi(optarg);
|
|
362
|
+
break;
|
|
363
|
+
case 't':
|
|
364
|
+
trader_threads = atoi(optarg);
|
|
365
|
+
break;
|
|
366
|
+
case 'o':
|
|
367
|
+
orders_per_client = atoi(optarg);
|
|
368
|
+
break;
|
|
369
|
+
case 'q':
|
|
370
|
+
que_size = atoi(optarg);
|
|
371
|
+
break;
|
|
372
|
+
case 's':
|
|
373
|
+
max_stock = atoi(optarg);
|
|
374
|
+
break;
|
|
375
|
+
case 'V':
|
|
376
|
+
verbose = 1;
|
|
377
|
+
break;
|
|
378
|
+
default:
|
|
379
|
+
fprintf(stderr,
|
|
380
|
+
"unrecognized command %c\n",
|
|
381
|
+
(char)c);
|
|
382
|
+
fprintf(stderr,"usage: %s",Usage);
|
|
383
|
+
exit(1);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
client_ids = (pthread_t *)malloc(client_threads*sizeof(pthread_t));
|
|
388
|
+
if(client_ids == NULL) {
|
|
389
|
+
exit(1);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
ca = (struct client_arg *)malloc(client_threads*sizeof(struct client_arg));
|
|
393
|
+
if(ca == NULL) {
|
|
394
|
+
exit(1);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
trader_ids = (pthread_t *)malloc(trader_threads*sizeof(pthread_t));
|
|
398
|
+
if(trader_ids == NULL) {
|
|
399
|
+
exit(1);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
ta = (struct trader_arg *)malloc(trader_threads*sizeof(struct trader_arg));
|
|
403
|
+
if(ta == NULL) {
|
|
404
|
+
exit(1);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
order_que = InitOrderQue(que_size);
|
|
408
|
+
if(order_que == NULL) {
|
|
409
|
+
exit(1);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
market = InitMarket(max_stock,INIT_COUNT);
|
|
413
|
+
if(market == NULL) {
|
|
414
|
+
exit(1);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
start = CTimer();
|
|
418
|
+
for(i=0; i < client_threads; i++) {
|
|
419
|
+
ca[i].id = i;
|
|
420
|
+
ca[i].order_count = orders_per_client;
|
|
421
|
+
ca[i].max_stock_id = max_stock;
|
|
422
|
+
ca[i].max_quantity = INIT_COUNT;
|
|
423
|
+
ca[i].order_que = order_que;
|
|
424
|
+
ca[i].verbose = verbose;
|
|
425
|
+
err = pthread_create(&client_ids[i],NULL,
|
|
426
|
+
ClientThread,(void *)&ca[i]);
|
|
427
|
+
if(err != 0) {
|
|
428
|
+
fprintf(stderr,"client thread create %d failed\n",i);
|
|
429
|
+
exit(1);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
done = 0;
|
|
434
|
+
for(i=0; i < trader_threads; i++) {
|
|
435
|
+
ta[i].id = i;
|
|
436
|
+
ta[i].order_que = order_que;
|
|
437
|
+
ta[i].market = market;
|
|
438
|
+
ta[i].done = &done;
|
|
439
|
+
ta[i].verbose = verbose;
|
|
440
|
+
err = pthread_create(&trader_ids[i],NULL,
|
|
441
|
+
TraderThread,(void *)&ta[i]);
|
|
442
|
+
if(err != 0) {
|
|
443
|
+
fprintf(stderr,"trader thread create %d failed\n",i);
|
|
444
|
+
exit(1);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/*
|
|
449
|
+
* wait for the clients to finish
|
|
450
|
+
*/
|
|
451
|
+
for(i=0; i < client_threads; i++) {
|
|
452
|
+
err = pthread_join(client_ids[i],NULL);
|
|
453
|
+
if(err != 0) {
|
|
454
|
+
fprintf(stderr,"client join %d failed\n",i);
|
|
455
|
+
exit(1);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/*
|
|
460
|
+
* tell the traders we are done
|
|
461
|
+
*/
|
|
462
|
+
done = 1;
|
|
463
|
+
V(order_que->empty);
|
|
464
|
+
|
|
465
|
+
for(i=0; i < trader_threads; i++) {
|
|
466
|
+
err = pthread_join(trader_ids[i],NULL);
|
|
467
|
+
if(err != 0) {
|
|
468
|
+
fprintf(stderr,"trader join %d failed\n",i);
|
|
469
|
+
exit(1);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
end = CTimer();
|
|
473
|
+
|
|
474
|
+
if(verbose == 1) {
|
|
475
|
+
PrintMarket(market);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
printf("%f transactions / sec\n",
|
|
479
|
+
(double)(orders_per_client*client_threads) / (end-start));
|
|
480
|
+
|
|
481
|
+
free(ca);
|
|
482
|
+
free(ta);
|
|
483
|
+
free(client_ids);
|
|
484
|
+
free(trader_ids);
|
|
485
|
+
FreeMarket(market);
|
|
486
|
+
FreeOrderQue(order_que);
|
|
487
|
+
|
|
488
|
+
return(0);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|