Chapter 2: Operating-System Structures

Operating Systems · Study Questions
ServicesMounting and pipesDual mode and the timerSystem callsFile descriptorsKernel structuresMultiprocessor structureLinking and booting
Sercan KÜLCÜ

Multiple Choice · Introduction

Question 1: Same Code, Different System

The file-system code is the same in two operating systems. In one, it runs in kernel mode and the other components use it by direct function calls; in the other, it runs as a separate process in user mode and is called with messages. In what respect do the two systems really differ?

A: The strategy for where components run (kernel / user mode) and how they communicate (function call / message) is the operating-system structure: the first is the monolithic approach, the second the microkernel approach. The API and the disk format may be the same in both systems.

Multiple Choice · Services

Question 2: Which Is Not an OS Service?

Operating systems offer a set of services to users and programs. Which of the following is not one of these services?

C: Calculating formulas is the application’s own logic; the OS only provides it CPU and memory. Program execution, communication and error detection are standard groups of OS services.

Multiple Choice · Key Concepts

Question 3: Mounting a USB Stick

On Linux, after mount /dev/sdb1 /mnt/usb, which statement is true?

D: UNIX has a single directory tree; the file systems of other devices are attached to this tree at mount points. The old contents of the mount point are not deleted; they just stay invisible until umount is run. A drive letter is the Windows approach.

Classic Question · Key Concepts

Question 4: Building the Pipeline

The shell runs cat a.txt | sort | uniq -c | wc -l command. In total, how many pipe(), fork(), execvp() and dup2() calls do the shell and its children make? Why must the shell close its own copies of the pipe ends?

Show answer
callcountwhy
pipe33 links between 4 commands
fork4each command is a separate process
execvp4each child replaces itself with its command
dup26cat: stdout (1) · sort, uniq: stdin + stdout (2 + 2) · wc: stdin (1)

A reader sees end-of-file (read = 0) only when all the write ends of the pipe have been closed. If the shell does not close its own copies, wc never terminates; in addition each child closes the extra ends after dup2.

Multiple Choice · Protection

Question 5: An Operation Needing No Privilege

Which of the following operations is not required to be a privileged instruction, i.e. a user-mode program may do it freely?

A: Reading the clock harms no one. A program that sets the timer could monopolize the CPU, one that disables interrupts disables the OS, and one that changes the page-table register reaches other processes’ memory. This is why B, C and D run only in kernel mode.

Classic Question · Protection

Question 6: Setting Up the Timer

A hardware timer’s counter decreases by 1 on every pulse of a 19.2 MHz clock and raises an interrupt at zero. The kernel wants 250 ticks per second (250 Hz). What value must be loaded into the counter? If handling each tick interrupt takes 3 µs, what percentage of CPU time goes to ticks? What changes at 1000 Hz?

Show answer
  • 250 Hz: 19,200,000 / 250 = 76,800 counts; tick interval 4 ms.
  • Overhead: 250 × 3 µs = 750 µs/s → 0.075%.
  • 1000 Hz: counter 19,200, overhead 3 ms/s → 0.3% (4 times as much).

Frequent ticks mean finer time slices and more responsive scheduling; the price is overhead and a CPU that wakes up even when idle (this is why tickless mode exists). Loading a value into the counter is a privileged instruction: a user program cannot stop the timer and monopolize the CPU.

Multiple Choice · System Calls

Question 7: Which One Does Not Enter the Kernel?

Which of the following operations can be completed with library code alone, without entering the kernel?

B: atoi, strlen, sprintf work only on data in the process’s own memory. Renaming and opening a file touch the file system, and creating a process touches kernel structures; these are system calls. printf is also a library function but calls write when it flushes its buffer.

Output Question · System Calls

Question 8: Which Descriptor Number?

The program has just started; only 0, 1, 2 are open and all files exist. What is printed to the screen? What is written to d.txt?

int a = open("a.txt", O_RDONLY);
int b = open("b.txt", O_RDONLY);
int c = dup(a);
close(a);
close(1);
int d = open("d.txt", O_WRONLY);
int e = dup(c);
printf("%d %d %d %d %d\n", a, b, c, d, e);
Show answer

Rule: open and dup return the smallest free number.

  • a = 3, b = 4, c = dup(3) = 5. close(a) frees 3.
  • close(1) closes standard output → d = 1; e = dup(5) = 3.
  • printf always writes to fd 1; fd 1 is now d.txt.

Nothing is printed to the screen; d.txt receives 3 4 5 1 3. The shell’s > redirection relies on exactly this rule.

Step-by-Step Trace · System Calls

Question 9: The Copy Loop

src is open on a 10,000-byte file, dst on an empty file. What does the program print? How many system calls are made for read and write in total?

src (10,000 B)position 0dst

src is open on a 10,000-byte file, dst on an empty file; position 0.

src (10,000 B)3000 Bposition 3000dst3000 B

The first read fills the buffer: 3000 B. As much as was read is written.

src (10,000 B)3000 Bposition 6000dst6000 B

