Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

908 Solutions to Selected Exercises Appendix C


process table entry

...


fd1:
fd2:
fd3:

flagsfd pointerfile

file status flags
current file offset
v-node pointer

file status flags
current file offset
v-node pointer

file table

v-node information
v_data

i-node information
current file size
i_vnode

v-node table

Figure C.2 Result ofdupandopen

Iffdis 3, however,after the three calls todup2,four descriptors arepointing to
the same file table entry.Inthis case, we need to close descriptor 3.

3.5 Since the shells process their command line from left to right, the command
./a.out > outfile 2>&1
first sets standardoutput tooutfile and then dupsstandardoutput onto
descriptor 2 (standarderror). Theresult is that standardoutput and standard
error areset to the same file. Descriptors 1 and 2 both point to the same file table
entry.With
./a.out 2>&1 > outfile
however,the dupis executed first, causing descriptor 2 to be the terminal
(assuming that the command is run interactively). Then standardoutput is
redirected to the fileoutfile.The result is that descriptor 1 points to the file
table entry foroutfile,and descriptor 2 points to the file table entry for the
terminal.
3.6 Youcan stilllseekandreadanywhere in the file, but awriteautomatically
resets the file offset to the end of file beforethe data is written. This makes it
impossible towriteanywhereother than at the end of file.

Chapter 4


4.1 Ifstatis called, it always tries to follow a symbolic link (Figure4.17), so the
program will never print a file type of ‘‘symbolic link.’’For the example shown in
the text, where/dev/cdromis a symbolic link to/dev/sr0,statreports that
/dev/cdromis a block special file, not a symbolic link. If the symbolic link
points to a nonexistent file,statreturns an error.
4.2 All permissions areturned off:
Free download pdf