CSE 4105 System Programming · Lab 7

The Linux Kernel · Booting Linux

kinfo (ask the kernel through /proc); myinit, your own PID 1 booted in QEMU.

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

  1. TODO 1: first_line
    fopen, fgets, fclose, cut the '\n'. Kernel: and Command line: appear.
  2. TODO 2: field_kb
    Find "MemTotal:" in /proc/meminfo and read the number with sscanf.
  3. TODO 3: read_stat
    After the last ')' in /proc/<pid>/stat: state, ppid … flags. One sscanf.
  4. TODO 4: classify
    flags & PF_KTHREAD → kernel thread. Compare your counts with ps -ef.
  5. Explore the lab PC
    uname -r; ls /boot; lsmod | wc -l; sudo dmesg | head; ulimit -s. Fill the table in the handout.
  6. 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

  1. Get a kernel
    sudo cp /boot/vmlinuz-$(uname -r) vmlinuz && sudo chmod a+r vmlinuz: the lab PC's own kernel.
  2. Boot the starter
    gcc -Wall -static -o init myinit_starter.c, ./mkinitramfs.sh, ./boot.sh. Read the panic: why?
  3. TODO 1: mount
    Mount /proc and /sys. Now the kernel version and command line appear.
  4. TODO 2: fork + exec
    Build kinfo with -static, run it as a child of your init, print its exit status.
  5. TODO 3: never exit
    sync() and reboot(RB_POWER_OFF): QEMU powers off cleanly, no panic.
  6. 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.

    ← Lab 6All lecturesLab 8 →