Second read: again 3000 B; file position 6000.

src (10,000 B)3000 Bposition 9000dst9000 B

Third read: 3000 B; 1000 B are left in the file.

src (10,000 B)1000 Bposition 10000dst10000 B

The fourth read returns only the remainder: 1000. write(dst, buf, n) writes only the filled part of the buffer.

src (10,000 B)position 10000dst10000 BEOF

The fifth read returns 0: end of file. The loop ends, but this call was counted too.

src (10,000 B)position 10000dst10000 BEOF

5 reads + 4 writes = 9 system calls, each with two mode switches. Writing a fixed 3000 bytes would make dst 12,000 B.

char buf[3000];
int reads = 0, writes = 0;
long total = 0;
ssize_t n;
for (;;) {
  n = read(src, buf, sizeof buf);
  reads++;
  if (n <= 0)
    break;
  write(dst, buf, n);
  writes++;
  total += n;
}
printf("reads=%d writes=%d total=%ld\n",
       reads, writes, total);

Classic Question · System Calls

Question 10: The Cost of Buffer Size

Assume each system call costs only 0.25 µs for the mode switch and entry/exit. A 2 MiB file is read first with a 1-byte buffer and then with an 8192-byte buffer in a read loop (including the last call that reports end of file). In each case how many read calls are made, and how large is this overhead?

Show answer
  • 2 MiB = 2 × 2²⁰ = 2,097,152 B.
  • 1 B buffer: 2,097,152 + 1 = 2,097,153 calls → × 0.25 µs ≈ 524.3 ms.
  • 8192 B buffer: 2,097,152 / 8192 = 256, + 1 = 257 calls → 64.25 µs.

The difference is roughly 8000-fold. This is why stdio (fread, getc) buffers in user space and goes to the kernel in large pieces: getc usually never enters the kernel at all.

Multiple Choice · System Calls

Question 11: A Failed exec

Instead of echo, a programmer mistakenly wrote ecoh; there is no program with that name on the system. What lines does the program print?

int main(void) {
  printf("A\n");
  fflush(stdout);
  pid_t p = fork();
  if (p == 0) {
    execlp("ecoh", "ecoh", "B", NULL);
    printf("C\n");
    exit(1);
  }
  int st;
  waitpid(p, &st, 0);
  printf("D%d\n", WEXITSTATUS(st));
  return 0;
}

B: execlp returns −1 when it cannot find the program, and the child continues with the next line: it prints C and ends with exit(1). The parent waits and reads exit code 1: D1. A successful exec would not return; the output would then be A, B, D0. fflush keeps A from staying in the buffer and being copied to the child.

Step-by-Step Trace · System Calls

Question 12: link and unlink

After each line, what are the link count and the number of open descriptors of i-node 70? Does the read in line 6 succeed? When are the file’s blocks freed?

/home/backupnotes.txt → 70(no entry)i-node 70link count = 1open fds = 0blocks: 3 blocks

A directory holds only (name, i-node number) pairs; the file itself is in i-node 70.

/home/backupnotes.txt → 70n.txt → 70i-node 70link count = 2open fds = 0blocks: 3 blocks

link adds a second name pointing to the same i-node; no data is copied, the link count becomes 2.

/home/backup(no entry)n.txt → 70i-node 70link count = 1open fds = 0blocks: 3 blocks

unlink removes only the directory entry; the file lives on under the name n.txt.

/home/backup(no entry)n.txt → 70i-node 70link count = 1open fds = 1blocks: 3 blocks

open gives the smallest free number: fd = 3. The kernel keeps the i-node open.

/home/backup(no entry)(no entry)i-node 70link count = 0open fds = 1blocks: 3 blocks (nameless)

The last name is deleted too: link count 0. But since the file is still open, the blocks are not freed.

/home/backup(no entry)(no entry)i-node 70link count = 0open fds = 1blocks: 3 blocks (nameless)

read succeeds (if the file is at least 100 B, n = 100): an open descriptor is tied to the i-node, not to the name.

/home/backup(no entry)(no entry)i-node 70link count = 0open fds = 0blocks: freed

Link count and open count are both 0: the i-node and its 3 blocks are freed only now. Temporary files are created this way.

/* initially: /home/notes.txt → i-node 70 */
link("/home/notes.txt", "/backup/n.txt");
unlink("/home/notes.txt");
int fd = open("/backup/n.txt", O_RDONLY);
unlink("/backup/n.txt");
ssize_t n = read(fd, buf, 100);
close(fd);

Multiple Choice · Structures

Question 13: The Real Gain of a Microkernel

Which of the following is an advantage of the microkernel structure?

C: Drivers and the file system are separate user-mode processes; when one crashes, the kernel and the other services keep running (MINIX 3’s reincarnation server). The privileged code is small. The opposite of A and B is true: IPC is the price of a microkernel; D describes a monolithic kernel.

Step-by-Step Trace · Structures

Question 14: One Read on a Microkernel

