Docsity

Concurrent System Design: File Cache with LRU Eviction & Deadlock Prevention, Exams of Operating Systems

Toggle sidebar
bg1
Computer Science 15-410: Operating Systems
Mid-Term Exam (A), Fall 2011
1. Please read the entire exam before starting to write. This should help you
avoid getting bogged down on one problem.
2. Be sure to put your name and Andrew ID below and also put your Andrew ID at the top of
each following page.
3. This is a closed-book in-class exam. You may not use any reference materials during the
exam.
4. If you have a clarification question, please write it down on the card we have provided. Please
don’t ask us questions of the form “If I answered like this, would it be ok?” or “Are you
looking for ...?”
5. The weight of each question is indicated on the exam. Weights of question parts are estimates
which may be revised during the grading process and are for your guidance only.
6. Please be concise in your answers. You will receive partial credit for partially correct answers,
but truly extraneous remarks may count against your grade.
7. Write legibly even if you must slow down to do so! If you spend some time to
think clearly about a problem, you will probably have time to write your answer legibly.
Andrew
Username
Full
Name
Question Max Points Grader
1. 10
2. 20
3. 15
4. 20
5. 10
75
Please note that there are system-call and thread-library “cheat sheets”
at the end of the exam.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Concurrent System Design: File Cache with LRU Eviction & Deadlock Prevention and more Exams Operating Systems in PDF only on Docsity!

Computer Science 15-410: Operating Systems

Mid-Term Exam (A), Fall 2011

1. Please read the entire exam before starting to write. This should help you

avoid getting bogged down on one problem.

  1. Be sure to put your name and Andrew ID below and also put your Andrew ID at the top of each following page.
  2. This is a closed-book in-class exam. You may not use any reference materials during the exam.
  3. If you have a clarification question, please write it down on the card we have provided. Please don’t ask us questions of the form “If I answered like this, would it be ok?” or “Are you looking for ...?”
  4. The weight of each question is indicated on the exam. Weights of question parts are estimates which may be revised during the grading process and are for your guidance only.
  5. Please be concise in your answers. You will receive partial credit for partially correct answers, but truly extraneous remarks may count against your grade.

7. Write legibly even if you must slow down to do so! If you spend some time to

think clearly about a problem, you will probably have time to write your answer legibly.

Andrew

Username

Full

Name

Question Max Points Grader

Please note that there are system-call and thread-library “cheat sheets”

at the end of the exam.

  1. 10 points Short answer.

(a) 5 points Define “rodata.” Give examples of what it is used for. Briefly explain a bad thing that could happen if we didn’t have “rodata.”

(b) 5 points Define: “internal fragmentation.” We are expecting an answer of three to five sentences.

#define TOTAL_WIRED 2 #define TOTAL_WIRELESS 2

int wired_avail = TOTAL_WIRED; int wireless_avail = TOTAL_WIRELESS; mutex_t controller_mutex; cond_t controller_cond;

#define ACQUIRE(avail, want) do {
while ((avail) && (want)) {
(avail)--;
(want)--;
}
} while(0)

#define RELEASE(avail, release, cond) do {
while (release) {
(avail)++;
(release)--;
cond_signal(cond);
}
} while(0)

void acquire_controllers(int wired_want, int wireless_want) { mutex_lock(&controller_mutex); while (wired_want || wireless_want) { ACQUIRE(wired_avail, wired_want); ACQUIRE(wireless_avail, wireless_want); if (wired_want || wireless_want) cond_wait(&controller_cond, &controller_mutex); } mutex_unlock(&controller_mutex); }

void release_controllers(int wired_release, int wireless_release) { mutex_lock(&controller_mutex); RELEASE(wired_avail, wired_release, &controller_cond); RELEASE(wireless_avail, wireless_release, &controller_cond); mutex_unlock(&controller_mutex); }

// No need to lock around play_game(): once we allocate our // controllers, a console must be available void acquire2_and_play(void *unused) { acquire_controllers(1, 1); play_game(); release_controllers(1, 1); }

