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.
- 117h4 topics
C for systems programming
The C you need to read and write real Linux code.
Show topicsNot startedUse pointers, pointer arithmetic and dynamic allocation without leaks or invalid accesses.
- Declare and dereference pointers
- Relate arrays and pointer arithmetic
- Allocate and free heap memory
- Distinguish stack, heap and static data
- Pass pointers to modify caller data
Model data with structs and handle C strings and bit flags safely.
- Define structs and use typedef
- Handle null-terminated strings safely
- Use bitwise operators for flags
- Understand padding and alignment
Compile multi-file projects with warnings on and a Makefile, and link static or shared libraries.
- Follow preprocess, compile, assemble, link stages
- Enable -Wall -Wextra and fix warnings
- Write a Makefile with pattern rules
- Build static and shared libraries
- Inspect binaries with nm and ldd
Find crashes, leaks and memory errors with gdb, valgrind and AddressSanitizer.
- Set breakpoints and step in gdb
- Inspect a core dump backtrace
- Find leaks with valgrind memcheck
- Catch overflows with -fsanitize=address
- 27h 30m3 topics
The kernel/user boundary
How programs ask the kernel to do work on their behalf.
Show topicsNot startedExplain privilege levels and why user programs cannot touch hardware directly.
- Describe user mode and kernel mode
- List the kernel's core subsystems
- Explain the role of glibc
- Read the Linux man page sections
Call system calls correctly and handle every failure using errno.
- Trace a syscall from wrapper to kernel
- Check return values on every call
- Report errors with errno and perror
- Handle EINTR and partial results
Diagnose a misbehaving program by reading its system call trace.
- Trace a command with strace
- Filter syscalls with -e trace
- Follow child processes with -f
- Summarise syscall counts with -c
- Trace library calls with ltrace
- 318h6 topics
Files, I/O & file systems
Everything is a file: descriptors, buffering, inodes and the VFS.
Show topicsNot startedCopy 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
- 413h 30m4 topics
Processes
Create, replace, wait for and organise processes.
Show topicsNot startedDescribe 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
- 59h3 topics
Signals
Asynchronous notifications and how to handle them safely.
Show topicsNot startedSend signals and predict each one's default action.
- List common signals and their numbers
- Send signals with kill and raise
- Explain terminate, core, stop and ignore
- Explain why SIGKILL cannot be caught
Install reliable handlers with sigaction that only do async-signal-safe work.
- Install handlers with sigaction
- Block signals with sigprocmask
- Set a volatile sig_atomic_t flag
- Use SA_RESTART to resume syscalls
- Check the async-signal-safe function list
Turn signals into readable events that fit into an event loop.
- Implement the self-pipe trick
- Block signals and create a signalfd
- Read signalfd_siginfo structs
- Handle SIGCHLD to reap children
Module resources:signal(7) ↗ - 613h 30m4 topics
Inter-process communication
Move data and coordinate between separate processes.
Show topicsNot startedConnect 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
- 715h4 topics
POSIX threads & synchronization
Concurrency inside one process, and how to keep it correct.
Show topicsNot startedStart 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) ↗ - 812h 30m3 topics
Memory management
Virtual memory, mapping files and what malloc actually does.
Show topicsNot startedExplain 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
- 919h 30m5 topics
Network sockets
TCP and UDP programming, from one client to thousands.
Show topicsNot startedResolve 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
- UDP socketsLearn visually 2h 30m
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
- 109h3 topics
Time, timers & daemons
Measure time, schedule work and run as a background service.
Show topicsNot startedMeasure 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
- 1121h3 topics
Capstone projects
Put every module together in two real programs.
Show topicsNot startedBuild 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