The Linux Programming Interface

(nextflipdebug5) #1
Solutions to Selected Exercises 1429

since it is interpreted relative to the location of the link file, and thus refers to a
nonexistent file in the parent directory. Consequently, chmod() fails with the error
ENOENT (“No such file or directory”). (A complete program containing the code of
this exercise is available in the file dirs_links/bad_symlink.c in the source code
distribution for this book.)
18-4. A solution is provided in the file dirs_links/list_files_readdir_r.c, which can be
found in the source code distribution for this book.
18-7. A solution is provided in the file dirs_links/file_type_stats.c, which can be found
in the source code distribution for this book.
18-9. Using fchdir() is more efficient. If we are performing the operation repeatedly
within a loop, then with fchdir() we can perform one call to open() before executing
the loop, and with chdir() we can place the getcwd() call outside the loop. Then we
are measuring the difference between repeated calls to fchdir(fd) and chdir(buf).
Calls to chdir() are more expensive for two reasons: passing the buf argument to the
kernel requires a larger data transfer between user space and kernel space, and the
pathname in buf must be resolved to the corresponding directory i-node on each
call. (Kernel caching of directory entry information reduces the work required for
the second point, but some work must still be done.)

Chapter 20


20-2. A solution is provided in the file signals/ignore_pending_sig.c in the source code
distribution for this book.
20-4. A solution is provided in the file signals/siginterrupt.c in the source code
distribution for this book.

Chapter 22


22-2. As with most UNIX implementations, Linux delivers standard signals before
realtime signals (SUSv3 doesn’t require this). This makes sense, because some
standard signals indicate critical conditions (e.g., hardware exceptions) that a
program should deal with as soon as possible.
22-3. Replacing sigsuspend() plus a signal handler with sigwaitinfo() in this program
provides a 25 to 40 percent speed improvement. (The exact figure varies somewhat
across kernel versions.)

Chapter 23


23-2. A modified program using clock_nanosleep() is provided in the file timers/
t_clock_nanosleep.c in the source code distribution for this book.
23-3. A solution is provided in the file timers/ptmr_null_evp.c in the source code
distribution for this book.

Chapter 24


24-1. The first fork() call creates one new child. Both parent and child carry on to execute
the second fork(), and thus each creates a further process, making a total of four
Free download pdf