Operating Systems: Quiz 1 October 27, 2006 Class: No. Name: Part I

advertisement
Operating Systems: Quiz 1
October 27, 2006
Class:
No.
Name:
Part I (20%)
Multiple Choice — Each of the following questions has only one correct answer. Fill the correct one
in the blank in front of each question.
1.
e
A device driver is: a) a type of system call; b) the part of a device that allows it to physically
function (e.g., spin a disk); c) a feature of a hardware device that helps it interact with the OS;
d) a peripheral hardware component of a computer system; e) a software routine that interfaces
with a hardware device.
2.
a
Batching of jobs improved early system performance by: a) reducing human set-up time b)
background processing; c) multiprogramming; d) overlapping CPU and I/O operations; e)
none of the above.
3.
b
The ability of a computer system to switch execution among several jobs that are in memory at the same time is called a) time slicing; b) multiprogramming; c) multiprocessing; d)
multitasking; e) none of the above.
4.
d
A counting semaphore was initialized to 4. Then 28 P(wait) operations and 18 V(signal)
operations were completed on this semaphore. Assume the resulting value of the semaphore is
0. Then 3 more V(signal) operations occur. What is the value of number of waiting processes?
a) 2 b) 0 c) 3 d) 6 e) none of the above.
5.
d
Elevator algorithm can be used for: a) process scheduling b) memory management c) printer
control d) disk scheduling e) sorting.
6.
c
Concurrency on a uniprocessor is achieved by: a) critical sections; b) message parsing; c) time
sharing; d) token passing.; e) none of the above.
7.
a
In the readers-writers problem, processes p and q are allowed to simultaneously access the
shared resource if and only if: a) p and q are both reading. b) p and q are both writing. c)
Either p or q or both is reading d) Either p or q or both is writing e) p is reading and q is
writing, or vice versa. f) Either p and q are both reading or p and q are both writing.
8.
b
DMA is a) Direct Manipulation Algorithm b) Direct Memory Access c) Dynamic Memory
Allocation d) Data Memory Allocation. e) Dynamic Memory Access.
9.
e
In a batch system the time defined as the time from which a job is submitted until the job is
returned to the user is called the a) run time b) burst time c) idle time d) inter-fault time e)
turnaround time.
10.
c
Which of the following is NOT referred to as an operating system? a) MS DOS b) Linux c)
Windows SDK d) Windows XP e) Unix.
Part II (10%)
True or False — For each of the following questions, you get 1 point for a correct answer, −0.5 point for
a wrong answer, and 0 point for blank. Your answers cannot all be true or false.
1.
T
Interrupts are used to achieve a typical timesharing OS.
2.
F
Dividing by zero will cause CPU to continue process but with give wrong arithmetic result.
3.
F
Monitors make processes free from deadlock.
4.
F
Setting the program counter register is a privileged operation.
5.
T
In many operating systems, many I/O devices are treated as files.
6.
F
Token passing is a protocol for process synchronization.
7.
F
The value of a semaphore represents how many processes are waiting for a common resource.
8.
T
For mutual exclusion, the initial value of the semaphore is set to 1.
9.
T
A Remote Procedure Call (RPC) can be used to call a procedure in another process on the
same machine.
10.
T
A bootstrap program loads the OS (Operating System) kernel into main memory and starts its
execution at power-up or reboot.
1
Part III (70%)
Question Answering
1. What is the difference between traps and interrupts? Give an example of each. (4%)
Ans)
• Trap is internally generated; for example, odd address, page fault, divide by zero.
• Interrupt is an external event; for example, I/O completion, network packet arrives.
2. There are two different modes in a modern OS. What are they (please name them) and what are they
for? (4%)
Ans)
• System mode (or supervisor mode) — for OS.
• User mode — for user applications.
3. What are race conditions and how do they occur? (4%)
Ans)
• Race conditions occur when two or more processes or threads attempt to manipulate the same data
object concurrently such that the final value of the data object is non-deterministic.
• They occur when concurrent processes or threads share some datum and attempt to access it
simultaneously with non-atomic instructions whose machine-level instructions may be executed in
different ways depending on how they are interleaved because of context switching.
4. What is the difference between a user-level instruction and a privileged instruction?(4%)
Ans) A user-level instruction is an instruction, such as ADD or PUSH, that does not require special
privileges, meaning it can be executed in either user (protected, application) or privileged (supervisor,
real, kernel) modes. A privileged instruction is an instruction, such as managing memory protection for
a process, and various I/O instructions, that can only be invoked in privileged mode (i.e. from the
kernel); if the instruction is illegally invoked from within a user process, then a software trap is raised.
5. Explain the advantages of monitors compared to semaphores. (4%)
Ans)
• implicit mutual exclusion ( by the guarantee that only one process can be in the monitor) — no
need to code it tediously as with semaphores.
• centralized and encapsulated access to the shared data guarded by mutual exclusion, so in order to
verify that the shared data are handled consistently, it is sufficient to verify that the monitor
methods are correct. With semaphores, the access is spread out and single incorrect user process
can corrupt the data/create deadlock.
6. Describe the difference between a synchronous and an asynchronous send operation. What can you say
about the size of the buffer in each? (5%)
Ans)
• Synchronous: sender waits (blocks) until the receiver is ready to receive. Don’t need a buffer, but a
buffer of size equal to one message may improve efficiency.
• Asynchronous: sender does not wait for receiver, just deposits message in buffer and continues
execution. If buffer is of size 0, it becomes synchronous send. Thus buffer should be large enough to
hold several messages.
7. What is a critical section? List the the requirements that a solution to the critical section problem must
satisfy. (5%)
Ans)
• Any section of code involved in reading and writing a share data area is called a critical section.
• To resolve critical section problem, at most one process is allowed to enter the critical section. In
addition, we need to prevent mutual blocking, e.g., to prevent deadlock, livelock, and starvation of
processes.
2
8. In the following code, three processes produce output using the routine putc and synchronize using two
semaphores L and R.
semaphore L = 3, R = 0; /* initialization */
/* Process 1 */
/* process 2 */
L1:
L2:
P(L);
P(R);
putc(’C’);
putc(’A’);
V(R);
putc(’B’);
V(R);
goto L1;
goto L2;
/* process 3 */
L3
P(R);
(a) How many D’s are printed when this set of processes runs?
putc(’D’);
goto L3;
3
(2%)
(b) What is the smallest number of A’s that might be printed when this set of processes runs?
(2%)
(c) Is CABABDDCABCABD a possible output sequence when this set of processes runs?
(d) Is CABACDBCABDD a possible output sequence when this set of processes runs?
no
yes
0
(2%)
(2%)
9. Consider the Dining Philosophers problem. If we arbitrarily decide that if a philosopher who has their
left fork waits a random amount of time after failing to acquire the right-hand fork before attempting to
acquire it again, will this remove the ability for the system to reach deadlock? Why or why not? (5%)
Ans) No. Adding a retry and a random wait time between them still does not relax any of the four
necessary and sufficient conditions for deadlock so you will still be able to reach deadlock. At best you
might delay occasionally.
10. What does SPOOLING stand for ? Describe the SPOOLING operation and the advantage of doing this.
(5%)
Ans) SPOOLING is Simultaneous Peripheral Operation On-line. The disk is used as a large buffer for
reading ahead on input devices and for storing output files until the output devices are able to accept
them. SPOOLING allows overlap of the I/O of one job with the computation of other jobs and improve
system throughput.
11. Consider the following two functions, where A and B are arbitrary computations:
f1(){
P(S1);
c1 = c1 + 1;
if(c1 == 1) P(d);
V(s1);
A;
P(s1);
c1 = c1 - 1;
if(c1 == 0) V(d);
V(s1);
}
f2() {
P(s2);
c2 = c2 + 1;
if(c2 == 1) P(d);
V(s2);
B;
P(s2);
c2 = c2 - 1;
if(c2 == 0) V(d);
V(s2);
}
Initially: s1=s2=d=1; c1=c2=0;
Assume that an unbounded number of processes are invoking either of the functions f1() or f2().
(a) How many invocations of the computation A can proceed concurrently? What are the values of s1,
c1, and d at that time? (4%)
Ans) Any number of instances of A can proceed concurrently.
If not new process is currently trying to enter, then s1 = 1, c1 = n (where n is the number of
concurrent instance of A) and d = 0. If another process is currently attempting to invoke A then
s1 = 0 and c1 = n + 1.
(b) While A is running, how many invocations of B can proceed concurrently? What are the values of
s2, c2, and d at that time? (4%)
Ans) B cannot execute concurrently with A. s2 = 0, c2 = 1, and d = 0.
(c) Can A and B starve? Explain why or why not? (4%)
Ans) If there is an unbounded sequence of invocation of either of the two functions, then the other
will starve because no process will ever execute V (d).
3
12. Write a monitor that implements an alarm clock that enables a calling program to delay itself for a
specified number of time units (ticks). You may assume the existence of a real hardware clock that
invokes a procedure tick in your monitor at regular intervals. (10%)
Ans)
monitor AlarmClock {
int now=0;
condition wakeup;
wakeme(int n) {
int alarm;
alarm = now + n;
while (now < alarm) wakeup.wait(alarm);
wakeup.signal;
}
tick() { /*invoked automatically by hardware*/
now = now + 1;
wakeup.signal;
}
}
4
Download