Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Section F.1. 

F.1.

F.1.1.

F.1.1.1. Chapter 5
  1. 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.

  2. 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 is a special case */
        if (fcntl(oldfd, F_GETFL) == -1) {                /* Is oldfd valid? */
            errno = EBADF;
            fd = -1;
        } else {
            fd = oldfd;
        }
    } else {
        close(newfd);
        fd = fcntl(oldfd, F_DUPFD, newfd);
    }

  3. The first point to realize is that, since fd2 is a duplicate of fd1, they both share a single open file description, and thus a single file offset. However, because fd3 is created via a separate call to open(), it has a separate file offset.

    • After the first write(), the file contents are Hello,.

    • Since fd2 shares a file offset with fd1, the second write() call appends to the existing text, yielding Hello, world.

    • The lseek() call adjusts the single file offset shared by fd1 and fd2 to point to the start of the file, and thus the third write() call overwrites part of the existing text to yield HELLO, world.

    • The file offset for fd3 has not so far been modified, and so points to the start of the file. Therefore, the final write() call changes the file contents to Gidday world.

    Run the program fileio/multi_descriptors.c in the source code distribution for this book to see these results.

F.1.1.2. Chapter 6
  1. Since the array mbuf is not initialized, it is part of the uninitialized data segment. Therefore, no disk space is required to hold this variable. Instead, it is allocated (and initialized to 0) when the program is loaded.

  2. A demonstration of the incorrect usage of longjmp() is provided in the file proc/bad_longjmp.c in the source code distribution for this book.

  3. Sample implementations of setenv() and unsetenv() are provided in the file proc/setenv.c in the source code distribution for this book.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial