Stock Nachos has an incomplete thread system. In this assignment, your job is to complete it, and then use it to solve several synchronization problems.
The first step is to read and understand the partial thread system we have written for you. This thread system implements thread fork, thread completion, and semaphores for synchronization. It also provides locks and condition variables built on top of semaphores.
After installing the Nachos distribution, run the program nachos (in the proj1 subdirectory) for a simple test of our code. Just as in Project 0, this causes the methods of nachos.threads.ThreadedKernel to be called in the order listed in threads/ThreadedKernel.java:
$ cd nachos/proj1 $ make $ ../bin/nachos nachos 5.0j initializing... config interrupt timer user-check grader *** thread 0 looped 0 times *** thread 1 looped 0 times *** thread 0 looped 1 times *** thread 1 looped 1 times *** thread 0 looped 2 times *** thread 1 looped 2 times *** thread 0 looped 3 times *** thread 1 looped 3 times *** thread 0 looped 4 times *** thread 1 looped 4 times Machine halting! Ticks: total 2130, kernel 2130, user 0 Disk I/O: reads 0, writes 0 Console I/O: reads 0, writes 0 Paging: page faults 0, TLB misses 0 Network I/O: received 0, sent 0
Trace the execution path (by hand) for the startup test case provided. When you trace the execution path, it is helpful to keep track of the state of each thread and which procedures are on each thread's execution stack. You will notice that when one thread calls TCB.contextSwitch(), that thread stops executing, and another thread starts running. The first thing the new thread does is to return from TCB.contextSwitch(). We realize this comment will seem cryptic to you at this point, but you will understand threads once you understand why the TCB.contextSwitch() that gets called is different from the TCB.contextSwitch() that returns.
Properly synchronized code should work no matter what order the scheduler chooses to run the threads on the ready list. In other words, we should be able to put a call to KThread.yield() (causing the scheduler to choose another thread to run) anywhere in your code where interrupts are enabled, and your code should still be correct. You will be asked to write properly synchronized code as part of the later assignments, so understanding how to do this is crucial to being able to do the project.
To aid you in this, code linked in with Nachos will cause KThread.yield() to be called on your behalf in a repeatable (but sometimes unpredictable) way. Nachos code is repeatable in that if you call it repeatedly with the same arguments, it will do exactly the same thing each time. However, if you invoke "nachos -s <some-long-value>", with a different number each time, calls to KThread.yield() will be inserted at different places in the code.
You are encouraged to add new classes to your solution as you see fit; the code we provide you is not a complete skeleton for the project. Be careful to add any additional classes to the packages (directories) permitted. See the list of "do not"s below.
There should be no busy-waiting in any of your solutions to this assignment.
Your project code will be automatically graded.
Of course, there is a downside. Everything that will be tested needs to have a standard interface that the autograder can use, leaving slightly less room for you to be creative. Your code must strictly follow these interfaces (the documented *Interface classes).
Since your submissions will be processed by a program, there are some very important things you must do, as well as things you must not do. These are the same requirements for all the projects and are listed here again for convenience.
For all of the projects in this class...
When you want to add non-java source files to your project, simply add entries to your Makefile. In this project,
Note: there are subtleties here! Make sure you exactly understand all the design choices in the provided implementation.
speak() atomically waits until listen() is called on the same Communicator object, and then transfers the word over to listen(). Once the transfer is made, both can return. Similarly, listen() waits until speak() is called, at which point the transfer is made, and both can return (listen() returns the word). Your solution should work even if there are multiple speakers and listeners for the same Communicator (note: this is equivalent to a zero-length bounded buffer; since the buffer has no room, the producer and consumer must interact directly, requiring that they wait for one another). In this case, a message is still passed between one speaker and one listener—the other visitors are queued. Note that there exists a very simple implementation using exactly one lock! If you have more, you should wonder..
You can test your implementation before you submit a final version by using the autograder. The autograder works similar to the submit script. There is a project specific autograder submission script. To submit, log into your course
account and run: prep cs120s
Then move into your nachos directory and runautograder-proj1
After completing the autograder submission, your submitted implementation will be
put in a queue to be autograded. Results will be emailed to you (The email system often
gets clogged. So you may need to wait a few hours to receive the email). The autograder
runs every hour. You can submit your code to the autograder as many times as
you like. The autograder will run up until the project submission deadline.
Please note: project solutions submitted to the autograder do not constitute a project submission. If you fail to submit your project as per the instructions below, you will not receive a grade for the project.
As with all projects you will submit it using the project specific submission
script. Only one submission per group is expected. To submit, log into your course
account and run: prep cs120s
Then move into your nachos directory and run: submit-proj1
You will have until the posted due date to submit this project. You can submit as many
times as you like. The last version submitted before the due date will be the one accepted.
Warning: For all projects, we encourage you to discuss, but don't cheat, the price is too high. Cheating includes:
We will manually check and also run several code plagiarism tools on submissions and multiple Internet distributions (if you can find it, so can we).