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.
CLONE_PARENT_SETTID
The kernels writes the thread ID of the newly created thread
into the location pointed to by the fifth parameters to CLONE.
Note that it would be semantically equivalent to use
CLONE_CHILD_SETTID but it is be more expensive in the kernel.
CLONE_CHILD_CLEARTID
The kernels clears the thread ID of a thread that has called
sys_exit() in the location pointed to by the seventh parameter
to CLONE.
The termination signal is chosen to be zero which means no signal
is sent. */
int clone_flags = (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGNAL
| CLONE_SETTLS | CLONE_PARENT_SETTID
| CLONE_CHILD_CLEARTID | CLONE_SYSVSEM
| 0);