



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Homework 1 Question 1 Which instructions should not be permitted in the user mode? Explain your answer in one or two sentences per each. a) Perform a trap. b) Read device register for a given device. c) Read the time of day clock. d) Disable all interrupts. Answer: d) and maybe b) a) User programs can perform a switch to kernel mode at any time either voluntarily by performing a system call, or involuntarily by performing an unexpected trap. The latter may happen, for instance, when the user program references a null pointer. b) Whether this is permissible depends on the device and depends on the register. It is ok to read the status bits to see if a floppy is mounted. It is not ok to read a device register that holds data that may belong to another user. c) Can be permitted in the user mode, because reading the time of day clock is not a privileged operation. d) Should not be permitted in the user mode, because interrupts must be handled on behalf of the operating system, it’s not safe for user programs to be able to service or ignore interrupts, nor is it safe for them to have raw access to devices. Question 2 Describe the actions taken by the kernel to context-switch between two processes. Answer:
Consider a system running 3 I/O bound threads and a single CPU bound thread. The three I/O threads are first on the run queue and issue constant streams of messages to three unique I/O devices; it takes the thread 1 ms to make a request, each I/O device 6 ms to service each request, and the threads yield immediately to wait for the devices. The CPU bound thread runs forever. Assume the system is running a preemptive roundrobin scheduler and each context switch takes 1 ms. a) Define the percent ``Useful CPU Usage'' of the system to be the ratio of (Total CPU time) minus (Time spent performing context switches) to (Total CPU time). What is the percent useful CPU usage if the scheduler runs with a time quantum of 1 ms? A: IO1 waits for 6ms 1 ms IO1 CS IO2 CS IO3 CS CPU CS IO1 CS IO2 CS IO3 CS CPU CS … As seen above, for every 1ms execution a 1ms Context Switch takes place: Total CPU Time: 16 ms Useful CPU Usage: (16ms 8ms) / 16 ms = 50% Context Switches: 8 ms
Requests
b) We implement the system with a time quantum of 1 ms but the owner of the CPU bound thread complains about poor performance. We blame the time spent in context switches. Propose a new time quantum value that increases useful CPU usage by at least 20 percent. A: We should increase the time quantum: let’s make it 7. 1ms 7 ms IO1 CS IO2 CS IO3 CS CPU CS IO1 CS IO2 CS IO3 CS CPU CS Total CPU Time: 28 ms Useful CPU Usage: (28ms 8ms) / 28 ms = ~71% Context Switches: 8 ms c) We implement the system with the new time quantum value but receive complaints from the owners of the I/O bounded threads that the new system is worse'' than the old system. What do they most likely mean byworse'', and briefly explain why it is happening. A: As the time quantum is larger, the IObounded threads have to wait longer for the CPU bound thread. For example in the first example they were waiting only 1 ms after their 6ms wait was over, in the second example they have to wait 7ms.
In class, we saw that an atomic test and set operation can be used to implement critical sections on a machine supporting parallelism (with multiple physical CPUs). But suppose that you were given a different atomic instruction called decrement. This instruction decrements a variable and leaves the initial value in register 0. Could decrement be used to implement critical sections? Show us how or explain why not. A: In testandset(x) where x is a binary or boolean variable, x is set to true (atomically) and the old value of x is returned. The use of testandset for implementing critical sections is as follows:
b) Name one situation where the basic atomic test and set operations should be used instead of semaphores. Justify your answer in one sentence. A: In the implementation of semaphores themselves, the basic atomic test and set is necessary.
Suppose we replace the wait() and signal() operations of monitors with a single construct await(B), where B is a general Boolean expression that causes the process executing it to wait until B become true. Explain why, in general, this construct cannot be implemented efficiently. A: When a process is leaving the monitor, it may or may not leave the variables in such a state to make B true. Consequently, all waiting threads need to be awoken to test if their predicate holds. Thus, monitor exits would be very expensive operations, requiring waking up potentially hundreds or thousands of waiting threads. Condition variables are a much more directed way to wake up just the right number of targeted threads.