Get rid of THREAD_JOIN_IMPLEMENTED by adding thread_join() stub.
[pintos-anon] / doc / threads.texi
1 @node Project 1--Threads, Project 2--User Programs, Pintos Tour, Top
2 @chapter Project 1: Threads
3
4 In this assignment, we give you a minimally functional thread system.
5 Your job is to extend the functionality of this system to gain a
6 better understanding of synchronization problems. Additionally, you
7 will use at least part of this increased functionality in future
8 assignments.
9
10 You will be working in primarily in the @file{threads} directory for
11 this assignment, with some work in the @file{devices} directory on the
12 side.  Compilation should be done in the @file{threads} directory.
13
14 Before you read the description of this project, you should read all
15 of the following sections: @ref{Introduction}, @ref{Coding Standards},
16 @ref{Project Documentation}, @ref{Debugging Tools}, and
17 @ref{Development Tools}.  You should at least skim the material in
18 @ref{Threads Tour}.  To complete this project you will also need to
19 read @ref{Multilevel Feedback Scheduling}.
20
21 @menu
22 * Understanding Threads::       
23 * Project 1 Code::              
24 * Debugging versus Testing::    
25 * Tips::                        
26 * Problem 1-1 Alarm Clock::     
27 * Problem 1-2 Join::            
28 * Problem 1-3 Priority Scheduling::  
29 * Problem 1-4 Advanced Scheduler::  
30 * Threads FAQ::                 
31 @end menu
32
33 @node Understanding Threads
34 @section Understanding Threads
35
36 The first step is to read and understand the initial thread system.
37 Pintos, by default, implements thread creation and thread completion,
38 a simple scheduler to switch between threads, and synchronization
39 primitives (semaphores, locks, and condition variables). 
40
41 However, there's a lot of magic going on in some of this code, so if
42 you haven't already compiled and run the base system, as described in
43 the introduction (@pxref{Introduction}), you should do so now.  You
44 can read through parts of the source code by hand to see what's going
45 on.  If you like, you can add calls to @func{printf} almost
46 anywhere, then recompile and run to see what happens and in what
47 order.  You can also run the kernel in a debugger and set breakpoints
48 at interesting spots, single-step through code and examine data, and
49 so on.  @xref{i386-elf-gdb}, for more information.
50
51 When a thread is created, you are creating a new context to be
52 scheduled. You provide a function to be run in this context as an
53 argument to @func{thread_create}. The first time the thread is
54 scheduled and runs, it will start from the beginning of that function
55 and execute it in the context. When that function returns, that thread
56 completes. Each thread, therefore, acts like a mini-program running
57 inside Pintos, with the function passed to @func{thread_create}
58 acting like @func{main}.
59
60 At any given time, Pintos is running exactly one thread, with the
61 others switched out.  The scheduler decides which thread to run next
62 when it needs to switch between them.  (If no thread is ready to run
63 at any given time, then the special ``idle'' thread runs.)  The
64 synchronization primitives are used to force context switches when one
65 thread needs to wait for another thread to do something.
66
67 The exact mechanics of a context switch are pretty gruesome and have
68 been provided for you in @file{threads/switch.S} (this is 80@var{x}86
69 assembly; don't worry about understanding it).  It involves saving the
70 state of the currently running thread and restoring the state of the
71 thread we're switching to.
72
73 Using the @command{gdb} debugger, slowly trace through a context
74 switch to see what happens (@pxref{i386-elf-gdb}).  You can set a
75 breakpoint on the @func{schedule} function to start out, and then
76 single-step from there.@footnote{@command{gdb} might tell you that
77 @func{schedule} doesn't exist, which is arguably a @command{gdb} bug.
78 You can work around this by setting the breakpoint by filename and
79 line number, e.g.@: @code{break thread.c:@var{ln}} where @var{ln} is
80 the line number of the first declaration in @func{schedule}.}  Be sure
81 to keep track of each thread's address
82 and state, and what procedures are on the call stack for each thread.
83 You will notice that when one thread calls @func{switch_threads},
84 another thread starts running, and the first thing the new thread does
85 is to return from @func{switch_threads}.  We realize this comment will
86 seem cryptic to you at this point, but you will understand threads
87 once you understand why the @func{switch_threads} that gets called is
88 different from the @func{switch_threads} that returns.
89
90 @strong{Warning}: In Pintos, each thread is assigned a small,
91 fixed-size execution stack just under @w{4 kB} in size.  The kernel
92 does try to detect stack overflow, but it cannot always succeed.  You
93 may cause bizarre problems, such as mysterious kernel panics, if you
94 declare large data structures as non-static local variables,
95 e.g. @samp{int buf[1000];}.  Alternatives to stack allocation include
96 the page allocator in @file{threads/palloc.c} and the block allocator
97 in @file{threads/malloc.c}.  Note that the page allocator doles out
98 @w{4 kB} chunks and that @func{malloc} has a @w{2 kB} block size
99 limit.  If you need larger chunks, consider using a linked structure
100 instead.
101
102 @node Project 1 Code
103 @section Code
104
105 Here is a brief overview of the files in the @file{threads}
106 directory.  You will not need to modify most of this code, but the
107 hope is that presenting this overview will give you a start on what
108 code to look at.
109
110 @table @file
111 @item loader.S
112 @itemx loader.h
113 The kernel loader.  Assembles to 512 bytes of code and data that the
114 PC BIOS loads into memory and which in turn loads the kernel into
115 memory, does basic processor initialization, and jumps to the
116 beginning of the kernel.  You should not need to look at this code or
117 modify it.
118
119 @item kernel.lds.S
120 The linker script used to link the kernel.  Sets the load address of
121 the kernel and arranges for @file{start.S} to be at the very beginning
122 of the kernel image.  Again, you should not need to look at this code
123 or modify it, but it's here in case you're curious.
124
125 @item start.S
126 Jumps to @func{main}.
127
128 @item init.c
129 @itemx init.h
130 Kernel initialization, including @func{main}, the kernel's ``main
131 program.''  You should look over @func{main} at least to see what
132 gets initialized.
133
134 @item thread.c
135 @itemx thread.h
136 Basic thread support.  Much of your work will take place in these
137 files.  @file{thread.h} defines @struct{thread}, which you will
138 modify in the first three projects.
139
140 @item switch.S
141 @itemx switch.h
142 Assembly language routine for switching threads.  Already discussed
143 above.
144
145 @item palloc.c
146 @itemx palloc.h
147 Page allocator, which hands out system memory in multiples of 4 kB
148 pages.
149
150 @item malloc.c
151 @itemx malloc.h
152 A very simple implementation of @func{malloc} and @func{free} for
153 the kernel.
154
155 @item interrupt.c
156 @itemx interrupt.h
157 Basic interrupt handling and functions for turning interrupts on and
158 off.
159
160 @item intr-stubs.S
161 @itemx intr-stubs.h
162 Assembly code for low-level interrupt handling.
163
164 @item synch.c
165 @itemx synch.h
166 Basic synchronization primitives: semaphores, locks, and condition
167 variables.  You will need to use these for synchronization through all
168 four projects.
169
170 @item test.c
171 @itemx test.h
172 Test code.  For project 1, you will replace this file with your test
173 cases.
174
175 @item io.h
176 Functions for I/O port access.  This is mostly used by source code in
177 the @file{devices} directory that you won't have to touch.
178
179 @item mmu.h
180 Functions and macros related to memory management, including page
181 directories and page tables.  This will be more important to you in
182 project 3.  For now, you can ignore it.
183 @end table
184
185 @menu
186 * devices code::                
187 * lib files::                   
188 @end menu
189
190 @node devices code
191 @subsection @file{devices} code
192
193 The basic threaded kernel also includes these files in the
194 @file{devices} directory:
195
196 @table @file
197 @item timer.c
198 @itemx timer.h
199 System timer that ticks, by default, 100 times per second.  You will
200 modify this code in Problem 1-1.
201
202 @item vga.c
203 @itemx vga.h
204 VGA display driver.  Responsible for writing text to the screen.
205 You should have no need to look at this code.  @func{printf} will
206 call into the VGA display driver for you, so there's little reason to
207 call this code yourself.
208
209 @item serial.c
210 @itemx serial.h
211 Serial port driver.  Again, @func{printf} calls this code for you,
212 so you don't need to do so yourself.  Feel free to look through it if
213 you're curious.
214
215 @item disk.c
216 @itemx disk.h
217 Supports reading and writing sectors on up to 4 IDE disks.  This won't
218 actually be used until project 2.
219
220 @item intq.c
221 @itemx intq.h
222 Interrupt queue, for managing a circular queue that both kernel
223 threads and interrupt handlers want to access.  Used by the keyboard
224 and serial drivers.
225 @end table
226
227 @node lib files
228 @subsection @file{lib} files
229
230 Finally, @file{lib} and @file{lib/kernel} contain useful library
231 routines.  (@file{lib/user} will be used by user programs, starting in
232 project 2, but it is not part of the kernel.)  Here's a few more
233 details:
234
235 @table @file
236 @item ctype.h
237 @itemx inttypes.h
238 @itemx limits.h
239 @itemx stdarg.h
240 @itemx stdbool.h
241 @itemx stddef.h
242 @itemx stdint.h
243 @itemx stdio.c
244 @itemx stdio.h
245 @itemx stdlib.c
246 @itemx stdlib.h
247 @itemx string.c
248 @itemx string.h
249 Implementation of the standard C library.  @xref{C99}, for information
250 on a few recently introduced pieces of the C library that you might
251 not have encountered before.  @xref{Unsafe String Functions}, for
252 information on what's been intentionally left out for safety.
253
254 @item debug.c
255 @itemx debug.h
256 Functions and macros to aid debugging.  @xref{Debugging Tools}, for
257 more information.
258
259 @item random.c
260 @itemx random.h
261 Pseudo-random number generator.
262
263 @item round.h
264 Macros for rounding.
265
266 @item syscall-nr.h
267 System call numbers.  Not used until project 2.
268
269 @item kernel/list.c
270 @itemx kernel/list.h
271 Doubly linked list implementation.  Used all over the Pintos code, and
272 you'll probably want to use it a few places yourself in project 1.
273
274 @item kernel/bitmap.c
275 @itemx kernel/bitmap.h
276 Bitmap implementation.  You can use this in your code if you like, but
277 you probably won't have any need for project 1.
278
279 @item kernel/hash.c
280 @itemx kernel/hash.h
281 Hash table implementation.  Likely to come in handy for project 3.
282
283 @item kernel/console.c
284 @itemx kernel/console.h
285 Implements @func{printf} and a few other functions.
286 @end table
287
288 @node Debugging versus Testing
289 @section Debugging versus Testing
290
291 When you're debugging code, it's useful to be able to be able to run a
292 program twice and have it do exactly the same thing.  On second and
293 later runs, you can make new observations without having to discard or
294 verify your old observations.  This property is called
295 ``reproducibility.''  The simulator we use, Bochs, can be set up for
296 reproducibility, and that's the way that @command{pintos} invokes it
297 by default.
298
299 Of course, a simulation can only be reproducible from one run to the
300 next if its input is the same each time.  For simulating an entire
301 computer, as we do, this means that every part of the computer must be
302 the same.  For example, you must use the same disks, the same version
303 of Bochs, and you must not hit any keys on the keyboard (because you
304 could not be sure to hit them at exactly the same point each time)
305 during the runs.
306
307 While reproducibility is useful for debugging, it is a problem for
308 testing thread synchronization, an important part of this project.  In
309 particular, when Bochs is set up for reproducibility, timer interrupts
310 will come at perfectly reproducible points, and therefore so will
311 thread switches.  That means that running the same test several times
312 doesn't give you any greater confidence in your code's correctness
313 than does running it only once.
314
315 So, to make your code easier to test, we've added a feature, called
316 ``jitter,'' to Bochs, that makes timer interrupts come at random
317 intervals, but in a perfectly predictable way.  In particular, if you
318 invoke @command{pintos} with the option @option{-j @var{seed}}, timer
319 interrupts will come at irregularly spaced intervals.  Within a single
320 @var{seed} value, execution will still be reproducible, but timer
321 behavior will change as @var{seed} is varied.  Thus, for the highest
322 degree of confidence you should test your code with many seed values.
323
324 On the other hand, when Bochs runs in reproducible mode, timings are not
325 realistic, meaning that a ``one-second'' delay may be much shorter or
326 even much longer than one second.  You can invoke @command{pintos} with
327 a different option, @option{-r}, to make it set up Bochs for realistic
328 timings, in which a one-second delay should take approximately one
329 second of real time.  Simulation in real-time mode is not reproducible,
330 and options @option{-j} and @option{-r} are mutually exclusive.
331
332 @node Tips
333 @section Tips
334
335 @itemize @bullet
336 @item
337 There should be no busy waiting in any of your solutions to this
338 assignment.  We consider a tight loop that calls @func{thread_yield}
339 to be one form of busy waiting.
340
341 @item
342 Proper synchronization is an important part of the solutions to these
343 problems.  It is tempting to synchronize all your code by turning off
344 interrupts with @func{intr_disable} or @func{intr_set_level}, because
345 this eliminates concurrency and thus the possibility for race
346 conditions, but @strong{don't}.  Instead, use semaphores, locks, and
347 condition variables to solve the bulk of your synchronization
348 problems.  Read the tour section on synchronization
349 (@pxref{Synchronization}) or the comments in @file{threads/synch.c} if
350 you're unsure what synchronization primitives may be used in what
351 situations.
352
353 You might run into a few situations where interrupt disabling is the
354 best way to handle synchronization.  If so, you need to explain your
355 rationale in your design documents.  If you're unsure whether a given
356 situation justifies disabling interrupts, talk to the TAs, who can
357 help you decide on the right thing to do.
358
359 Disabling interrupts can be useful for debugging, if you want to make
360 sure that a section of code is not interrupted.  You should remove
361 debugging code before turning in your project.
362
363 @item
364 All parts of this assignment are required if you intend to earn full
365 credit on this project.  However, some will be more important in
366 future projects:
367
368 @itemize @minus
369 @item
370 Problem 1-1 (Alarm Clock) could be handy for later projects, but it is
371 not strictly required.
372
373 @item
374 Problem 1-2 (Join) will be needed for future projects.  We don't give
375 out solutions, so to avoid extra work later you should make sure that
376 your implementation of @func{thread_join} works correctly.
377
378 @item
379 Problems 1-3 and 1-4 won't be needed for later projects.
380 @end itemize
381
382 @item
383 Problem 1-4 (MLFQS) builds on the features you implement in Problem
384 1-3.  You should have Problem 1-3 fully working before you begin to
385 tackle Problem 1-4.
386
387 @item
388 In the past, many groups divided the assignment into pieces, then each
389 group member worked on his or her piece until just before the
390 deadline, at which time the group reconvened to combine their code and
391 submit.  @strong{This is a bad idea.  We do not recommend this
392 approach.}  Groups that do this often find that two changes conflict
393 with each other, requiring lots of last-minute debugging.  Some groups
394 who have done this have turned in code that did not even successfully
395 boot.
396
397 Instead, we recommend integrating your team's changes early and often,
398 using a source code control system such as CVS (@pxref{CVS}) or a
399 group collaboration site such as SourceForge (@pxref{SourceForge}).
400 This is less likely to produce surprises, because everyone can see
401 everyone else's code as it is written, instead of just when it is
402 finished.  These systems also make it possible to review changes and,
403 when a change introduces a bug, drop back to working versions of code.
404
405 @item
406 You should expect to run into bugs that you simply don't understand
407 while working on this and subsequent projects.  When you do, go back
408 and reread the appendix on debugging tools, which is filled with
409 useful debugging tips that should help you to get back up to speed
410 (@pxref{Debugging Tools}).  Be sure to read the section on backtraces
411 (@pxref{Backtraces}), which will help you to get the most out of every
412 kernel panic or assertion failure.
413 @end itemize
414
415 @node Problem 1-1 Alarm Clock
416 @section Problem 1-1: Alarm Clock
417
418 Improve the implementation of the timer device defined in
419 @file{devices/timer.c} by reimplementing @func{timer_sleep}.
420 Threads call @code{timer_sleep(@var{x})} to suspend execution until
421 time has advanced by at least @w{@var{x} timer ticks}.  This is
422 useful for threads that operate in real-time, for example, for
423 blinking the cursor once per second.  There is no requirement that
424 threads start running immediately after waking up; just put them on
425 the ready queue after they have waited for approximately the right
426 amount of time.
427
428 A working implementation of this function is provided.  However, the
429 version provided is poor, because it ``busy waits,'' that is, it spins
430 in a tight loop checking the current time until the current time has
431 advanced far enough.  This is undesirable because it wastes time that
432 could potentially be used more profitably by another thread.  Your
433 solution should not busy wait.
434
435 The argument to @func{timer_sleep} is expressed in timer ticks, not in
436 milliseconds or any another unit.  There are @code{TIMER_FREQ} timer
437 ticks per second, where @code{TIMER_FREQ} is a macro defined in
438 @code{devices/timer.h}.
439
440 Separate functions @func{timer_msleep}, @func{timer_usleep}, and
441 @func{timer_nsleep} do exist for sleeping a specific number of
442 milliseconds, microseconds, or nanoseconds, respectively, but these will
443 call @func{timer_sleep} automatically when necessary.  You do not need
444 to modify them.
445
446 If your delays seem too short or too long, reread the explanation of the
447 @option{-r} option to @command{pintos} (@pxref{Debugging versus
448 Testing}).
449
450 @node Problem 1-2 Join
451 @section Problem 1-2: Join
452
453 Implement @code{thread_join(tid_t)} in @file{threads/thread.c}.  There
454 is already a prototype for it in @file{threads/thread.h}, which you
455 should not change.  This function causes the currently running thread
456 to block until the thread whose thread id is passed as an argument
457 exits.  If @var{A} is the running thread and @var{B} is the argument,
458 then we say that ``@var{A} joins @var{B}.''
459
460 Incidentally, we don't use @code{struct thread *} as
461 @func{thread_join}'s parameter type because a thread pointer is not
462 unique over time.  That is, when a thread dies, its memory may be,
463 whether immediately or much later, reused for another thread.  If
464 thread @var{A} over time had two children @var{B} and @var{C} that
465 were stored at the same address, then @code{thread_join(@var{B})} and
466 @code{thread_join(@var{C})} would be ambiguous.  Introducing a thread
467 id or @dfn{tid}, represented by type @code{tid_t}, that is
468 intentionally unique over time solves the problem.  The provided code
469 uses an @code{int} for @code{tid_t}, but you may decide you prefer to
470 use some other type.
471
472 The model for @func{thread_join} is the @command{wait} system call
473 in Unix-like systems.  (Try reading the manpages.)  That system call
474 can only be used by a parent process to wait for a child's death.  You
475 should implement @func{thread_join} to have the same restriction.
476 That is, a thread may only join its immediate children.
477
478 A thread need not ever be joined.  Your solution should properly free
479 all of a thread's resources, including its @struct{thread},
480 whether it is ever joined or not, and regardless of whether the child
481 exits before or after its parent.  That is, a thread should be freed
482 exactly once in all cases.
483
484 Joining a given thread is idempotent.  That is, joining a thread T
485 multiple times is equivalent to joining it once, because T has already
486 exited at the time of the later joins.  Thus, joins on T after the
487 first should return immediately.
488
489 Calling @func{thread_join} on an thread that is not the caller's
490 child should cause the caller to return immediately.  For this purpose,
491 children are not inherited, that is, if @var{A} has child @var{B} and
492 @var{B} has child @var{C}, then @var{A} always returns immediately
493 should it try to join @var{C}, even if @var{B} is dead.
494
495 Consider all the ways a join can occur: nested joins (@var{A} joins
496 @var{B}, then @var{B} joins @var{C}), multiple joins (@var{A} joins
497 @var{B}, then @var{A} joins @var{C}), and so on.  Does your join work
498 if @func{thread_join} is called on a thread that has not yet been
499 scheduled for the first time?  You should handle all of these cases.
500 Write test code that demonstrates the cases your join works for.
501
502 Be careful to program this function correctly.  You will need its
503 functionality for project 2.
504
505 @node Problem 1-3 Priority Scheduling
506 @section Problem 1-3: Priority Scheduling
507
508 Implement priority scheduling in Pintos.  Priority scheduling is a key
509 building block for real-time systems.  Implement functions
510 @func{thread_set_priority} to set the priority of the running thread
511 and @func{thread_get_priority} to get the running thread's priority.
512 (This API only allows a thread to examine and modify its own
513 priority.)  There are already prototypes for these functions in
514 @file{threads/thread.h}, which you should not change.
515
516 Thread priority ranges from @code{PRI_MIN} (0) to @code{PRI_MAX} (59).
517 The initial thread priority is passed as an argument to
518 @func{thread_create}.  If there's no reason to choose another
519 priority, use @code{PRI_DEFAULT} (29).  The @code{PRI_} macros are
520 defined in @file{threads/thread.h}, and you should not change their
521 values.
522
523 When a thread is added to the ready list that has a higher priority
524 than the currently running thread, the current thread should
525 immediately yield the processor to the new thread.  Similarly, when
526 threads are waiting for a lock, semaphore or condition variable, the
527 highest priority waiting thread should be woken up first.  A thread
528 may raise or lower its own priority at any time, but lowering its
529 priority such that it no longer has the highest priority must cause it
530 to immediately yield the CPU.
531
532 One issue with priority scheduling is ``priority inversion'': if a
533 high priority thread needs to wait for a low priority thread (for
534 instance, for a lock held by a low priority thread, or in
535 @func{thread_join} for a thread to complete), and a middle priority
536 thread is on the ready list, then the high priority thread will never
537 get the CPU because the low priority thread will not get any CPU time.
538 A partial fix for this problem is to have the waiting thread
539 ``donate'' its priority to the low priority thread while it is holding
540 the lock, then recall the donation once it has acquired the lock.
541 Implement this fix.
542
543 You will need to account for all different orders in which priority
544 donation and inversion can occur.  Be sure to handle multiple
545 donations, in which multiple priorities are donated to a thread.  You
546 must also handle nested donation: given high, medium, and low priority
547 threads @var{H}, @var{M}, and @var{L}, respectively, if @var{H} is
548 waiting on a lock that @var{M} holds and @var{M} is waiting on a lock
549 that @var{L} holds, then both @var{M} and @var{L} should be boosted to
550 @var{H}'s priority.
551
552 You only need to implement priority donation when a thread is waiting
553 for a lock held by a lower-priority thread.  You do not need to
554 implement this fix for semaphores, condition variables, or joins,
555 although you are welcome to do so.  However, you do need to implement
556 priority scheduling in all cases.
557
558 @node Problem 1-4 Advanced Scheduler
559 @section Problem 1-4: Advanced Scheduler
560
561 Implement Solaris's multilevel feedback queue scheduler (MLFQS) to
562 reduce the average response time for running jobs on your system.
563 @xref{Multilevel Feedback Scheduling}, for a detailed description of
564 the MLFQS requirements.
565
566 Demonstrate that your scheduling algorithm reduces response time
567 relative to the original Pintos scheduling algorithm (round robin) for
568 at least one workload of your own design (i.e.@: in addition to the
569 provided test).
570
571 You must write your code so that we can turn the MLFQS on and off at
572 compile time.  By default, it must be off, but we must be able to turn
573 it on by inserting the line @code{#define MLFQS 1} in
574 @file{constants.h}.  @xref{Conditional Compilation}, for details.
575
576 @node Threads FAQ
577 @section FAQ
578
579 @enumerate 1
580 @item
581 @b{I am adding a new @file{.h} or @file{.c} file.  How do I fix the
582 @file{Makefile}s?}@anchor{Adding c or h Files}
583
584 To add a @file{.c} file, edit the top-level @file{Makefile.build}.
585 You'll want to add your file to variable @samp{@var{dir}_SRC}, where
586 @var{dir} is the directory where you added the file.  For this
587 project, that means you should add it to @code{threads_SRC}, or
588 possibly @code{devices_SRC} if you put in the @file{devices}
589 directory.  Then run @code{make}.  If your new file doesn't get
590 compiled, run @code{make clean} and then try again.
591
592 When you modify the top-level @file{Makefile.build}, the modified
593 version should be automatically copied to
594 @file{threads/build/Makefile} when you re-run make.  The opposite is
595 not true, so any changes will be lost the next time you run @code{make
596 clean} from the @file{threads} directory.  Therefore, you should
597 prefer to edit @file{Makefile.build} (unless your changes are meant to
598 be truly temporary).
599
600 There is no need to edit the @file{Makefile}s to add a @file{.h} file.
601
602 @item
603 @b{How do I write my test cases?}
604
605 Test cases should be replacements for the existing @file{test.c}
606 file.  Put them in a @file{threads/testcases} directory.
607 @xref{TESTCASE}, for more information.
608
609 @item
610 @b{Why can't I disable interrupts?}
611
612 Turning off interrupts should only be done for short amounts of time,
613 or else you end up losing important things such as disk or input
614 events.  Turning off interrupts also increases the interrupt handling
615 latency, which can make a machine feel sluggish if taken too far.
616 Therefore, in general, setting the interrupt level should be used
617 sparingly.  Also, any synchronization problem can be easily solved by
618 turning interrupts off, since while interrupts are off, there is no
619 concurrency, so there's no possibility for race conditions.
620
621 To make sure you understand concurrency well, we are discouraging you
622 from taking this shortcut at all in your solution.  If you are unable
623 to solve a particular synchronization problem with semaphores, locks,
624 or conditions, or think that they are inadequate for a particular
625 reason, you may turn to disabling interrupts.  If you want to do this,
626 we require in your design document a complete justification and
627 scenario (i.e.@: exact sequence of events) to show why interrupt
628 manipulation is the best solution.  If you are unsure, the TAs can
629 help you determine if you are using interrupts too haphazardly.  We
630 want to emphasize that there are only limited cases where this is
631 appropriate.
632
633 You might find @file{devices/intq.h} and its users to be an
634 inspiration or source of rationale.
635
636 @item
637 @b{Where might interrupt-level manipulation be appropriate?}
638
639 You might find it necessary in some solutions to the Alarm problem.
640
641 You might want it at one small point for the priority scheduling
642 problem.  Note that it is not required to use interrupts for these
643 problems.  There are other, equally correct solutions that do not
644 require interrupt manipulation.  However, if you do manipulate
645 interrupts and @strong{correctly and fully document it} in your design
646 document, we will allow limited use of interrupt disabling.
647
648 @item
649 @b{What does ``warning: no previous prototype for `@var{function}''
650 mean?}
651
652 It means that you defined a non-@code{static} function without
653 preceding it by a prototype.  Because non-@code{static} functions are
654 intended for use by other @file{.c} files, for safety they should be
655 prototyped in a header file included before their definition.  To fix
656 the problem, add a prototype in a header file that you include, or, if
657 the function isn't actually used by other @file{.c} files, make it
658 @code{static}.
659 @end enumerate
660
661 @menu
662 * Problem 1-1 Alarm Clock FAQ::  
663 * Problem 1-2 Join FAQ::        
664 * Problem 1-3 Priority Scheduling FAQ::  
665 * Problem 1-4 Advanced Scheduler FAQ::  
666 @end menu
667
668 @node Problem 1-1 Alarm Clock FAQ
669 @subsection Problem 1-1: Alarm Clock FAQ
670
671 @enumerate 1
672 @item
673 @b{Why can't I use most synchronization primitives in an interrupt
674 handler?}
675
676 As you've discovered, you cannot sleep in an external interrupt
677 handler.  Since many lock, semaphore, and condition variable functions
678 attempt to sleep, you won't be able to call those in
679 @func{timer_interrupt}.  You may still use those that never sleep.
680
681 Having said that, you need to make sure that global data does not get
682 updated by multiple threads simultaneously executing
683 @func{timer_sleep}.  Here are some pieces of information to think
684 about:
685
686 @enumerate a
687 @item
688 Interrupts are turned off while @func{timer_interrupt} runs.  This
689 means that @func{timer_interrupt} will not be interrupted by a
690 thread running in @func{timer_sleep}.
691
692 @item
693 A thread in @func{timer_sleep}, however, can be interrupted by a
694 call to @func{timer_interrupt}, except when that thread has turned
695 off interrupts.
696
697 @item
698 Examples of synchronization mechanisms have been presented in lecture.
699 Going over these examples should help you understand when each type is
700 useful or needed.  @xref{Synchronization}, for specific information
701 about synchronization in Pintos.
702 @end enumerate
703
704 @item
705 @b{What about timer overflow due to the fact that times are defined as
706 integers? Do I need to check for that?}
707
708 Don't worry about the possibility of timer values overflowing.  Timer
709 values are expressed as signed 63-bit numbers, which at 100 ticks per
710 second should be good for almost 2,924,712,087 years.
711
712 @item
713 @b{The test program mostly works but reports a few out-of-order
714 wake ups.  I think it's a problem in the test program.  What gives?}
715 @anchor{Out of Order 1-1}
716
717 This test is inherently full of race conditions.  On a real system it
718 wouldn't work perfectly all the time either.  There are a few ways you
719 can help it work more reliably:
720
721 @itemize @bullet
722 @item
723 Make time slices longer by increasing @code{TIME_SLICE} in
724 @file{timer.c} to a large value, such as 100.
725
726 @item
727 Make the timer tick more slowly by decreasing @code{TIMER_FREQ} in
728 @file{timer.h} to its minimum value of 19.
729 @end itemize
730
731 The former two changes are only desirable for testing problem 1-1 and
732 possibly 1-3.  You should revert them before working on other parts
733 of the project or turn in the project.
734
735 @item
736 @b{Should @file{p1-1.c} be expected to work with the MLFQS turned on?}
737
738 No.  The MLFQS will adjust priorities, changing thread ordering.
739 @end enumerate
740
741 @node Problem 1-2 Join FAQ
742 @subsection Problem 1-2: Join FAQ
743
744 @enumerate 1
745 @item
746 @b{Am I correct to assume that once a thread is deleted, it is no
747 longer accessible by the parent (i.e.@: the parent can't call
748 @code{thread_join(child)})?}
749
750 A parent joining a child that has completed should be handled
751 gracefully and should act as a no-op.
752 @end enumerate
753
754 @node Problem 1-3 Priority Scheduling FAQ
755 @subsection Problem 1-3: Priority Scheduling FAQ
756
757 @enumerate 1
758 @item
759 @b{Doesn't the priority scheduling lead to starvation? Or do I have to
760 implement some sort of aging?}
761
762 It is true that strict priority scheduling can lead to starvation
763 because thread may not run if a higher-priority thread is runnable.
764 In this problem, don't worry about starvation or any sort of aging
765 technique.  Problem 4 will introduce a mechanism for dynamically
766 changing thread priorities.
767
768 This sort of scheduling is valuable in real-time systems because it
769 offers the programmer more control over which jobs get processing
770 time.  High priorities are generally reserved for time-critical
771 tasks. It's not ``fair,'' but it addresses other concerns not
772 applicable to a general-purpose operating system.
773
774 @item
775 @b{After a lock has been released, does the program need to switch to
776 the highest priority thread that needs the lock (assuming that its
777 priority is higher than that of the current thread)?}
778
779 When a lock is released, the highest priority thread waiting for that
780 lock should be unblocked and put on the ready to run list.  The
781 scheduler should then run the highest priority thread on the ready
782 list.
783
784 @item
785 @b{If a thread calls @func{thread_yield} and then it turns out that
786 it has higher priority than any other threads, does the high-priority
787 thread continue running?}
788
789 Yes.  If there is a single highest-priority thread, it continues
790 running until it blocks or finishes, even if it calls
791 @func{thread_yield}.
792
793 @item
794 @b{If the highest priority thread is added to the ready to run list it
795 should start execution immediately.  Is it immediate enough if I
796 wait until next timer interrupt occurs?}
797
798 The highest priority thread should run as soon as it is runnable,
799 preempting whatever thread is currently running.
800
801 @item
802 @b{What happens to the priority of the donating thread?  Do the priorities
803 get swapped?}
804
805 No.  Priority donation only changes the priority of the low-priority
806 thread.  The donating thread's priority stays unchanged.  Also note
807 that priorities aren't additive: if thread A (with priority 5) donates
808 to thread B (with priority 3), then B's new priority is 5, not 8.
809
810 @item 
811 @b{Can a thread's priority be changed while it is sitting on the ready
812 queue?}
813
814 Yes.  Consider this case: low-priority thread L currently has a lock
815 that high-priority thread H wants.  H donates its priority to L (the
816 lock holder).  L finishes with the lock, and then loses the CPU and is
817 moved to the ready queue.  Now L's old priority is restored while it
818 is in the ready queue.
819
820 @item
821 @b{Can a thread's priority change while it is sitting on the queue of a
822 semaphore?}
823
824 Yes.  Same scenario as above except L gets blocked waiting on a new
825 lock when H restores its priority.
826
827 @item
828 @b{Why is @file{p1-3.c}'s FIFO test skipping some threads?  I know my
829 scheduler is round-robin'ing them like it's supposed to.   Our output
830 starts out okay, but toward the end it starts getting out of order.}
831
832 The usual problem is that the serial output buffer fills up.  This is
833 causing serial_putc() to block in thread @var{A}, so that thread
834 @var{B} is scheduled.  Thread @var{B} immediately tries to do output
835 of its own and blocks on the serial lock (which is held by thread
836 @var{A}).  Now that we've wasted some time in scheduling and locking,
837 typically some characters have been drained out of the serial buffer
838 by the interrupt handler, so thread @var{A} can continue its output.
839 After it finishes, though, some other thread (not @var{B}) is
840 scheduled, because thread @var{B} was already scheduled while we
841 waited for the buffer to drain.
842
843 There's at least one other possibility.  Context switches are being
844 invoked by the test when it explicitly calls @func{thread_yield}.
845 However, the time slice timer is still alive and so, every tick (by
846 default), a thread gets switched out (caused by @func{timer_interrupt}
847 calling @func{intr_yield_on_return}) before it gets a chance to run
848 @func{printf}, effectively skipping it.  If we use a different jitter
849 value, the same behavior is seen where a thread gets started and
850 switched out completely.
851
852 Normally you can fix these problems using the same techniques
853 suggested on problem 1-1 (@pxref{Out of Order 1-1}).
854
855 @item
856 @b{What happens when a thread is added to the ready list which has
857 higher priority than the currently running thread?}
858
859 The correct behavior is to immediately yield the processor.  Your
860 solution must act this way.
861
862 @item
863 @b{What should @func{thread_get_priority} return in a thread while
864 its priority has been increased by a donation?}
865
866 The higher (donated) priority.
867
868 @item
869 @b{Should @file{p1-3.c} be expected to work with the MLFQS turned on?}
870
871 No.  The MLFQS will adjust priorities, changing thread ordering.
872
873 @item
874 @b{@func{printf} in @func{sema_up} or @func{sema_down} makes the
875 system reboot!}
876
877 Yes.  These functions are called before @func{printf} is ready to go.
878 You could add a global flag initialized to false and set it to true
879 just before the first @func{printf} in @func{main}.  Then modify
880 @func{printf} itself to return immediately if the flag isn't set.
881 @end enumerate
882
883 @node Problem 1-4 Advanced Scheduler FAQ
884 @subsection Problem 1-4: Advanced Scheduler FAQ
885
886 @enumerate 1
887 @item
888 @b{What is the interval between timer interrupts?}
889
890 Timer interrupts occur @code{TIMER_FREQ} times per second.  You can
891 adjust this value by editing @file{devices/timer.h}.  The default is
892 100 Hz.
893
894 You can also adjust the number of timer ticks per time slice by
895 modifying @code{TIME_SLICE} in @file{devices/timer.c}.
896
897 @item
898 @b{Do I have to modify the dispatch table?}
899
900 No, although you are allowed to. It is possible to complete
901 this problem (i.e.@: demonstrate response time improvement)
902 without doing so.
903
904 @item
905 @b{When the scheduler changes the priority of a thread, how does this
906 affect priority donation?}
907
908 Short (official) answer: Don't worry about it. Your priority donation
909 code may assume static priority assignment.
910
911 Longer (unofficial) opinion: If you wish to take this into account,
912 however, your design may end up being ``cleaner.''  You have
913 considerable freedom in what actually takes place. I believe what
914 makes the most sense is for scheduler changes to affect the
915 ``original'' (non-donated) priority.  This change may actually be
916 masked by the donated priority.  Priority changes should only
917 propagate with donations, not ``backwards'' from donees to donors.
918
919 @item
920 @b{What is meant by ``static priority''?}
921
922 Once thread A has donated its priority to thread B, if thread A's
923 priority changes (due to the scheduler) while the donation still
924 exists, you do not have to change thread B's donated priority.
925 However, you are free to do so.
926
927 @item
928 @b{Do I have to make my dispatch table user-configurable?}
929
930 No.  Hard-coding the dispatch table values is fine.
931 @end enumerate