Any and all printed and written materials are allowed, including books, course notes, and handwritten notes.
No electronic devices of any kind are allowed, including cellphones, calculators, pagers, laptops, etc. Please turn these off before beginning the exam.
Please show all work on these pages. Feel free to use the back of each page as needed. Make sure that your work for each problem is clearly marked, especially on the backs of pages. These pages will be graded and returned electronically, like exercises, and the results will be made available online.
Please note that point values have no correspondence with difficulty. We suggest that you solve the problems that you best understand first.
Please also note that the space I gave you to answer is the space I might need, and not necessarily the space you will need. There is no deduction for more verbose answers (though succinct and comprehensive answers are very much appreciated by your instructor!).
Problem | Score | of | Maximum |
---|---|---|---|
1 | of | 10 | |
2 | of | 10 | |
3 | of | 10 | |
4 | of | 10 | |
5a | of | 10 | |
5b | of | 10 | |
6a | of | 10 | |
6b | of | 10 | |
6c | of | 10 | |
7 | of | 10 | |
8 | of | 10 | |
Total | of | 100 |
execl
? Why or why not?
#define SIZE 20 int begin=0, end=0; char queue[SIZE]; int empty() { return begin==end; } int full() { return ((end+1)%SIZE)==begin; } void enqueue(char c) { pthread_mutex_lock(&can_enqueue); //1 pthread_mutex_lock(&can_modify); int empty_before=empty(); queue[end]=c; end=(end+1)%SIZE; int full_after=full(); pthread_mutex_unlock(&can_modify); if (empty_before) { pthread_mutex_unlock(&can_dequeue); } if (!full_after) { pthread_mutex_unlock(&can_enqueue); } } char dequeue() { char out; pthread_mutex_lock(&can_dequeue); pthread_mutex_lock(&can_modify); int full_before = full(); out = queue[begin]; begin=(begin+1)%SIZE; int empty_after = empty(); pthread_mutex_unlock(&can_modify); if (full_before) { pthread_mutex_unlock(&can_enqueue); } if (!empty_after) { pthread_mutex_unlock(&can_dequeue); } return out; }
can_modify
inside the
subroutines empty()
and full()
?
Requested (and approved) | Granted (and held) | Deficit | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
R1 | R2 | R3 | R4 | R1 | R2 | R3 | R4 | R1 | R2 | R3 | R4 | |||||
P1 | 1 | 0 | 0 | 0 | - | P1 | 1 | 0 | 0 | 0 | = | P1 | 0 | 0 | 0 | 0 |
P2 | 3 | 0 | 0 | 0 | P2 | 1 | 0 | 0 | 0 | P2 | 2 | 0 | 0 | 0 | ||
P3 | 0 | 2 | 0 | 0 | P3 | 0 | 2 | 0 | 0 | P3 | 0 | 0 | 0 | 0 | ||
P4 | 3 | 2 | 2 | 0 | P4 | 1 | 2 | 2 | 0 | P4 | 2 | 0 | 0 | 0 | ||
Totals | ||||||||||||||||
R1 | R2 | R3 | R4 | |||||||||||||
4 | 4 | 4 | 4 |
sem_wait(void)
that has the exact behavior of sem_wait
, but uses mutexes to
turn a regular integer global variable int sem;into a semaphore.