File descriptors

From Hackepedia
Revision as of 10:33, 5 October 2005 by Pbug (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

A file descriptor is a handle in a program that allows data to be read and written. It is assigned a number starting at 0 and going to the file descriptor limit. A descriptor of -1 indicates an error. When a new process is created (by means of the fork(2) system call) it inherits all open file descriptors from the parent process. New file descriptors are made by the following system calls open(2), pipe(2), and socket(2). A descriptor can be duplicated thus creating a new descriptor and number with the dup(2) system call. A descriptor can be destroyed by means of the close(2) system call. All programs executed from a shell have the following 3 descriptors open bound to the terminal: Stdin, Stdout and Stderr, representing descriptor numbers 0, 1 and 2 respectively. More about descriptors can be found in the fd manpage on the OpenBSD system; "man 4 fd" to see this.