CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 6 of 6 · 3 min read

Exercises

Easy

Easy 1: Shared or private (20 minutes)

For each item, write shared or private: a global array, a local variable, the heap, the registers, an open file, the program counter.

Done when: shared, private, shared, private, shared, private.

Hint

Anything on the stack or in registers belongs to one thread.

Easy 2: Count the threads (20 minutes)

A program starts with one thread, creates 4 more, and 2 of them end. How many threads does the process have, and what would Threads: show?

Done when: you write 3.

Hint

1 + 4 − 2.

Easy 3: Pick process or thread (20 minutes)

Choose process or thread for each need: a browser tab that must not crash the others; a server cache shared by all request handlers; two programs from different users.

Done when: process, thread, process, each with one reason.

Hint

Think about who must be protected from whom.

Medium

Medium 1: Run the comparison (30 minutes)

Compile and run the counter program. Then change it so the thread and the child both add 1 to counter 1,000 times, and print the result after each.

Done when: the child's changes do not appear (0) and the thread's do (1000), with a sentence explaining why.

Hint

Use waitpid for the child and pthread_join for the thread.

Medium 2: Time creation (30 minutes)

Run the timing program three times. Report the three ratios and their average. Change N to 20,000 and say whether the ratio changes.

Done when: a table of ratios and a sentence saying the ratio stays about the same.

Hint

Compile with -O2 for steadier numbers.

Medium 3: List the threads (30 minutes)

Write a program that creates 4 threads that each sleep 5 seconds. While it runs, list its threads with ps -L -p PID and read Threads: from /proc/PID/status.

Done when: five rows in ps -L, matching Threads: 5.

Hint

ps -L lists one row per thread.

Hard

Hard 1: Stack addresses (60 minutes)

Start three threads at the same time (join them all at the end) and print the address of a local variable in each. Compute the difference between neighbouring addresses and compare it with the default stack size shown by ulimit -s.

Done when: three addresses, two differences near 8 MB or the system's thread stack size, and a sentence on why they are far apart.

Hint

Use uintptr_t to subtract addresses.

Hard 2: Concurrent but not parallel (60 minutes)

Run two threads that each spin for 2 seconds, once restricted to one processor with taskset -c 0 and once with two processors. Compare the elapsed times.

Done when: about 4 seconds on one processor and about 2 on two, with a note on concurrency versus parallelism.

Hint

Use taskset -c 0 ./prog for one processor.

Hard 3: When a thread crashes (60 minutes)

Write a program with two threads where one divides by zero and the other prints a message every 200 ms. Show that the process ends and that the printing thread stops. Then write the same with two processes and show that one survives.

Done when: two outputs: all work stops in the thread version, and the other process continues in the process version.

Hint

Division by zero in integer arithmetic raises SIGFPE.