1. POSIX.1-2017 定义的 pthread_create/pthread_join/pthread_exit 与 Linux 内核 task_struct 的对应
POSIX.1-2017 定义的 pthread_create/pthread_join/pthread_exit 与 Linux 内核 task_struct 如何对应?
- pthread 线程 API 与内核对线程的表示
- 用户态线程与内核调度实体
- clone 系统调用
POSIX 线程(pthread)在 Linux 上由内核线程实现,每个 pthread 对应一个内核的 task_struct(通过 clone 系统调用创建,共享地址空间等资源)。pthread_create 用 clone 创建线程(共享 VMA、文件描述符、信号处理等),返回内核线程;pthread_join 等待线程结束(内核线程退出后回收其资源);pthread_exit 退出线程(内核线程退出,释放资源)。对应关系:用户态 pthread 是内核 task_struct 的封装,pthread 的 tid 即内核线程的 pid(tgid 是进程/task group)。pthread_join 对应内核的 wait/reap 线程的退出状态。
pthread 是 POSIX 线程 API,底层由内核 task_struct 承担,Linux 用 clone 复用进程机制实现线程。pthread_join 回收内核线程资源,理解对应关系便于排查线程问题。