这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=91

March 7, 2013

life of a NPTL pthread

Filed 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 is the basic technology, and knowledge of the NPTL thread’s life will 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 processes 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 may get/set the maximum number of processes to running with by the command `ulimit   -u`.

Since they’re processes in nature, thus, they are scheduled by the kernel all.

Keep in mind that 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 context 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 argument for executor function. In `pthread_create`,  below preparation works are performed in sequence:

  • thread attributes are initialized,  with the argument attribute, and then a stack and thread context are allocated,   the thread context  including TCB, internal locks  etc., are initialized. In fact, the new thread id is the address of allocated stack. Thus, the count of allowed threads in a process is also limited by the memory for the process.
  • calling process/thread thread attribute flags and scheduling parameters are copied to the new thread, the join ID which 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 attributes as arguments.

In internal function `create_thread`,  system routine `__clone` is called, with demanded arguments to create a thread(in fact a child process). When calling `__clone`,  a fixed helper routine `start_thread` is feed instead of the thread executor function, when `__clone` successfully create the new thread (child process), the helper routine `start_thread` is 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 is 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 be EXITING.
  • if the system support robust mutex, and if there’s robust mutex hooked on the thread, make all of them dead, thus, any sequent access of them will be signalled with EOWNERDEAD.
  • recycle the stack of the thread, leaving TCB not freed.
  • if the thread is detached,  TCB is freed.
  • call system call to terminate the thread.

4. pthread_join and pthread_detach

`pthread_join` is supposed to join the target thread, provided that the target thread is joinable. If the target thread is not terminated yet, the calling thread will waiting for the target thread to be terminated, after the target thread is terminated, it will clean up the TCB of the target thread which is not recycled when the target thread finish running, and then return to the calling thread. If the target thread is 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 will recycle its TCB when it is terminated, and a joinable thread will remain its TCB when it is terminated, until a calling of `pthread_join` is called to recycle it. A thread is default to be joinable, unless it is explicitly set to be detached when it is created.

life of a NPTL pthread的更多相关文章

  1. NPTL vs PThread

    NPTL vs PThread POSIX threads (pthread) is not an implementation, it is a API specification (a stand ...

  2. Linux线程的实现 & LinuxThread vs. NPTL & 用户级内核级线程 & 线程与信号处理

    另,线程的资源占用可见:http://www.cnblogs.com/charlesblc/p/6242111.html 进程 & 线程的很多知识可以看这里:http://www.cnblog ...

  3. pthread多线程编程的学习小结

    pthread多线程编程的学习小结  pthread 同步3种方法: 1 mutex 2 条件变量 3 读写锁:支持多个线程同时读,或者一个线程写     程序员必上的开发者服务平台 —— DevSt ...

  4. posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序

    posix 线程(一):线程模型.pthread 系列函数 和 简单多线程服务器端程序 一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属 ...

  5. Linux多线程编程和Linux 2.6下的NPTL

    Linux多线程编程和Linux 2.6下的NPTL 在Linux 上,从内核角度而言,基本没有什么线程和进程的区别--大家都是进程.一个进程的多个线程只是多个特殊的进程他们虽然有各自的进程描述结构, ...

  6. 线程模型、pthread 系列函数 和 简单多线程服务器端程序

    一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属于1:1模型. (一).N:1用户线程模型 “线程实现”建立在“进程控制”机制之上,由用 ...

  7. clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结(转)

    进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合,这些资源在Linux中被抽 象成各种数据对象:进程控制块.虚存空间.文件系统,文件I/O.信号处理函数.所以创建一个进程的 过程就是这 ...

  8. [转] Linux多线程编程之pthread

    转载出处:https://blog.csdn.net/skyroben/article/details/72793409 1.背景知识 Linux没有真正意义上的线程,它的实现是由进程来模拟,所以属于 ...

  9. Pthread spinlock自旋锁

    锁机制(lock) 是多线程编程中最常用的同步机制,用来对多线程间共享的临界区(Critical Section) 进行保护. Pthreads提供了多种锁机制,常见的有:1) Mutex(互斥量): ...

随机推荐

  1. codeforces 441B. Valera and Fruits 解题报告

    题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...

  2. django错误 - Reason given for failure: CSRF cookie not set.

    练习Django表单提交时遇到如下问题: 在网上各种查找,终于找到了解决方法. 1.在from 表单中添加 {% csrf_token %} 2.在视图中添加 from django.template ...

  3. cf 620C Pearls in a Row(贪心)

    d.有一串数字,要把这些数字分成若干连续的段,每段必须至少包含2个相同的数字,怎么分才能分的段数最多? 比如 是1 2 1 3 1 2 1 那么 答案是 21 34 7 即最多分在2段,第一段是1~3 ...

  4. npm 基本命令行

    npm是随同NodeJS一起安装的包管理工具. 检查版本 npm -v 升级npm npm install npm -g 使用淘宝镜像升级 cnpm install npm -g npm instal ...

  5. Quartz 2D(常用API函数、绘制图形、点线模式)

    Quzrtz 2D 绘图的核心 API 是 CGContextRef ,它专门用于绘制各种图形. 绘制图形关键是两步: 1.获取 CGContextRef ; 2.调用 CGContextRef 的方 ...

  6. 2016 Multi-University Training Contest 3 1011【鸽巢原理】

    题解: 坐标(0,m)的话,闭区间,可能一共有多少曼哈顿距离? 2m 但是给一个n,可能存在n(n+1)/2个曼哈顿距离 所以可以用抽屉原理了 当n比抽屉的数量大,直接输出yes 不用计算 那...N ...

  7. python 字典 dict items values keys

    dict.items() 1 >>> d = dict(one=1,two=2) 2 >>> it1 = d.items() 3 >>> it1 ...

  8. 百度编辑器ueditor插件的基本使用

    注意:该插件是基于tpframe开发,请在tpframe框架上使用 插件下载地址:https://pan.baidu.com/s/1MOJbd1goQC0Bn5-7HcIdKA 插件下载下来后解压到a ...

  9. Typora练习测试

    目录 一级标题 二级标题 三级标题 一级标题 二级标题 三级标题 这是下划线 删除线 字体加粗ctrl+b 这是倾斜线 1111 牛奶 面包 鸡蛋 包子 蛋糕 测试 牛奶 面包 鸡蛋 电脑 鼠标 键盘 ...

  10. Spring事务引发dubbo服务注册问题

    文章清单 1. 问题 2. 查找bug过程 3. 解决方案 使用spring boot+dubbo写项目,一个服务,之前是正常的,后来调用方出现空指针异常,第一反应提供方出了问题. 1. 看控制台,服 ...