File descriptor

File descriptor

In computer programming, a file descriptor is an abstract key for accessing a file. The term is generally used in POSIX operating systems. In Microsoft Windows terminology and in the context of the C standard I/O library, "file handle" is preferred, though the latter case is technically a different object (see below).

In POSIX, a file descriptor is an integer, specifically of the C type int. There are 3 standard POSIX file descriptors which presumably every process (save perhaps a daemon) should expect to have:

Generally, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files. In POSIX this data structure is called a file descriptor table, and each process has its own file descriptor table. The user application passes the abstract key to the kernel through a system call, and the kernel will access the file on behalf of the application, based on the key. The application itself cannot read or write the file descriptor table directly.

In Unix-like systems, file descriptors can refer to files, directories, block or character devices (also called "special files"), sockets, FIFOs (also called named pipes), or unnamed pipes.

The FILE * file handle in the C standard I/O library routines is technically a pointer to a data structure managed by those library routines; one of those structures usually includes an actual low level file descriptor for the object in question on Unix-like systems. Since "file handle" refers to this additional layer, it is not interchangeable with "file descriptor".

To further complicate terminology, Microsoft Windows also uses the term "file handle" to refer to the more low-level construct, akin to POSIX's file descriptors. Microsoft's C libraries also provide compatibility functions which "wrap" these native handles to support the POSIX-like convention of integer file descriptors as detailed above.

Operations on file descriptors

A modern Unix typically provides the following operations on file descriptor

Creating file descriptors

* open(), open64(), creat(), creat64()
* socket()
* socketpair()
* pipe()

Deriving file descriptors

* dirfd()
* fileno()

Operations on a single file descriptor

* read(), write()
* readv(), writev()
* pread(), pwrite()
* recv(), send()
* recvmsg(), sendmsg() (inc. allowing sending FDs)
* sendfile()
* lseek(), lseek64()
* fstat(), fstat64()
* fchmod()
* fchown()
* fdopen()
* gzdopen()
* ftruncate()
* fsync()
* fdatasync()
* fstatvfs()

Operations on multiple file descriptors

* select(), pselect()
* poll(), epoll()

Operations on the file descriptor table

* close()
* dup(): duplicates an existing file descriptor guaranteeing to be the lowest number available file descriptor
* dup2(): the new file descriptor will have the value passed as an argument
* fcntl (F_DUPFD)
* fcntl (F_GETFD and F_SETFD)

Operations that modify process state

* fchdir(): sets the process's current working directory based on a directory file descriptor
* mmap(): maps ranges of a file into the process's address space

File locking

* flock()
* fcntl (F_GETLK, F_SETLK and F_SETLKW)
* lockf()

ockets

* connect()
* bind()
* listen()
* accept(): creates a new file descriptor for an incoming connection
* getsockname()
* getpeername()
* getsockopt(), setsockopt()
* shutdown(): shuts down one or both halves of a full duplex connection

Miscellaneous

* ioctl(): a large collection of miscellaneous operations on a single file descriptor, often associated with a device

Upcoming operations

A series of new operations on file descriptors has been added to Solaris and Linux, as well as numerous C libraries, to be standardized in a future version of POSIX. [cite book
title = Extended API Set, Part 2
publisher = The Open Group
date = Oct 2006
url = http://www.opengroup.org/bookstore/catalog/c063.htm
isbn = 1931624674
] The at suffix signifies that the function takes an additional first argument supplying a file descriptor from which relative paths are resolved, the forms lacking the at suffix thus becoming equivalent to passing a file descriptor corresponding to the current working directory.

* openat()
* faccessat()
* fchmodat()
* fchownat()
* fstatat()
* futimesat()
* linkat()
* mkdirat()
* mknodat()
* readlinkat()
* renameat()
* symlinkat()
* unlinkat()
* mkfifoat()
* fdopendir()

The Native API of the Windows NT family of operating systems also allows callers to specify a root directory when they open a file or other object by name. [ [http://msdn2.microsoft.com/library/ms804358.aspx ZwCreateFile] in MSDN Library]

File descriptors as capabilities

Unix file descriptors are capabilities. They can be passed between processes across Unix domain sockets using the sendmsg() system call.

A Unix process' file descriptor table is an example of a C-list.

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • file descriptor — atverto failo numeris statusas T sritis informatika apibrėžtis Numeris, priskiriamas failui, kai jis atveriamas. Priklauso nuo įtaiso, su kuriuo susiejamas failas, pavyzdžiui, Linukso sistemoje įvedimo failui priskiriamas 0, o išvedimo – 1.… …   Enciklopedinis kompiuterijos žodynas

  • Binary File Descriptor library — The BFD, or Binary File Descriptor library, is the GNU Project s main mechanism for the portable manipulation of object files in a variety of formats. As of 2003, it supports approximately 50 file formats for some 25 processor architectures.BFD… …   Wikipedia

  • File locking — is a mechanism that enforces access to a computer file by only one user or process at any specific time. The purpose of locking is to prevent the classic interceding update scenario. The interceding update problem may be illustrated as in the… …   Wikipedia

  • Descriptor — may refer to file descriptor, an abstract key for accessing a file index term, also known as a descriptor in information retrieval molecular descriptor, which helps characterize a chemical compound segment descriptor, used for memory addressing… …   Wikipedia

  • file handle — atverto failo numeris statusas T sritis informatika apibrėžtis Numeris, priskiriamas failui, kai jis atveriamas. Priklauso nuo įtaiso, su kuriuo susiejamas failas, pavyzdžiui, Linukso sistemoje įvedimo failui priskiriamas 0, o išvedimo – 1.… …   Enciklopedinis kompiuterijos žodynas

  • file number — atverto failo numeris statusas T sritis informatika apibrėžtis Numeris, priskiriamas failui, kai jis atveriamas. Priklauso nuo įtaiso, su kuriuo susiejamas failas, pavyzdžiui, Linukso sistemoje įvedimo failui priskiriamas 0, o išvedimo – 1.… …   Enciklopedinis kompiuterijos žodynas

  • descriptor — [di skrip′tər] n. 1. a word or phrase used as a label to describe or classify 2. Comput. a term used to identify or locate a file or specific data …   English World dictionary

  • File Allocation Table — For other uses, see Fat (disambiguation). FAT Developer Microsoft Full Name File Allocation Table FAT12 (12‑bit version) FAT16/FAT16B (16‑bit versions) FAT32 (32‑bit version with 28 bits used) Introduced …   Wikipedia

  • File Allocation Table — Das File Allocation Table (vom Englischen etwa für „Dateizuordnungstabelle“; Abkürzung FAT, englische Aussprache [fæt]) ist ein von Microsoft entwickeltes Dateisystem. Inhaltsverzeichnis 1 Hintergrund 2 Aufbau 2.1 Bootsektor …   Deutsch Wikipedia

  • Memory-mapped file — A memory mapped file is a segment of virtual memory which has been assigned a direct byte for byte correlation with some portion of a file or file like resource. This resource is typically a file that is physically present on disk, but can also… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”