OS Midterm Solution
OS Midterm Solution
Faculty of Engineering
Computer and Systems Engineering
Fall 2014
Midterm Exam
CS333: Operating Systems
Saturday, November 22, 2014
Allowed Time: 90 Minutes
1 of 4
Solution: Multi-threaded programs are executed at very high speed (when compared to
their single threaded counter parts) on a uniprocessor machine, wherein there exist 1-1 mapping between user-level threads and Kernel-level threads. This is because Kernel-level-thread
in multi-threaded program enables at least one thread to issue a block system call independently without influencing other threads and thereby allowing other threads to uninterruptly
continue with their execution. However, in single threaded counter parts of multi-threaded
program, machine generally spends a significant amount of time waiting for I/O operation
to be complete. Therefore on uniprocessor machine, a process that was allowed to be blocked
continues executing its other threads.
9. (5 points) List at least two types of hardware operations that are used to implement higher level
synchronization primitives such as locks, semaphores, and monitors.
Solution:
Load and store of a single word.
Disabling and enabling interrupts.
Atomic instructions such as compare and swap.
10. (5 points) Explain the difference between user mode and kernel mode. Why two execution
modes are important for achieving the goals of an operating system?
Solution: In user mode certain areas of memory are protected from the users use, certain
registers are inaccessible, and certain instructions may not be executed.
The distinction between two modes are important for the OS to maintain control over the
hardware and to protect itself from any possible changes made by the programs.
2 of 4
P(1)
turn = 0
turn = 1
locked[0] = true
(locked[1-id] & turn == (1 - id)) false
, exit busy wait
/*critical section*/
locked[1] = true
(locked[1-id] & turn == (1 - id)) false
exit busy wait
/*critical section*/
3 of 4
locked[0]
false
false
true
locked[1]
true
false
false
turn
0
1
1
true
true
true
false
false
true
1
1
1
true
true
true
true
1
1
12. (12 points) In lab #1, you learned about both the fork and waitpid system calls. Consider the
following program that uses them:
int i;
void forkProcess(){
pid_t pid;
int status;
i++;
pid = fork();
if (pid == 0){ /* child */
printf("%d", i);
return;
} else{ /* parent */
i--;
waitpid(pid, &status, 0);
printf("%d", i);
return;
}
}
void main(){
i = 0;
forkProcess();
forkProcess();
}
(a) (4 points) What is the total number of processes (including the main parent process) that
will be created when this program is run?
Solution: Four processes.
(b) (4 points) Show the output that will be printed when this program is run because of the
statement printf (%d, i);. The output should be a sequence of numbers printed by all the
processes that are created when this program runs.
Solution: Note that the fork creates a clone of the process. The child process will have
its own global variables, yet their value will be the same as the parent at the time of
forking. 1 2 1 0 1 0
(c) (4 points) If the line of waitpid(pid, &status, 0); in the function f orkP rocess() is removed
and the program is re-executed, show the output that will be printed in this case. The output
should be a sequence of numbers printed by all the processes that are created when this
program runs.
Solution: The values printed by each process are the same as in part (b), however, the
order of the printing will change based on the scheduling of the processes. Constraints:
The sequence includes two 0s, three 1s, and one 2.
At least one 1 must precede the 2. This is because the first forked child will not
proceed to fork another child (which will print 2) until it has printed 1.
At least one 0 must precede the last 1. This is to guarantee that the main process
has printed 0 before it has forked its second child.
Possible correct answers: 1 1 0 0 2 1, 0 0 1 1 1 2, 0 1 0 1 2 1
4 of 4