summaryrefslogtreecommitdiffstats
path: root/doc/sample.tmpl
diff options
context:
space:
mode:
Diffstat (limited to 'doc/sample.tmpl')
-rw-r--r--doc/sample.tmpl104
1 files changed, 104 insertions, 0 deletions
diff --git a/doc/sample.tmpl b/doc/sample.tmpl
new file mode 100644
index 0000000..2d07635
--- /dev/null
+++ b/doc/sample.tmpl
@@ -0,0 +1,104 @@
1
2 +-----------------+
3 | CS 140 |
4 | SAMPLE PROJECT |
5 | DESIGN DOCUMENT |
6 +-----------------+
7
8---- GROUP ----
9
10Ben Pfaff <blp@stanford.edu>
11
12---- PRELIMINARIES ----
13
14>> If you have any preliminary comments on your submission, notes for
15>> the TAs, or extra credit, please give them here.
16
17(This is a sample design document.)
18
19>> Please cite any offline or online sources you consulted while
20>> preparing your submission, other than the Pintos documentation,
21>> course text, and lecture notes.
22
23None.
24
25 JOIN
26 ====
27
28---- DATA STRUCTURES ----
29
30>> Copy here the declaration of each new or changed `struct' or `struct'
31>> member, global or static variable, `typedef', or enumeration.
32>> Identify the purpose of each in 25 words or less.
33
34A "latch" is a new synchronization primitive. Acquires block
35until the first release. Afterward, all ongoing and future
36acquires pass immediately.
37
38 /* Latch. */
39 struct latch
40 {
41 bool released; /* Released yet? */
42 struct lock monitor_lock; /* Monitor lock. */
43 struct condition rel_cond; /* Signaled when released. */
44 };
45
46Added to struct thread:
47
48 /* Members for implementing thread_join(). */
49 struct latch ready_to_die; /* Release when thread about to die. */
50 struct semaphore can_die; /* Up when thread allowed to die. */
51 struct list children; /* List of child threads. */
52 list_elem children_elem; /* Element of `children' list. */
53
54---- ALGORITHMS ----
55
56>> Briefly describe your implementation of thread_join() and how it
57>> interacts with thread termination.
58
59thread_join() finds the joined child on the thread's list of
60children and waits for the child to exit by acquiring the child's
61ready_to_die latch. When thread_exit() is called, the thread
62releases its ready_to_die latch, allowing the parent to continue.
63
64---- SYNCHRONIZATION ----
65
66>> Consider parent thread P with child thread C. How do you ensure
67>> proper synchronization and avoid race conditions when P calls wait(C)
68>> before C exits? After C exits? How do you ensure that all resources
69>> are freed in each case? How about when P terminates without waiting,
70>> before C exits? After C exits? Are there any special cases?
71
72C waits in thread_exit() for P to die before it finishes its own
73exit, using the can_die semaphore "down"ed by C and "up"ed by P as
74it exits. Regardless of whether whether C has terminated, there
75is no race on wait(C), because C waits for P's permission before
76it frees itself.
77
78Regardless of whether P waits for C, P still "up"s C's can_die
79semaphore when P dies, so C will always be freed. (However,
80freeing C's resources is delayed until P's death.)
81
82The initial thread is a special case because it has no parent to
83wait for it or to "up" its can_die semaphore. Therefore, its
84can_die semaphore is initialized to 1.
85
86---- RATIONALE ----
87
88>> Critique your design, pointing out advantages and disadvantages in
89>> your design choices.
90
91This design has the advantage of simplicity. Encapsulating most
92of the synchronization logic into a new "latch" structure
93abstracts what little complexity there is into a separate layer,
94making the design easier to reason about. Also, all the new data
95members are in `struct thread', with no need for any extra dynamic
96allocation, etc., that would require extra management code.
97
98On the other hand, this design is wasteful in that a child thread
99cannot free itself before its parent has terminated. A parent
100thread that creates a large number of short-lived child threads
101could unnecessarily exhaust kernel memory. This is probably
102acceptable for implementing kernel threads, but it may be a bad
103idea for use with user processes because of the larger number of
104resources that user processes tend to own.