On a microkernel, a read request is completed with four messages; each message needs two mode switches and one context switch. How much overhead is there, excluding disk time? Compare it with a monolithic kernel.

user modeApplicationFile serverDisk drivermicrokernel (IPC)

On a monolithic kernel a read is a single system call: one mode switch on entry and one on return; everything in between is an in-kernel function call.

user modeApplicationFile serverDisk drivermicrokernel (IPC)12message 1: 2 mode switches + 1 context switch

The application sends the message: trap into the kernel (1), the kernel switches to the file server (context switch) and returns it to user mode (2).

user modeApplicationFile serverDisk drivermicrokernel (IPC)12message 2: 2 mode switches + 1 context switch

The file server finds which block to read and sends a message to the disk driver.

user modeApplicationFile serverDisk drivermicrokernel (IPC)12message 3: 2 mode switches + 1 context switch

The driver reads the block with the I/O the kernel permits and sends the result to the file server.

user modeApplicationFile serverDisk drivermicrokernel (IPC)12message 4: 2 mode switches + 1 context switch

The file server returns 4096 bytes to the application. Total 4 × 1.4 = 5.6 µs: 28 times as much as the monolithic kernel. When the disk takes milliseconds the difference stays small; for small requests served from the cache it is decisive.

app      → file srv: READ(fd, 4096)
file srv → disk drv: BLOCK 812
disk drv → file srv: block data
file srv → app     : 4096 bytes
mode switch: 0.1 µs
context switch: 1.2 µs
monolithic: single system call

Multiple Choice · Structures

Question 15: Where Does a Loadable Module Run?

modprobe loads a network-card driver at run time. In which mode and in which address space does it run?

D: A loaded module becomes part of the kernel and talks to the rest of the kernel by direct function calls; there is no IPC in between, and a module bug can crash the whole kernel. Modules make Linux flexible but do not turn it into a microkernel: Linux is modular monolithic.

Multiple Choice · Structures

Question 16: Exokernel vs. Virtual Machine

What is the key difference between an exokernel and a virtual machine monitor (hypervisor)?

C: An exokernel allocates blocks, pages and time slices and tracks who owns what; abstractions such as files and processes live in the libOS, and no remapping layer is needed. It is the hypervisor that gives a copy of the machine (A is reversed). Loading components at run time is a property of extensible systems.

Classic Question · Hardware and Structure

Question 17: Two Cores, One List

On an SMP system two cores call add_task at the same time: CPU0 adds task X, CPU1 adds task Y; initially the list is head → A. If the steps run in the order CPU0(1), CPU1(1), CPU0(2), CPU1(2), what does the list become? What is the fix?

struct task *head;  /* ready list */
void add_task(struct task *t) {
  t->next = head;   /* (1) */
  head = t;         /* (2) */
}
Show answer
  • CPU0(1): X.next = A. CPU1(1): Y.next = A.
  • CPU0(2): head = X → X → A. CPU1(2): head = Y → Y → A.

X is dropped from the list: no pointer can reach it, so it is never scheduled. The outcome depends on the order of the steps (a race condition).

On SMP every CPU can run kernel code, so shared kernel structures must be protected with a lock (e.g. a spinlock): lock; (1); (2); unlock. This is why Linux moved from the “big kernel lock” to fine-grained locks.

Classic Question · Linking and Booting

Question 18: Static or Dynamic?

40 different programs that use the C library run on a server at the same time; the library code is 2 MiB. How much memory does the library code need if the library is linked statically, and if it is linked dynamically (shared, libc.so)? When a security hole in the library is fixed, what must be done in each case?

Show answer
  • Static: the code is copied into every executable → in memory 40 × 2 MiB = 80 MiB (each file also grows by 2 MiB on disk).
  • Dynamic: the code pages are read-only, so a single copy is mapped into all processes → 2 MiB (+ a small private data area per process).
  • Patch: with static linking all 40 programs must be relinked; with dynamic linking only libc.so is updated, and programs use the new code when they are restarted.

The advantage of static linking is independence: the program needs no other file, and there is no symbol resolution at load time.

Classic Question · Linking and Booting

Question 19: From the Power Button to the Login Screen

On a Linux computer that uses UEFI, put the following steps in the correct order.
(a) The bootloader loads the kernel and initramfs and jumps to the kernel.
(b) The CPU resets and starts fetching instructions from the firmware in flash.
(c) systemd (PID 1) starts the services and the login screen.
(d) UEFI performs POST: it tests memory and finds the PCIe/USB devices.
(e) The bootloader is loaded from the EFI system partition (NVRAM order).
(f) The kernel sets up memory management and drivers and mounts the root file system.

Show answer

b → d → e → a → f → c

  • The first code to run is the firmware; the OS is not in memory yet.
  • If Secure Boot is on, the bootloader’s signature is verified in step (e).
  • The kernel starts the first user process (PID 1); all others descend from it.

The End

Chapter 2: Operating-System Structures: Study Questions
Lecture slides: Chapter 2: Operating-System Structures · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.