Docsity

Thread Synchronization and Mutexes: Mid-Term Exam Preparation, Exams of Operating Systems

Toggle sidebar
bg1
Computer Science 15-410: Operating Systems
Mid-Term Exam (B), Fall 2009
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. 15
3. 15
4. 15
5. 20
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 Thread Synchronization and Mutexes: Mid-Term Exam Preparation and more Exams Operating Systems in PDF only on Docsity!

Computer Science 15-410: Operating Systems

Mid-Term Exam (B), Fall 2009

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.

I have not received advance information on the content of this 15-410 mid-term exam by dis- cussing it with anybody who took part in the conflict exam session or via any other avenue.

Signature: Date

Please note that there are system-call and thread-

library “cheat sheets” at the end of the exam.

If we cannot read your writing, we will be un-

able to assign a high score to your work.

  1. 15 points main() wrapper.

The C language specification states that if the main() function executes a return statement, the result is as if the value given to return had been passed as a parameter to exit() instead.

As we discussed in class, in many C run-time environments, this is implemented via a helpful “wrapper function” which “surrounds” the execution of main(). We also mentioned that, because surprising things would happen if a programmer accidentally wrote a function with the same name as the name of the main() wrapper, the main() wrapper sometimes has a name which is not a legal C identifier, such as $main().

For that reason, and for other reasons which apply in certain environments, it may be necessary to write the main() wrapper in assembly language. Luckily, it’s usually not too hard–which is good, since in this problem you will be asked to do that for the 15-410 P2/P3 run-time environment. You may assume that main() is declared as int main(int argc, char *argv[])—in other words, you don’t need to worry about non-410 frills such as the environment vector or the “aux vector.” You don’t need to worry about multi-threaded programs—exit() was defined before threads anyway.

(a) 5 points Briefly outline the “todo list” for the main() wrapper.

(b) 10 points Now write the assembly code for the main() wrapper, which will be named $main (you may assume the assembler is willing to export a symbol named $main). Please be sure that your code is clear and comprehensible.

/* Second implementation - high quality! */

mutex_t bm; cond_t done; int next_ticket = 1; int ready_for = 1; extern void log_debug(char *s, ...); // thread-safe

void request(int direction) { // "direction" neither used nor needed!

int me, my_ticket; me = thr_getid();

mutex_lock(&bm); my_ticket = next_ticket++;

if (ready_for == my_ticket) { mutex_unlock(&bm); log_debug("%d happily crossing\n", me); return; } else { // I/O is not a "short instruction sequence"! mutex_unlock(&bm); log_debug("%d eagerly waiting\n", me); mutex_lock(&bm);

while (ready_for != my_ticket) cond_wait(&done, &bm); mutex_unlock(&bm);

log_debug("%d happily crossing\n", me); } }

void complete(void) { mutex_lock(&bm); ++ready_for; cond_signal(&done); mutex_unlock(&bm); }

(a) 5 points Briefly and clearly state the most problematic thread-synchronization problem

you believe the first implementation suffers from. Show a clear and compelling execution trace which supports your claim.

(c) 5 points Please clearly describe a simple code change which solves the problem with the second implementation which you identified in part (b). It should be possible for you to unambiguously describe the change in one sentence—or maybe two. You may show code if you wish, but you probably shouldn’t: we are looking for a fix to one of these algorithms, not a new algorithm. Once you have described the change, also briefly describe why it works.

  1. 15 points Distributed computing.

The 15-410 course staff, having been stressed out for some time by inking student source code, has decided to take a weekend retreat to relax. Of course, the diversity of the staff introduces complications: Milo, who works remotely from the Bahamas, must fly in; Joshua, an adept single-engine pilot, wishes to fly himself to the retreat; Jacob insists on hitchhiking; nobody is willing to ride in a car driven by Michael; etc. So the decision is made that everybody will travel independently. Because he has many independent-study classes this semester, Jacob volunteers to set up a multi-threaded TA-location server, based on a 410 Pebbles kernel extended with Michael’s “PebNet” networking stack. As they travel, members of the course staff will be able to monitor each other’s progress by sending queries to the location server. The communication protocol is fairly simple. The server keeps track of how far each TA has traveled and how many times each TA has been queried by others. Each TA is represented by a thread in the server, which will receive queries directed to its personal “service identi- fier.” Each client message consists of two integers, namely an operation code and an optional parameter. Each reply from the server consists of the operation code of the query, plus a single- integer response code. The “PebNet” messaging middleware layer transparently handles any lost, duplicated, or re-ordered network packets, so each client enjoys error-free communication between itself and the server. PebNet associates each query with a handle which identifies the remote entity which issueshd the query; this handle is used to direct responses to the right place. Finally, of course PebNet is completely thread-safe. The application protocol includes three messages.

