Chapter 4
This commit is contained in:
@@ -1,42 +1,45 @@
|
||||
# Chapter 4 – Advanced File I/O
|
||||
|
||||
## File Metadata and the `stat` Family
|
||||
- Use `stat()`, `fstat()`, `lstat()` to retrieve file metadata:
|
||||
- File type, size, inode number, permissions, ownership, timestamps (atime, mtime, ctime).
|
||||
- The `stat` structure includes all these details.
|
||||
## Scatter/Gather I/O
|
||||
- Scatter/gather I/O allows reading or writing data to/from multiple buffers in a single system call.
|
||||
- Uses `readv()` and `writev()`:
|
||||
- `readv(fd, iov, iovcnt)`: reads into multiple buffers (`iov` array).
|
||||
- `writev(fd, iov, iovcnt)`: writes from multiple buffers.
|
||||
- Improves efficiency by reducing system calls.
|
||||
|
||||
## File Types
|
||||
- Regular file, directory, symbolic link, character/block device, FIFO, socket.
|
||||
- Use macros like `S_ISREG()`, `S_ISDIR()` to test file types.
|
||||
## Event Poll (`select()`, `poll()`, `epoll`)
|
||||
- Traditional multiplexing: `select()`, `poll()` (limited scalability).
|
||||
- Linux-specific: `epoll` for efficient event notification.
|
||||
- `epoll_create()`: create epoll instance.
|
||||
- `epoll_ctl()`: control interest list (add, modify, remove fds).
|
||||
- `epoll_wait()`: wait for events.
|
||||
- Edge-triggered vs level-triggered modes.
|
||||
|
||||
## File Permissions and Modes
|
||||
- Represented by 12 bits (e.g., `rwxr-xr--`), plus sticky/SUID/SGID bits.
|
||||
- Manipulate with `chmod()`, `fchmod()`, `umask()`.
|
||||
- Ownership changed with `chown()` and `fchown()`.
|
||||
## Memory-Mapped Files (`mmap()`)
|
||||
- Maps files or devices into memory for direct access.
|
||||
- System calls: `mmap()`, `munmap()`.
|
||||
- Benefits: faster I/O, shared memory.
|
||||
- Use `msync()` to synchronize changes.
|
||||
- Resizing mappings, changing protections.
|
||||
- Advisory calls:
|
||||
- `posix_fadvise()`: give advice about expected I/O patterns.
|
||||
- `readahead()`: prefetch file data.
|
||||
- Pros and cons of memory mapping.
|
||||
|
||||
## Links
|
||||
- **Hard links**: multiple filenames point to the same inode.
|
||||
- **Symbolic links**: separate files that reference other paths; can be broken.
|
||||
- Use `link()`, `unlink()`, `symlink()`, `readlink()`.
|
||||
## Asynchronous I/O and I/O Scheduling
|
||||
- Distinction between synchronous and asynchronous I/O.
|
||||
- Linux AIO APIs for asynchronous operations.
|
||||
- I/O schedulers:
|
||||
- Manage disk request ordering.
|
||||
- Examples: CFQ, noop, deadline.
|
||||
- Optimizing I/O performance by choosing schedulers.
|
||||
- Concepts of disk addressing and request life cycle.
|
||||
|
||||
## Directories
|
||||
- Open with `opendir()`, read entries with `readdir()`.
|
||||
- Low-level: `getdents()` system call.
|
||||
## Miscellaneous
|
||||
- `ioctl()`: device-specific operations.
|
||||
- Zero-copy transfers: `sendfile()`, `splice()`.
|
||||
|
||||
## File Timestamps
|
||||
- `utime()`, `utimes()`, `futimens()`, and `utimensat()` to update access/modification times.
|
||||
---
|
||||
|
||||
## File Descriptors and Flags
|
||||
- Manipulate with `fcntl()`:
|
||||
- Set non-blocking mode (`O_NONBLOCK`), FD_CLOEXEC flag, file locks.
|
||||
- Duplicate with `dup()` and `dup2()`.
|
||||
> This chapter covers advanced Linux I/O techniques aimed at improving performance and flexibility in system programming.
|
||||
|
||||
## File Locking
|
||||
- `flock()` for advisory locking (whole file).
|
||||
- `fcntl()` for POSIX record locking (ranges).
|
||||
|
||||
## Temporary Files
|
||||
- `tmpfile()`, `mkstemp()` are safe methods to create temp files.
|
||||
|
||||
## Summary
|
||||
- Chapter 3 focuses on extended file handling: metadata, links, directories, file modes, and advanced descriptor manipulation.
|
||||
|
||||
BIN
chp4/buccaneer.txt
Executable file
BIN
chp4/buccaneer.txt
Executable file
Binary file not shown.
41
chp4/readv.c
Normal file
41
chp4/readv.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
char foo[48], bar[51], baz[49];
|
||||
int fd = open("buccaneer.txt", O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// setup our iovec structures
|
||||
struct iovec iov[3];
|
||||
iov[0].iov_base = foo;
|
||||
iov[0].iov_len = sizeof(foo);
|
||||
iov[1].iov_base = bar;
|
||||
iov[1].iov_len = sizeof(bar);
|
||||
iov[2].iov_base = baz;
|
||||
iov[2].iov_len = sizeof(baz);
|
||||
|
||||
// read into the structures with a single call
|
||||
int nr = readv(fd, iov, 3);
|
||||
if (nr == -1) {
|
||||
perror("readv");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
printf("%d: %s", i, (char *)iov[i].iov_base);
|
||||
|
||||
if (close(fd)) {
|
||||
perror("close");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
45
chp4/writev.c
Normal file
45
chp4/writev.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
struct iovec iov[3];
|
||||
|
||||
char *buf[] = {
|
||||
"The term buccaneer comes from the word boucan.\n",
|
||||
"A boucan is a wooden frame used for cooking meet.\n",
|
||||
"Buccaneer is the West Indies name for a pirate.\n"
|
||||
};
|
||||
|
||||
int fd = open("buccaneer.txt", O_WRONLY | O_CREAT | O_TRUNC);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// fill out three iovec structures
|
||||
for (int i = 0; i < 3; i++) {
|
||||
iov[i].iov_base = buf[i];
|
||||
iov[i].iov_len = strlen(buf[i]) + 1;
|
||||
}
|
||||
|
||||
// with a single call, write them all out
|
||||
ssize_t nr = writev(fd, iov, 3);
|
||||
if (nr == -1) {
|
||||
perror("writev");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("wrote %lu bytes\n", nr);
|
||||
|
||||
if (close(fd)) {
|
||||
perror("close");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user