system-calls-know-hows

linux c system calls related stuff

View on GitHub

Linux C System Calls Tutorial

A comprehensive guide to low-level Linux system calls in C.

╔══════════════════════════════════════════════════════════════╗
║           LINUX SYSTEM CALLS ARCHITECTURE                    ║
╠══════════════════════════════════════════════════════════════╣
║                                                              ║
║   User Space                                                 ║
║   ┌──────────────────────────────────────────────────┐       ║
║   │  Your C Program                                  │       ║
║   │  ┌──────────┐  ┌──────────┐  ┌──────────┐        │       ║
║   │  │ open()   │  │ fork()   │  │ malloc() │  ...   │       ║
║   │  └────┬─────┘  └────┬─────┘  └────┬─────┘        │       ║
║   └───────┼─────────────┼─────────────┼──────────────┘       ║
║           │             │             │                      ║
║   ════════╪═════════════╪═════════════╪═════════════════     ║
║           │  System Call Interface    │  (int 0x80/syscall)  ║ 
║   ════════╪═════════════╪═════════════╪═════════════════     ║
║           │             │             │                      ║
║   Kernel Space                                               ║
║   ┌───────┼─────────────┼─────────────┼──────────────┐       ║
║   │       ▼             ▼             ▼              │       ║
║   │  sys_open()    sys_fork()    sys_brk()           │       ║
║   │       │             │             │              │       ║
║   │       ▼             ▼             ▼              │       ║
║   │  [VFS Layer] [Process Mgmt] [Memory Mgmt]        │       ║
║   │       │             │             │              │       ║
║   │       ▼             ▼             ▼              │       ║
║   │  [Hardware Drivers & Resources]                  │       ║
║   └──────────────────────────────────────────────────┘       ║
║                                                              ║
╚══════════════════════════════════════════════════════════════╝

Table of Contents

1. Process Management

2. File Operations

3. Memory Management

4. Resource Management

5. Inter-Process Communication (IPC)

6. POSIX Threads (pthreads)

7. Socket Programming

8. Linux Kernel Internals

How to Use This Tutorial

  1. Each section contains detailed explanations with ASCII diagrams
  2. Example code is provided in the examples/ directory
  3. Compile examples with: gcc -o program program.c
  4. Some examples require root privileges
  5. Always check return values in production code

System Call Conventions

Return Values

Error Handling Pattern

#include <errno.h>
#include <string.h>
#include <stdio.h>

if (syscall(...) == -1) {
    fprintf(stderr, "Error: %s\n", strerror(errno));
    // Handle error
}

System Call vs Library Call

┌────────────────────────────────────────────────────┐
│  Library Call (e.g., printf, fopen, malloc)        │
│  ┌───────────────────────────────────────────────┐ │
│  │  Higher level, buffered, portable             │ │
│  │  May call multiple system calls               │ │
│  └───────────────────┬───────────────────────────┘ │
└────────────────────────────────────────────────────┘
                       │
                       ▼
┌────────────────────────────────────────────────────┐
│  System Call (e.g., write, open, brk)              │
│  ┌───────────────────────────────────────────────┐ │
│  │  Low level, direct kernel interface           │ │
│  │  Atomic operations                            │ │
│  │  Context switch to kernel mode                │ │
│  └───────────────────────────────────────────────┘ │
└────────────────────────────────────────────────────┘

Prerequisites

Compiling Examples

# Basic compilation
gcc -o example example.c

# With warnings and debugging
gcc -Wall -Wextra -g -o example example.c

# With optimization
gcc -O2 -o example example.c

References