


















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!



















think clearly about a problem, you will probably have time to write your answer legibly.
(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:
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
/* 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.
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.