Exercise 4.2 from The Linux Programming Interface by Michael Kerrisk