Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

8 UNIX System Overview Chapter 1


Working Directory


Every process has aworking directory,sometimes called thecurrent working directory.
This is the directory from which all relative pathnames areinterpreted. A process can
change its working directory with thechdirfunction.
For example, the relative pathnamedoc/memo/joerefers to the file or directory
joe, in the directorymemo, in the directorydoc,which must be a directory within the
working directory.Fromlooking just at this pathname, we know that bothdocand
memohave to be directories, but we can’t tell whetherjoeis a file or a directory.The
pathname/usr/lib/lintis an absolute pathname that refers to the file or directory
lintin the directorylib, in the directoryusr,which is in the root directory.

Home Directory


When we log in, the working directory is set to ourhome directory.Our home directory
is obtained from our entry in the passwordfile (Section 1.3).

1.5 Input and Output


File Descriptors


File descriptors arenormally small non-negative integers that the kernel uses to identify
the files accessed by a process. Whenever it opens an existing file or creates a new file,
the kernel returns a file descriptor that we use when we want to read or write the file.

StandardInput, StandardOutput, and StandardError


By convention, all shells open three descriptors whenever a new program is run:
standardinput, standardoutput, and standarderror.Ifnothing special is done, as in
the simple command
ls
then all three areconnected to the terminal. Most shells provide a way to redirect any
or all of these three descriptors to any file. For example,
ls > file.list
executes the ls command with its standardoutput redirected to the file named
file.list.

Unbuffered I/O


Unbuffered I/O is provided by the functionsopen,read,write,lseek,andclose.
These functions all work with file descriptors.

Example


If we’rewilling to read from the standardinput and write to the standardoutput, then
the program in Figure1.4 copies any regular file on a UNIX system.
Free download pdf