Open (system call)
From Wikipedia, the free encyclopedia
In most modern operating systems, a program that needs access to a file stored in a filesystem uses the open system call. This system call allocates resources associated to the file (the file descriptor), and returns a handle that the process will use to refer to that file from then on.
After using the file, the process should use the close system call.
This system call usually takes some arguments indicating a file path, the kind of access requested on the file (read, write, append, etc.) and how the file information should be interpreted (text, binary, record-based). In several operating systems this system call can also be used to create a non existent file and returning a file descriptor for it.
[edit] POSIX usage and semantics
The open system call interface is standardized by the POSIX specification. A file is opened or created by calling the open function:
int open (const char *pathname, int oflag, .../*,mode_t mode */);
The value returned is the new file descriptor (in POSIX, file descriptors are non-negative integer values). This integer is usually an index on a table of open files for the process. The operating system keeps independent tables for different processes. The file descriptor contains, among other things, a position pointer that indicates which place of the file will be acted upon by further file operations.
The same filesystem file can be opened simultaneously by several processes, and even by the same process (resulting in several file descriptors for the same file). Operations on the descriptors like moving the file pointer, or closing it are independent (they do not affect other descriptors for the same physical file). However, operations of the physical file (like a write) can be seen by operations on the other descriptors (a posterior read can read the written data)
Alternatively, open can return a negative number indicating that the open operation failed
The pathname argument is the name of the file to open. It is a file path indicating in which place of the file system the file should be found (or created if that is requested)
There are a multiple options for this function, which are specified by the oflag argument.
This argument formed by OR'ing together one or more of the following constants (from <fcntl.h>)
- O_RDONLY
- Open for reading only,
- O_WRONLY
- Open for writing only,
- O_RDWR
- Open for reading and writing,
Optionally, you can also use the following parameters (the following list is not exhaustive, it intends to show what kind of options can be used with open):
- O_APPEND
- When using this flag, all data written will be appended to the end of the file. The file operations will ignore the position pointer inside the file descriptor and always append at the end of the file, even when other processes are also accessing the file
- O_CREAT
- This flag will make open to create the file if it does not exist, or overwrite an existing one.
- O_EXCL
- When this flag is used with O_CREAT, it avoids the overwriting behaviour.
- O_NONBLOCK
- Operations on the file descriptor will return control to the process immediately instead of waiting to finish.
the mode_t argument is optional, and is relevant only when creating a new file. It indicates the file permissions for the newly created file.
[edit] References
- Advanced Programming in the UNIX Environment by W. Richard Stevens ISBN 81-7808-096-6