Practice

Every practice problem from the nine chapters in one place. Try each one before opening the answer.

Chapter 1. Introduction

1. What are the three goals of an operating system?
Show the answer
Execute user programs and make solving user problems easier; make the computer system convenient to use; use the computer hardware in an efficient manner.
2. Distinguish the kernel, system programs and application programs.
Show the answer
The kernel is the one program running at all times. System programs ship with the OS but are not part of the kernel. Application programs are all programs not associated with the operating system.
3. Name two kinds of software interrupt (trap).
Show the answer
A software error such as division by zero, and a request for an operating-system service (a system call). Other process problems such as infinite loops also lead the OS to step in.
4. Why is a timer needed?
Show the answer
To prevent a process from looping forever or hogging resources: the OS sets a counter (privileged) that is decremented by the clock; at zero an interrupt returns control to the OS.
5. Explain the difference between multiprogramming and multitasking.
Show the answer
Multiprogramming keeps several jobs in memory so the CPU always has one to run when the current job waits. Multitasking (timesharing) switches so often that users can interact with each program; response time should be under one second.
6. Match each cloud service to an example: SaaS, PaaS, IaaS — a database server ready to use; storage for backups; a word processor in the browser.
Show the answer
SaaS: word processor in the browser. PaaS: database server ready for application use. IaaS: storage available over the Internet for backups.

Open the Chapter 1 slides →

Chapter 2. Operating-System Services

1. List the OS services that help the user and those that help the system run efficiently.
Show the answer
For the user: user interface, program execution, I/O operations, file-system manipulation, communications, error detection. For the system: resource allocation, logging, protection and security.
2. Why do programs normally use an API instead of calling system calls directly?
Show the answer
The API (Win32, POSIX, Java) is portable and easier to use; the library hides the details of passing parameters and trapping into the kernel, so programs work across versions and systems that provide the same API.
3. Give the type of each call: fork(), ioctl(), getpid(), shm_open(), chmod(), CloseHandle().
Show the answer
Process control; device management; information maintenance; communications; protection; file management.
4. printf("Greetings") — which part runs in user mode and which in kernel mode?
Show the answer
printf() is a C library function that formats the text in user mode; it then calls the write() system call, which runs in kernel mode and sends the bytes to the device.
5. Give one benefit and one drawback of the layered approach.
Show the answer
Benefit: each layer uses only the layers below, so it is simple to build and debug one layer at a time. Drawback: defining the layers is hard and each request passes through several layers, which adds overhead.
6. What does an ABI define, and why does it make applications OS specific?
Show the answer
The Application Binary Interface defines how binary code components interface on a given OS and architecture (calling conventions, binary formats, system-call numbers). A binary built for one ABI does not run on another.

Open the Chapter 2 slides →

Chapter 3. Processes

1. Name the parts of a process in memory and what each holds.
Show the answer
Text: program code. Data: global variables. Heap: memory allocated at run time. Stack: temporary data — function parameters, return addresses, local variables. Plus the current activity: program counter and registers.
2. What is stored in a PCB?
Show the answer
Process state, program counter, CPU registers, CPU-scheduling information, memory-management information, accounting information and I/O status information.
3. Why is context-switch time called pure overhead?
Show the answer
While saving one process’s state into its PCB and loading another’s, the system does no useful work for any process.
4. What is the difference between a zombie and an orphan?
Show the answer
A zombie has terminated but its parent has not yet called wait(), so its entry remains. An orphan is a process whose parent terminated without calling wait().
5. With a bounded buffer of size 5, the producer has added 5 items and the consumer none. What happens on the next produce? On an unbounded buffer?
Show the answer
Bounded: the buffer is full, so the producer must wait until the consumer removes an item. Unbounded: the producer never waits.
6. What is a rendezvous?
Show the answer
Message passing where both send and receive are blocking: the sender waits until the message is received, and the receiver waits until a message is available. A zero-capacity link forces it.

Open the Chapter 3 slides →

Chapter 9. Main Memory

1. A process has base 300040 and limit 120900. Is address 420940 legal?
Show the answer
No. Legal addresses satisfy base ≤ a < base + limit = 420940, so 420940 is the first illegal address; the hardware traps to the OS (addressing error).
2. Explain external and internal fragmentation, and which one paging avoids.
Show the answer
External: enough total free memory exists but it is not contiguous. Internal: an allocated block is larger than requested and the extra is unused. Paging avoids external fragmentation but still has internal fragmentation in the last page.
3. Page size 4 KB, process size 21,000 bytes. How many frames, and how much internal fragmentation?
Show the answer
21,000 = 5 × 4096 + 520, so 6 frames; internal fragmentation = 4096 − 520 = 3,576 bytes.
4. A 32-bit system with 4 KB pages and 4-byte entries: how big is a single-level page table?
Show the answer
2³² / 2¹² = 2²⁰ entries × 4 bytes = 4 MB per process.
5. Split 32-bit address 0x00403ABC for two-level paging (10 | 10 | 12).
Show the answer
Binary 0000 0000 0100 0000 0011 1010 1011 1100: p1 = first 10 bits = 1, p2 = next 10 bits = 3, d = 0xABC = 2748.
6. How long is the swapping part of a context switch for a 200 MB process at 100 MB/s?
Show the answer
Swap out 2000 ms plus swap in of a same-sized process 2000 ms = 4000 ms.

Open the Chapter 9 slides →

Chapter 10. Virtual Memory

