Unix I/O Cheatsheet
Overview
- In Unix, everything is a file (devices, kernel, etc.)
- Uniform interface for I/O: open, close, read, write
File Types
- Regular files: Arbitrary data (text or binary)
- Directories: Index for a group of files
- Sockets: For inter-process communication
Directory Hierarchy
- Root directory: "/"
- Absolute pathnames: Start with "/", path from root
- Relative pathnames: Path from current working directory
File Operations
open: Informs kernel you're ready to access a file
- Returns file descriptor (int), -1 on error
close: Informs kernel you're finished accessing a file
- Important to check return value, especially in threaded programs
read: Copies bytes from file to memory, updates file position
- Returns number of bytes read, -1 on error
write: Copies bytes from memory to file, updates file position
- Returns number of bytes written, -1 on error
File Metadata
- Per-file metadata maintained by kernel
- Accessed using stat and fstat functions