CS560 Midterm -- March 13, 2003
Answer all questions.
Question 1
Multiple choice: Choose the best answer to each.
A: The long-term scheduler:
- a. Is responsible for moving jobs between queues in a
multi-level feedback queue scheduling algorithm.
- b. Makes decisions about the distant future of a system.
- c. Sets a process's initial priority.
- d. Decides whether a process enters the system, or
whether it must wait for other processes to exit before
entering the system.
B: Which of the following is not a necessary condition
for deadlock:
- a. Circular wait.
- b. Hold-and-wait.
- c. Multiple processes.
- d. No preemption.
C: Which of the following statements best compares
timestamp-based transactions and two-phase commit transactions:
- a. Timestamps are faster when there are very
few read/write conflicts among transactions.
- b. Two-phased commit is faster when transactions
are longer.
- c. Two-phased commit is faster when transactions
consist of many write operations.
- d. Timestamps are faster when there are many
concurrent transactions.
D: In the dining philosophers, what is the problem
with a solution where odd philosophers pick up their right
chopstick first, and even philosopher pick up their left
chopstick first. Choose all that apply:
- a. There is a possibility for deadlock.
- b. There is a possibility for starvation.
- c. There is a possibility for livelock.
- d. It is inefficient -- there are significant
periods of time when a philosopher should
be able to eat, but cannot because a non-eating
philosopher is holding his chopstick.
- e. One philosopher gets to eat disproportionately
more than the others.
E: Which of the following is the definition of a
safe state:
- a. A safe state is one where there is an ordering
of the processes P1 to PN such that if Pi is less than Pj, then
Pi does not need any of Pj's resources to complete.
- b. A safe state is one where there is an ordering
of the processes P1 to PN such that if Pj is less than Pi, then
Pi does not need any of Pj's resources to complete.
- c. A safe state is one where there is an ordering
of the processes P1 to PN such that when all Pi, i < j, have
completed, Pj can get all of its resources without waiting.
- d. A safe state is one where there is an ordering
of the processes P1 to PN such that when all Pi, i > j, have
completed, Pj can get all of its resources without waiting.
F: Why do we not want a time quantum to be too small?
- a. Because CPU-bound processes will not get their
fair share of the CPU, and overall CPU utilization
will be decreased.
- b. Because I/O-bound processes will not get their
fair share of the CPU, and overall CPU utilization
will be decreased.
- c. Because CPU-bound processes will not get their
fair share of the CPU, and overall average process turnaround
time will be increased.
- d. Because I/O-bound processes will not get their
fair share of the CPU, and overall average process turnaround
time will be increased.
- e. Because context-switch overhead will
increase the average process turnaround time.
- f. Because the operating system will own the
CPU for too long, and overall average process turnaround
time will be increased.
G: Why do we not want a time quantum to be too big?
- a. Because CPU-bound processes will not get their
fair share of the CPU, and overall CPU utilization
will be decreased.
- b. Because I/O-bound processes will not get their
fair share of the CPU, and overall CPU utilization
will be decreased.
- c. Because CPU-bound processes will not get their
fair share of the CPU, and overall average process turnaround
time will be increased.
- d. Because I/O-bound processes will not get their
fair share of the CPU, and overall average process turnaround
time will be increased.
- e. Because context-switch overhead will
increase the average process turnaround time.
- f. Because the operating system will own the
CPU for too long, and overall average process turnaround
time will be increased.
H: Which of the following statements is false?
- a. Kernel-only memory locations protect
devices on the memory bus from user processes.
- b. Kernel-only memory locations protect
the interrupt vector from user processes.
- c. DMA protects the CPU from mass disk
transfers.
- d. A hardware timer protects the system from
programs that do not voluntarily give up the CPU.
I: In the kthreads implementation below, why does
kt_exit() call longjmp()?
void kt_exit()
{
JRB tmp;
InitKThreadSystem();
if (ktRunning == ktOriginal) {
ktRunning->state = DEAD;
tmp = jrb_find_int(ktActive, ktRunning->tid);
jrb_delete_node(tmp);
tmp = jrb_find_int(ktBlocked, ktRunning->tid);
if (tmp != NULL) WakeKThread((K_t)tmp->val.v);
KtSched();
} else {
longjmp(ktRunning->exitbuf,1);
}
}
|
- a. This cleans out the stack so that
it can be freed.
- b. This is how a context switch
is performed, allowing the CPU to
run another thread.
- c. This pops all stack frames down to
the original calling of the thread's procedure, and
acts like the thread's procedure has returned.
- d. This causes the thread's exit code to
execute on another stack so that the stack may be
freed.
Question 2
Suppose at time x, the user presses return on his keyboard.
At time x + 1 (seconds), a user process executes a
read(0, &c, 1) call, which returns
at time x + 2. Below are 12 events that can occur
in the operating system. Choose the events that must
occur in this scenario, and put them
in the correct order that they will occur. Note, not all events
will occur at all, and some events may occur more than once.
You can only choose events from this list, and you may
not make up your own.
- a. The operating system fields a hardware interrupt
and suspends the currently running process.
- b. If the keyboard buffer is not full,
the operating system reads a character from
the keyboard device driver, typically using a privileged memory
location.
- c. The operating system reads a character from
the keyboard device driver, typically using a privileged memory
location.
- d. The operating system checks its keyboard buffer
to see if it is empty.
- e. The operating system takes a character out of
the keyboard buffer, and places it into a register.
- f. The operating system checks its keyboard buffer
to see if it is full.
- g. The operating system calls the scheduler, which
selects a user process to execute.
- h. The operating system fields a software exception
suspending the currently running process.
- i. The operating system resets the keyboard DMA device
driver so that the keyboard can do another transaction.
- j. If the keyboard buffer is not full, the operating system
puts the character into the buffer.
- k. The operating system schedules a DMA transaction
with the keyboard device driver, writing to the keyboard buffer.
- l. The operating system takes a character out of
the keyboard buffer, and places it into user memory.
Question 3
Here are the first few lines of kt_sched():
void KtSched()
{
declarations...
if(setjmp(ktRunning->jmpbuf) != 0)
{
FreeFinishedThreads();
return;
}
...
Explain exactly what is going on in these few lines
of code.
Question 4
Suppose we execute the following code:
#define NTHREADS (10)
pthread_mutex_t *locks[NTHREADS];
void *thread(void *arg)
{
int id, *ip;
ip = (int *) arg;
id = *ip;
while(1) {
pthread_mutex_lock(locks[(id+NTHREADS-1)%NTHREADS]);
pthread_mutex_lock(locks[id]);
pthread_mutex_lock(locks[(id+1)%NTHREADS]);
sleep(random%10+1);
pthread_mutex_unlock(locks[(id+NTHREADS-1)%NTHREADS]);
pthread_mutex_unlock(locks[id]);
pthread_mutex_unlock(locks[(id+1)%NTHREADS]);
}
}
main()
{
int i;
pthread_t t[NTHREADS];
pthread_attr_t attrs[NTHREADS];
int ids[NTHREADS];
for (i = 0; i < NTHREADS; i++) {
locks[i] = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(locks[i]);
}
for (i = 0; i < NTHREADS; i++) {
ids[i] = i;
pthread_attr_init(attr+i);
pthread_attr_setscope(attr+i, PTHREAD_SCOPE_SYSTEM);
pthread_create(t+i, attr+i, thread, id+i);
}
pthread_exit(NULL);
}
Part 1: Prove that this code can deadlock. By prove, I mean
show that the book's definition of deadlock holds.
Part 2: Modify the contents of the while loop so that
no deadlock can occur because one of the necessary conditions of
deadlock cannot be met.
You may add variables if you like -- if so, state how they are initialized.
Now prove that your code cannot deadlock.
Part 3: Modify the contents of the while loop so that
no deadlock can occur because a different one
of the necessary conditions of
deadlock cannot be met.
You may add variables if you like -- if so, state how they are initialized.
Now prove that your code cannot deadlock.
Extra Credit (one point -- don't waste your time on this unless you are done early)
You are declaring a bridge contract with spades as trumps. Your left-hand
opponent leads a heart. Assuming trumps do not split 4-0 and your
opponents do not misdefend, how do you play the hand to maximize your
chances of making 11 tricks? If you duck the first round of hearts,
they will lead another round.
Dummy
S T8
H A62
D JT52
C KT72
You
S AKQ9742
H 73
D K763
C -