Linux process vs thread

Question

I have a query related to the implementation of threads in Linux. Linux does not have an explicit thread support. In userspace, we might use an thread library (like NPTL) for creating threads. Now if we use NPTL its support 1:1 mapping. The kernel will use the clone() function to implement threads. Suppose I have created 4 threads. Then it would mean that: - There will be 4 task_struct. - Inside the task_struct, there will be provision of sharing resouces as per the arguments to clone (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND) Now I have the following query:
1. Will the 4 threads have the same PID. If someone can elaborate, how the PIDs are shared.
2. How are the different threads identified; is there some tid (thread ID) concept?

Answer

The four threads will have the same PID but only when viewed from above. What you (as a user) call a PID is not what the kernel (looking from below) calls a PID.In the kernel, each thread has it's own ID, called a PID (although it would possibly make more sense to call this a TID, or thread ID) and they also have a TGID (thread group ID) which is the PID of the thread that started the whole process.Simplistically, when a new process is created, it appears as a thread where both the PID and TGID are the same (new) number.When a thread starts another thread, that started thread gets its own PID (so the scheduler can schedule it independently) but it inherits the TGID from the original thread.That way, the kernel can happily schedule threads independent of what process they belong to, while processes (thread group IDs) are reported to you. The following hierarchy of threads may help1:

                      USER VIEW
<-- PID --> <----------------- PID ----------------->
+---------+
| process |
_| pid= |_
_/ | tgid= | \_ (new thread) _
_ (fork) _/ +---------+ \
/ +---------+
+---------+ | process |
| process | | pid= |
| pid= | | tgid= |
| tgid= | +---------+
+---------+
<-- PID --> <--------- PID --------> <--- PID --->
KERNEL VIEW

You can see that starting a new process gives you a new PID and a new TGID (both set to the same value), while starting a new thread gives you a new PID while maintaining the same TGID as the thread that started it.

From:
http://stackoverflow.com/questions/9305992/linux-threads-and-process

Linux process vs thread的更多相关文章

  1. Linux Process VS Thread VS LWP

    Process program program==code+data; 一个进程可以对应多个程序,一个程序也可以变成多个进程.程序可以作为一种软件资源长期保存,以文件的形式存放在硬盘 process: ...

  2. process vs thread

    process vs thread http://blog.csdn.net/mishifangxiangdefeng/article/details/7588727 6.进程与线程的区别:系统调度是 ...

  3. Activity, Service,Task, Process and Thread之间的关系

    Activity, Service,Task, Process and Thread之间到底是什么关系呢? 首先我们来看下Task的定义,Google是这样定义Task的:a task is what ...

  4. kafka.common.KafkaException: Failed to acquire lock on file .lock in /tmp/kafka-logs. A Kafka instance in another process or thread is using this directory.

    1.刚才未启动zookeeper集群的时候,直接启动kafka脚本程序,kafka报错了,但是进程号启动起来来,再次启动出现如下所示的问题,这里先将进程号杀死,再启动脚本程序. [hadoop@sla ...

  5. Shell script for logging cpu and memory usage of a Linux process

    Shell script for logging cpu and memory usage of a Linux process http://www.unix.com/shell-programmi ...

  6. Failed to acquire lock on file .lock in /tmp/kafka-logs. A Kafka instance in another process or thread is using this directory.

    1. 问题现象 启动 kafka 时报错:Failed to acquire lock on file .lock in /tmp/kafka-logs. A Kafka instance in an ...

  7. yum安装提示错误Thread/process failed: Thread died in Berkeley DB library

    问题描述: yum 安装更新提示 rpmdb: Thread/process failed: Thread died in Berkeley DB library 问题解决: 01.删除yum临时库文 ...

  8. Performance Tuning Using Linux Process Management Commands

    [root@bigdata-server-02 /]# ps --help all Usage: ps [options] Basic options: -A, -e all processes -a ...

  9. Difference between Process and thread?

    What are the differences between a process and a thread? How are they similar? How can 2 threads com ...

随机推荐

  1. Xcode 改时间问题 lua代码没反应问题

    Xcode 改时间问题  rm -fr $_TARGET_BUILD_CONTENTS_PATH/$1/*

  2. sql 代码调试

    begin --开启事务 begin tran --插入 INSERT INTO [DICTIONARY] (TablName,Column_Id,Column_Name) --数据源 select ...

  3. 使用密码记录工具keepass来保存密码

    在第一章,曾经给过您建议,密码不要保存在文档中,那样不安全,如果密码很多而且又很复杂,人的大脑是不可能很容易记住的,只能记录下来,如果不能记在文档中那记在哪里呢?下面介绍给您一款记录密码的软件,使用. ...

  4. 一个小白App开发需要了解的基本技术

    本文针对小白用户对App做一个简单的介绍,首先要了解App都有哪些类型,不同的类型适用于哪些需求,用户可以根据自己的需求选择不同的App开发. 一 App有哪些形式 WebApp:简单来说,Web A ...

  5. 如何给CentOS安装字体库

    很多时候,我们需要做一些图像生成工作(譬如验证码之类的),这时候,我们一般都需要用到系统的字体库.但事情却总非尽善人意,我们所使用的Linux操作系统无法像Windows操作系统那样足够“旗舰”,字体 ...

  6. 《App研发录》 源码

    第1章源码: 1.1 重新规划Android项目结构 1.1.zip 1.2 为Activity定义新的生命周期 1.2.zip 1.3 统一事件编程模型 1.3.zip 1.4 实体化编程 1.4. ...

  7. JAVA理论概念大神之概念汇总

    我个人觉得,JAVA之所以能够经久不衰,有一个很重要的原因就是:JAVA的理论总是给人一种,虽然不知道是什么,但是感觉很厉害的样子.就单是这一点,他就已经超越许多其他语言了,至少吹牛的时候谈资总是很多 ...

  8. 负载均衡session会话保持方法

    负载均衡时,为了保证同一用户session会被分配到同一台服务器上,可以使用以下方法:1.使用cookie将用户的session存入cookie里,当用户分配到不同的服务器时,先判断服务器是否存在该用 ...

  9. Java的学习之路

    记事本 EditPlus eclipse Java的学习软件,已经系统性学习Java有一段时间了,接下来我想讲一下我在Java学习用到的软件. 1.第一个软件:记事本 记事本是Java学习中最基础的编 ...

  10. Sql Server系列:数据类型转换函数

    T-SQL提供了两个显示转换的函数:CAST函数和CONVERT函数. 1. CAST函数 语法: CAST ( expression AS data_type [ ( length ) ] ) 示例 ...