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.

9 . You have been hired to simulate one of the Cal fraternities. Your job isto write a computer program to pair up men and women as they enteraFriday night mixer. Each man and each woman will be represented byone <strong>thread</strong>. When the man or woman enters the mixer, its <strong>thread</strong> willcall one of two procedures, man or woman, depending on the sex of the<strong>thread</strong>. You must write C code to implement these procedures. Eachprocedure takes a single parameter, name, whichisjustaninteger namefor the <strong>thread</strong>. The procedure must wait until there is an available <strong>thread</strong>of the opposite sex, and must then exchange names with this <strong>thread</strong>. Eachprocedure must return the integer name of the <strong>thread</strong> it paired up with.Men and women may enter the fraternity inany order, and many <strong>thread</strong>smay call the man and woman procedures simultaneously. It doesn't matterwhich man is paired up with which woman (Cal frats aren't very choosy),as long as each pair contains one man and one woman and each gets theother's name. Use semaphores and shared variables to implementthetwoprocedures. Be sure to give initial values for the semaphores and indicatewhich variables are shared between the <strong>thread</strong>s. There must not be anybusy-waiting in your solution.10. Implement the synchronization for a \lockup-free" cache, using conditionvariables. A lockup-free cache is one that can continue to accept requestseven while it is waiting for a response from memory (or equivalently, thedisk). This is useful, for instance, if the processor can pre-fetch data intothe cache before it is needed this hides memory latency only if it does notinterfere with normal cache accesses.The behavior of a lockup-free cache can be modelled with <strong>thread</strong>s, whereeach <strong>thread</strong> can ask the cache to read or write the data at some physicalmemory location. For a read, if the data is cached, the data can beimmediately returned. If the data is not cached, the cache must (i) kicksomething out of the cache to clear space (potentially having to write itback tophysical memory if it is dirty), (ii) ask memory to fetch the item,and (iii) when the data returns, put it in the cache and return the datato the original caller. The cache stores data in one unit chunks, so a writerequest need not read the location in before over-writing. While memoryis being queried, the cache can accept requests from other <strong>thread</strong>s. Ofcourse, the cache is xed size, so it is possible (although unlikely) that allitems in the cache may have been kicked out by earlier requests.You are to implement the routines CacheRead(addr) and CacheWrite(addr,val) these routines call DiskRead(addr) and DiskWrite(addr, val) on acache miss {you can assume these disk operations are already implemented.11. You have been hired by the CS Division to write code to help synchronizea professor and his/her students during oce hours. The professor, of6

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

Saved successfully!

Ooh no, something went wrong!