File Descriptors: Basics#
File Descriptors#
Universal handle for everything that’s got to do with I/O. You’d be surprised what fits under the “I/O” hat (signals, for example)
Not only files, but any kind of IO
Pipes
Sockets
FIFOs
Terminals
Device Special Files ⟶ entry points into kernel space
Type:
int
Standard Filedescriptors#
File descriptors 0, 1, 2 are pre-allocated
Refer to terminal (“console”)
Number |
POSIX Macro |
|
---|---|---|
0 |
|
|
1 |
|
|
2 |
|
|
Interactive shell: all three associated with terminal
Standard input and output used for I/O redirection and pipes
Standard error receives errors, warnings, and debug output
File Descriptor Lifecycle - open/read/write/close
#
Creation: depends on type of medium
Usage
read()
andwrite()
for typical IO scenariosioctl()
for non-IO operations…
Cleanup:
close()
must be called on all file descriptors to free in-kernel memory