CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 7 of 7 · 2 min read

Exercises

Easy

Easy 1: Read a line (20 minutes)

What does read(3, buf, 832) = 832 tell you?

Done when: a read of up to 832 bytes from descriptor 3 returned 832 bytes.

Hint

the number after = is the number of bytes returned.

Easy 2: Error names (20 minutes)

Match the problem to the error: file does not exist, permission refused, too many open files.

Done when: ENOENT, EACCES, EMFILE.

Hint

see section 1.

Easy 3: Slowdown (20 minutes)

A call takes 100 ns normally and 45,000 ns under strace. By what factor is it slower?

Done when: 450.

Hint

divide the two times.

Medium

Medium 1: Trace hello (45 minutes)

Compile and trace the hello program. Count the lines, find the one that failed, and find the write.

Done when: 34 calls, one error (access on ld.so.preload), and the write line.

Hint

strace -o t.txt ./hello; wc -l t.txt gives 35 lines: the 34 calls and the exit line.

Medium 2: Trace a failure (45 minutes)

Run strace -e trace=file cat /root/nothing (or any path that does not exist) and report the call, the path and the error.

Done when: one openat line with ENOENT, and the exit status 1.

Hint

-e trace=file selects calls that take a file name.

Medium 3: Count (45 minutes)

Run strace -c -S calls on two commands of your choice, and report the total calls, the errors and the most frequent call of each.

Done when: two tables summarised, with a comparison of the call mix.

Hint

-S calls sorts by count.

Hard

Hard 1: Find the hidden work (90 minutes)

Trace sh -c 'ls | wc -l' with -f -e trace=execve,pipe2,dup2,clone. List the processes started and explain how the pipe is made.

Done when: the shell, ls and wc, a pipe2 call and dup2 calls that join standard output of one to standard input of the next.

Hint

the pipe is made by the shell before the two clone calls.

Hard 2: Measure the overhead (90 minutes)

Run the loop program without strace, with strace -c, and with strace -e trace=none. Report the three times per call and explain the differences.

Done when: three timings, with the first the smallest, and a note that even filtering out all calls still stops the program at each call.

Hint

-o /dev/null hides the trace without removing its cost.

Hard 3: Why is it slow (90 minutes)

A program copies a file by reading and writing one byte at a time. Predict the call count for a 1 MiB file and the effect of using a 4 KiB buffer, then check with strace -c.

Done when: 1,048,576 reads and writes each for one byte, 256 each for 4 KiB buffers, a ratio of 4,096, and the measured counts.

Hint

1 MiB / 1 byte = 1,048,576; 1 MiB / 4 KiB = 256.