interrupt.h, interrupt.cc | manage enabling and disabling interrupts as partof the machine emulation.timer.h, timer.cc | emulate a clock that periodically causes an interrupt tooccur.stats.h { collect interesting statistics.Properly synchronized code should work no matter what order the schedulerchooses to run the <strong>thread</strong>s on the ready list. In other words, we should be ableto put a call to Thread::Yield (causing the scheduler to choose another <strong>thread</strong>to run) anywhere in your code where interrupts are enabled without changingthe correctness of your code. You will be asked to write properly synchronizedcode as part of the later assignments, so understanding how todothisiscrucialto being able to do the project.To aid you in this, code linked in with <strong>Nachos</strong> will cause Thread::Yield tobe called on your behalf in a repeatable but unpredictable way. <strong>Nachos</strong> code isrepeatable in that if you call it repeatedly with the same arguments, it will doexactly the same thing each time. However, if you invoke \nachos -rs #", witha dierent number each time, calls to Thread::Yield will be inserted at dierentplaces in the code.Make sure to run various test cases against your solutions to these problemsfor instance, for part two, create multiple producers and consumers anddemonstrate that the output can vary, within certain boundaries.Warning: in our implementation of <strong>thread</strong>s, each <strong>thread</strong> is assigned a small,xed-size execution stack. This may cause bizarre problems (such as segmentationfaults at strange lines of code) if you declare large data structures to beautomatic variables (e.g., \int buf[1000]"). You will probably not notice thisduring the semester, but if you do, you may change the size of the stack bymodifying the StackSize dene in switch.h.Although the solutions can be written as normal C routines, you will ndorganizing yourcodetobeeasierifyou structure your code as C++ classes.Also, there should be no busy-waiting in anyofyour solutions to this assignment.The assignment is items 1, 2, 5, 6 and 7 listed below.1. Implement locks and condition variables. You may either use semaphoresas a building block, or you may use more primitive <strong>thread</strong> routines (suchas Thread::Sleep). We have provided the public interface to locks andcondition variables in synch.h. You need to dene the private data andimplement the interface. Note that it should not take you very much codeto implement either locks or condition variables.2. Implement producer/consumer communication through a bounded buer,using locks and condition variables. (A solution to the bounded buerproblem is described in Silberschatz, Peterson and Galvin, using semaphores.)2
The producer places characters from the string "Hello world" into thebuer one character at a time it must wait if the buer is full. Theconsumer pulls characters out of the buer one at a time and prints themto the screen it must wait if the buer is empty. Test your solution withamulti-character buer and with multiple producers and consumers. Ofcourse, with multiple producers or consumers, the output display willbegobbledygook the point is to illustrate3. The local laundromat has just entered the computer age. As each customerenters, he or she puts coins into slots at one of two stations andtypes in the number of washing machines he/she will need. The stationsare connected to a central computer that automatically assigns availablemachines and outputs tokens that identify the machines to be used. Thecustomer puts laundry into the machines and inserts each token into themachine indicated on the token. When a machine nishes its cycle, itinforms the computer that it is available again. The computer maintainsan array available[NMACHINES] whose elements are non-zero if the correspondingmachines are available (NMACHINES is a constant indicatinghow manymachines there are in the laundromat), and a semaphore nfreethat indicates how manymachines are available.The code to allocate and release machines is as follows:int allocate() /* Returns index of available machine.*/{int i}P(nfree) /* Wait until a machine is available */for (i=0 i < NMACHINES i++)if (available[i] != 0) {available[i] = 0return i}release(int machine) /* Releases machine */{available[machine] = 1V(nfree)}The available array is initialized to all ones, and nfree is initialized toNMACHINES.(a) It seems that if two peoplemake requests at the two stations atthe same time, they will occasionally be assigned the same machine.3