Exercises

team bbl


3.1

When reading or writing a disk file, are the functions described in this chapter really unbuffered? Explain.

3.2

Write your own dup2 function that performs the same service as the dup2 function described in Section 3.12, without calling the fcntl function. Be sure to handle errors correctly.

3.3

Assume that a process executes the following three function calls:

     fd1 = open(pathname, oflags);     fd2 = dup(fd1);     fd3 = open(pathname, oflags); 

Draw the resulting picture, similar to Figure 3.8. Which descriptors are affected by an fcntl on fd1 with a command of F_SETFD? Which descriptors are affected by an fcntl on fd1 with a command of F_SETFL?

3.4

The following sequence of code has been observed in various programs:

       dup2(fd, 0);       dup2(fd, 1);       dup2(fd, 2);       if (fd > 2)           close(fd); 

To see why the if test is needed, assume that fd is 1 and draw a picture of what happens to the three descriptor entries and the corresponding file table entry with each call to dup2. Then assume that fd is 3 and draw the same picture.

3.5

The Bourne shell, Bourne-again shell, and Korn shell notation

      digit1>&digit2 

says to redirect descriptor digit1 to the same file as descriptor digit2. What is the difference between the two commands

      ./a.out > outfile 2>&1      ./a.out 2>&1 > outfile 

(Hint: the shells process their command lines from left to right.)

3.6

If you open a file for readwrite with the append flag, can you still read from anywhere in the file using lseek? Can you use lseek to replace existing data in the file? Write a program to verify this.

    team bbl



    Advanced Programming in the UNIX Environment
    Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
    ISBN: 0321525949
    EAN: 2147483647
    Year: 2005
    Pages: 370

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net