Advanced File I/O
Overview
This week extends the foundation of Linux file I/O by exploring advanced file descriptor manipulation and low-level file positioning.
Students will learn how to duplicate file descriptors, control file behavior using fcntl, and manage files beyond simple read/write operations — including seeking, truncation, and sparse file handling.
By the end of this week, students will have a complete understanding of how Linux manages file descriptors and how processes interact with files efficiently and flexibly.
Key Concepts
File Descriptor Duplication
dup()anddup2()system calls- File descriptor table and shared open file descriptions
- Standard output redirection example with
dup2() - Relationship between
dup2()and shell redirection
File Positioning
- The
lseek()system call: repositioning file offsets - Seeking modes:
SEEK_SET– from beginningSEEK_CUR– from current positionSEEK_END– from end of file
- Practical uses: appending, measuring file size, random access
Sparse Files and Holes
- Concept of holes (unallocated regions in files)
- Creating and inspecting sparse files
- Comparing allocated and logical sizes
SEEK_DATAandSEEK_HOLE(modern extensions for efficient traversal)
File Control and Management
- The
fcntl()system call:- Duplicating descriptors (
F_DUPFD) - Managing flags (
FD_CLOEXEC,O_APPEND,O_NONBLOCK)
- Duplicating descriptors (
- Truncating files with
ftruncate() - Retrieving file information with
fstat()
Practice / Lab
Descriptor Duplication
- Redirect program output to files using file descriptor duplication.
- Explore descriptor inheritance and redirection behavior.
File Positioning and Sparse Files
- Create files with gaps using
lseek()to demonstrate sparse allocation. - Compare logical and physical file sizes using shell utilities.
File Control and Metadata
- Use
fcntl()to retrieve and modify descriptor properties. - Truncate and inspect file length using system utilities.
Homework
Samples
References & Resources
Required
- Kerrisk, The Linux Programming Interface
- Chapter 5: File I/O – Deeper Concepts
- Chapter 6: Advanced File I/O
Recommended
- Understanding Sparse Files (Red Hat)
- Beej’s Guide – File Descriptors
- Linux manual page - fcntl(2)
- Linux manual page - dup(2)
- Linux manual page - lseek(3p)
- lseek() in C/C++ to read the alternate nth byte and write it in another file
Quiz (Self-check)
- What happens to the file offset when a descriptor is duplicated?
- How does
dup2()differ from shell redirection? - What is the difference between
SEEK_ENDandSEEK_CUR? - How does a sparse file conserve storage space?
- What are the purposes of
SEEK_DATAandSEEK_HOLE? - What does
FD_CLOEXECaccomplish in practice?
Suggested Tools
strace– observe system calls likedup2andlseekls,du,stat– inspect logical and physical file propertieshexdump– verify data placement within sparse files