-
Notifications
You must be signed in to change notification settings - Fork 0
/
asst1.diff
2752 lines (2697 loc) · 89.2 KB
/
asst1.diff
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
This asst1 patch generated by /import/adams/1/cs3231/bin/cs3231_generate_diff for z5059988 on Sat 31 Mar 13:58:03 AEDT 2018
diff --unidirectional-new-file -EdbwBr -u -X /home/cs3231/assigns/asst1/src/../diffex /home/cs3231/assigns/asst1/src/design.txt /import/ravel/3/z5059988/cs3231/asst1-src/design.txt
--- design.txt 2018-03-09 13:16:22.000000000 +1100
+++ design.txt 2018-03-31 13:43:57.000000000 +1100
@@ -1 +1,176 @@
-Place your design document here.
+######################################
+Part 1: Concurrent Mathematics Problem
+######################################
+
+The counter variable is a shared resource between the threads of execution.
+Since all these threads update the counter, without the use of
+synchronisation, the OS could for example interleave the instructions such
+that `counter = counter + 1` is executed in multiple threads before
+`b = counter` is executed, leaving an unexpected value in `b`.
+
+To prevent concurrent/unsafe modification of the counter variable, we employed
+the use of a lock to achieve mutual exclusion. We identified the critical
+section in adder() beginning from the assignment `a = counter` and ending at
+the end of the if/else block. Accordingly, we acquire the lock prior to
+entering the critical region and release the lock after the critical region
+has finished executing. Since our lock is used in every adder() thread, we
+create the lock in the maths() function and destroy it after all the adder()
+threads terminated.
+
+#######################
+Part 2: Simple Deadlock
+#######################
+
+In the original code, bill() and ben() are called in separate threads
+and acquire the same two locks in the reverse order. This causes a deadlock
+for example, when the following interleaving occurs:
+
+bill's thread | ben's thread
+1: lock_acquire(locka) |
+2: | lock_acquire(lockb)
+3: lock_acquire(lockb) |
+4: | lock_acquire(locka)
+
+This occurs since bill's thread is waiting for lockb to be released (which ben
+currently has) and ben's thread is waiting for locka to be released (which bill
+currently has). As each thread is waiting for the other to release a requested
+resource, a deadlock occurs.
+
+We used the prevention strategy to deal with the deadlock, removing the circular
+wait condition by ordering the resource requests such that the threads acquire
+the locks in the same order. This solution ensures that the first lock is
+required to be held by a thread before the second lock can be acquired. As such,
+neither thread can acquire a lock that the other thread will need if the other
+thread has already acquired one of its locks.
+
+With this request ordering, there are four possible interleavings of requests
+(none of which result in deadlock):
+
+Case 1: One of the threads acquires both locks before the other thread. When
+the other thread requests locka, it blocks until the first thread releases it.
+Since the first thread has access to all the resources it requires, it will
+eventually complete and release its resources for the other thread to use.
+
+bill's thread | ben's thread
+1: lock_acquire(locka) |
+2: lock_acquire(lockb) |
+3: | lock_acquire(locka)
+4: | lock_acquire(lockb)
+
+OR
+
+bill's thread | ben's thread
+1: | lock_acquire(locka)
+2: | lock_acquire(lockb)
+3: lock_acquire(locka) |
+4: lock_acquire(lockb) |
+
+Case 2: One of the threads acquires locka, then the other thread requests
+locka and blocks. The first thread proceeds to acquire lockb. Again, the first
+thread has access to all the resources it requires, so it will eventually
+complete and release its resources for the other thread to use.
+
+bill's thread | ben's thread
+1: lock_acquire(locka) |
+2: | lock_acquire(locka)
+3: lock_acquire(lockb) |
+4: | lock_acquire(lockb)
+
+OR
+
+bill's thread | ben's thread
+1: | lock_acquire(locka)
+2: lock_acquire(locka) |
+3: | lock_acquire(lockb)
+4: lock_acquire(lockb) |
+
+################################################
+Part 3: Bounded Buffer Producer/Consumer Problem
+################################################
+
+- The circular buffer used by the producer and consumer consists of a fixed-size
+ array and two indexes, `head` (referring to the first item for consumption)
+ and `tail` (referring to the next available slot for production).
+- We used a lock (buffer_lock) to restrict access to the buffer (a shared
+ resource between producer/consumer threads). Any time that the buffer is
+ modified (producer produces an item or consumer consumes an item), the lock is
+ used to ensure mutual exclusion.
+- We used two condition variables:
+ - `has_capacity`: to track whether the buffer has at least one free slot
+ - `has_data`: to track whether the buffer has at least one item available for
+ consumption
+- Consumer:
+ - waits on `has_data`, sleeping if there are no items available to consume
+ - signals `has_capacity` after it finishes consuming item by adjusting `head`
+- Producer:
+ - waits on `has_capacity`, sleeping if there is no space in the buffer to
+ produce items
+ - signals `has_data` once it finishes producing an item by adding it to the
+ end of the queue and adjusting `tail`
+- Before either thread blocks while waiting for data or space, we check the
+ number of items in the queue. This is done in a loop so that when the thread
+ unblocks, the condition is checked again to ensure there is still data or
+ space. The member `buffer.num_items` is used instead of relying on the `head`
+ and `tail` values in order to correctly distinguish between the empty and
+ full cases.
+- An alternative solution is to use semaphores in place of condition variables.
+ However, for the sake of code clarity, we decided to use condition variables.
+
+###########################
+Part 4: Bar Synchronisation
+###########################
+
+At a high-level, this problem is essentially a more involved producer-consumer
+scenario in which the producers are customers who order drinks ("producing"
+orders) and the consumers are bartenders, who serve the customers ("consuming"
+the orders). Concurrency issues arise from the use of shared resources (bottles)
+and managing the queue of orders to ensure all customers are served the correct
+drinks.
+
+Once a drink is ordered by a customer, a condition variable (`order_made`) is
+signalled in order_drink() to wake up a bartender, who waits for the signal in
+take_order(). The newly created order is added to a global queue of orders
+named `pending_orders`. Since multiple threads require access to the queue,
+we require mutual exclusion to prevent race conditions (e.g. an order being
+served by multiple bartenders). This is achieved through the use of a lock
+`que_lock`, acquired and released before and after enqueuing and dequeuing.
+
+To prevent bartender and customer threads from busy waiting, we added a
+semaphore to the `barorder` struct so that P() and V() could be executed
+from any function with access to the order. In order_drink(), we create the
+semaphore and decrement it using P(), which blocks the customer until the order
+is ready. Once the order is ready, serve_order() is called, which invokes
+V() on the semaphore, signaling that the customer thread can wake up and
+either produce more orders or go home.
+
+The fill_order() function can be thought of as a wrapper to the mix() function
+in that it simply manages access to the bottles used to mix the drinks in such
+a way that deadlocks do not occur and bottle access is mutually exclusive.
+Since the mix() function simply increments the number of doses for up to
+DRINK_COMPLEXITY bottles used in the drink, fill_order() only needs to acquire
+and release locks on those specific bottles before and after calling mix()
+to achieve mutual exclusion. This way, multiple bartender threads can mix()
+concurrently if the orders consist of a disjoint subset of drinks. We
+implemented this by creating a global `bottle_locks` array to store individual
+locks for each bottle, acquiring them in fill_order() as needed.
+
+Since there is guarantee on the order of bottles requested by customers,
+there is the possibility of multiple customers ordering drinks from the same
+bottles but in different orders, with the OS interleaving the orders in such a
+way that a deadlock occurs. As such, we had to employ a deadlock prevention
+scheme to avoid cyclic wait conditions, implemented sorting (using quicksort())
+the `order->requested_bottles` array before acquiring locks for each requested
+bottle. This ensured a consistent resource ordering convention, requiring that
+all threads acquire locks in the same order.
+
+As locks in OS/161 are not re-entrant, customers may request multiple doses
+from the same bottle in a single order, causing a bartender to block against
+itself if it tried to acquire the same lock more than once (essentially
+causing a single-process deadlock). To prevent this, bartenders call
+lock_do_i_hold() to check if they already hold the lock before
+re-requesting it.
+
+Finally, our solution ensures that all locks, semaphores and condition
+variables used are appropriately freed. Our bar_open() function initially
+creates the global locks, including one for each of the bottles and our
+bar_close() correspondingly destroys these synchronisation primitives.
diff --unidirectional-new-file -EdbwBr -u -X /home/cs3231/assigns/asst1/src/../diffex /home/cs3231/assigns/asst1/src/kern/asst1/Makefile /import/ravel/3/z5059988/cs3231/asst1-src/kern/asst1/Makefile
--- kern/asst1/Makefile 1970-01-01 10:00:00.000000000 +1000
+++ kern/asst1/Makefile 2018-03-28 11:47:48.000000000 +1100
@@ -0,0 +1,15 @@
+make:
+ cd ../compile/ASST? && bmake && bmake install
+
+run:
+ cd ../../../root && sys161 kernel "$(args)"
+
+run-gdb:
+ cd ../../../root && sys161 -w kernel "$(args)"
+
+gdb:
+ cd ../../../root && os161-gdb kernel
+
+conf:
+ cd ../../ && ./configure && cd kern/conf && ./config ASST? && \
+ cd ../compile/ASST? && bmake depend && bmake && bmake install
diff --unidirectional-new-file -EdbwBr -u -X /home/cs3231/assigns/asst1/src/../diffex /home/cs3231/assigns/asst1/src/kern/asst1/Queue.c /import/ravel/3/z5059988/cs3231/asst1-src/kern/asst1/Queue.c
--- kern/asst1/Queue.c 1970-01-01 10:00:00.000000000 +1000
+++ kern/asst1/Queue.c 2018-03-21 18:53:45.000000000 +1100
@@ -0,0 +1,86 @@
+#include <types.h>
+#include <lib.h>
+
+#include "item.h"
+#include "Queue.h"
+
+typedef struct node *link;
+
+typedef struct node {
+ item data;
+ link next;
+} node;
+
+//list-like concrete representation of queue
+typedef struct queue {
+ link head, tail;
+ unsigned long length;
+} queue;
+
+//creates and returns a new empty queue
+Queue create_queue(void) {
+ Queue new = kmalloc(sizeof(queue));
+ KASSERT(new != NULL);
+ new->length = 0;
+ new->head = new->tail = NULL;
+ return new;
+}
+
+//frees all memory associated with a given queue
+void dispose_queue(Queue q) {
+ KASSERT(q != NULL);
+ item temp;
+ while (is_queue_empty(q) == false) {
+ temp = dequeue(q);
+ del(temp);
+ }
+ kfree(q);
+}
+
+//inserts a new item at the back of a given queue
+void enqueue(Queue q, item data) {
+ KASSERT(q != NULL);
+ link new = kmalloc(sizeof(node));
+ KASSERT(new != NULL);
+ new->data = copy(data);
+ new->next = NULL;
+ if (is_queue_empty(q) == true) {
+ q->head = new;
+ } else {
+ q->tail->next = new;
+ }
+ q->tail = new;
+ q->length++;
+}
+
+//removes and returns a copy of the front item of a given queue
+item dequeue(Queue q) {
+ KASSERT(q != NULL);
+ KASSERT(is_queue_empty(q) == false);
+ link front = q->head;
+ item data = copy(front->data);
+ q->head = front->next;
+ del(front->data);
+ kfree(front);
+ q->length--;
+ return data;
+}
+
+//returns the data at the front of a given queue
+item get_head(Queue q) {
+ KASSERT(q != NULL);
+ KASSERT(is_queue_empty(q) == false);
+ return q->head->data;
+}
+
+//returns the length of a given queue
+unsigned long get_queue_length(Queue q) {
+ KASSERT(q != NULL);
+ return q->length;
+}
+
+//indicates whether a given queue is empty
+bool is_queue_empty(Queue q) {
+ KASSERT(q != NULL);
+ return q->length == 0;
+}
diff --unidirectional-new-file -EdbwBr -u -X /home/cs3231/assigns/asst1/src/../diffex /home/cs3231/assigns/asst1/src/kern/asst1/Queue.h /import/ravel/3/z5059988/cs3231/asst1-src/kern/asst1/Queue.h
--- kern/asst1/Queue.h 1970-01-01 10:00:00.000000000 +1000
+++ kern/asst1/Queue.h 2018-03-21 18:53:45.000000000 +1100
@@ -0,0 +1,23 @@
+#include <types.h>
+
+#include "item.h"
+
+#ifndef QUEUE_H
+#define QUEUE_H
+
+typedef struct queue *Queue;
+
+//queue initialisation
+Queue create_queue(void);
+void dispose_queue(Queue q);
+
+//queue progression
+void enqueue(Queue q, item data);
+item dequeue(Queue q);
+
+//queue query
+item get_head(Queue q);
+unsigned long get_queue_length(Queue q);
+bool is_queue_empty(Queue q);
+
+#endif
diff --unidirectional-new-file -EdbwBr -u -X /home/cs3231/assigns/asst1/src/../diffex /home/cs3231/assigns/asst1/src/kern/asst1/bar-tests/bar_driver.c /import/ravel/3/z5059988/cs3231/asst1-src/kern/asst1/bar-tests/bar_driver.c
--- kern/asst1/bar-tests/bar_driver.c 1970-01-01 10:00:00.000000000 +1000
+++ kern/asst1/bar-tests/bar_driver.c 2018-03-31 13:43:57.000000000 +1100
@@ -0,0 +1,344 @@
+#include "opt-synchprobs.h"
+#include <types.h>
+#include <lib.h>
+#include <synch.h>
+#include <test.h>
+#include <thread.h>
+
+#include "bar_driver.h"
+
+
+/*
+ * DEFINE THIS MACRO TO SWITCH ON MORE PRINTING
+ *
+ * Note: Your solution should work whether printing is on or off
+ *
+ */
+
+/* #define PRINT_ON */
+
+/* this semaphore is for cleaning up at the end. */
+static struct semaphore *alldone;
+
+/*
+ * Data type used to track number of doses each bottle gives
+ */
+
+struct bottle {
+ int doses;
+};
+
+struct bottle bottles[NBOTTLES];
+
+static int customers;
+static struct lock *cust_lock;
+
+/* A function used to manage staff leaving */
+
+static void go_home(void);
+
+/*
+ * **********************************************************************
+ * CUSTOMERS
+ *
+ * Customers are rather simple, they arrive and give their order to a
+ * bartender and wait for their drink.
+ *
+ * Eventually their glass arrives with the requested contents (exactly
+ * as requested), they drink until the glass is empty, take a short
+ * break, and do it all again until they have emptied the desired
+ * number of glasses (customers passing out is not simulated).
+ *
+ * Lastly, they indicate to the bar staff that they have
+ * finished for the day by calling go_home()
+ *
+ */
+
+static void customer(void *unusedpointer, unsigned long customernum)
+{
+ struct barorder order;
+ int i,j;
+
+ (void) unusedpointer; /* avoid compiler warning */
+
+ order.go_home_flag = 0;
+ int num_orders = (random() % 20) + 1; /* randomise the number of orders */
+
+ i = 0; /* count number of iterations */
+ do {
+
+
+#ifdef PRINT_ON
+ kprintf("C %ld is ordering\n", customernum);
+#endif
+
+ /* Clear away previously-requested ingredients and select a new drink */
+ for (j = 0; j < DRINK_COMPLEXITY; j++) {
+ order.requested_bottles[j] = 0;
+ }
+
+ /* randomise the specific drinks and number of overall drinks*/
+ int num_drinks = (random() % DRINK_COMPLEXITY) + 1;
+ for (int k = 0; k < num_drinks; ++k) {
+ order.requested_bottles[k] = (random() % NBOTTLES) + 1;
+ }
+
+ /* order the drink, this blocks until the order is fulfilled */
+ order_drink(&order);
+
+#ifdef PRINT_ON
+ kprintf("C %ld drinking %d, %d, %d\n",
+ customernum,
+ order.glass.contents[0],
+ order.glass.contents[1],
+ order.glass.contents[2]);
+#endif
+
+ /* Drink up */
+ for (j = 0; j < DRINK_COMPLEXITY; j++) {
+ order.glass.contents[j] = 0;
+ }
+
+
+ /* I needed that break.... */
+ thread_yield();
+
+ i++;
+ } while (i < num_orders); /* keep going until .... */
+
+#ifdef PRINT_ON
+ kprintf("C %ld going home\n", customernum);
+#else
+ (void)customernum;
+#endif
+
+ /*
+ * Now we go home.
+ */
+ go_home();
+ V(alldone);
+}
+
+
+/*
+ * **********************************************************************
+ * BARTENDERS
+ *
+ * bartenders are only slightly more complicated than the customers.
+ * They take_orders, and if valid, they fill them and serve them.
+ * When all the customers have left, the bartenders go home.
+ *
+ *
+ */
+static void bartender(void *unusedpointer, unsigned long staff)
+{
+
+ struct barorder *order;
+ int i;
+
+ /* avoid compiler warning */
+ (void)unusedpointer;
+ (void)staff;
+
+ i = 0; /* count orders filled for stats */
+ while (1) {
+
+#ifdef PRINT_ON
+ kprintf("S %ld taking order\n", staff);
+#endif
+
+ order = take_order();
+
+ if (order->go_home_flag == 0) {
+
+#ifdef PRINT_ON
+ kprintf("S %ld filling\n", staff);
+#endif
+
+
+ i++;
+ fill_order(order);
+
+#ifdef PRINT_ON
+ kprintf("S %ld serving\n", staff);
+#endif
+
+ serve_order(order);
+ } else {
+ /* Immediately return the order without filling, and then go home */
+ serve_order(order);
+ break;
+ }
+
+ };
+
+#ifdef PRINT_ON
+ kprintf("S %ld going home after mixing %d drinks\n", staff, i);
+#endif
+ V(alldone);
+}
+
+
+/*
+ * **********************************************************************
+ * RUN THE BAR
+ *
+ * This routine sets up the bar prior to opening and cleans up after
+ * closing.
+ *
+ * It calls two routines (bar_open() and bar_close() in bar.c) in which
+ * you can insert your own initialisation code.
+ *
+ * It also prints some statistics at the end.
+ *
+ */
+
+int run_bar(int nargs, char **args)
+{
+ int i, result;
+
+ (void) nargs; /* avoid compiler warnings */
+ (void) args;
+
+ /* this semaphore indicates everybody has gone home */
+ alldone = sem_create("alldone", 0);
+ if (alldone == NULL) {
+ panic("run_bar: out of memory\n");
+ }
+
+ /* initialise the bottle doses to 0 */
+ for (i = 0 ; i < NBOTTLES; i++) {
+ bottles[i].doses = 0;
+ }
+
+ /* initialise the count of customers and create a lock to
+ facilitate updating the counter by multiple threads */
+ customers = NCUSTOMERS;
+
+ cust_lock = lock_create("cust lock");
+ if (cust_lock == NULL) {
+ panic("no memory");
+ }
+
+ /**********************************************************************
+ * call your routine that initialises the rest of the bar
+ */
+ bar_open();
+
+ /* Start the bartenders */
+ for (i = 0; i<NBARTENDERS; i++) {
+ result = thread_fork("bartender thread", NULL,
+ &bartender, NULL, i);
+ if (result) {
+ panic("run_bar: thread_fork failed: %s\n",
+ strerror(result));
+ }
+ }
+
+ /* Start the customers */
+
+ for (i=0; i<NCUSTOMERS; i++) {
+ result = thread_fork("customer thread", NULL,
+ &customer, NULL, i);
+ if (result) {
+ panic("run_bar: thread_fork failed: %s\n",
+ strerror(result));
+ }
+ }
+
+ /* Wait for everybody to finish. */
+ for (i = 0; i < NCUSTOMERS + NBARTENDERS; i++) {
+ P(alldone);
+ }
+
+ for (i = 0; i < NBOTTLES; i++) {
+ kprintf("Bottle %d used for %d doses\n", i + 1,
+ bottles[i].doses);
+ }
+
+ /***********************************************************************
+ * Call your bar clean up routine
+ */
+ bar_close();
+
+ lock_destroy(cust_lock);
+ sem_destroy(alldone);
+ kprintf("The bar is closed, bye!!!\n");
+ return 0;
+}
+
+
+
+/*
+ * **********************************************************************
+ * MIX
+ *
+ * This function take a glass and an order and mixes the
+ * drink as required. It does it such that the contents
+ * EXACTLY matches the request.
+ *
+ * Yes, mix counts double and triple servings of the same tint.
+ *
+ * MIX NEEDS THE ROUTINE THAT CALLS IT TO ENSURE THAT MIX HAS EXCLUSIVE
+ * ACCESS TO THE BOTTLES IT NEEDS. And ideally, only exclusive access to
+ * the tints that are required in the mix.
+ *
+ * YOU MUST USE THIS MIX FUNCTION TO FILL GLASSES. We use it for
+ * testing when marking.
+ *
+ */
+
+void mix(struct barorder *order)
+{
+ int i;
+
+ /*
+ * add drinks to the glass in order given and increment number of
+ * doses from particular bottle
+ */
+
+ for (i = 0; i < DRINK_COMPLEXITY; i++){
+ int bottle;
+ bottle = order->requested_bottles[i];
+ order->glass.contents[i] = bottle;
+
+ if (bottle > NBOTTLES) {
+ panic("Unknown bottle");
+ }
+ if (bottle > 0) {
+ bottles[bottle-1].doses++;
+ }
+ }
+}
+
+/*
+ * go_home()
+ *
+ * This function is called by customers when they go home. It is used
+ * to keep track of the number of remaining customers to allow bartender
+ * threads to exit when no customers remain.
+ */
+
+
+static void go_home(void)
+{
+
+ lock_acquire(cust_lock);
+ customers --;
+
+ /* the last customer to leave tells the staff to go home */
+ if (customers == 0) {
+ struct barorder go_home_order;
+ int i;
+ lock_release(cust_lock); /* don't hold the lock longer than strictly needed */
+ go_home_order.go_home_flag = 1;
+
+ for (i = 0; i < NBARTENDERS; i++) {
+ order_drink(&go_home_order); /* returns without order being filled */
+ }
+ } else {
+ lock_release(cust_lock);
+ }
+}
+
+
diff --unidirectional-new-file -EdbwBr -u -X /home/cs3231/assigns/asst1/src/../diffex /home/cs3231/assigns/asst1/src/kern/asst1/bar.c /import/ravel/3/z5059988/cs3231/asst1-src/kern/asst1/bar.c
--- kern/asst1/bar.c 2018-03-09 13:16:22.000000000 +1100
+++ kern/asst1/bar.c 2018-03-28 11:37:16.000000000 +1100
@@ -6,17 +6,14 @@
#include "bar.h"
#include "bar_driver.h"
-
-
-
-/*
- * **********************************************************************
- * YOU ARE FREE TO CHANGE THIS FILE BELOW THIS POINT AS YOU SEE FIT
- *
- */
+#include "Queue.h"
+#include "quicksort.h"
/* Declare any globals you need here (e.g. locks, etc...) */
-
+Queue pending_orders;
+struct lock *que_lock;
+struct cv *order_made;
+struct lock *bottle_locks[NBOTTLES];
/*
* **********************************************************************
@@ -31,15 +28,17 @@
* function makes the order available to staff threads and then blocks
* until a bartender has filled the glass with the appropriate drinks.
*/
-
-void order_drink(struct barorder *order)
-{
- (void) order; /* Avoid compiler warning, remove when used */
- panic("You need to write some code!!!!\n");
+void order_drink(struct barorder *order) {
+ order->order_ready = sem_create("order ready", 0);
+ if (order->order_ready == NULL) panic("%s: order_ready create failed", __FILE__);
+ lock_acquire(que_lock);
+ enqueue(pending_orders, order);
+ cv_signal(order_made, que_lock);
+ lock_release(que_lock);
+ P(order->order_ready);
+ sem_destroy(order->order_ready);
}
-
-
/*
* **********************************************************************
* FUNCTIONS EXECUTED BY BARTENDER THREADS
@@ -53,11 +52,11 @@
* customers. When submitted, it returns a pointer to the order.
*
*/
-
-struct barorder *take_order(void)
-{
- struct barorder *ret = NULL;
-
+struct barorder *take_order(void) {
+ lock_acquire(que_lock);
+ while (is_queue_empty(pending_orders)) cv_wait(order_made, que_lock);
+ struct barorder *ret = dequeue(pending_orders);
+ lock_release(que_lock);
return ret;
}
@@ -72,18 +70,33 @@
* REQUIRED BOTTLES (AND, IDEALLY, ONLY THE BOTTLES) IT NEEDS TO USE TO
* FILL THE ORDER.
*/
+void fill_order(struct barorder *order) {
-void fill_order(struct barorder *order)
-{
+ /* enforce resource ordering to prevent deadlock */
+ quicksort(order->requested_bottles, 0, DRINK_COMPLEXITY - 1);
- /* add any sync primitives you need to ensure mutual exclusion
- holds as described */
+ /* lock all bottles needed for the order */
+ for (int i = 0; i < DRINK_COMPLEXITY; ++i) {
+ int bottle = order->requested_bottles[i];
+ if (bottle > NBOTTLES) panic("Unknown bottle");
+ if (bottle <= 0) continue;
+ struct lock *l = bottle_locks[bottle - 1];
+ if (!lock_do_i_hold(l)) lock_acquire(l);
+ }
/* the call to mix must remain */
mix(order);
+ /* release all bottle locks */
+ for (int i = 0; i < DRINK_COMPLEXITY; ++i) {
+ int bottle = order->requested_bottles[i];
+ if (bottle > NBOTTLES) panic("Unknown bottle");
+ if (bottle <= 0) continue;
+ struct lock *l = bottle_locks[bottle - 1];
+ if (lock_do_i_hold(l)) lock_release(l);
}
+}
/*
* serve_order()
@@ -91,15 +104,10 @@
* Takes a filled order and makes it available to (unblocks) the
* waiting customer.
*/
-
-void serve_order(struct barorder *order)
-{
- (void) order; /* avoid a compiler warning, remove when you
- start */
+void serve_order(struct barorder *order) {
+ V(order->order_ready);
}
-
-
/*
* **********************************************************************
* INITIALISATION AND CLEANUP FUNCTIONS
@@ -114,10 +121,16 @@
* bartenders and customers. Typically, allocation and initialisation of
* synch primitive and variable.
*/
-
-void bar_open(void)
-{
-
+void bar_open(void) {
+ pending_orders = create_queue();
+ que_lock = lock_create("queue lock");
+ if (que_lock == NULL) panic("%s: que_lock create failed", __FILE__);
+ order_made = cv_create("order made");
+ if (order_made == NULL) panic("%s: order_made create failed", __FILE__);
+ for (int i = 0; i < NBOTTLES; ++i) {
+ bottle_locks[i] = lock_create("bottle lock");
+ if (bottle_locks[i] == NULL) panic("%s: bottle_lock %d create failed", __FILE__, i);
+ }
}
/*
@@ -126,9 +139,11 @@
* Perform any cleanup after the bar has closed and everybody
* has gone home.
*/
-
-void bar_close(void)
-{
-
+void bar_close(void) {
+ dispose_queue(pending_orders);
+ lock_destroy(que_lock);
+ cv_destroy(order_made);
+ for (int i = 0; i < NBOTTLES; ++i) {
+ lock_destroy(bottle_locks[i]);
+ }
}
-
diff --unidirectional-new-file -EdbwBr -u -X /home/cs3231/assigns/asst1/src/../diffex /home/cs3231/assigns/asst1/src/kern/asst1/bar.h /import/ravel/3/z5059988/cs3231/asst1-src/kern/asst1/bar.h
--- kern/asst1/bar.h 2018-03-09 13:16:22.000000000 +1100
+++ kern/asst1/bar.h 2018-03-28 11:37:16.000000000 +1100
@@ -1,25 +1,21 @@
-#ifndef BAR_H
-#define BAR_H
#include <synch.h>
#include "barglass.h"
+#ifndef BAR_H
+#define BAR_H
+
/*
- * You are free to add anything you think you require to this file,
- * with the exceptions noted below.
+ * struct barorder is the main type referred to in the code. It must
+ * be preserved as noted for our later testing to work
*/
-
-
-/* struct barorder is the main type referred to in the code. It must
- be preserved as noted for our later testing to work */
-
struct barorder {
unsigned int requested_bottles[DRINK_COMPLEXITY]; /* Do not change */
int go_home_flag; /* Do not change */
struct glass glass; /* Do not change */
/* This struct can be extended with your own entries below here */
-
+ struct semaphore *order_ready; /* used to block until order ready */
};
#endif
diff --unidirectional-new-file -EdbwBr -u -X /home/cs3231/assigns/asst1/src/../diffex /home/cs3231/assigns/asst1/src/kern/asst1/item.h /import/ravel/3/z5059988/cs3231/asst1-src/kern/asst1/item.h
--- kern/asst1/item.h 1970-01-01 10:00:00.000000000 +1000
+++ kern/asst1/item.h 2018-03-21 18:53:45.000000000 +1100
@@ -0,0 +1,18 @@
+#include "bar.h"
+
+#ifndef ITEM_H
+#define ITEM_H
+
+typedef struct barorder *item;
+typedef item key;
+
+#define key(it) ((it))
+#define del(it) ((it) = (it))
+#define copy(x) ((x))
+#define eq(x,y) ((x) == (y))
+#define lt(x,y) ((x) < (y))
+#define le(x,y) ((x) <= (y))
+#define gt(x,y) ((x) > (y))
+#define ge(x,y) ((x) >= (y))
+
+#endif
diff --unidirectional-new-file -EdbwBr -u -X /home/cs3231/assigns/asst1/src/../diffex /home/cs3231/assigns/asst1/src/kern/asst1/math.c /import/ravel/3/z5059988/cs3231/asst1-src/kern/asst1/math.c
--- kern/asst1/math.c 2018-03-09 13:16:22.000000000 +1100
+++ kern/asst1/math.c 2018-03-28 11:37:16.000000000 +1100
@@ -34,15 +28,6 @@
/* We use a semaphore to wait for adder() threads to finish */
struct semaphore *finished;
-
-/*
- * **********************************************************************
- * ADD YOUR OWN VARIABLES HERE AS NEEDED
- * **********************************************************************
- */
-
-
-
/*
* adder()
*
@@ -63,21 +48,16 @@
* + only synchronise the critical section, no more.
*
*/
-
-static void adder(void * unusedpointer, unsigned long addernumber)
-{
+static void adder(void *unusedpointer, unsigned long addernumber) {
unsigned long int a, b;
int flag = 1;
- /*
- * Avoid unused variable warnings.
- */
- (void) unusedpointer; /* remove this line if variable is used */
-
while (flag) {
- /* loop doing increments until we achieve the overall number
- of increments */
+ /* loop doing increments until we achieve the overall number of increments */
+ /* start of critical section */
+ struct lock *counter_lock = (struct lock*) unusedpointer;
+ lock_acquire(counter_lock);
a = counter;
if (a < NADDS) {
counter = counter + 1;
@@ -88,12 +68,13 @@
/* check we are getting sane results */
if (a + 1 != b) {
- kprintf("In thread %ld, %ld + 1 == %ld?\n",
- addernumber, a, b) ;
+ kprintf("In thread %ld, %ld + 1 == %ld?\n", addernumber, a, b);
}
} else {
flag = 0;
}
+ lock_release(counter_lock);
+ /* end of critical section */
}
/* signal the main thread we have finished and then exit */
@@ -112,9 +93,7 @@
* + Starts the define number of adder threads
* + waits, prints statistics, cleans up, and exits
*/
-
-int maths (int data1, char **data2)
-{
+int maths (int data1, char **data2) {
int index, error;
unsigned long int sum;
@@ -129,7 +107,7 @@
finished = sem_create("finished", 0);
if (finished == NULL) {
- panic("maths: sem create failed");
+ panic("%s: sem create failed", __FILE__);
}
/*
@@ -137,7 +115,10 @@
* INSERT ANY INITIALISATION CODE YOU REQUIRE HERE
* ********************************************************************
*/
-
+ struct lock *counter_lock = lock_create("counter lock");
+ if (counter_lock == NULL) {
+ panic("%s: counter_lock create failed", __FILE__);
+ }
/*
* Start NADDERS adder() threads.
@@ -142,12 +123,9 @@
/*
* Start NADDERS adder() threads.
*/
-
kprintf("Starting %d adder threads\n", NADDERS);
-
for (index = 0; index < NADDERS; index++) {
-
- error = thread_fork("adder thread", NULL, &adder, NULL, index);
+ error = thread_fork("adder thread", NULL, &adder, counter_lock, index);
/*
* panic() on error.
@@ -152,10 +130,8 @@
/*
* panic() on error.
*/
-
if (error) {