Exam, autumn 2023
Written exam, 29 November 2023, 15:00–18:00. 22 questions. No printed or hand-written support material; a specific basic calculator was allowed.
1 Application isolation
Application isolation is an important property of an operating system because:
As referee, the OS isolates users and applications from each other, so a fault in one cannot take down the others.
2 Availability
The property of availability of an operating system can be improved by:
Availability is the portion of time the system is working. It goes up if failures are rarer (longer MTTF) or repairs are quicker (shorter MTTR).
3 Upcalls and virtual machines
An upcall from the host kernel to a virtual machine player can be seen as analogous to:
An upcall is the user-level version of an interrupt: signal handlers correspond to the interrupt vector, and the signal stack to the interrupt stack.
4 End of the interrupt handler
The atomic operation at the end of the interrupt handler does all of the following except:
At the end of the handler the CPU atomically restores PC, stack and processor status word, and switches back to user mode.
5 Bottom half of the interrupt handler
Which of the following statements is not true regarding the bottom-half of the interrupt handler?
Interrupt handlers do the minimum and then wake up other threads to do any real work, so communicating with kernel threads is exactly what they do.
6 Trap
A trap is defined as:
System calls and exceptions are caused by the instruction being executed (synchronous) and move the CPU from user to kernel mode.
7 Processor exceptions
All of the following are examples of processor exceptions except:
Exceptions are triggered by unexpected program behaviour. A timer interrupt comes from a hardware device, asynchronously.
8 Mode switch
Which of the following C-statements would trigger a mode switch (a transition from user mode to kernel mode) during program execution?
printf eventually calls the write system call to output to the terminal. The others are plain user-mode instructions.
9 Fork bomb
- (5 pts) A forkbomb is a simple program where the parent forks itself in an endless loop. Write a forkbomb in C.
- (3 pts) What is the expected consequence of running this code?
- (2 pts) What can be done from an operating system's perspective to guard against a forkbomb?
Show solution
void main()
{
while(1) fork();
}The main point is that fork() is called repeatedly without any exec(), so the parent code runs again in every child. This rapidly overwhelms the system (memory, process limits, and so on) unless a maximum user process limit is set, for example with ulimit.
Naming ulimit is not required for full points. The point is that the OS can control the number of processes a user creates, because the fork bomb has to call fork(), which is a system call.
10 Stack and heap
Consider a process address space for the following questions.
- (3 pts) What is the stack used for?
- (2 pts) Write a snippet of C code that allocates an integer variable to the stack. Use not more than 2–3 lines of code.
- (3 pts) What is the heap used for?
- (2 pts) Write a snippet of C code that allocates a region of memory of size 256 bytes to the heap. Use not more than 2–3 lines of code.
Show solution
- The stack is a LIFO data structure used for storing variables local to a function and recording the return address of function calls.
void func() { int a; }- The heap stores dynamically allocated memory where the data would not normally be appropriate to store on the stack, for example large buffers or arrays and structures.
void *mem = malloc(256);
11 Sum with a thread
Write a small C program using the pthread library that starts a new thread that calculates the sum of the integers 1 to 1000 and returns the result as the thread exit status. The main process waits for the thread to complete and prints the result. Pseudocode is acceptable but take care to use the correct pthread library function names. Use the following skeleton code as a starting point:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// Function to calculate the sum of integers in the specified range
void *calculateSum(void *args) {
int sum = 0;
// Fill in with your answer here
}
int main() {
pthread_t thread;
int result;
// Fill in with your answer here
printf("The sum of integers from 1 to 1000: %d\n", result);
return 0;
}Show solution
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// Function to calculate the sum of integers in the specified range
void *calculateSum(void *args) {
int sum = 0;
for (int i = 1; i <= 1000; i++) {
sum += i;
}
// Exit the thread and return the sum as the exit status
pthread_exit((void *)sum);
}
int main() {
pthread_t thread;
int result;
// Create a new thread
if (pthread_create(&thread, NULL, calculateSum, NULL) != 0) {
perror("Error creating thread");
exit(EXIT_FAILURE);
}
// Wait for the thread to complete
if (pthread_join(thread, (void **)&result) != 0) {
perror("Error joining thread");
exit(EXIT_FAILURE);
}
// Print the result
printf("The sum of integers from 1 to 1000: %d\n", result);
return 0;
}12 Synchronising the sum
Consider the threaded program from the previous exercise. Assume the variable sum is global instead of local to calculateSum. Assume the main process starts 10 threads to distribute the calculation evenly (thread 1 sums from 1–100, thread 2 from 101–200, thread 3 from 201–300, and so on). Each thread must update the global variable sum with its result. How would you synchronise the code so that the sum is always correct? Specifically:
- (5 pts) Describe the minimum critical section of the thread function
calculateSum. - (5 pts) What synchronisation mechanism would you use and where would you place it?
Your answer does not need to include C code or pseudocode, but you may add it if you wish.
Show solution
- The critical section only needs to be the part of the code that reads and updates the global variable
sum. Store the thread's own work in a local variable, then add it to the global sum:localsum = sum(start..end) // begin critical section sum = sum + localsum // end critical section - A simple mutex lock, around the critical section only:
localsum = sum(start..end) // begin critical section pthread_mutex_lock(&lock); sum = sum + localsum // end critical section pthread_mutex_unlock(&lock);
13 Thread abstraction
What is meant by a thread abstraction?
The thread abstraction gives the illusion of an infinite number of processors, but threads execute with variable speed, so programs must work with any schedule.
14 Wrapping pthread_create
POSIX pthread_create() requires several parameters. Imagine writing a simple wrapper function for pthread_create() called start_thread(). This wrapper function can take only one parameter. What should that parameter be?
A thread is a separately schedulable task: thread_create(thread, func, args) runs func(args). Without the function there is nothing to run.
15 False sharing
False sharing is:
Cache coherence works on whole cache lines, so cores writing different variables in the same line still invalidate each other.
16 Page replacement policies
Consider a process with 3 pages of physical memory (3 slots). When all 3 pages are allocated and an unmapped page is referenced resulting in a page fault, a page replacement policy must choose a page to evict. Memory is initially empty. The process has the following reference sequence (each letter is a different page):
A B A B C B D D D A E A E F A B C
Fill in the number of page faults for MIN (optimal), FIFO and LRU.
Show solution
| Policy | Faults | Final contents |
|---|---|---|
| MIN | 7 | A B C |
| FIFO | 9 | F B C |
| LRU | 9 | C B A |
A dot marks a reference that caused a page fault:
MIN: A. B. A B C. B D. D D A E. A E F. A B C.
FIFO: A. B. A B C. B D. D D A. E. A E F. A B. C.
LRU: A. B. A B C. B D. D D A. E. A E F. A B. C.17 Round robin
Consider the round robin scheduler with the following tasks, all arriving at t = 0:
| Task | Length |
|---|---|
| 1 | 20 |
| 2 | 1 |
| 3 | 5 |
| 4 | 2 |
Response time is the time spent waiting after arrival before a task starts executing. Turnaround time is the time to complete the task after arrival. Wait time is the total time spent in the ready queue. Find the average response, turnaround and wait time for a time quantum of 2 and of 5.
Show solution
| Quantum | Response | Turnaround | Wait |
|---|---|---|---|
| 2 | 2.5 | 13 | 6 |
| 5 | 5.5 | 14.5 | 7.5 |
Quantum 2: the schedule is 1 (0–2), 2 (2–3, done), 3 (3–5), 4 (5–7, done), 1 (7–9), 3 (9–11), 1 (11–13), 3 (13–14, done), 1 (14–28, done). Tasks start at 0, 2, 3 and 5; they finish at 28, 3, 14 and 7. Wait time is turnaround minus length: 8, 2, 9, 5.
Quantum 5: 1 (0–5), 2 (5–6, done), 3 (6–11, done), 4 (11–13, done), 1 (13–28, done). Tasks start at 0, 5, 6 and 11 and finish at 28, 6, 11 and 13.
The course checked this with the OSTEP simulator: ./scheduler.py -p RR -l 20,1,5,2 -q 2 -c. Answers within the rounding range (for example 2–3 for 2.5) were accepted.
18 FAT volume size
Entries in the FAT filesystem table are 32 bits (4 bits are reserved, leaving 28 bits for blocks on disk). Given a block size of 4096 bytes, what is the largest possible volume size?
228 blocks × 212 bytes = 240 bytes = 1 TB.
19 FFS inodes
The Fast File System (FFS) uses what data structure to store inodes at its highest level?
FFS keeps an inode table (an array indexed by inode number), analogous to the FAT table.
20 File descriptor
A file descriptor (or handle) is:
open returns a handle (file descriptor) that is used in later calls on the file.
21 Buffer overflow on stack and heap
Consider a programming error that leads to a buffer overflow where an attacker can control the data written into the buffer.
- (3 pts) What consequences can this have when the buffer is stored on the stack?
- (3 pts) What consequences can this have when the buffer is stored on the heap?
- (3 pts) There are many measures to mitigate or reduce the impact of buffer overflows. Name one such measure and describe it briefly.
Show solution
- Stack local variables can be corrupted or modified, and the return address can be controlled, giving the attacker control over program execution.
- Allocated data on the heap after the buffer may be corrupted or modified. There is no return address on the heap, but function pointers can be stored there, so execution could also be controlled by the attacker.
- Example mitigations:
- Safe libc calls such as
fgetsinstead ofgets - Code review
- Compiler options like
-fstack-protector(stack canaries) and-pie(position independent executable) - Shadow stacks (not covered in the course, but a valid answer)
- ASLR, which makes it difficult to find valid addresses within the process address space
- Running the vulnerable program with reduced permissions or in a container or VM
- Safe libc calls such as
22 Asymmetric encryption
For two parties to communicate secretly using asymmetric encryption, it is necessary that:
For secrecy you encrypt with the receiver's public key, and only the receiver's private key can decrypt. Private keys are never shared.