Project 1: Threads Synchronization
In this assignment, we give you a minimally functional thread system. Your job is to extend the functionality of this system to gain a better understanding of synchronization problems.
You will be working primarily in the threads directory for this assignment, with some work in the devices directory on the side. Compilation should be done in the threads directory.
Before you read the description of this project, you should read all of the following sections: 1. Introduction, C. Coding Standards, E. Debugging Tools, and F. Development Tools. You should at least skim the material from A.1 Loading through A.5 Memory Allocation, especially A.3 Synchronization.
2.1 Background
2.1.1 Understanding Threads
The first step is to read and understand the code for the initial thread system. Pintos already implements thread creation and thread completion, a simple scheduler to switch between threads, and synchronization primitives (semaphores, locks, condition variables, and optimization barriers).
Some of this code might seem slightly mysterious. If you haven’t already compiled and run the base system, as described in the introduction (see section 1. Introduction), you should do so now. You can read through parts of the source code to see what’s going on. If you like, you can add calls to printf()
almost anywhere, then recompile and run to see what happens and in what order. You can also run the kernel in a debugger and set breakpoints at interesting spots, single-step through code and examine data, and so on.
When a thread is created, you are creating a new context to be scheduled. You provide a function to be run in this context as an argument to thread_create()
. The first time the thread is scheduled and runs, it starts from the beginning of that function and executes in that context. When the function returns, the thread terminates. Each thread, therefore, acts like a mini-program running inside Pintos, with the function passed to thread_create()
acting like main()
.
At any given time, exactly one thread runs and the rest, if any, become inactive. The scheduler decides which thread to run next. (If no thread is ready to run at any given time, then the special “idle” thread, implemented in idle()
, runs.) Synchronization primitives can force context switches when one thread needs to wait for another thread to do something.
The mechanics of a context switch are in threads/switch.S, which is 80×86 assembly code. (You don’t have to understand it.) It saves the state of the currently running thread and restores the state of the thread we’re switching to.
Using the GDB debugger, slowly trace through a context switch to see what happens (see section E.5 GDB). You can set a breakpoint on schedule()
to start out, and then single-step from there.(2) Be sure to keep track of each thread’s address and state, and what procedures are on the call stack for each thread. You will notice that when one thread calls switch_threads()
, another thread starts running, and the first thing the new thread does is to return from switch_threads()
. You will understand the thread system once you understand why and how the switch_threads()
that gets called is different from the switch_threads()
that returns. See section A.2.3 Thread Switching, for more information.
Warning: In Pintos, each thread is assigned a small, fixed-size execution stack just under 4 kB in size. The kernel tries to detect stack overflow, but it cannot do so perfectly. You may cause bizarre problems, such as mysterious kernel panics, if you declare large data structures as non-static local variables, e.g. int buf[1000];. Alternatives to stack allocation include the page allocator and the block allocator (see section A.5 Memory Allocation).
2.1.2 Source Files
Here is a brief overview of the files in the threads directory. You will not need to modify most of this code, but the hope is that presenting this overview will give you a start on what code to look at.
loader.S, loader.h: The kernel loader. Assembles to 512 bytes of code and data that the PC BIOS loads into memory and which in turn finds the kernel on disk, loads it into memory, and jumps to start()
in start.S. See section A.1.1 The Loader, for details. You should not need to look at this code or modify it. start.S: Does basic setup needed for memory protection and 32-bit operation on 80×86 CPUs. Unlike the loader, this code is actually part of the kernel. See section A.1.2 Low-Level Kernel Initialization, for details. kernel.lds.S: The linker script used to link the kernel. Sets the load address of the kernel and arranges for start.S to be near the beginning of the kernel image. See section A.1.1 The Loader, for details. Again, you should not need to look at this code or modify it, but it’s here in case you’re curious. init.c, init.h: Kernel initialization, including main()
, the kernel’s “main program.” You should look over main()
at least to see what gets initialized. You might want to add your own initialization code here. See section A.1.3 High-Level Kernel Initialization, for details.
thread.c, thread.h: Basic thread support. Much of your work will take place in these files. thread.h defines struct thread
, which you are likely to modify in all four projects. See A.2.1 struct thread
and A.2 Threads for more information.
switch.S, switch.h: Assembly language routine for switching threads. Already discussed above. See section A.2.2 Thread Functions, for more information.
palloc.c, palloc.h: Page allocator, which hands out system memory in multiples of 4 kB pages. See section A.5.1 Page Allocator, for more information.
malloc.c, malloc.h: A simple implementation of malloc()
and free()
for the kernel. See section A.5.2 Block Allocator, for more information.
interrupt.c, interrupt.h: Basic interrupt handling and functions for turning interrupts on and off. See section A.4 Interrupt Handling, for more information. intr-stubs.Sintr-stubs.hAssembly code for low-level interrupt handling. See section A.4.1 Interrupt Infrastructure, for more information.
synch.c, synch.h: Basic synchronization primitives: semaphores, locks, condition variables, and optimization barriers. You will need to use these for synchronization in all four projects. See section A.3 Synchronization, for more information.
io.h: Functions for I/O port access. This is mostly used by source code in the devices directory that you won’t have to touch.
vaddr.h, pte.h: Functions and macros for working with virtual addresses and page table entries. These will be more important to you in project 3. For now, you can ignore them.
flags.h: Macros that define a few bits in the 80×86 “flags” register. Probably of no interest. See [ IA32-v1], section 3.4.3, “EFLAGS Register,” for more information.
2.1.2.1 devices code
The basic threaded kernel also includes these files in the devices directory:
timer.c, timer.h: System timer that ticks, by default, 100 times per second. You will modify this code in this project.
vga.c, vga.h: VGA display driver. Responsible for writing text to the screen. You should have no need to look at this code. printf()
calls into the VGA display driver for you, so there’s little reason to call this code yourself. serial.c, serial.h: Serial port driver. Again, printf()
calls this code for you, so you don’t need to do so yourself. It handles serial input by passing it to the input layer (see below).
block.c, block.h: An abstraction layer for block devices, that is, random-access, disk-like devices that are organized as arrays of fixed-size blocks. Out of the box, Pintos supports two types of block devices: IDE disks and partitions. Block devices, regardless of type, won’t actually be used until project 2.
ide.c, ide.h: Supports reading and writing sectors on up to 4 IDE disks.
partition.c,partition.h: Understands the structure of partitions on disks, allowing a single disk to be carved up into multiple regions (partitions) for independent use.
kbd.c, kbd.h: Keyboard driver. Handles keystrokes passing them to the input layer (see below).input.cinput.hInput layer. Queues input characters passed along by the keyboard or serial drivers.intq.cintq.hInterrupt queue, for managing a circular queue that both kernel threads and interrupt handlers want to access. Used by the keyboard and serial drivers.
rtc.c, rtc.h: Real-time clock driver, to enable the kernel to determine the current date and time. By default, this is only used by thread/init.c to choose an initial seed for the random number generator.
speaker.c, speaker.h: Driver that can produce tones on the PC speaker.
pit.c, pit.h: Code to configure the 8254 Programmable Interrupt Timer. This code is used by both devices/timer.c and devices/speaker.c because each device uses one of the PIT’s output channel.
2.1.2.2 lib files
Finally, lib and lib/kernel contain useful library routines. (lib/user will be used by user programs, starting in project 2, but it is not part of the kernel.) Here’s a few more details:
ctype.h, inttypes.h, limits.h, stdarg.h, stdbool.h, stddef.h, stdint.h, stdio.c, stdio.h, stdlib.c, stdlib.h, string.c, string.h: A subset of the standard C library. See section C.2 C99, for information on a few recently introduced pieces of the C library that you might not have encountered before. See section C.3 Unsafe String Functions, for information on what’s been intentionally left out for safety.
debug.c, debug.h: Functions and macros to aid debugging. See section E. Debugging Tools, for more information.
random.c, random.h: Pseudo-random number generator. The actual sequence of random values will not vary from one Pintos run to another, unless you do one of three things: specify a new random seed value on the -rs kernel command-line option on each run, or use a simulator other than Bochs, or specify the -r option to pintos
.
round.h: Macros for rounding.
syscall-nr.h: System call numbers. Not used until project 2.
kernel/list.c, kernel/list.h: Doubly linked list implementation. Used all over the Pintos code, and you’ll probably want to use it a few places yourself in project 1.
kernel/bitmap.c, kernel/bitmap.h: Bitmap implementation. You can use this in your code if you like, but you probably won’t have any need for it in project 1.
kernel/hash.c, kernel/hash.h: Hash table implementation. Likely to come in handy for project 3.
kernel/console.c, kernel/console.h, kernel/stdio.h: Implements printf()
and a few other functions.
2.1.3 Synchronization
Proper synchronization is an important part of the solutions to these problems. Any synchronization problem can be easily solved by turning interrupts off: while interrupts are off, there is no concurrency, so there’s no possibility for race conditions. Therefore, it’s tempting to solve all synchronization problems this way, but don’t. Instead, use semaphores, locks, and condition variables to solve the bulk of your synchronization problems. Read the tour section on synchronization (see section A.3 Synchronization) or the comments in threads/synch.c if you’re unsure what synchronization primitives may be used in what situations.
In the Pintos projects, the only class of problem best solved by disabling interrupts is coordinating data shared between a kernel thread and an interrupt handler. Because interrupt handlers can’t sleep, they can’t acquire locks. This means that data shared between kernel threads and an interrupt handler must be protected within a kernel thread by turning off interrupts.
This project only requires accessing a little bit of thread state from interrupt handlers. For the alarm clock, the timer interrupt needs to wake up sleeping threads. In the advanced scheduler, the timer interrupt needs to access a few global and per-thread variables. When you access these variables from kernel threads, you will need to disable interrupts to prevent the timer interrupt from interfering.
When you do turn off interrupts, take care to do so for the least amount of code possible, or you can end up losing important things such as timer ticks or input events. Turning off interrupts also increases the interrupt handling latency, which can make a machine feel sluggish if taken too far.
The synchronization primitives themselves in synch.c are implemented by disabling interrupts. You may need to increase the amount of code that runs with interrupts disabled here, but you should still try to keep it to a minimum.
Disabling interrupts can be useful for debugging, if you want to make sure that a section of code is not interrupted. You should remove debugging code before turning in your project. (Don’t just comment it out, because that can make the code difficult to read.)
There should be no busy waiting in your submission. A tight loop that calls thread_yield()
is one form of busy waiting.
2.1.4 Development Suggestions
In the past, many groups divided the assignment into pieces, then each group member worked on his or her piece until just before the deadline, at which time the group reconvened to combine their code and submit. This is a bad idea. We do not recommend this approach. Groups that do this often find that two changes conflict with each other, requiring lots of last-minute debugging. Some groups who have done this have turned in code that did not even compile or boot, much less pass any tests.
Instead, we recommend integrating your team’s changes early and often, using a source code control system such as CVS (see section F.3 CVS), or better yet git. This is less likely to produce surprises, because everyone can see everyone else’s code as it is written, instead of just when it is finished. These systems also make it possible to review changes and, when a change introduces a bug, drop back to working versions of code.
You should expect to run into bugs that you simply don’t understand while working on this and subsequent projects. When you do, reread the appendix on debugging tools, which is filled with useful debugging tips that should help you to get back up to speed (see section E. Debugging Tools). Be sure to read the section on backtraces (see section E.4 Backtraces), which will help you to get the most out of every kernel panic or assertion failure.
2.2 Requirements
2.2.1 Alarm Clock (Not Graded)
Reimplement timer_sleep()
, defined in devices/timer.c. Although a working implementation is provided, it “busy waits,” that is, it spins in a loop checking the current time and calling thread_yield()
until enough time has gone by. Reimplement it to avoid busy waiting.
Function: void timer_sleep (int64_t ticks)Suspends execution of the calling thread until time has advanced by at least x timer ticks. Unless the system is otherwise idle, the thread need not wake up after exactly x ticks. Just put it on the ready queue after they have waited for the right amount of time. timer_sleep()
is useful for threads that operate in real-time, e.g. for blinking the cursor once per second. The argument to timer_sleep()
is expressed in timer ticks, not in milliseconds or any another unit. There are TIMER_FREQ
timer ticks per second, where TIMER_FREQ
is a macro defined in devices/timer.h
. The default value is 100. We don’t recommend changing this value, because any change is likely to cause many of the tests to fail.
Separate functions timer_msleep()
, timer_usleep()
, and timer_nsleep()
do exist for sleeping a specific number of milliseconds, microseconds, or nanoseconds, respectively, but these will call timer_sleep()
automatically when necessary. You do not need to modify them.
If your delays seem too short or too long, reread the explanation of the -r option to pintos
(see section 1.1.5 Debugging versus Testing).
The alarm clock implementation is not needed for later projects, although it could be useful for project 4.
2.2.3 Read/Write Semaphore (50pts)
A read-write semaphore is a concurrency control mechanism which allows multiple readers and a single writer. If only readers are accessing the shared data structure, the new readers can access the data structure immediately, while the writer is blocked. If the writer is accessing the shared data structure, all the other readers/writers are blocked. In this project, you should implement the following data structures and API.
struct rw_semaphore
should be defined at synch.h.
void rwsema_init (struct rw_semaphore *);
Initializes a read-write semaphore structure. This function must be called before using the following 4 APIS.
void down_write (struct rw_semaphore *);
Acquires the exclusive lock on the read-write semaphore. If there are any active readers or another writer, the calling thread is blocked until it is safe to acquire the write lock. Note that this function must not be called from an interrupt context.
void down_read (struct rw_semaphore *);
Acquires a shared lock on the read-write semaphore. This function allows multiple threads to acquire the read lock simultaneously as long as there is no active writer. If a writer is active, the calling thread is blocked until it is safe to acquire the read lock. Note that this function must not be called from an interrupt context.
void up_write (struct rw_semaphore *);
Releases an exclusive lock on the read-write semaphore. This function ends the exclusive access for the writing thread and wakes up the next waiting writer thread. If there is no next waiting writer, wakes up the next waiting reader.
void up_read (struct rw_semaphore *);
Releases a shared lock on the read-write semaphore. This function decreases the reader count. And wakes up the next waiting reader if present; otherwise, wakes up the next waiting writer.
2.2.3 Sequence Lock (50pts)
A sequence lock (often referred to as seqlock) is a concurrency control mechanism for the shared data structure whose modification is not frequent. Sequence of the lock data structure is initialized to the zero. This sequence is incremented every time when the write lock is acquired and released. Thus, the reader can detect the conflict if the sequence is odd (write operation is ongoing) or if the sequence before reading and after is different. In this project, you should implement the following data structures and API.
struct seqlock
should be defined at synch.h.
void seqlock_init (struct seqlock *);
Initializes a seqlock structure. This function must be called before using the following 4 APIs.
int64_t read_seqlock_begin (struct seqlock *);
Begins a read operation on a seqlock. This function retrieves the current sequence number of the seqlock to be used for verifying consistency after the read operation. Return the current sequence number of the seqlock. This value should be stored and used later to check for consistency with read_seqretry().
bool read_seqretry(struct seqlock *, int64_t seq);
Checks if a read operation should be retried based on the provided sequence number. This function verifies if the sequence number has changed or if a write is in progress. The sequence number obtained from a previous call to read_seqlock_begin() must be provided to int64_t seq.Return true if the read operation should be retried (i.e., the sequence number has changed or is odd). Otherwise, return false.
void write_seqlock (struct seqlock *);
Begins a write operation on a seqlock. This function marks the start of a write critical section by incrementing the sequence number. When it returns the only process who can modify the data structure protected by the sequence lock is the current process. It should serialize the write operations protected by the sequence lock cooperating with the write_sequnlock().
void write_sequnlock (struct seqlock *);
Ends a write operation on a seqlock. This function marks the end of a write critical section by incrementing the sequence number again. It should allow .Increments the sequence field of the seqlock to mark the end of a write operation.
In this part of the project, you can just use spinlock to implement sequence or use atomic instructions of x86. If you use atomic instructions to implement sequence, please descirbe your implementation fully. You can get 50% extra points of sequence lock if your justification is right. Thus, you can get 25% extra points of total project 1.
2.2.5 Design Document
Breifly introduce your design and impelmentation. If you implement sequence lock with the atomic instrcutions, please rigoroulsy justify your algorithm and show the proof.
2.3 FAQ
How do I update the Makefiles when I add a new source file? To add a .c file, edit the top-level Makefile.build. Add the new file to variable dir_SRC, where dir is the directory where you added the file. For this project, that means you should add it to threads_SRC
or devices_SRC
. Then run make
. If your new file doesn’t get compiled, run make clean
and then try again. When you modify the top-level Makefile.build and re-run make
, the modified version should be automatically copied to threads/build/Makefile. The converse is not true, so any changes will be lost the next time you run make clean
from the threads directory. Unless your changes are truly temporary, you should prefer to edit Makefile.build. A new .h file does not require editing the Makefiles.
**What does warning: no previous prototype for
func’ mean?** It means that you defined a non-
static function without preceding it by a prototype. Because non-
static functions are intended for use by other .c files, for safety they should be prototyped in a header file included before their definition. To fix the problem, add a prototype in a header file that you include, or, if the function isn't actually used by other .c files, make it
static`.
What is the interval between timer interrupts? Timer interrupts occur TIMER_FREQ
times per second. You can adjust this value by editing devices/timer.h. The default is 100 Hz. We don’t recommend changing this value, because any changes are likely to cause many of the tests to fail.
How long is a time slice? There are TIME_SLICE
ticks per time slice. This macro is declared in threads/thread.c. The default is 4 ticks. We don’t recommend changing this value, because any changes are likely to cause many of the tests to fail.
How do I run the tests? See section 1.2 Grading .
Why do I get a test failure in pass()
? You are probably looking at a backtrace that looks something like this: 0xc0108810: debug_panic (lib/kernel/debug.c:32) 0xc010a99f: pass (tests/threads/tests.c:93) 0xc010bdd3: test_mlfqs_load_1 (...threads/mlfqs-load-1.c:33) 0xc010a8cf: run_test (tests/threads/tests.c:51) 0xc0100452: run_task (threads/init.c:283) 0xc0100536: run_actions (threads/init.c:333) 0xc01000bb: main (threads/init.c:137)
This is just confusing output from the backtrace
program. It does not actually mean that pass()
called debug_panic()
. In fact, fail()
called debug_panic()
(via the PANIC()
macro). GCC knows that debug_panic()
does not return, because it is declared NO_RETURN
(see section E.3 Function and Parameter Attributes), so it doesn’t include any code in fail()
to take control when debug_panic()
returns. This means that the return address on the stack looks like it is at the beginning of the function that happens to follow fail()
in memory, which in this case happens to be pass()
. See section E.4 Backtraces, for more information.
How do interrupts get re-enabled in the new thread following schedule()
? Every path into schedule()
disables interrupts. They eventually get re-enabled by the next thread to be scheduled. Consider the possibilities: the new thread is running in switch_thread()
(but see below), which is called by schedule()
, which is called by one of a few possible functions: • thread_exit()
, but we’ll never switch back into such a thread, so it’s uninteresting. • thread_yield()
, which immediately restores the interrupt level upon return from schedule()
. • thread_block()
, which is called from multiple places: ◦ sema_down()
, which restores the interrupt level before returning. ◦ idle()
, which enables interrupts with an explicit assembly STI instruction. ◦ wait()
in devices/intq.c, whose callers are responsible for re-enabling interrupts. There is a special case when a newly created thread runs for the first time. Such a thread calls intr_enable()
as the first action in kernel_thread()
, which is at the bottom of the call stack for every kernel thread but the first.
2.3.1 Alarm Clock FAQ
Do I need to account for timer values overflowing? Don’t worry about the possibility of timer values overflowing. Timer values are expressed as signed 64-bit numbers, which at 100 ticks per second should be good for almost 2,924,712,087 years. By then, we expect Pintos to have been phased out of the CS 140 curriculum.