libc 库中 pthread_create 通过层层封装,最终通过调用clone 系统调用创建线程。
libc 代码中:
pthread_create.c中,
函数调用关系 __pthread_create_2_1 -》create_thread-》do_clone
create_thread 函数传递给do_clone 的flag 为
/* We rely heavily on various flags the CLONE function understands:
CLONE_VM (共享内存描述符和所有页表), CLONE_FS (共享根目录和当前工作目录所在的表,以及用于屏蔽新文件许可权的位掩码值), CLONE_FILES(共享打开文件表)
These flags select semantics with shared address space and
file descriptors according to what POSIX requires.
CLONE_SIGNAL
This flag selects the POSIX signal semantics.
CLONE_SETTLS
The sixth parameter to CLONE determines the TLS area for the
new thread.

这篇博客详细介绍了pthread_create在libc库中的实现过程,从__pthread_create_2_1开始,逐步调用create_thread和do_clone。在create_thread中,设置的clone标志包括CLONE_VM、CLONE_FS、CLONE_FILES等,以实现共享内存、文件描述符和信号等POSIX线程特性。同时,讨论了不同clone标志的含义和它们在内核中的行为,如CLONE_PARENT_SETTID和CLONE_CHILD_CLEARTID用于线程ID的传递和清除。
最低0.47元/天 解锁文章
1614





