diff --git a/README.md b/README.md index 68f6230..4aa3ae0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ -# system-programming -A repo for my experiments on system programming +# πŸ“š Linux System Programming – Chapter Index + +Welcome! This is a collection of chapter summaries from the book **Linux System Programming** by Robert Love. Each chapter is organized in its own folder. + +## Available Chapters + +- [Chapter 1 – Introduction and Core Concepts](chp1/README.md) +- [Chapter 2 – File I/O](chp2/README.md) +- [Chapter 3 – Buffered I/O](chp3/README.md) +- [Chapter 4 – Advanced File I/O](chp4/README.md) + +> Each file contains an English summary of the chapter's key concepts. + diff --git a/chp1/README.md b/chp1/README.md new file mode 100755 index 0000000..fcc56e6 --- /dev/null +++ b/chp1/README.md @@ -0,0 +1,28 @@ +# Chapter 1 – Introduction and Core Concepts + +## πŸ”§ What is System Programming? +- Low-level programming that interacts directly with the **kernel** and system libraries via **system calls**, typically written in **C**. +- Includes shell tools (bash, gcc, coreutils), daemons, webservers, etc. + +## Why Learn It? +- Even in high-level environments (JS, Java, Python), understanding internal mechanisms leads to better, more efficient code. +- Still essential for core components of Linux systems: kernel, drivers, glibc, gcc. + +## Core Elements +1. **System calls** – entry points from user process to the kernel (e.g., `read()`, `write()`). +2. **C Library** (glibc) – interface between user programs and system calls. +3. **C Compiler** (gcc) – handles APIs, ABIs, and object formats. + +## API vs ABI +- **API**: interface at the source code level (e.g., libc functions). +- **ABI**: binary layout and conventions (e.g., syscall invocation, parameter ordering). Defines compatibility between systems and architectures. + +## Standards and Compatibility +- POSIX/SUS as a reference, but Linux is not officially certified. +- The **Linux Standard Base (LSB)** initiative extends POSIX/SUS to ensure binary compatibility across distributions. + +## Linux Basics +- **Files**: name + inode, represented by **file descriptors** (int). Linear byte streams (no formatting). +- **Processes**: entities with address space, pid, users, groups, permissions, signals. +- **IPC**: inter-process communication mechanisms (pipe, FIFO, socket, shared memory, semaphores). +- **Error handling**: functions return values and set `errno`. Useful functions: `perror()`, `strerror()`, `strerror_r()`. diff --git a/chp2/README.md b/chp2/README.md new file mode 100755 index 0000000..94affd1 --- /dev/null +++ b/chp2/README.md @@ -0,0 +1,30 @@ +# Chapter 2 – File I/O + +## Basic Concepts +- File I/O is central in Linux: every entity (file, socket, device) is treated as a file. +- The kernel manages a **file table** per process, with descriptors and metadata like offset and mode. + +## Core Operations +1. **`open()`**: + - Opens a file with flags (O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_SYNC, O_DIRECT). + - Returns a file descriptor or -1 on error. + - Sets owner and permissions for newly created files. +2. **`read()` / `write()`**: + - `read(fd, buf, n)` reads up to **n** bytes, returns bytes read or -1. + - `write(fd, buf, n)` writes, handles partial writes, append mode and non-blocking I/O. + - Common errors: EAGAIN, EINTR, ECC. +3. **I/O Synchronization**: + - `fsync()` and `fdatasync()` flush to disk. + - `O_SYNC`, `O_DSYNC`, `O_RSYNC` for open-time synchronization. + - `sync()` syncs all files to disk. +4. **`lseek()`**: + - Moves the **offset** within the fileβ€”can be positive/negative; allows sparse writes. +5. **`close()`**: + - Frees the descriptor. Returns 0 or -1 on error. + +## Positional I/O +- `pread()` / `pwrite()` allow read/write at specific offsets without altering the shared file offset (useful in multithreaded environments). + +## Multiplexed I/O +- Functions like `select()` and `poll()` manage multiple file descriptors for asynchronous I/O. +- More scalable option: `epoll()` (covered in Chapter 4). diff --git a/chp3/README.md b/chp3/README.md new file mode 100644 index 0000000..eacadc8 --- /dev/null +++ b/chp3/README.md @@ -0,0 +1,26 @@ +# Chapter 3 – Buffered I/O + +## Difference Between Byte-Level and Buffered I/O +- **System calls** like `read()` and `write()` operate at a low level: each call may result in direct disk access. +- **C libraries** use buffered I/O (`fread()`, `fwrite()`, `fprintf()`), accumulating data in memory to reduce system calls. + +## Buffered I/O API +- Uses `FILE *` type from ``. +- Functions: + - `fopen()`, `fclose()`: open/close file. + - `fread()`, `fwrite()`: binary I/O. + - `fprintf()`, `fscanf()`, `fgets()`, `fputs()`: formatted or text I/O. + +## Types of Buffering +- **Fully buffered**: flush when buffer is full. +- **Line buffered**: flush at end of line (e.g., terminals). +- **Unbuffered**: no accumulation (e.g., stderr). + +## Buffer Management +- `setvbuf()` or `setbuf()` allows custom buffering. +- Useful for optimizing performance or interactive environments. + +## When to Use Buffered I/O +- When performance matters and immediate file system synchronization isn't required. +- For formatted I/O, the C library offers more simplicity and readability. + diff --git a/chp4/README.md b/chp4/README.md new file mode 100755 index 0000000..802516c --- /dev/null +++ b/chp4/README.md @@ -0,0 +1,42 @@ +# Chapter 3 – 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. + +## File Types +- Regular file, directory, symbolic link, character/block device, FIFO, socket. +- Use macros like `S_ISREG()`, `S_ISDIR()` to test file types. + +## 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()`. + +## 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()`. + +## Directories +- Open with `opendir()`, read entries with `readdir()`. +- Low-level: `getdents()` system call. + +## 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()`. + +## 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.