Chapter 33: Threads — Further Details

 

EmbeddedPathashala · TLPI Series

Chapter 33: Threads — Further Details

Thread stacks, signals, process control, implementation models, LinuxThreads vs NPTL

6Tutorial Files

12+Code Examples

30+Interview Q&A

FreeAlways Free

What You Will Learn

Chapter 33 of The Linux Programming Interface builds on the POSIX Threads (Pthreads) foundation from Chapter 29 and explores the deeper, trickier corners of multithreaded Linux programming. You will understand how threads interact with the classic Unix signal model, what really happens when a multithreaded process calls fork() or exec(), how the kernel schedules threads (M:1, 1:1, M:N models), and the key differences between the two Linux thread libraries — LinuxThreads (legacy) and NPTL (modern).

Each tutorial file covers one focused topic with plain-English explanations, inline diagrams, real C code examples you can compile, and interview questions to test your understanding.

Key Concepts in This Chapter

Thread Stack Size pthread_attr_setstacksize Signal Mask pthread_sigmask pthread_kill sigwait fork() in threads exec() in threads pthread_atfork M:1 Threading 1:1 Threading M:N Threading LinuxThreads NPTL KSE clone() syscall futex RLIMIT_STACK

Tutorial Files

File 01 · Index

Chapter Overview & Navigation

You are here. Start here to understand the structure of the series and the big picture of what Chapter 33 covers.

Chapter Map Key Concepts Prerequisites

File 02 · Section 33.1

Thread Stacks

Every thread gets its own private stack. Learn the default sizes, why they matter, how to query the minimum, and how to resize stacks using pthread_attr_setstacksize(). Includes the virtual address space calculation for max thread count.

Default Stack Sizes pthread_attr_setstacksize RLIMIT_STACK sysconf(_SC_THREAD_STACK_MIN)

Read Tutorial →

File 03 · Section 33.2

Threads and Signals

Signals and threads are a notoriously tricky combination. Learn what is process-wide vs per-thread in the signal model, how to safely use pthread_sigmask(), send signals with pthread_kill(), and the recommended sigwait() pattern for asynchronous signal handling.

Process-wide vs Per-thread pthread_sigmask pthread_kill sigwait Signal Delivery to Threads

Read Tutorial →

File 04 · Section 33.3

Threads and Process Control

What happens when a multithreaded program calls fork(), exec(), or exit()? The answers are surprising and important. Covers the “only the calling thread survives fork” rule, mutex state inheritance dangers, and pthread_atfork() handlers.

fork() in Threads exec() in Threads exit() in Threads pthread_atfork Fork Handlers

Read Tutorial →

File 05 · Section 33.4

Thread Implementation Models

A conceptual deep-dive into how threading libraries are built. Understand M:1 (user-level), 1:1 (kernel-level), and M:N (two-level) thread models. Understand what a Kernel Scheduling Entity (KSE) is and why Linux chose the 1:1 model for NPTL.

M:1 Model 1:1 Model M:N Model KSE Scheduler Scalability

Read Tutorial →

File 06 · Sections 33.5 + 33.6

Linux Pthreads: LinuxThreads vs NPTL

A detailed comparison of Linux’s two Pthreads implementations. Understand why LinuxThreads broke many POSIX rules (different PIDs per thread, broken signals), how NPTL fixed them using new kernel features like CLONE_THREAD and futexes, and how to detect and select the threading implementation at runtime.

LinuxThreads NPTL clone() Flags futex getconf GNU_LIBPTHREAD_VERSION LD_ASSUME_KERNEL

Read Tutorial →

Free Embedded Systems Education

EmbeddedPathashala is a free platform for students and freshers breaking into embedded Linux and systems programming.

Visit EmbeddedPathashala

Leave a Reply

Your email address will not be published. Required fields are marked *