Lesson 7 of 20 · 15m
Threads Are Tasks Too: clone() and the Thread Group
clone() flags make threads and processes the same object to the scheduler — with different blast radii for fds, memory, and crashes.
The scheduler doesn't know what a thread is
top shows your Java service at 800% CPU on an 8-core box. ps -Lf shows 200 entries under one PID. To the kernel, none of that is exotic: there is no "thread" object — there are tasks, and some tasks share an address space.
fork() and pthread_create() are both clone() underneath. fork is clone with no sharing flags: new memory, new fd table, new signal handlers. A pthread is clone with CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD: same address space, same file descriptors, same working directory — private stack and registers. When you ls /proc/$PID/task/ you see one subdirectory per task in the thread group; each has its own TID, and getpid() returns the shared TGID. That's why ps -eLf and top -H report thread IDs in the PID column — they are PIDs to the kernel.
What sharing actually buys and costs
Shared address space means no context-switch flush of page tables when the scheduler jumps between threads of the same process — cheaper than processes for tightly coupled work. Shared fd table means one thread's open() result is usable by all the others — and one thread's leaked fd is everyone's EMFILE. Shared signal disposition means kill targets the group unless you aim at a TID with tgkill.
The costs people forget: every thread gets its own stack — glibc's default is 8 MB of address space — so 10,000 threads reserve 80 GB of VA (the physical pages arrive lazily, but /proc/$PID/status VmSize balloons and monitoring panics). Limits live in /proc/sys/kernel/threads-max, RLIMIT_NPROC, and per-cgroup pids.max — the last one being what makes the classic :(){ :|:& };: fork bomb a footnote instead of an outage on a modern box. ps -o nlwp -p $PID shows the live thread count, and [kworker]/[kthreadd] in ps aux are the kernel's own threads — same task objects, no userspace address space at all.
Locks and futexes
Threads share memory, so they need synchronization, and glibc mutexes are built on futex(2) — a syscall that parks a task in D-state-ish sleep until another task wakes it. When strace -p shows a thread spinning in futex, it's waiting on a lock; when pidstat -w shows voluntary context switches dominating, locks or I/O are the cause. The wchan column in ps -eo pid,wchan,comm tells you which kernel function it's parked inside. A futex that never wakes is the signature of a lost wakeup — the waiter parked on an address the waker raced past — and strace -p shows exactly one task asleep where nobody will ever write.
Two famous architectural splits
- Postgres forks; MySQL threads. Every Postgres connection is a
forked process — crash isolation, no shared-memory races on connection state, heavier per connection. MySQL runs a thread per connection in one process — cheaper to create, one bad pointer takes the whole server down. Both are correct answers to the same question with different blast-radius tradeoffs. - Go/Java vs the kernel view. Goroutines are M:N — user-space scheduled onto a pool of OS threads the kernel sees as ordinary tasks.
runtimegrows the pool when goroutines block in syscalls. The kernel never sees your goroutine; it sees ~10 threads and their syscalls.
Try this
ps -eLf | head # LWP column = per-task TIDs
ls /proc/$$/task # your shell's thread group (usually 1)
top -H -p $(pgrep -n java) # per-thread CPU inside the JVM
cat /proc/sys/kernel/threads-maxField notes
ps aux collapsing threads into one row is the usual cause of "my service is using one CPU" — it's using eight, you just asked ps for processes. And a task's state — R, S, D, Z, T — is per-thread: one thread stuck in D on dead NFS parks that thread, not the process, though the user usually can't tell the difference until top -H shows who stopped.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Red Hat Called This Bug a 5.9. Two Weeks Later It Was a 9.8.
CVE-2026-98432 is a use-after-free in the IPv4 TCP handshake, reachable by anybody who can reach a listening port, sitting in the kernel since 4.4. Red Hat scored it 5.9 moderate and deferred the fix. Then 8.1. Then 9.8.
Januscape: A 16-Year-Old Linux KVM Escape Bug
CVE-2026-53359 lets a guest VM crash or potentially root your Proxmox host, and it hid in KVM's shared code for 16 years. What to patch and how to check.
CVE-2026-31431 "Copy Fail": How a 9-Year-Old Linux Kernel Bug Gives Any Local User Root in 732 Bytes
CVE-2026-31431 chains AF_ALG, splice(), and authencesn's ESN scratch write into a deterministic 4-byte page cache write that gives an unprivileged local user root. Full technical breakdown, exploit mechanics, detection, mitigation, and patch status per distro.
Self-Host OpenClaw: VPS Setup to Production (2026)
Self-host OpenClaw from zero to production. Server provisioning, SSH lockdown, Docker, reverse proxy, Fail2ban, performance tuning, and full backup strategy in one guide.