Exercise 5.2 from The Linux Programming Interface by Michael Kerrisk