

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Overview: In this assignment, you are given part of a working thread system; your job is to complete it, and then to use it to solve a synchronization problem. The first step is to read and understand the partial thread system that has been written for you. This thread system implements thread fork and thread completion, along with semaphores for synchronization. Compile and run the program nachos to see a simple test of the code. Trace the execution path (by hand) for the simple 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 SWITCH, another thread starts running, and the first thing the newly running thread does is to return from SWITCH. This comment may seem cryptic to you at this point, but you will understand threads once you understand why the SWITCH that gets called is different from the SWITCH that returns. (Note: because the debuggers gdb and ddd do not understand threads, you will get bizarre results if you try to trace in gdb, or ddd, across a call to SWITCH.)
The files for this assignment, located in the threads/ and machine/ subdirectories, are:
Properly synchronized code should work no matter what order the scheduler chooses to run the threads on the ready list. In other words, I should be able to put a call to Thread::Yield (causing the scheduler to choose another thread to run) anywhere in your code where interrupts are enabled
without changing the correctness of your code. 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 Thread::Yield to be called on your behalf in a repeatable but unpredictable way. Nachos code is repeatable in that if you invoke ./nachos with the same arguments each time, it will do exactly the same thing. However, if you invoke ./nachos - rs #, with a different number each time, calls to Thread::Yield will be inserted at different (random) places in your code.
Make sure you run various test cases against your solutions to this problem; for instance, for part two, create multiple producers and consumers and demonstrate that the output can vary, within certain boundaries.
Warning: In this implementation of threads, each thread is assigned a small fixed‐size execution stack. This may cause bizarre problems (such as segmentation faults at strange lines of code) if you declare large data structures to be automatic variables (e.g., int buf[1000]). You will probably not notice this during the semester, but if you do, you may change the size of the stack by modifying StackSize defined in switch.h.
You should write object‐oriented code with C++ classes. Also, there should be no busy‐waiting in any of your solutions to this assignment.
The Assignment:
Can changing the relative priorities of the producer and consumer threads have any affect on the output? For instance, what happens with two producers and one consumer, when one of the producers
is higher priority than the other? What if the two producers are at low priority, but the consumer is at high priority? Etc.
Your final submitted program must compile to a binary which is invoked as