Information Technology Reference
In-Depth Information
existingPath must not be a directory. Why does Linux mandate
this restriction?
Answer: Preventing multiple hard links to a directory prevents cycles, en-
suring that the directory structure is always a directed acyclic
graph (DAG).
Additionally, allowing hard links to a directory would muddle a
directory's parent directory entry (e.g., “ .. ” as discussed in the
sidebar on page 336.)
Open and close. To start accessing a file, a process calls open() to get
a file descriptor it can use to refer to the open file.
File descriptor is Unix
Definition: file descriptor
terminology; in other systems and APIs objects playing similar roles may be
called file handles or file streams.
Definition: file handle
Definition: file stream
Operating systems require processes to explicitly open() files and access
them via file descriptors rather than simply passing the path name to read()
and write() calls for two reasons. First, path parsing and permission checking
can be done just when a file is opened and need not be repeated on each read
or write. Second, when a process opens a file, the operating system creates a
data structure that stores information about the process's open le such as the
le's ID, whether the process can write or just read the le, and a pointer to
the process's current position within the le. The le descriptor can thus be
thought of as a reference to the operating system's per-open-le data structure
that the operating system will use for managing the process's access to the le.
When an application is done using a file, it calls close() , which releases the
open file record in the operating system.
File access. While a le is open, an application can access the le's data in
two ways. First, it can use the traditional procedural interface, making system
calls to read() and write() on an open file. Calls to read() and write()
start from the process's current le position, and they advance the current le
position by the number of bytes successfully read or written. So, a sequence of
read() or write() calls moves sequentially through a file. To support random
access within a file, the seek() call changes a process's current position for a
specified open file.
Rather than using read() and write() to access a le's data, an application
can use mmap() to establish a mapping between a region of the process's virtual
memory and some region of the file. Once a file has been mapped, memory loads
and stores to that virtual memory region will read and write the le's data either
by accessing a shared page from the kernel's le cache, or by triggering a page
fault exception that causes the kernel to fetch the desired page of data from the
Search WWH ::




Custom Search