15. Consider a process that has switched to kernel mode and has acquired a spinlock to modify a
kernel data structure. In modern operating systems, will this process initiate a disk read before it
releases the spinlock? (Yes/No)
Ans: No
16. When a user space process executes the wakeup/signal system call on a pthread condition variable,
does it always lead to an immediate context switch of the process that calls signal (immediately
after the signal instruction)? (Yes/No)
Ans: No
17. Consider a process in kernel mode that acquires a spinlock. For correct operation, it must dis-
able interrupts on its CPU core for the duration that the spinlock is held, in both single core and
multicore systems. [T/F]
Ans. T
18. Consider a process in kernel mode that acquires a spinlock in a multicore system. For correct
operation, we must ensure that no other kernel-mode process running in parallel on another core
will request the same spinlock. [T/F]
Ans. F
19. Multiple threads of a program must use locks when accessing shared variables even when execut-
ing on a single core system. [T/F]
Ans: T
20. Recall that the atomic instruction compare-and-swap (CAS) works as follows:
CAS(&var, oldval, newval) writes newval into var and returns true if the old value of
var is oldval. If the old value of var is not oldval, CAS returns false and does not change
the value of the variable. Write code for the function to acquire a simple spinlock using the CAS
instruction.
Ans: while(!CAS(&lock, 0, 1));
21. The simple spinlock implementation studied in class does not guarantee any kind of fairness or
FIFO order amongst the threads contending for the spin lock. A ticket lock is a spinlock imple-
mentation that guarantees a FIFO order of lock acquisition amongst the threads contending for the
lock. Shown below is the code for the function to acquire a ticket lock. In this function, the vari-
ables next ticket and now serving are both global variables, shared across all threads, and
initialized to 0. The variable my ticket is a variable that is local to a particular thread, and is
not shared across threads. The atomic instruction fetch and increment(&var) atomically
adds 1 to the value of the variable and returns the old value of the variable.
acquire():
my_ticket = fetch_and_increment(&next_ticket)
while(now_serving != my_ticket); //busy wait
You are now required to write the code to release the spinlock, to be executed by the thread holding
the lock. Your implementation of the release function must guarantee that the next contending
3