Chapter 15: Linux
Multiple Choice · History
Question 1: How Was Linux Born?
Which statement is true about the origin of the Linux kernel?
B: Linux contains no UNIX code; it reimplements the POSIX interface. In 1991 Torvalds developed his own monolithic kernel on MINIX and combined it with GNU’s compiler, shell and tools; this is why the name “GNU/Linux” is also used for the full system.
Output Question · Pipelines
Question 2: Output of a Pipeline
The commands are run in order. What do the second and third commands print? Why must uniq -c be preceded by sort?
$ printf "net\ndisk\nnet\ncpu\ndisk\nnet\n" > log.txt $ sort log.txt | uniq -c | sort -rn | head -2 $ uniq -c log.txt | head -3
Show answer
3 net
2 disk 1 net
1 disk
1 netuniq -c counts only consecutive identical lines. sort brings identical lines together: 1 cpu, 2 disk, 3 net; sort -rn sorts by number from largest to smallest, head -2 takes the first two lines. In an unsorted file each line counts as a separate group. For the pipeline in the second command the shell sets up 4 processes and 3 pipes; sort cannot produce output before it sees the end of its input (EOF).
Multiple Choice · Redirection
Question 3: The Order of Redirections
/nonexistent does not exist. In which command does the error message of ls get written to the screen instead of going to the file?
D: redirections are applied from left to right. 2>&1 first makes stderr point to stdout’s current target (the terminal); only then is stdout redirected to the file. In A, stdout goes to the file first and stderr is then mapped to it; B sends both there, C sends only stderr to the file.
Classic Question · pipe and dup2
Question 4: Closing the Pipe Ends
The shell sets up the line ls | wc -l with pipe, fork, dup2 and exec. Write the steps in order. What happens if the shell forgets to close its own write end?
Show answer
pipe(fd): lowest free numbers, read end 3, write end 4.- 1st child:
dup2(4, 1), closes the extra ends,exec("ls"). ls writes into the pipe without knowing anything about it. - 2nd child:
dup2(3, 0), closes the extra ends,exec("wc"). - The shell closes both ends and waits for the two children with
wait. - As long as the write end stays open in any process, wc does not see EOF: even after ls finishes, wc waits forever, and the shell waits for it.
Multiple Choice · Loadable Modules
Question 5: Loading a Module
Which statement is true about loadable kernel modules (.ko) in Linux?
C: modprobe reads /lib/modules/$(uname -r)/modules.dep; insmod does not. A module runs with kernel privilege in the kernel’s address space: its bug can crash the whole system. Loading needs CAP_SYS_MODULE; with Secure Boot on, the signature is also verified.
Multiple Choice · Signals
Question 6: The Signal That Cannot Be Caught
With sigaction, for which signal can a process neither install a handler nor ignore it?
D: SIGKILL and SIGSTOP cannot be caught, blocked or ignored, so that the administrator can always stop a process. kill PID sends SIGTERM (15) by default: the process can clean up its resources and exit politely. SIGTERM is tried first, SIGKILL only if necessary.
Classic Question · EEVDF
Question 7: nice and Virtual Runtime
In the fair class the weights are: nice 0 → 1024, nice 5 → 335, nice −5 → 3121. The virtual-runtime increase is Δv = real time × 1024 / weight. (a) If each task runs for 6 ms, how far does its vruntime advance? (b) In what ratio do a continuously ready nice 0 task and a nice 5 task share a single CPU?
Show answer
(a) nice 0: 6 × 1024 / 1024 = 6 ms; nice 5: 6 × 1024 / 335 ≈ 18.34 ms; nice −5: 6 × 1024 / 3121 ≈ 1.97 ms.
(b) The share is proportional to the weight: 1024 / (1024 + 335) ≈ 75.3% and 335 / 1359 ≈ 24.7%.
The vruntime of the low-weight task advances quickly, so it is picked less often. EEVDF additionally picks, among only the eligible (v ≤ V) tasks, the one with the earliest virtual deadline.
Classic Question · Page-Table Walk
Question 8: Page-Table Indices
On x86-64 there are 4 KiB pages and a 4-level page table; each table has 512 entries. For the virtual address 0x00007F3A5C1E2468 what are the PGD, PUD, PMD and PTE indices and the offset within the page? If the PTE frame number is 0x1A2B3, what is the physical address? How many extra memory reads does a TLB miss need?
Show answer
The offset is 12 bits, each index 9 bits: 47–39 PGD, 38–30 PUD, 29–21 PMD, 20–12 PTE, 11–0 offset.
| PGD | PUD | PMD | PTE | offset |
|---|---|---|---|---|
| 254 | 233 | 224 | 482 | 0x468 |
For example PGD = address >> 39 = 0xFE = 254. Physical address = 0x1A2B3 × 4096 + 0x468 = 0x1A2B3468.
A TLB miss needs 4 table reads (5 with 5-level paging). Huge pages (2 MiB, 1 GiB) shorten the walk.
Multiple Choice · Device Files
Question 9: Major and Minor
ls -l /dev output contains the line brw-rw---- 1 root disk 8, 1 sda1. Which statement is true?
B: the leading b means a block device (random access, page cache and I/O scheduler), c a character device. Instead of the size column “major, minor” appears: major selects the driver, minor the unit that driver manages (here partition 1 of sda). Network interfaces are not in /dev.
Multiple Choice · ext4
Question 10: ext4 Journaling Mode
An ext4 partition is mounted with the default data=ordered mode. A program is appending new blocks to the end of a file, and the power fails before fsync() is called. After remounting, which is guaranteed?
B: in ordered mode only metadata is written to the journal, but the data blocks are written to their final place before the related transaction commits. Since fsync was not called, the transaction may not have been committed yet (ext4 commits about every 5 s by default); then the append is lost entirely, but the file never shows garbage blocks. The mode that journals data as well is data=journal (A); the mode with no ordering guarantee is data=writeback (D); since the journal is replayed at mount, a full fsck is not needed (C).
Output Question · Links
Question 11: Hard and Symbolic Links
The commands are run in order in an empty directory. What does each command print?
$ echo data > a.txt $ ln a.txt b.txt # hard link $ ln -s a.txt c.txt # symbolic link $ stat -c %h a.txt # link count $ rm a.txt $ cat c.txt $ echo new > a.txt $ cat b.txt; cat c.txt $ stat -c %h b.txt
Show answer
2 cat: c.txt: No such file or directory data new 1
- A hard link is a second name for the same i-node: link count 2.
rmdeletes only the name, the count drops to 1, the data stays. - A symbolic link stores the path of its target: when a.txt is deleted the link dangles.
- The new a.txt is a new i-node: c.txt reaches it through the path (“new”), while b.txt keeps pointing to the old i-node (“data”).
Classic Question · Permission Arithmetic
Question 12: umask and chmod
umask 033: what are the permissions of (a) the file created by touch notes.txt and (b) the directory created by mkdir archive? (c) Why is the calculation “666 − 033 = 633” wrong? (d) What happens when chmod u+x,g+w,o+r is applied to a file whose permissions are 640?
Show answer
A new file requests 666, a new directory 777; the bits in the umask are cleared: requested & ~umask.
(a) 666 & ~033 = 644 → rw-r--r--. (b) 777 & ~033 = 744 → rwxr--r--: group and others can list the directory but cannot enter it.
(c) umask clears bits, it does not subtract. The x bits of 033 are not in a file anyway; subtraction gives 633 (rw--wx-wx) and turns on the wrong bits.
(d) Symbolic mode adds to the existing bits: rw- + x = rwx (7), r-- + w = rw- (6), --- + r = r-- (4) → 764, rwxrw-r--.
Multiple Choice · Access Control
Question 13: The Order of Permission Checking
project.txt has the permissions ----rwxr-- (0074), owner ali, group dev; ali is also a member of the dev group. ali, veli (a dev member) and ayse (in another group) try to read the file. Who can read it?
A: the kernel looks at the first matching class. If the euid equals the owner, only the owner bits (---) are used and group membership is never examined: ali cannot read. veli gets the group bits (rwx), ayse the other bits (r--); both can read. The owner can give himself the permission back with chmod.
Multiple Choice · Containers
Question 14: Namespace or cgroup?
Which Linux features make a container (a) see only its own processes and have PID 1 inside, and (b) use at most 512 MB of memory?
B: namespaces determine what can be seen (PID, mount, network, hostname, user mapping), cgroups how much can be used (cpu.max, memory.max, pids.max). seccomp restricts system calls, SELinux/AppArmor restricts access. PID 1 inside is an ordinary PID on the host.
Multiple Choice · Android
Question 15: The Android Sandbox
Which is the key mechanism that separates an Android app’s data from other apps?
C: zygote forks, and the child switches to the app’s unique uid, SELinux context and seccomp filter; file permissions are checked against this uid. Binder passes the caller’s uid to the service so that the service can check permissions. Android has no glibc; its C library is Bionic.