Skip to content
All paths
Intermediate → AdvancedFoundations

Linux System Programming

Learn how Linux really works by programming against it in C. Starting from pointers and the build toolchain, you work through system calls, file I/O, processes, signals, IPC, threads, virtual memory and sockets, then finish by building a mini shell and a concurrent TCP chat server that runs as a systemd service.

What you will be able to do

  • Write, build and debug memory-safe C programs with gcc, make, gdb and valgrind
  • Explain the user/kernel boundary and trace any program's system calls with strace
  • Manage files, processes, signals and IPC directly through POSIX and Linux APIs
  • Write correct multithreaded code with mutexes and condition variables and diagnose deadlocks
  • Build scalable TCP/UDP servers with non-blocking I/O and epoll
  • Ship a long-running program as a well-behaved systemd service

Roadmap

Bar length shows each module's share of study time.

  1. 1

    C for systems programming

    The C you need to read and write real Linux code.

    17h4 topics
    Not started
    Show topics
  2. 2

    The kernel/user boundary

    How programs ask the kernel to do work on their behalf.

    7h 30m3 topics
    Not started
    Show topics
  3. 3

    Files, I/O & file systems

    Everything is a file: descriptors, buffering, inodes and the VFS.

    18h6 topics
    Not started
    Show topics
    • Copy files with raw system calls, handling short reads and writes.

      • Open files with flags and modes
      • Read and write in a loop
      • Handle short reads and writes
      • Close descriptors and avoid leaks
      • Use stdin, stdout and stderr descriptors
    • Choose between stdio and raw syscalls and control buffering and flushing.

      • Compare FILE streams with descriptors
      • Explain full, line and unbuffered modes
      • Control buffering with setvbuf and fflush
      • Measure syscall counts with strace -c
    • Reposition, duplicate and reconfigure descriptors to implement redirection.

      • Seek and create sparse files with lseek
      • Redirect output with dup2
      • Set O_NONBLOCK and FD_CLOEXEC via fcntl
      • Use O_APPEND and O_EXCL correctly
      • Make durable writes with fsync
    • Read file metadata with stat and explain how names map to inodes.

      • Read metadata with stat and fstat
      • Create hard and symbolic links
      • Explain link counts and unlink
      • Compare ext4 and xfs at a high level
    • Explain how the VFS unifies different file systems and read kernel data from /proc.

      • Describe the VFS abstraction layers
      • List mounts from /proc/self/mounts
      • Read process info from /proc/PID
      • Explore kernel settings in /sys
    • Walk directory trees and create files with the intended permissions.

      • Iterate directories with opendir and readdir
      • Explain rwx bits on files and directories
      • Apply umask to new files
      • Change modes with chmod and fchmod
    Module resources:open(2) ↗inode(7) ↗
  4. 4

    Processes

    Create, replace, wait for and organise processes.

    13h 30m4 topics
    Not started
    Show topics
    • Describe what a process owns and how it moves between scheduler states.

      • List what a process owns
      • Read PIDs, PPIDs and credentials
      • Trace running, sleeping and stopped states
      • Inspect processes via /proc/PID/status
    • Launch a child program, wait for it and decode its exit status.

      • Create a child with fork
      • Replace the image with execvp
      • Reap children with waitpid
      • Decode status with WIFEXITED and WEXITSTATUS
      • Explain copy-on-write after fork
    • Avoid zombie processes and pass configuration through the environment.

      • Compare exit and _exit
      • Create and reap a zombie
      • Explain orphan adoption by init
      • Read and set environment variables
    • Explain how shells manage foreground and background jobs.

      • Explain process groups and sessions
      • Set groups with setpgid and setsid
      • Hand the terminal over with tcsetpgrp
      • Trace Ctrl-C delivery to a job
  5. 5

    Signals

    Asynchronous notifications and how to handle them safely.

    9h3 topics
    Not started
    Show topics
    Module resources:signal(7) ↗
  6. 6

    Inter-process communication

    Move data and coordinate between separate processes.

    13h 30m4 topics
    Not started
    Show topics
    • Connect processes with pipes and named FIFOs like a shell pipeline does.

      • Create a pipe and fork
      • Close unused pipe ends
      • Build a two-command pipeline with dup2
      • Create a named FIFO with mkfifo
      • Explain SIGPIPE and EOF behaviour
    • Share a memory region between processes and synchronise access with semaphores.

      • Create shared memory with shm_open
      • Size and map it with ftruncate and mmap
      • Synchronise with POSIX semaphores
      • Clean up with shm_unlink
    • Exchange discrete, prioritised messages between processes.

      • Open a queue with mq_open
      • Send and receive with priorities
      • Set queue attributes and limits
      • Compare with pipes and shared memory
    • Build a local client/server over a Unix socket and pick the right IPC mechanism.

      • Create a stream socket on a path
      • Accept clients and exchange data
      • Compare stream and datagram modes
      • Pass descriptors with SCM_RIGHTS
      • Choose between pipes, sockets and shm
  7. 7

    POSIX threads & synchronization

    Concurrency inside one process, and how to keep it correct.

    15h4 topics
    Not started
    Show topics
    • Start worker threads, pass them arguments and collect their results.

      • Create threads with pthread_create
      • Pass arguments and return values
      • Join or detach threads
      • Compare threads with processes
      • Build with -pthread
    • Find a data race and fix it with a correctly scoped mutex.

      • Reproduce a lost-update race
      • Protect shared data with a mutex
      • Keep critical sections short
      • Detect races with ThreadSanitizer
    • Build a bounded blocking queue with a mutex and condition variables.

      • Wait on a condition in a while loop
      • Signal and broadcast waiting threads
      • Build a bounded producer-consumer queue
      • Explain spurious wakeups
    • Prevent deadlocks with lock ordering and choose rwlocks or atomics when they fit.

      • Name the four deadlock conditions
      • Prevent deadlock with lock ordering
      • Debug a hung program with gdb thread apply
      • Use read-write locks for read-heavy data
      • Use C11 atomics for simple counters
    Module resources:pthreads(7) ↗
  8. 8

    Memory management

    Virtual memory, mapping files and what malloc actually does.

    12h 30m3 topics
    Not started
    Show topics
    • Explain how virtual addresses map to physical pages and read a process memory map.

      • Explain pages, page tables and the TLB
      • Read a memory map in /proc/PID/maps
      • Describe page faults and demand paging
      • Explain overcommit and the OOM killer
    • Map files and anonymous memory and choose shared or private mappings.

      • Map a file read-only and scan it
      • Compare MAP_SHARED and MAP_PRIVATE
      • Allocate anonymous memory with mmap
      • Change protections with mprotect
      • Flush changes with msync
    • Explain how malloc obtains and recycles memory and write a basic allocator.

      • Explain brk, sbrk and mmap for allocation
      • Describe chunks, headers and free lists
      • Explain fragmentation and arenas
      • Write a first-fit allocator
      • Test it with LD_PRELOAD
  9. 9

    Network sockets

    TCP and UDP programming, from one client to thousands.

    19h 30m5 topics
    Not started
    Show topics
    • Resolve hosts with getaddrinfo and convert addresses and byte order correctly.

      • Describe the socket, bind, listen, accept calls
      • Resolve names with getaddrinfo
      • Convert byte order with htons and ntohs
      • Support IPv4 and IPv6 together
    • Write a TCP echo server and client that handle framing and disconnects.

      • Explain the TCP three-way handshake
      • Write an iterative echo server
      • Set SO_REUSEADDR before bind
      • Frame messages on a byte stream
      • Handle disconnects and EPIPE
    • Send and receive datagrams and handle loss and reordering.

      • Send with sendto and receive with recvfrom
      • Explain datagram boundaries and size limits
      • Handle loss with timeouts and retries
      • Compare UDP and TCP trade-offs
    • Serve many clients from one thread by multiplexing descriptors with epoll.

      • Watch descriptors with select
      • Scale further with poll
      • Register interest with epoll_ctl
      • Compare level- and edge-triggered modes
      • Benchmark the three approaches
    • Structure a server as a non-blocking event loop with per-connection buffers.

      • Set sockets to O_NONBLOCK
      • Handle EAGAIN on read and write
      • Keep per-connection read and write buffers
      • Compare thread-per-connection with event loops
  10. 10

    Time, timers & daemons

    Measure time, schedule work and run as a background service.

    9h3 topics
    Not started
    Show topics
    • Measure elapsed time with the right clock and drive periodic work with timerfd.

      • Compare CLOCK_REALTIME and CLOCK_MONOTONIC
      • Measure durations with clock_gettime
      • Sleep precisely with nanosleep
      • Create periodic timers with timerfd
      • Add timeouts to poll and epoll
    • Explain each step of the traditional double-fork daemon recipe.

      • Fork and call setsid
      • Fork again and change directory
      • Reset umask and close descriptors
      • Write a PID file and log to syslog
    • Ship a foreground program as a systemd service with logging, restarts and sandboxing.

      • Write a simple service unit file
      • Log to stdout for journald
      • Configure Restart and reload on SIGHUP
      • Add sandboxing like ProtectSystem
      • Notify readiness with sd_notify
  11. 11

    Capstone projects

    Put every module together in two real programs.

    21h3 topics
    Not started
    Show topics
    • Build a shell that runs commands with pipes, redirection, built-ins and background jobs.

      • Parse a command line into tokens
      • Run commands with fork and execvp
      • Add < > redirection and pipes
      • Implement cd and exit built-ins
      • Run background jobs and reap with SIGCHLD
    • Build a chat server that broadcasts messages to many clients using epoll or a thread pool.

      • Design a line-based chat protocol
      • Accept clients with an epoll event loop
      • Broadcast messages to all clients
      • Handle slow clients and disconnects
      • Load test with many netcat clients
    • Prove the capstone is leak-free and race-free, then run it under systemd.

      • Run valgrind and sanitizers clean
      • Profile hot paths with perf
      • Write a systemd unit for the server
      • Document build and usage in a README