Update - The update message takes one parameter, the distance traveled since the previous update. The server updates the tracked total distance and replies with that value. Query - The query message takes one parameter, the identity of a TA whose progress is being queried. The server looks up the TA, increments the count of queries of that TA’s location, retrieves the distance traveled so far, and replies with that value. If the TA does not exist, or has already arrived at the retreat and is no longer “in the system,” the reply value is -1. Fin - The fin message includes a “dummy” parameter, which the server ignores. This message indicates that the TA who sent it has completed the journey and that the server should free all resources associated with that TA. The server replies with the number of queries for that TA’s progress which were performed.

#include <thread.h> #include <mutex.h> #include <pebnet.h> // pebnet.h declares the next 5 lines, inlined here: typedef struct { ... } pn_conn_t; typedef { ...} pn_remote_t; pn_conn_t *pn_create(int service); // create message endpoint void pn_recv(pn_conn_t *cp, pn_remote_t *who, int *cmdp, int *valp); // receive msg void pn_send(pn_conn_t *cp, pn_remote_t *who, int cmd, int val); // send msg void pn_destroy(pn_conn_t *cp); // destroy message endpoint

ta_init(nta, DAVE); thr_create(ta_serve, nta); nta = (ta_t ) malloc(sizeof (nta)); ta_init(nta, GARTH); thr_create(ta_serve, nta); // ... same deal for other staff members ...

thr_exit(0); // does NOT kill off other threads! }

/**** TA lifecycle functions ****/ void ta_init(ta_t *ta, int id) { mutex_init(&ta->lock); ta->id = id; ta->distance = ta->num_queries = 0; ta->conn = pn_create(id); // use my "ta #" as my PebNet service locator ta_insert(ta); }

void ta_destroy(ta_t *ta) { mutex_destroy(&ta->lock); pn_destroy(ta->conn); }

/**** Client request processing functions ****/ int ta_update(ta_t *ta, int progress) { mutex_lock(&ta->lock); ta->distance += progress; int distance = ta->distance; mutex_unlock(&ta->lock);

return distance; }

int ta_query(int id) { ta_t *ta = ta_lookup(id); if (!ta) return -1;

mutex_lock(&ta->lock); ta->num_queries++; int distance = ta->distance; mutex_unlock(&ta->lock);

return distance;

int ta_querycnt(ta_t *ta) { mutex_lock(&ta->lock); int queries = ta->num_queries; mutex_unlock(&ta->lock);

return queries; }

/**** The main processing loop ****/ void *ta_serve(void *p) { ta_t *me = p; int exiting = 0;

while (!exiting) { pn_remote_t whoever; int cmd = 0, arg = 0, reply = 0;

pn_recv(me->conn, &whoever, &cmd, &arg); // block until request switch (cmd) { case MSG_UPDATE: reply = ta_update(me, arg); break; case MSG_QUERY: reply = ta_query(arg); break; case MSG_FIN: reply = ta_querycnt(me); exiting = 1; break; default: panic("protocol botch"); break; } pn_send(me->conn, &whoever, cmd, reply); // reply (reliably) }

ta_remove(me->id); ta_destroy(me); free(me);

return NULL; }

(b) 8 points Present a solution for this problem. Both good solutions and distasteful solu- tions are possible; a good description of a distasteful solution is better than nothing, as it will receive some credit, but full credit will be reserved for solutions which are robust and scalable (i.e., could handle hundreds of TA’s or longer-running TA-object manipulations). Your solution should include one to three sentences of description and a “code plan”: a very brief description of a code change, and possibly a list of functions or places in the code where the change needs to be applied. Your goal is to fix the application, not to re-write it, so please do not show large amounts of code!

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

...space for mutex implementation...

(b) 15 points Now please declare a struct cond and implement cond init(), cond wait(), and cond signal() (you do not need to implement either cond broadcast() or cond destroy()). If you wish, you may also declare an auxiliary structure, struct aux, but this is strictly optional. It is strongly recommended that you rough out an implementation on the scrap paper provided at the end of the exam, or on the back of some other page, before you write anything here. If we cannot understand the solution you provide on this page, your grade will suffer! typedef struct cond {

} cond_t;

typedef struct aux {

} aux_t;