Tuesday, August 01, 2006

Understand Linux Kernel---My Notes

File-Handling System Calls

When a user accesses the contents of either a regular file or a directory, he actually accesses some data stored in a hardware block device. In this sense, a file system is a user-level view of the physical organization of a hard disk partition. Since a process in User Mode cannot directly interact with the low-level hardware components, each actual file operation must be performed in Kernel Mode.

Therefore, the UNIX operating system defines several system calls related to file handling.

Whenever a process wants to perform some operation on a specific file, it uses the proper

System calls and passes the file pathname as a parameter.

All UNIX kernels devote great attention to the efficient handling of hardware block devices in order to achieve good overall system performance. In the chapters that follow, we will describe topics related to file handling in Linux and specifically how the kernel reacts to file related system calls. In order to understand those descriptions, you will need to know how the main file-handling system calls are used; they are described in the next section.

Opening a file

Processes can access only "opened" files. In order to open a file, the process invokes the

system call:

fd = open (path, flag, mode)

The three parameters have the following meanings:

path

Denotes the pathname (relative or absolute) of the file to be opened.

flag

Specifies how the file must be opened (e.g., read, write, read/write, append). It can

also specify whether a nonexistent file should be created.

mode

Specifies the access rights of a newly created file.

This system call creates an "open file" object and returns an identifier called file descriptor.

An open file object contains:

Some file-handling data structures, like a pointer to the kernel buffer memory area

where file data will be copied; an offset field that denotes the current position in the

file from which the next operation will take place (the so-called file pointer); and so

on.

Some pointers to kernel functions that the process is enabled to invoke. The set of

permitted functions depend on the value of the flag parameter.

Let's limit ourselves here to describing

some general properties specified by the POSIX semantics:

A file descriptor represents an interaction between a process and an opened file, while

an open file object contains data related to that interaction. The same open file object

may be identified by several file descriptors.

Several processes may concurrently open the same file. In this case, the file system

assigns a separate file descriptor to each file, along with a separate open file object.

When this occurs, the Unix file system does not provide any kind of synchronization

among the I/O operations issued by the processes on the same file. However, several

system calls such as flock ( ) are available to allow processes to synchronize

them selves on the entire file or on portions of it (see Chapter 12).

In order to create a new file, the process may also invoke the create ( ) system call, which is

handled by the kernel exactly like open ( ).

Monday, July 10, 2006

Real-Time Tux