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.

Jump to a question
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22

1 Application isolation

2 marksRead: What an operating system is

Application isolation is an important property of an operating system because:

2 Availability

2 marksRead: What an operating system is

The property of availability of an operating system can be improved by:

3 Upcalls and virtual machines

2 marksRead: The kernel abstraction

An upcall from the host kernel to a virtual machine player can be seen as analogous to:

4 End of the interrupt handler

2 marksRead: The kernel abstraction

The atomic operation at the end of the interrupt handler does all of the following except:

5 Bottom half of the interrupt handler

2 marksRead: The kernel abstraction

Which of the following statements is not true regarding the bottom-half of the interrupt handler?

6 Trap

2 marksRead: The kernel abstraction

A trap is defined as:

7 Processor exceptions

2 marksRead: The kernel abstraction

All of the following are examples of processor exceptions except:

8 Mode switch

2 marksRead: The kernel abstraction The programming interface

Which of the following C-statements would trigger a mode switch (a transition from user mode to kernel mode) during program execution?

9 Fork bomb

10 marksRead: The programming interface

  1. (5 pts) A forkbomb is a simple program where the parent forks itself in an endless loop. Write a forkbomb in C.
  2. (3 pts) What is the expected consequence of running this code?
  3. (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

10 marksRead: Address translation C for the exam

Consider a process address space for the following questions.

  1. (3 pts) What is the stack used for?
  2. (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. (3 pts) What is the heap used for?
  4. (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
  1. The stack is a LIFO data structure used for storing variables local to a function and recording the return address of function calls.
  2. void func() { int a; }
  3. 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.
  4. void *mem = malloc(256);

11 Sum with a thread

10 marksRead: Concurrency and threads

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

10 marksRead: Synchronisation

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:

  1. (5 pts) Describe the minimum critical section of the thread function calculateSum.
  2. (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
  1. 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
  2. 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

2 marksRead: Concurrency and threads

What is meant by a thread abstraction?

14 Wrapping pthread_create

2 marksRead: Concurrency and threads

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?

15 False sharing

2 marksRead: Multi-object synchronisation and deadlock

False sharing is:

16 Page replacement policies

9 marksRead: Caching and virtual memory

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
PolicyFaultsFinal contents
MIN7A B C
FIFO9F B C
LRU9C 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

12 marksRead: Scheduling

Consider the round robin scheduler with the following tasks, all arriving at t = 0:

TaskLength
120
21
35
42

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
QuantumResponseTurnaroundWait
22.5136
55.514.57.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

2 marksRead: Storage and file systems

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?

19 FFS inodes

2 marksRead: Storage and file systems

The Fast File System (FFS) uses what data structure to store inodes at its highest level?

20 File descriptor

2 marksRead: The programming interface Storage and file systems

A file descriptor (or handle) is:

21 Buffer overflow on stack and heap

9 marksRead: Security

Consider a programming error that leads to a buffer overflow where an attacker can control the data written into the buffer.

  1. (3 pts) What consequences can this have when the buffer is stored on the stack?
  2. (3 pts) What consequences can this have when the buffer is stored on the heap?
  3. (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
  1. Stack local variables can be corrupted or modified, and the return address can be controlled, giving the attacker control over program execution.
  2. 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.
  3. Example mitigations:
    • Safe libc calls such as fgets instead of gets
    • 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

22 Asymmetric encryption

2 marksRead: Security

For two parties to communicate secretly using asymmetric encryption, it is necessary that: