Tasks
The same steps as the lab slide. Do them on a Linux PC with the files from the zip; the tools below help you check your thinking first.
Lab 7.1 · The Linux Kernel
- TODO 1: first_line
fopen, fgets, fclose, cut the '\n'. Kernel: and Command line: appear. - TODO 2: field_kb
Find "MemTotal:" in /proc/meminfo and read the number with sscanf. - TODO 3: read_stat
After the last ')' in /proc/<pid>/stat: state, ppid … flags. One sscanf. - TODO 4: classify
flags & PF_KTHREAD → kernel thread. Compare your counts with ps -ef. - Explore the lab PC
uname -r; ls /boot; lsmod | wc -l; sudo dmesg | head; ulimit -s. Fill the table in the handout. - Think
Why is cat /proc/uptime different every time, though no file changed? Write 3 sentences.
Submit: kinfo.c, its output on the lab PC, and the filled table. Keep kinfo: Lab 7.2 runs it as a child of your own init.
Lab 7.2 · Booting Linux
- Get a kernel
sudo cp /boot/vmlinuz-$(uname -r) vmlinuz && sudo chmod a+r vmlinuz: the lab PC's own kernel. - Boot the starter
gcc -Wall -static -o init myinit_starter.c, ./mkinitramfs.sh, ./boot.sh. Read the panic: why? - TODO 1: mount
Mount /proc and /sys. Now the kernel version and command line appear. - TODO 2: fork + exec
Build kinfo with -static, run it as a child of your init, print its exit status. - TODO 3: never exit
sync() and reboot(RB_POWER_OFF): QEMU powers off cleanly, no panic. - Experiments
EXTRA=rdinit=/bin/kinfo ./boot.sh; EXTRA=nokaslr; boot with -m 128. Note what changes.
Quit a stuck QEMU with Ctrl-A then X. Submit: myinit.c and the serial output of your final boot.
Try it here
These run in your browser, with no server and no internet. They are built from the same reference programs as the lab, so their output matches what your finished C program should print. They do not replace the real lab: the kernel, gcc and your own code are the point.
Read the files
Every text file from lab07_files.zip. Read here, then download the zip to work on them.