Suggestions for working on this problem:

  1. Read both parts of the problem before working on either part. Also, if you get “stuck” on one part, maybe working on the other part for a while will help.
  2. When tracing the execution of the code, we recommend a tabular format very similar to this: Old Avail TID Request Holds New Avail Status Comment (2,2) 0 (1,1) (1,1) (1,1) gaming (1,1) 1 (1,1) (1,1) (0,0) gaming (0,0) 2 (1,1) (0,0) (0,0) blocked on (1,1) (0,0) 0 (-1,-1) (0,0) (1,1) done (1,1) 2 (1,1) (1,1) (0,0) gaming This format should help you think about the execution as a series of requests while keep- ing track of what is acquired by each thread over time. Releasing a resource can be conveniently represented as acquiring a negative resource. The tuples represent some number of wired and wireless controllers, in that order (the same order as parameters to acquire controllers()). The first line in the example represents that 2 wired controllers and 2 wireless controllers are available initially, then Thread 0 requests 1 wired controller and 1 wireless controller. The thread acquires everything requested, leaving 1 wired and 1 wireless controller available, and begins gaming. Because you should list every request that occurs, the “new avail” column of one row should always be equal to the “old avail” column of the next row. The comments column can contain whatever explanations you think would make your trace more clear. Be sure that any execution trace or argument you provide us with is easy to read and conclusively demonstrates the claim you are making.
  3. It is strongly recommended that you write down a draft version of any execution trace using the scrap paper provided at the end of the exam, or on the back of some other page, before you start to write your solution on the next page. If we cannot understand the solution you provide, your grade will suffer!
  4. Finally, a hint: you should consider the can-deadlock and cannot-deadlock possibilities as being equally likely.

The remainder of this page is intentionally blank.

For his next party, Harry buys some 3-player video games, although he does not buy any new equipment. The existing code does not change, but now the system will run an arbitrary mixture of threads running the old acquire2 and play() function and threads running a new acquire3 and play() function (for reading convenience, both are shown below; you should assume that the only code that allocates or frees the game controllers is in those two functions). Again, notice that the players try to be polite, always allocating as few wireless controllers as possible.

// No need to lock around play_game(): once we allocate our // controllers, a console must be available void acquire2_and_play(void *unused) { acquire_controllers(1, 1); play_game(); release_controllers(1, 1); } // As in acquire2_and_play, no need to synchronize around play_game(); void acquire3_and_play(void *unused) { acquire_controllers(1, 2); play_game(); release_controllers(1, 2); } (b) 10 points Can the code for this new party deadlock? If so, provide an execution sequence using the same format as before. If deadlock is not possible, provide a clear and concise argument that it cannot happen; your reasoning should be convincing enough that it could be included with the code as documentation, and should probably mention one or more of the four “deadlock ingredients” by name.

You may use this page as extra space for the gaming-deadlock question if you wish.

/* Our filesystem has a max of 1000 nodes */ #define INODE_LIMIT 1000 #define GC_LIMIT 10 #define LRU_LIMIT (INODE_LIMIT - GC_LIMIT)

typedef struct q_node { int id; struct q_node *prev; struct q_node *next; } q_node;

