10.07.2015 Views

Nachos Assignment #1: Build a thread system Tom Anderson ...

Nachos Assignment #1: Build a thread system Tom Anderson ...

Nachos Assignment #1: Build a thread system Tom Anderson ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!