1. What is a lazy swapper?
Show the answer
A swapper that never brings a page into memory unless it will be needed; one that deals with pages is called a pager.
2. Why does the dirty (modify) bit reduce page-fault cost?
Show the answer
A victim page that has not been modified is already identical on disk, so it need not be written out; only dirty pages cost a write.
3. List the steps of basic page replacement.
Show the answer
1. Find the desired page on disk. 2. Find a free frame, or select a victim with the replacement algorithm and write it out if dirty. 3. Read the page into the frame and update the tables. 4. Restart the instruction that caused the trap.
4. How many faults does OPT give on 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 with 3 frames, and why is OPT not used in practice?
Show the answer
9 faults. It needs to know future references, so it is used only as a yardstick for other algorithms.
5. Order the four classes of enhanced second chance from best to worst victim.
Show the answer
(0,0) not used, not modified; (0,1) not used but modified; (1,0) used but clean; (1,1) used and modified.
6. What is the argument behind MFU?
Show the answer
The page with the smallest count was probably just brought in and has yet to be used, so MFU replaces the page with the largest count instead.
7. Using 4 frames on 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1, how many faults do FIFO, OPT and LRU give?
Show the answer
FIFO 10, OPT 8, LRU 8. Use the simulator with 4 frames to see the steps.

Open the Chapter 10 slides →

Chapter 4. Threads & Concurrency

1. Give the four benefits of multithreading.
Show the answer
Responsiveness, resource sharing, economy and scalability.
2. Distinguish concurrency from parallelism.
Show the answer
Concurrency: more than one task makes progress (possible on one core by interleaving). Parallelism: more than one task runs at the same instant, which needs multiple cores.
3. Using Amdahl’s law, show that 75% parallel code on 2 cores gives a speedup of 1.6.
Show the answer
1 / (0.25 + 0.75 / 2) = 1 / 0.625 = 1.6.
4. Which model allows a user thread to be bound to a kernel thread?
Show the answer
The two-level model (a variation of many-to-many).
5. Compare asynchronous and deferred cancellation.
Show the answer
Asynchronous cancellation terminates the target thread immediately; deferred cancellation lets the target check periodically whether it should be cancelled, so it can stop at a safe point.
6. What does CLONE_VM do in Linux clone()?
Show the answer
The child task shares the same memory space as the parent, which makes it behave like a thread.

Open the Chapter 4 slides →

Chapter 5. CPU Scheduling

1. What are the four situations in which a CPU-scheduling decision may happen, and which make scheduling preemptive?
Show the answer
Running → waiting, running → ready, waiting → ready, terminates. Scheduling only on 1 and 4 is nonpreemptive; deciding also on 2 or 3 makes it preemptive.
2. Define turnaround, waiting and response time.
Show the answer
Turnaround: from submission to completion. Waiting: total time spent in the ready queue. Response: from submission until the first response is produced.
3. What is the convoy effect?
Show the answer
With FCFS, short processes wait behind one long process, like cars behind a slow truck; e.g. many I/O-bound processes behind one CPU-bound process.
4. Why is SJF hard to implement, and how is it approximated?
Show the answer
The length of the next CPU burst is not known. It can be asked from the user or estimated (usually from the lengths of previous bursts).
5. How does aging solve starvation?
Show the answer
The priority of a waiting process is gradually increased with time, so even a low-priority process eventually becomes the highest priority and runs.
6. If 5 processes arrive per second on average and each waits 3 seconds, how many are in the queue on average?
Show the answer
Little’s law: n = λ × W = 5 × 3 = 15 processes.

Open the Chapter 5 slides →

Chapter 6. Synchronization Tools

1. State the three requirements of a solution to the critical-section problem.
Show the answer
Mutual exclusion, progress and bounded waiting.
2. Why is disabling interrupts not a good solution on multiprocessors?
Show the answer
It only prevents preemption on the CPU where it is done; processes on other CPUs can still enter. Disabling on all CPUs is slow, and long critical sections would block everything and starve others.
3. Which requirement does the shared-turn-only algorithm violate? Explain.
Show the answer
Progress: the processes must strictly alternate. If Pj stays in its remainder section with turn = j, Pi cannot enter even though nobody is in the critical section.
4. What does test_and_set return and what does it store?
Show the answer
It atomically returns the original value of *target and sets *target to true. A lock is acquired when it returns false.
5. What is the difference between a binary semaphore and a counting semaphore?
Show the answer
A binary semaphore ranges only between 0 and 1 (like a mutex lock); a counting semaphore can range over an unrestricted domain, e.g. the number of identical resources.
6. Why does a monitor guarantee mutual exclusion?
Show the answer
Only one process may be active within the monitor at a time; implemented with a semaphore mutex = 1, each procedure is wrapped in wait(mutex) … signal(mutex).

Open the Chapter 6 slides →

Chapter 8. Deadlocks

1. Name the four necessary conditions for deadlock.
Show the answer
Mutual exclusion, hold and wait, no preemption and circular wait — all four must hold simultaneously.
2. A resource-allocation graph has a cycle. Is there a deadlock?
Show the answer
If every resource type has only one instance, yes. If some have several instances, there may or may not be a deadlock.
3. How can hold and wait be prevented, and what is the cost?
Show the answer
Require a process to request all its resources before it starts, or to request only when it holds none. Resource utilization is low and starvation is possible.
4. What is the most common prevention method, and how does it work?
Show the answer
Invalidating circular wait: number every resource type and require each process to request resources in increasing order.
5. Compute Need for P2 with Max (9, 0, 2) and Allocation (3, 0 ,2).
Show the answer
Need = Max − Allocation = (6, 0, 0).
6. Give three factors for choosing which deadlocked process to abort.
Show the answer
Any three of: priority; how long it has computed and how long it still needs; resources it has used; resources it needs to complete; how many processes must be terminated; interactive or batch.

Open the Chapter 8 slides →