typedef struct { int refcount; // -1 for "not present" q_node node; mutex_t lock; } inode_t;

inode_t inodes[INODE_LIMIT]; int lru_entries; cond_t gc_cvar; mutex_t lru_lock; lru_t lru;

/* 1 = inode already here, 0 = added */ int register_inode(int inode_id) { inode_t inode = &inodes[inode_id]; mutex_lock(&inode->lock); / Is this inode already registered? */ if (inode->refcount != -1) { mutex_unlock(&inode->lock); return 1; } inode->refcount = 0; mutex_unlock(&inode->lock);

/* Insert inode into the LRU */ mutex_lock(&lru_lock); lru_entries++; lru_add(&lru, &inode->node); if (lru_entries > LRU_LIMIT) { cond_signal(&gc_cvar); } mutex_unlock(&lru_lock); return 0; }

int use_inode(int inode_id) { return alter_inode(inode_id, 1); }

int release_inode(int inode_id) { return alter_inode(inode_id, -1); }

/* Private, only called internally */ static int alter_inode(int inode_id, int delta) { inode_t *inode = &inodes[inode_id]; mutex_lock(&inode->lock); if (inode->refcount == -1) { mutex_unlock(&inode->lock); return -1; } inode->refcount += delta; mutex_unlock(&inode->lock);

/* Update the LRU */ mutex_lock(&lru_lock); lru_mark_used(&lru, &inode->node); if (lru_entries > LRU_LIMIT) { cond_signal(&gc_cvar); } mutex_unlock(&lru_lock); return 0; }

void garbage_collect() { q_node *node, *next; int id; mutex_lock(&lru_lock); cond_wait(&gc_cvar, &lru_lock); for (node = lru_front(&lru); lru_entries > LRU_LIMIT && node != NULL; node = next) { next = node->next; inode_t inode = &inodes[node->id]; mutex_lock(&inode->lock); if (inode->refcount != 0) { mutex_unlock(&inode->lock); continue; } id = node->id; inode->refcount = -1; lru_remove(&lru, node); lru_entries--; mutex_unlock(&inode->lock); mutex_unlock(&lru_lock); remove_inode(id); / This takes awhile */ mutex_lock(&lru_lock); } mutex_unlock(&lru_lock); }

void gc_thread() { while (1) { garbage_collect(); } }

(a) 4 points Consider the following function, designed to “check out” a file from the cache, first fetching it from the server if necessary. int open_inode_handle(int inode) { int found_it;

/* Is the inode in the local cache? */ if (use_inode(inode) >= 0) return 1;

/* Oops, must download from the server */ if (fetch_inode(inode) < 0) return -1;

/* If registration "fails", don’t worry: some other thread

  • must have randomly chosen the same file we did, and we
  • both downloaded it. That’s silly, but it is unlikely to
  • happen, so we don’t care: all we care about is that it
  • got cached by one of us. */ register_inode(inode);

/* Should not fail, it’s registered! */ found_it = use_inode(inode); assert(found_it >= 0); return found_it; } What situation (perhaps an unlikely one) could cause the assert() to fail? Describe a small code change that would solve this problem.

(c) 3 points Explain how to improve the implementation of use inode(), release inode(), alter inode(), and/or garbage collect() to avoid the problems you identified. It should be possible for you to de- scribe each of your proposed changes convincingly in one to three sentences.

You may use this page as extra space for your race-condition solution if you wish.

Here is the interface.

  • void brlock init(struct brlock *b, int num seats) - Initializes a big-reader lock to provide the given number of seats. The num seats argument to brlock init() specifies the maximum number of threads that will be using the brlock. Each relevant thread will keep track of a “seat number” (from 0 to num seats-1), which indicates (in an abstract sense) which subset of the big-reader lock’s internal memory the reader thread is free to access and modify without causing inter-processor memory conflicts.
  • void brlock lock(struct brlock *b, int my seat, int mode) - Acquires the big-reader lock in the given mode (BRLOCK READ or BRLOCK WRITE) from the given seat.
  • void brlock unlock(struct brlock *b, int my seat) - Releases the big-reader lock from the given seat (according to the mode in which that thread acquired it).
  • void brlock destroy(struct brlock *b) - Destroys the brlock. (You don’t need to implement this function on the exam.)

Your mission is to avoid (or at least lessen) cache conflicts among readers, as described in the “mission statement” above. For the purposes of this exam question you may assume that cache lines are 4 bytes (one word),^1 so concurrent writes to adjacent words will not cause bouncing.

While implementing brlocks, you may use mutexes, condition variables, and semaphores, but you may not use rwlocks, or any atomic operations, such as xchg, xadd, or cmpxchg. Condition variables and semaphores guarantee FIFO ordering, but mutexes may not. For exam purposes you may assume that all library functions and other associated functions will not fail.

The remainder of this page is intentionally blank.

(^1) This is false. A more realistic size would be 64 bytes, but it would be painful for you to worry about cache-aligning your data structures on the exam.

A pseudo-code usage example follows.

struct brlock b; #define NUM_READERS 8192

void *reader_thread(void *arg) { int my_seat = (int)arg;

while (1) { void *data;

brlock_lock(&b, my_seat, BRLOCK_READ); data = read_global_table(); // fetch or compute something from the table brlock_unlock(&b, my_seat); process_data(data); // think for a while about what we fetched/computed } }

int main(int argc, char *argv[]) { int i, my_seat;

brlock_init(&b, NUM_READERS+1);

for (i = 0; i < NUM_READERS; i++) { thr_create(reader_thread, (void *)i); }

my_seat = i;

while (1) { char input[BUF_SIZE];

readline(sizeof (input), input); // Blocks for user input--a long time!

// This part is very expensive! // Luckily, it happens rarely and only one thread does it. brlock_lock(&b, my_seat, BRLOCK_WRITE); rewrite_global_table(input); brlock_unlock(&b, my_seat); } }

(b) 15 points Now please write your code for brlock lock() and brlock unlock().

You may use this page as extra space for your brlock solution if you wish.