-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcs343.txt
More file actions
1169 lines (715 loc) · 33.8 KB
/
cs343.txt
File metadata and controls
1169 lines (715 loc) · 33.8 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
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
* What is a multi-exit loop?
A loop that has one or more exit locations occurring within the body of the loop.
* What is a flag variable?
A variable used solely to affect control flow.
* What are flag variables equivalent to?
Goto statements.
* What is static multi-level exit?
Exiting multiple control structures, while the exit points are known at
compile time.
* How can dynamic multi-level exit be used to handle exceptional events?
Since dynamic multi-level exit allows you to return from any statement to
anywhere, you can return to an "exception handler" following an
exceptional event.
* What are the traditional approaches to handling exceptional events?
- Return code: returns value indicating normal or exceptional execution.
- Status flag: set global variable indicating normal or exceptional execution.
- Fix-up routine: routine called for an exceptional event to return
a corrective result.
* What are the drawbacks of the traditional approaches to handling exceptional events?
- Checking return code or status flag is optional.
- Return code mixes exceptional and normal values.
* What is a source execution w.r.t exception handling?
The execution raising an exception.
* What is a faulting execution w.r.t exception handling?
The execution changing control flow due to an exception.
* What is the static/dynamic call/return table?
call/raise
return/handled| static dynamic
----------------------------------------------------
static | sequel termination exception
dynamic | routine virtual routine
* Why is starting a full-coroutine cycle difficult?
Because it requires every coroutine to know at least one other coroutine,
causing a dependency cycle that must be addressed.
# 5 Concurrency
* What is a thread?
An independent sequential execution path through a program.
* What is a process?
A program component that has its own thread and has the same state information
as a coroutine.
* What is a task?
Process that is reduced along some particular dimension. Does "only one thing".
* What is parallel execution?
When 2 or more operations occur at the same time.
Can only occur when multiple processors are present.
* What is concurrent execution?
Any situation in which execution of multiple threads appears to be
performed in parallel.
* What is Amdahl's law?
S_c = 1 / ((1 - P) + P/C)
* What bounds the speedup of concurrent sections?
The critical path of computation.
* What are the three mechanisms required for concurrency?
1. Creation: Can cause another thread to be created.
2. Synchronization: Establish timing relationships among threads.
3. Communication: Transmit data among threads.
* What are the 5 rules of the mutual exclusion game?
1. Only one thread can be in a critical section at a time with respect to a
particular object. (Safety)
2. Threads may run at arbitrary speed and in arbitrary order, while the
underlying system guarantees a thread makes progress.
3. If a thread is not in the entry or exit code controlling access to the
critical section, it may not prevent other threads from entering the
critical section.
4. In selecting a thread for entry to a critical section, a selection cannot
be postponed indefinitely (liveness).
5. After a thread starts entry into the critical section, it must eventually
enter.
* What is livelock?
No one being picked to enter the critical section indefinitely.
* What can cause unfairness?
Threads not being serviced in first-come first-served order.
* What causes barging?
Waiting threads are overtaken by a thread arriving later.
* What are the 5 conditions to get into a dead-lock?
1. There exists more than one shared resource requiring mutual exclusion.
2. A process holds a resource while waiting for access to a resource held
by another process. (Hold-and-wait)
3. Once a process has gained access to a resource, the runtime system cannot
get it back. (no preemption)
4. There exists a circular wait of processes on resources.
5. These conditions must occur simultaneously.
* What is a synchronization dead-lock?
Failure in co-operation. A blocked task is never unblocked.
# 7.3 Deadlock prevention
* How does deadlock prevention work?
Eliminate one or more of the conditions required for a deadlock.
* What is synchronization prevention?
Eliminating all synchronization from a program.
Therefore, there is no communication between the threads, and all tasks
run completely independently.
* What is the problem with disallowing hold and wait?
There is poor resource utilization, and some possible starvation.
This is because all resources have to be held from the start of the program,
even if they may not be used!
* Why is there poor resource utilization when disallowing hold and wait?
All resources that the task could use have to be reserved at the same time,
including resources that may never be used.
* Why is starvation possible when disallowing hold and wait?
All resources required by a task may never be available at the same time.
* What is the problem with allowing preemption w.r.t deadlock prevention?
Preemption is dynamic, and cannot be applied statically.
* How can one prevent a circular wait for resources?
Using an ordered resource policy.
* What is the ordered resource policy?
1. Divide all resources into classes. (R1, R2, R3, ...)
2. Rule: Can only request a resource from class Ri if holding no resources
from any class Rj, j >= i.
* What is the general principle of banker's algorithm?
Generate a safe sequence of allocations that result in no deadlock.
* What information does the oracle need to know in the banker's algorithm?
1. The number of resources available of each type.
2. The resource allocation needed for each task in the worst case.
* What is the banker's algorithm?
1. Given the state of execution, ask "is there a task that can run to
completion such that its maximum requirements is less than the available
resources"?
2. Execute that task to completion, all resources that it held should be
free now.
3. Repeat until all tasks are finished.
* What is an allocation graph?
Create a graph of processes and resource usage at each moment a resource is
allocated.
* How can you use an allocation graph to check for deadlocks?
If a graph contains no cycles, then no process in the system is deadlocked.
However, if a resource has several instances, cycles are not always deadlocks.
* What can you do with resources with several instances in an allocation graph?
Break them up into multiple nodes.
Now if there is a cycle, we know that the system is deadlocked.
* How can you avoid deadlocks using an allocation graph?
1. Assign resources to tasks such that there is no cycle.
2. Complete the task, reducing the graph.
3. Repeating until all tasks are finished.
# 7.5 Detection and Recovery
* What is the alternative to deadlock prevention?
Let the deadlock happen and recover from it.
* What does deadlock recovery involve?
Preemption of one or more processes in a cycle.
* What in preempting a task for deadlock recovery?
1. The decider of the task must prevent starvation.
2. Preemption victim must be killed and restarted to its last checkpoint state.
3. Even that might not work since the victim may have made changes before the
preemption.
* What happens when Windows deadlocks?
Blue screen!
# 7.6 Which method to choose?
* What is the only deadlock prevention policy to have practical value?
Ordered resource policy.
# Exam questions
* After raising a nonlocal exception between coroutines in uC++, what are two additional actions that must be performed to make it work?
1. The faulting coroutine must have called _Enable.
2. The source coroutine must resume the faulting coroutine so the nonlocal
exception can propagate.
* How does Dekker's algorithm work without atomic assignment?
No simultaneous assignments on the three shared variables.
Last will never be assigned at the same time by both you and me, because
the assignment happens only in the critical section, where only one task can
be in at the same time.
Me and you are only written to by one task and read by the other task.
* Given only N bits to construct an N-task software solution for mutual
exclusion, what rule of the mutual-exclusion game is always broken?
Rule 5: Starvation.
This is because you can only set boolean flags, so there is no way to keep
track of who came first!
* For the N-task bakery algorithm, how many bits are used?
NM bits, where N is the number of tasks, and M is the maximum size of the
ticket.
* Does the Bakery algorithm solve all 5 rules of the critical-section game
perfectly?
No, it is only probablistically correct because the ticket number can
overflow.
* Given an atomic compare/assignment instruction, implement an atomic increment routine.
int inc(int& val) {
while (true) {
int old = val;
if (CAssn(val, old, old + 1)) {
return old;
}
}
}
* How can you write a spinning counting semaphore using a binary semaphore?
class cSem {
private:
int counter;
bSem sem;
public:
cSem(int initial) : counter(initial) {}
void P() {
while (true) {
sem.P();
if (counter > 0) {
counter = counter - 1;
sem.V();
return;
} else {
sem.V();
}
}
}
void V() {
sem.P();
counter = counter + 1;
sem.V();
}
};
* Why, in C++, is A || B and B || A not identical?
The || operator "short-circuits" if the left-hand side is truthy. This means
that the expression on the right-hand side may not be evaluated, and any
side effects that it could cause are not executed.
* What is the key restriction with labelled exits that make them acceptable control transfers?
Labelled exits should not be used to create a loop, and should only be used to
transfer out of control structures.
* What execution context is saved when making a routine call?
The current stack of the caller. This includes all statically allocated
variables, and the current execution state (the program counter).
* Does the compiler know where a routine's return statement transfer control to?
No. The return statement transfers control to the routine's first resumer. This
cannot be statically inferred.
The return address is found on the coroutine's stack.
* How can a callback routine be implemented in C++?
A function is passed in as a parameter (routine pointer).
* What is the problem with using longjmp/setjmp in C++ programs?
No destructors are executed while unwinding the stack.
* What are two reasons why derived exception-types are a useful feature to have in an EHM?
1. Allows the faulting execution only worry about categories of exceptions,
instead of specific ones.
2. Allows new exception, specific exceptions to be declared without needing to
add new checks.
3. Allows a handler to match multiple exception types.
* What is the difficulty of accessing local variables in an resumption handler?
You don't know exactly where on the stack the local variables are. There is
an arbitrary number of stack frames between the handler and the source.
* Why does a coroutine need its own stack?
So that it can save its execution state in between suspend and resumes.
* What is the difference between a coroutine and a task?
Only one coroutine can be executing instructions at the same time.
Tasks each have their own thread of control.
* What is the difference between the suspend and resume operations of a coroutine?
suspend suspends the current coroutine and resumes the coroutine that last
resumed it.
resume suspends the current coroutine and resumes "this" coroutine.
* Does a program with coroutines, but without tasks, ever need synchronization locks?
No, because only one coroutine can be executing at the same time, and the
program will invoke the coroutines deterministically.
* How can concurrency improve the efficiency of programs executed on a single CPU?
The programs can be waiting on external resources (e.g. file IO). This is case,
it is possible to proceed with the rest of the program instead of blocking it.
* Does a concurrent program on a single CPU system without preemption ever need mutual exclusion?
No, because only one thread is active at a single time and the program can make
deterministic choices on how to invoke them.
* What makes the retract-intent solution better than the declare-intent solution even though they both violate rule 4 (infinite postponement)?
The declare-intent solution will fail when a problematic solution happens once.
(i.e. They both declare intent at the same time.)
The retract-intent solution will only keep failing when its problematic solution
constantly occurs.
(i.e. Both tasks keep declaring and retracting intent at the same time.)
* What is the disadvantage of only providing COBEGIN and COEND operations for concurrency?
Interleaving dependencies cannot be expressed.
For example:
START T1
S1
START T2
S2
WAIT T1
S3
WAIT T2
S4
* What are the problems that result from encapsulating a thread library into the C++ OO model?
When starting the thread in the base-class constructor, it cannot be guaranteed
that all constructors in the inheritance hierarchy are executed before
invocation of the thread's routine.
When waiting for the thread in the base-class destructor, it cannot be
guaranteed that the thread's routine has finished when executing destructors
in the inheritance hierarchy.
* What is the swap instruction in pseudocode?
void swap(int& a, int& b) {
int temp = a;
a = b
b = temp;
}
* How can the swap instruction be used to solve the N-task mutex problem for rules 1-4?
int lock = 0
void run() {
int closed = 1;
while (true) {
swap(lock, closed);
if (closed == 0) {
break;
}
}
/* critical section */
lock = 0;
}
* What is the primary benefit of using an exit in the middle of a loop?
You can avoid duplicating priming code.
* Why should a loop exit NOT have an else clause?
It is redundant since the loop will only continue if the loop exit condition
was false.
* What does multi-level exits allow the elimination of?
Flag variables.
* How does Sequel provide simple exception handling?
The block's stack is unwound after the end of the block.
* Why does C longjmp execute faster than C++ throw?
Because it doesn't call destructors, so it can be a direct stack transfer.
* What happens when a resumption exception is not handled?
It is rethrown as a termination exception.
* What class of problems does a coroutine handle better than the basic control-flow constructs?
Problems that can be simplified to traversing a DFA or state-machine.
* Why does coroutine main termination resume the start coroutine work for 80% of all coroutines?
For semi-coroutines, the starter is often the only resumer.
For full-coroutines, starters almost always lead to uMain, which can delete
unterminated coroutines.
* What actions occur in uC++ if a coroutine does not handle an exception?
Raises the non-local exception UnhandledException to the last resumer.
* What is the relationship between a user and a kernel thread?
User threads run on kernel threads.
User threads are scheduled by a library, while kernel threads are scheduled by
the OS.
* What does unbounded overtaking mean?
A low-priority thread can reenter the critical section any number of times
until the high-priority thread declares its intent.
* What is the property of a hardware atomic instruction that allows it to solve mutual exclusion?
Nothing can preempt in the middle of the instruction.
* What rule do hardware atomic instructions violate?
Variable order and speed of execution. (Rule 2)
* How is barging prevented when implementing a mutex lock?
Hold onto the internal lock between releasing and unblocking tasks.
* Can spin locks be used for synchronization and mutual exclusion?
Both.
* Can owner locks be used for synchronization and mutual exclusion?
Only mutual exclusion.
* Can condition locks be used for synchronization and mutual exclusion?
Only synchronization.
* Can barriers be used for synchronization and mutual exclusion?
Only synchronization.
* Can semaphores be used for synchronization and mutual exclusion?
Both.
* Why does modularization cause problems with multi-level exit?
Labels are known only in a routine's scope, so the modules may no longer know
about them.
* When a resumption handler completes, does it perform a static or dynamic return?
Dynamic. Because you don't know where it's going to resume.
* When a termination handler completes, does it perform a static or dynamic return?
Static. Because you know that the statements right after the catch will be
executed.
* What's an advantage of restricting the placement of resume to within a coroutine's members?
The coroutine programmer is able to fully control how the coroutine is resumed.
* What is the advantage of a stackful coroutine vs a stackless one?
A stackful coroutine can modularize code containing a suspend into a helper function.
It can also support recursion.
* What does RW safe mean?
RW-safe means that an algorithm works even if simultaneous writes to the same
location scramble the bits or simulataneous read during a write to the same
location sees the bits flicker.
* What does ABA mean in the ABA problem w.r.t lock-free data structures?
A is removed.
Before A can be removed fully, the thread is preempted and
A then B is removed and A is put back.
When the thread is restarted its view of A is now incorrect.
* Why are condition locks weaker than semaphores?
Condition locks contain no state and cannot remember previous actions performed
on the lock, like a previous signal.
* Why is synchronization more complex for blocking locks than spinning locks?
Blocking locks get only one change to test lock state.
Therefore, you must ensure that the blocking and acquiring is atomic w.r.t to
the releaser.
* When is a loop in a concurrent program a busy-wait and when is it not a busy-wait?
A loop needs a bound or else its a busy wait.
* What can fat atomic instructions eliminate completely?
Busy waiting.
* What is the advantage of using fat atomic instructions?
You can perform a critical section atomically without expensive solutions.
* What is the disadvantage of using fat atomic instructions?
It blocks interrupts during that time.
* Construct a synchronization deadlock with a semaphore.
sem = 0;
sem.P();
* Construct a mutual-exclusion deadlock with a semaphore.
sem = 1;
sem.P();
other task:
sem.P();
# Lock Programming
* How does buffering work?
- Tasks communicate unidirectionally through a queue.
- Producer adds items to the back of the queue.
- Consumer removes items from the front of the queue.
* What is a split binary semaphore?
A collection of semaphores where at most one of the collection has the value 1.
* When are split binary semaphores used?
When different kinds of tasks have to block separately.
* What technique do split binary semaphores use to solve mutual-exclusion problems?
Baton passing.
* What are the rules of baton passing?
- There is exactly one baton.
- Nobody moves in the entry/exit code unless they have the baton.
- Once the baton is released, one cannot read/write variables in the entry/exit.
* Can mutex/condition locks perform baton passing to prevent barging?
Only if the signalled task does not need to re-acquire the lock before continuing.
* What is the readers and writers problem?
- Multiple tasks share a resource. Some read, some write.
- Allow multiple concurrent readers, but serialize writer access.
* How can we use split-binary semaphores to segregate tasks in the readers and writers problem?
Separate tasks into 3 categories:
- Arrivers
- Readers
- Writers
* What is a shadow queue?
A queue that stores additional information about tasks blocked in a semaphore.
# 7 Concurrent Errors
* What is a race condition?
When there is missing synchronization or mutual exclusion.
At least two threads race along assuming that it exists.
* What is a live-lock?
When at least two threads are stuck waiting for another to go first.
* What is a property of live-locks?
There always exits some mechanism to break ties on simultaneous arrival that
deals effectively with the live-lock.
* What is starvation?
A selection algorithm ignores one or more tasks so they are never executed.
* What is a deadlock?
When one or more tasks are waiting for an event that will never occur.
* What is starvation?
When a task tries to acquire a lock, but never does.
# Indirect communication
* What is the idea of "critical regions" w.r.t concurrency APIs?
- Declare which variables are to be shared.
- Access to shared variables are restricted to within a REGION statement.
- Mutual exclusion within that statement.
* What are some problems of the "critical regions" idea of concurrency?
- Nesting these critical regions can cause a deadlock.
- Simultaneous reads are impossible.
* What are conditional critical regions?
Introduce a condition that must be true as well as having mutual exclusion
before you can enter the critical region.
* What is a monitor?
An abstract data type that combines shared data with serialization of its
modification.
* What is a mutex member?
A member function that does not begin execution if there is another active
mutex member.
* What are the two types of synchronization scheduling?
External and internal.
* What is external scheduling? How does one do it in uC++?
Tasks are scheduled outside the monitor. _Accept statement.
* What is internal scheduling? How does one do it in uC++?
Tasks are scheduled inside the monitor. uCondition variables.
* What is the advantage of external scheduling over internal scheduling?
External scheduling is easier to specify and explain over internal scheduling.
* When is external scheduling unusable?
- Scheduling depends on member parameter values.
- Scheduling must block in the monitor, but cannot guarantee that the next call
fulfils cooperation.
* What is the nested monitor problem?
Acquire monitor X, call to monitor Y, wait on condition in Y.
Now monitor Y's mutex is released, but monitor X's mutex is not.
* When does explicit scheduling occur?
- An accept statement blocks the active task on the acceptor stack and makes
a task ready from the specified mutex member queue.
- A signal moves a task from the specified condition to the signalled stack.
* When does implicit scheduling occur?
When a task waits in or exits from a mutex member and a new task is selected.
* What are the possible orderings of relative priority between task queues in a monitor?
Calling queue has to be lowest priority (or equal or lower), anything else is fine.
* What is a disadvantage of implicit (automatic) signal monitors?
Poor performance.
* What is a coroutine monitor?
A coroutine with implicit mutual exclusion on calls to specified member routines.
* What is a Java monitor?
- A monitor with a single implicit condition variable.
- Internal scheduling is no-priority nonblocking.
- Barging can occur.
# Direct communication
* What is a task?
A class with a distinguished member (main) that has its own execution state.
* How is a task similar to a monitor?
Tasks provide mutual exclusion and synchronization so only one thread is active
in that object.
* What does _Else do in uC++?
It is executed if there is no caller waiting on one of the functions specified
in the previous _Accept statements.
* What happens after a task's destructor is called?
The task behaves like a monitor.
* How can an internal buffer increase concurrency?
One can process and put multiple results into the buffer concurrently, rather
than running the process inside the mutex member itself.
* What are some issues with the internal buffer method to increase concurrency?
- Unless the average time for production and consumption is almost equal, the
buffer is either always full or empty.
- Since tasks are monitor-like, no calls can occur while the server is working.
* What is a worker task?
An additional (private) task that is used to offload the work to another thread.
* What is an administrator?
A server managing multiple clients and worker tasks.
Does little real work, just manages.
* Why does the administrator not make calls to other tasks?
Because those calls may block the administrator, but we want the administrator
to always keep accepting calls.
* How does an administrator work?
Maintains a list of work to pass to worker tasks.
* What are some typical worker types?
- Timer
- Notifier
- Simple worker
- Complex worker
- Courier
* What is a timer worker?
A task that prompts the administrator at specified time intervals.
* What is a notifier worker?
A task that performs a potentially blocking wait for an external event.
* What is a simple worker?
A task that does work and returns the result to the administrator.
* What is a complex worker?
A task that does work and interacts directly with client of the work.
* What is a courier worker?
A task that performs a potentially blocking call on behalf of the administrator.
* How can clients increase concurrency with servers?
Perform server calls asynchronously.
* Does uC++ provide asynchronous calls out of the box?
No.
* How do tickets work w.r.t asynchronous calls?
- Tickets are matched with results and given to the client.
- Clients retrieve their result by passing the ticket to the server.
* What is a disadvantage of using tickets for managing asynchronous calls?
Protocols are error-prone because the caller may not obey the protocol.
* How do call-back routines work w.r.t asynchronous calls?
- A routine is passed to the server on the initial call.
- Routine is called when the result is ready.
* What is the advantage of using call-back routines for managing asynchronous calls?
The server does not need to store any results, it can drop it off immediately.
* What are futures?
A future provides asynchronous communcation but without an explicit protocol.
* What is the difference between Future_ESM and Future_ISM?
Future_ESM must be explicitly deleted.
Future_ISM has garbage collection.
* What is the _Select clause w.r.t futures?
The _Select statement waits for one or more heterogeneous futures based on the
logical clause passed inside it.
The selector is blocked until future.available() is true.
* What is the difference between future() and _Select(future)?
future() gets the value, which also means that it can throw an exception.
* What is something important to consider when you have e.g. _Select(f1 || f2)?
When the statement finishes, you don't know which future is available so you
have to check.
# 10 Optimization
* Why is optimization useful or necessary?
To conserve resources and for good performance.
* What are the 3 general forms of optimization?
1. Reordering.
2. Eliding.
3. Replication.
* What is reordering w.r.t optimization?
Data and code are reordered to increase performance in certain contexts.
* What is eliding w.r.t optimization?
Removal of unnecessary data, data accesses, and computation.
* What is replication w.r.t optimization?
Processors, memory, data, code are duplicated because of limitations in
processing or communication.
* How can the compile/hardware optimize sequential programs?
- Reorder disjoint operations.
- Elide unnecessary operations.
- Execute in parallel if multiple functional-minutes.
* How are caches loaded?
In cache lines of 32/64/128 bytes.
* Why is the necessary to eagerly move reads up the memory hierarchy?
So that the next read will be cached.
* Why is it advantageous to lazily move writes down the memory hierarchy?
Because writing is expensive so we don't want to block on it very often.
* What is cache coherence?
A hardware protocol for ensuring update of duplicate data.
* What are the advantages and drawbacks of eager cache consistency?
+ Data changes appear instantaneously.
- Slow and expensive.
* What are the advantages and drawbacks of lazy cache consistency?
+ Faster
- Concurrent programs can read stale data.
* What is cache thrashing?
When threads continuously read/write the same memory locations, causing the
excessive cache updates.
* What is false sharing?
Since cache lines can contain multiple variables, cache thrashing can through
different variables.
* How can sequential optimizations cause problems for concurrent programs?
Concurrent sections accessing shared variables can become corrupted by
sequential optimizations.
* How can reordering optimizations cause problems for concurrent programs?
- Flag variable reads might be done in a different order than intended.
- Locks can have their function calls be reordered.
* How can eliding optimizations cause problems for concurrent programs?
- Flag variables can be copied to a register, and thus never being updated.
- "Meaningless" sleep statements may be removed causing delays to be skipped.
* How can replication optimizations cause problems for concurrent programs?
- Modern processors execute multiple instructions in parallel.
- Sometimes the order of instructions matters however.
- e.g. A variable cannot be visible before being initialized.
* What are the sources of all optimization problems?
Races on shared variables.
* How can you prevent optimization problems?
Protect all shared data with locks.
* What is a race free program?
No variables in the program has races.
* Is a race free program free of all races?
No. There can still be races internal to locks.
* What are the two approaches lock programmers can take to eliminate races in locks?
1. Ad hoc
2. Formal