Part I
- (10%) Consider a computer that does not have a TEST AND SET LOCK instruction but does have an instruction to swap the contents of a register and a memory word in a single indivisible action. Can that be used to write a routine enter region such as the one found in Fig. 2–12.
- (20%) Measurements of a certain system have shown that the average process runs for a time T before blocking on I/O. A process switch requires a time S, which is effectively wasted (overhead). For round- robin scheduling with quantum Q, give a formula for the CPU efficiency (i.e., the useful CPU time divided by the total CPU time) for each of the following:
(a) Q = ∞
(b) Q > T
(c) S<Q<T (d) Q = S
(e) Q nearly 0 - (10%) Consider the interprocess-communication scheme where mailboxes are used. Suppose a process P wants to wait for two messages, one from mailbox A and one from mailbox B. What sequence of send and receive should it execute so that the messages can be received in any order?
- (10%) Consider the following program that uses the Pthreads API. What would be the output of the pro- gram? (Note that the line numbers are for references only.)
1
int value = 1; static void *runner(void *param);
int main(int argc, char **argv)
{
pid_t pid = fork();
if (pid > 0) {
printf("A = %d\n", value);
}
else if (pid == 0) {
pid_t pid = fork();
if (pid > 0) {
printf("B = %d\n", value);
}
else if (pid == 0) {
pid_t pid = fork();
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, runner, NULL);
pthread_join(tid, NULL);
if(pid>0)
printf(“C = %d\n”, value);
else
printf("D = %d\n", value);
}
else {
exit(1); }
}
else {
exit(1); }
return 0; }
static void *runner(void *param)
{
value += 1;
pthread_exit(0); }
Listing 1: pthread.c
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <sys/types.h>
Part II (50%)
Write a program to simulate the dining philosopher problem mentioned in the textbook using the Pthreads API on Linux. Make sure that your implementation is able to handle 5 philosophers and is free of race condition.
2
Gentle Reminder
Once again, as mentioned in the instructions, neither late nor copied homework will be accepted.
3





