Page 239 - DCAP103_Principle of operating system
P. 239
Principles of Operating Systems
Notes The first path instructs the system to go upward (to the usr directory), then to go down to the
directory lib/ to find the file dictionary.
The second argument (dot) names the current directory. When the cp command gets a directory
name (including dot) as its last argument, it copies all the files there. Of course, a more normal
way to do the copy would be to type cp /usr/lib/dictionary. Here the use of dot saves the user
the trouble of typing dictionary a second time.
Nevertheless, typing cp /usr/lib/dictionary dictionary also works fine, as does cp /usr/lib/
dictionary /usr/ast/dictionary. All of these do exactly the same thing.
Some programs need to access a specific file without regard to what the
working directory is. In that case, they should always use absolute path names.
For example, a spelling checker might need to read /usr/lib/dictionary to
do its work.
6.12.4 Directory Operations
The system calls for managing directories exhibit more variation from system to system than
system calls for files. To give an impression of what they are and how they work, we will give
a sample (taken from UNIX).
1. Create. A directory is created. It is empty except for dot and dotdot, which are put there
automatically by the system (or in a few cases, by the mkdir program).
2. Delete. A directory is deleted. Only an empty directory can be deleted. A directory
containing only dot and dotdot is considered empty as these cannot usually be deleted.
3. Opendir. Directories can be read. For example, to list all the files in a directory, a listing
program opens the directory to read out the names of all the files it contains. Before a
directory can be read, it must be opened, analogous to opening and reading a file.
4. Closedir. When a directory has been read, it should be closed to free up internal table
space.
5. Readdir. This call returns the next entry in an open directory. Formerly, it was possible to
read directories using the usual read system call, but that approach has the disadvantage
of forcing the programmer to know and deal with the internal structure of directories. In
contrast, readdir always returns one entry in a standard format, no matter which of the
possible directory structures is being used.
6. Rename. In many respects, directories are just like files and can be renamed the same way
files can be.
7. Link. Linking is a technique that allows a file to appear in more than one directory. This
system call specifies an existing file and a path name, and creates a link from the existing
file to the name specified by the path. In this way, the same file may appear in multiple
directories. A link of this kind, which increments the counter in the file’s i-node (to keep
track of the number of directory entries containing the file), is sometimes called a hard
link.
8. Unlink. A directory entry is removed. If the file being unlinked is only present in one
directory (the normal case), it is removed from the file system. If it is present in multiple
directories, only the path name specified is removed. The others remain. In UNIX, the
system call for deleting files (discussed earlier) is, in fact, unlink.
The above list gives the most important calls, but there are a few others as well, for example,
for managing the protection information associated with a directory.
232 LOVELY PROFESSIONAL UNIVERSITY