FSOLUTIONS TO SELECTED EXERCISES

Chapter 5

5-3.     A solution is provided in the file fileio/atomic_append.c in the source code distribution for this book. Here is an example of the results that we see when we run this program as suggested:

$ ls -l f1 f2-rw-------    1 mtk      users      2000000 Jan  9 11:14 f1-rw-------    1 mtk      users      1999962 Jan  9 11:14 f2

Because the combination of lseek() plus write() is not atomic, one instance of the program sometimes overwrote bytes written by the other instance. As a result, the file f2 contains less than 2 million bytes.

5-4.     A call to dup() can be rewritten as:

fd = fcntl(oldfd, F_DUPFD, 0);

A call to dup2() can be rewritten as:

if (oldfd == newfd) {                /* oldfd == newfd ...

Get The Linux Programming Interface now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.