OS principles Flashcards

1
Q

Operating systems are typically interrupt-driven. Explain what this means for OS kernel design. In your answer, make sure to describe different types of interrupts and how they are used by the kernel.

A

An interrupt-driven OS kernel will react to interrupts triggered by hardware or software.

Hardware interrupts tend to come from drivers and their physical devices, whilst software interrupts are either triggered by traps (access to bad memory, etc) or on purpose via syscalls (open, close, etc) which themselves generate traps and an interrupt ID.

An impact this has on kernel design could be;
- Kernel requires an interrupt vector (ISR) to be set up so the CPU can jump to correct instructions in kernel mode

One could further explain how interrupts can become clutter for the OS, for example, an unbuffered device sending an interrupt for every byte received would flood the kernel and CPU.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain how a userspace program would make use of the special interrupt 0x80 to invoke the exit system call on an x86 CPU running Linux.

In your answer, mention when the transition to kernel mode happens, show the role of the
interrupt vector using a diagram, and explain how the appropriate kernel code is located and loaded into the CPU.

A

A typical userspace program would just call the provided wrapper function, ‘exit(0)’, in order to invoke the syscall. It could also be called directly with ‘syscall(0x80)’. This will pop the program’s stack frame and ‘exit’ the process.

The transition to kernel mode is caused by the OS itself generating a ‘trap’ with the value 0x80. This interrupt/trap causes the CPU drop to kernel mode (often setting the mode-bit to signify ring 0), lookup the pointer to the handler function from the ISR table (which the kernel set up when it started), jump to the instruction and begin executing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A music fan uses a mobile device to record a concert using the built-in
microphone. Explain the job performed by the device driver, including in your
answer:

(i) The role of interrupts. [2 marks]
(ii) Direct memory access. [2 marks]
(iii) How data are made available to the userspace program. [2 marks]

A

i) Interrupts are used here to notify the CPU that the audio device, the microphone, has data available. They are also used to notify the driver that it should tell the audio controller to start listening for data.
ii) DMA means that the controller on the device will fill up an on-device memory buffer instead of interrupting with every bit of information received.
iii) The read syscall should read the data from the virtual file created by the device. For example, in Linux, the microphone will be at /dev/sndX. So when that file is opened for reading, the data being captured by the microphone will be made available to the reading process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Many operating systems provide some parts of their functionality in the form of loadable kernel modules.

Explain the benefits of such a modular architecture by giving an example of a kernel that makes use of loadable modules.

Discuss any similarities or differences between such a modular kernel and a microkernel.

A

The Solaris kernel makes use of a modular design. This approach has a number of benefits, including;

  • Ease of module development
  • No overhead of syscalls for message passing

A microkernel implements only the core functionality required to coordinate modules in userspace. This is an extreme of the approach taken by a modular kernel. The microkernel incurs penalties for message passing, as the userspace modules must use syscalls to talk to the kernel itself. As previously mentioned, this is not a problem with modular kernels.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

You are given the following C program:

#include 
void main() {
    printf(“Hello world!\n”);
}

Using example addresses, explain how the symbol ‘printf’ is translated into a physical memory address when using:

i. Load-time binding. [3 marks]
ii. Run-time binding. [3 marks]
Explain any assumptions you make.

A

i) Load-time binding requires a contiguous memory space as the compiler has generated only relative addresses for the variables.

eg.
void printf; 0x100000 –> [process base logical address] + 0x100000

ii) Run-time binding works over non-contiguous memory spaces by having different logical and physical addresses.

eg.
void printf; 0x100000 –> [process base logical address] + 0x100000 –> [MMU mapping logical -> physical]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the role of the OS in address binding.

A

The OS is responsible for providing logical addresses to the programs, at either load or run time. It must also provide addresses in to manage dynamic address allocation, like when mmap is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the process of using the ‘write’ system call to write to a file from a user space program using the x86 syscall instruction.

Explain how the call is made, how parameters are passed, and what happens in kernel mode before the data is committed to disk.

A

The call is made by passing arguments to the ‘write’ function provided by the system standard library. Internally, this will use the syscall instruction. Arguments can be passed to the syscall instruction using registers, or by pushing them on to the stack.

For write, the syscall needs a pointer to some memory to write, a file descriptor to write to and a number of bytes to be written.

Not sure about what “happens in kernel mode before the data…”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the importance of the timer interrupt for general-purpose operating systems.

A

Without it, there would be no way to interrupt processes than have been given CPU time, and therefore no way to implement preemptive multitasking. This would mean that a program could keep control of the CPU for as long as it wished, with the OS / any other program unable to regain control.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A driver for a microphone device delivers audio samples at 44kHz. In a naïve implementation, each sample generates an interrupt, which takes 1μs to process.

What percentage of the CPU time is taken by processing data from this device? State any assumptions you make.

Suggest a way to reduce the CPU utilization caused by this driver.

A

So a single sample would be produced every 1 / 44000 seconds = ~23us. Since the interrupt takes 1us, 1 / 23 = ~4.3% of CPU time is used by the device.

A way to improve CPU utilisation would be to implement DMA, forcing the controller to fill a buffer with inputs on-device before interrupting the kernel.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

For an OS that implements only multi-programming (but not time-sharing or multi-tasking) describe a use-case and the role of the OS.

A

Purely for isolation of userspace from hardware. OS will still provide common utilities etc and provide abstractions over hardware for the single job running to use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe the three core protection mechanisms relied upon by a multi-tasking/time-sharing OS. For each protection mechanism, explain the vulnerabilities that are addressed.

A

Memory Protection

  • Keeps processes within their own memory space unless they explicitly share the memory
  • Addresses memory-corruption/stealing attacks where a malicious process can tamper with / view private memory

Scheduling

  • Allows more than one job to run at once
  • Stops a single process from blocking CPU indefinitely with a while loop, for example

Privileged Operations

  • Forces processes to go through OS to execute privileged instructions like reading/writing to disk
  • Prevents malicious processes from reading or deleting any data on the disk, or from user’s deleting other users’ data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Show how interrupts can be used to implement an API to the OS. Explain the functionality provided by such an API and give an example of a call to the API, showing how parameters are passed.

A

I’ll use the ‘write’ syscall as an example. It is implemented via the syscall with value 0x80.

You can trigger the syscall via the library method write, passing all arguments in C, or via assembly. In assembly, you push the arguments into registers (syscall id, fd to write to, pointer to get bytes from, n bytes to write) and then execute the syscall instruction.

This will generate a software interrupt in the OS, dropping to kernel mode (setting mode bit where appropriate), jumping to the ISR table, looking up the interrupt ID and then executing the appropriate syscall handling code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain the term ‘virtual machine’ and describe three use-cases for VMs.

A

A ‘virtual machine’ is an abstraction over the hardware of a computer, often using special HW instructions on the host machine. Comes in three flavours, Native (Bare Metal), Hosted and Emulated.

1) Running many guest VMs on a single piece of hardware in a cloud environment (Native / Bare Metal with KVM)
2) Running macOS in Windows to use macOS specific tools (Hosted via VirtualBox and Vt-d)
3) Running a Gameboy emulator, where every instruction is emulated in software (Emulated via C++)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly