CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 8 of 8 · 3 min read

Exercises

Easy

Easy 1: Describe your machine (20 minutes)

Run uname -sr, nproc and grep MemTotal /proc/meminfo on a Linux machine. Convert the memory to gigabytes.

Done when: three readings and the memory in GB, found by dividing the kB by 1,024 twice.

Hint

One kB in /proc/meminfo is 1,024 bytes.

Easy 2: Count the time (20 minutes)

A program makes 5 million system calls at 90 ns each. Give the time in seconds.

Done when: you write 0.45 seconds.

Hint

5,000,000 × 90 ns is 450 million ns.

Easy 3: Which descriptor (20 minutes)

A process has 0, 1, 2 and 5 open. What number does the next open return? After it closes descriptor 1 and opens another file, what number does it get?

Done when: you write 3, and then 1.

Hint

open returns the smallest unused number.

Medium

Medium 1: Read a process (30 minutes)

Start sleep 300 & in a shell. Find its PID, read its /proc/PID/status and report Name, State, PPid and Threads. Then find the parent and say what it is.

Done when: four values, and the parent identified as your shell.

Hint

Use ps -o ppid= -p PID for the parent.

Medium 2: Measure it yourself (30 minutes)

Compile and run the getppid timing program three times and report the three times per call. Compare with a loop that makes the same number of plain function calls to a function that returns 1.

Done when: three measurements, a plain-call time in a table beside them, and the ratio.

Hint

Use volatile or __attribute__((noinline)) so the compiler keeps the plain calls.

Medium 3: Count descriptors (30 minutes)

Write a C program that opens a file 10 times without closing it, then prints how many entries /proc/self/fd has.

Done when: a program that prints 14 (descriptors 0, 1, 2, the 10 files, and the one used to read the directory, which depends on the method, so explain your count).

Hint

Count directory entries with opendir and readdir, skipping . and ...

Hard

Hard 1: Trace a command (60 minutes)

Run strace -c ls and strace -c cat /etc/hostname. List the five most frequent calls in each. Explain two calls that you did not expect.

Done when: two top-five tables and two explanations from the manual pages.

Hint

Read man 2 mmap or man 2 fstat for calls you do not know.

Hard 2: Find the family tree (60 minutes)

Using ps -eo pid,ppid,comm, draw the tree of processes on your machine from process 1 down to your shell and a command you started.

Done when: a tree with at least four levels, each node with a PID and a name.

Hint

Each row's PPID is the parent's PID.

Hard 3: Write a tiny status tool (60 minutes)

Write a C program that takes a PID, reads /proc/PID/status and prints the name, state, parent and resident memory. Handle a PID that does not exist.

Done when: a working program that prints the four fields and a clear error for a missing PID.

Hint

fopen returns NULL when the directory does not exist, and errno says why.