Some code from semaphores book