The Linux Programming Interface

(nextflipdebug5) #1

SOLUTIONS 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);
Free download pdf