This is an old article written in 2013, put on Gegahost.net http://raison.gegahost.net/?p=91
March 7, 2013life of a NPTL pthreadfiled UNDER:CONCURRENCY,LINUX,POSIX-TAGS:NPTL, Pthread-raison @ 12:52 pm
(Original work by Peixu Zhu)
NPTL Pthread is the default Pthread implementation under current Linux distributions. In Concurrent/parallel programming, multiple threading are the basic technology, and knowledge of the NPTL thread ' s life wi ll make us clear on using pthread.
1. The nature of the NPTL pthread.
Naturally a NPTL pthread is a light weight pseudo process in Linux. Thus, the maximum of allowed Pthread number is limited by the limitation of allowed-to-create in the system. For a specific user, the number of processes to create is also limited. In system scope, we may get/set the maximum number of process at '/proc/sys/kernel/pid_max ', or by administration command ' Sysctl ', and get/set maximum number of threads at '/proc/sys/kernel/threads-max '. For a specific user, we could get/set the maximum number of processes to running with by the command ' Ulimit-u '.
Since they ' re processes in nature, thus, they is scheduled by the kernel all.
Keep in mind, each thread exhaust a Process ID.
2. The creation of NPTL Pthread
A NPTL thread is created by the system routine ' __clone ', which allow child process to share parts of its execution conte XT with the calling process, including memory space, file descriptors, signal handler table, etc.
The NPTL pthread Library offers library function ' pthread_create ' to create a thread. As calling ' Pthread_create ', user provide it with necessary arguments like the attribute, the executor function, the Argum ENT for executor function. In ' pthread_create ', below preparation works is performed in sequence:
- thread attributes is initialized, with the argument attribute, and then a stack and thread context is AL located, the thread context including TCB, internal locks etc., is initialized. In fact, the new thread ID is the address of the allocated stack. Thus, the count of allowed threads in a process was also limited by the memory for the process.
- calling Process/thread thread attribute flags and scheduling parameters is copied to the new thread, the join ID whic H required by ' Pthread_join ' is initialized.
- new Thread scheduling policy is determined by the calling process scheduling parameters and argument attribute.
- calling internal routine ' create_thread ' to create the thread, with the thread context, new thread stack, and Attribu TES as arguments.
In internal function ' Create_thread ', System routine ' __clone ' are called, with demanded arguments to create a thread (in F Act a child process). When calling ' __clone ', a fixed helper routine ' start_thread ' are feed instead of the thread executor function, when ' __CL One ' successfully create the new thread (child process), the helper routine ' Start_thread ' was executed, when ' Start_thread ' Finished, it returns the error code as the exiting code of the thread. The new thread executor function is executed in ' Start_thread '.
3. Cleanup of NPTL Pthread
In helper routine ' Start_thread ', after the thread executor function was executed, the return value is stored in the thread Context, then, the routine runs step by step as below:
- Run the destructor for the thread-local data.
- If the thread is the last thread in process, terminate the process.
- Set the thread to is EXITING.
- If the system support robust mutex, and if there ' s robust mutexes hooked on the thread, make all of them dead, thus, any SEQ Uent access of them is signalled with eownerdead.
- Recycle the stack of the thread, leaving TCB not freed.
- If the thread is a detached, TCB is freed.
- Call system call to terminate the thread.
4. Pthread_join and Pthread_detach
' Pthread_join ' is a supposed to join the target thread, provided the target thread is joinable. If the target thread is not terminated yet, the calling thread would waiting for the target thread to being terminated, after The target thread is terminated, it'll clean up the TCB of the target thread which was not recycled when the target Threa D finish running, and then return to the calling thread. If the target thread is a already terminated, the calling thread returns from ' Pthread_join ' soon.
' Pthread_detach ' is supposed to detach a joinable target thread. If the target thread is detached ready, error returned. The routine does not terminate the target thread, it just change ' joinid ' of the thread context.
A detached thread would recycle it TCB when it was terminated, and a joinable thread would remain its TCB when it was Termina Ted, until a calling of ' Pthread_join ' is called to recycle it. A thread is the default to being joinable, unless it is explicitly set to being detached when it is created.
Life of a NPTL pthread