获取进程的PID(process ID)

#include <unistd.h>
pid_t getpid(void);

获取线程的TID(thread ID)

1)gettid或者类似gettid的方法  :获取内核中真实的线程ID

2)直接调用pthread_self() : posix描述的线程ID。

  在POSIX线程库下每一线程也有一个ID,类型pthread_t,就是通过pthrea_self()得到的。该ID由线程库维护,每一个进程下的线程ID可能相同。

  Linux下POSIX线程库实现的线程其实也是一个进程(LWP),该进程与main(启动线程的进程)共享一些资源,比如代码段、数据段等。

详细:

  man一下gettid得到如下结果:

NAME
gettid - get thread identification SYNOPSIS
#include <sys/types.h> pid_t gettid(void); Note: There is no glibc wrapper for this system call; see NOTES. DESCRIPTION
gettid() returns the caller's thread ID (TID). In a single-threaded
process, the thread ID is equal to the process ID (PID, as returned by
getpid()). In a multithreaded process, all threads have the same PID,
but each one has a unique TID. For further details, see the discussion
of CLONE_THREAD in clone().

  gettid返回调用者的线程ID;在单线程的进程中,tid=pid(线程id),在多线程进程中,不同的线程,所有的线程又相同的pid。

  man一下pthread_self:

SYNOPSIS
#include <pthread.h> pthread_t pthread_self(void); Compile and link with -pthread. DESCRIPTION
The pthread_self() function returns the ID of the calling thread. This
is the same value that is returned in *thread in the pthread_create()
call that created this thread.

  pthread_self返回的是posix定义的线程ID,与内核tid不同。作用是可以用来区分同一进程中不同的线程。

++ pthread

  pthread是POSIX线程(POSIX threads)简称pthread,是线程的POSIX标准。该标准定义了创建和操纵线程的一整套API。

转:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
//#include <sys/syscall.h> #define __NR_gettid 186
void *f()
{
int status;
printf("begin: pid: %d, tid:%ld, self: %ld\n", getpid(), (long int)syscall(__NR_gettid), pthread_self());
int ret = fork();
if(ret == ){
printf("[child] pid: %d, tid:%ld, self: %ld\n", getpid(), (long int)syscall(__NR_gettid), pthread_self());
}else if(ret > ){
printf("[parent] pid: %d, tid:%ld, self: %ld\n", getpid(), (long int)syscall(__NR_gettid), pthread_self());
waitpid(-, &status, );
}
} int main()
{ int i = ;
pthread_t pth[];
while(i++<){
pthread_create(&pth[i], NULL, f, NULL);
sleep();
}
pause();
}

So Why? 有两个进程ID(thread ID)

  描述线程的id,为什么需要两个不同的ID呢?这是因为线程库实际上由两部分组成:内核的线程支持+用户态的库支持(glibc),Linux在早 期内核不支持线程的时候glibc就在库中(用户态)以纤程(就是用户态线程)的方式支持多线程了,POSIX thread只要求了用户编程的调用接口对内核接口没有要求。

  linux上的线程实现就是在内核支持的基础上以POSIX thread的方式对外封装了接口,所以才会有两个ID的问题。

getpid 与 gettid 与 pthread_self的更多相关文章

  1. gettid和pthread_self区别

    http://blog.csdn.net/rsyp2008/article/details/45150621 1 线程ID获取方法 Linux下获取线程有两种方法: 1)gettid或者类似getti ...

  2. gettid 和pthread_self的区别

    转: Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库 ...

  3. gettid()和pthread_self()的区别

    Linux中,每个线程有一个tid,类型long,由sys_gettid()取得. Linux内核中并没有实现线程,而是由glibc线程库实现的POSIX线程.每个线程也有一个id,类型 pthrea ...

  4. Linux2.6内核实现的是NPTL

    NPTL是一个1×1的线程模型,即一个线程对于一个操作系统的调度进程,优点是非常简单.而其他一些操作系统比如Solaris则是MxN的,M对应创建的线程数,N对应操作系统可以运行的实体.(N<M ...

  5. linux内核——进程,轻量级进程,线程,线程组

    1.进程.轻量级进程.线程.线程组之间的关系 2.及它们的标识相关说明 一.进程.轻量级进程.线程.线程组之间的关系 借助上图说明: 进程P0有四条执行流,即线程, 主线程t0是它的第一个线程,且与进 ...

  6. linux服务器开发二(系统编程)--线程相关

    线程概念 什么是线程 LWP:Light Weight Process,轻量级的进程,本质仍是进程(在Linux环境下). 进程:独立地址空间,拥有PCB. 线程:也有PCB,但没有独立的地址空间(共 ...

  7. 20155211 课下测试ch12补做

    20155211 课下测试ch12补做 有关线程图,下面说法正确的是() A.图的原点表示没有任何线程完成一条指令的初始状态 B.向右向上是合法的转换 C.向左向下是合法的转换 D.对角线是合法的转换 ...

  8. 补交课下测试(ch12并发编程) 08.第八周

    有关线程图,下面说法正确的是() A .图的原点表示没有任何线程完成一条指令的初始状态 B . 向右向上是合法的转换 C .向左向下是合法的转换 D .对角线是合法的转换 E .一个程序执行的历史被模 ...

  9. 20155332 补交ch12课下作业

    20155332 补交ch12课下作业 课下测试提交晚了,我课后补做了一遍,答对13题,答错3题. 试题内容如下所示: 课本内容 1.并发(Concurrency) 访问慢I/O设备:就像当应用程序等 ...

随机推荐

  1. Inversion of Control Containers and the Dependency Injection pattern(转)

    In the Java community there's been a rush of lightweight containers that help to assemble components ...

  2. PhpStorm (强大的PHP开发环境)2016.2.1 附注册方法

    最新版PhpStorm 2016正式版改进了PHP 7支持,改进代码完成功能. PhpStorm 是最好的PHP开发工具,使用它进行PHP开发将会让你感觉到编程的乐趣. 快乐无极终于从oschina看 ...

  3. 11-Java 界面设计

    (一)Java界面设计概述 1.Java 界面设计的用途 2.AWT 简介 (1)Abstract Windows Toolkit 是最原始的工具包. 3.Swing 简介 4.SWT 简介 5.如何 ...

  4. Type Project has no default.properties file! Edit the project properties to set one.

    Description Resource Path Location Type Project has no default.properties file! Edit the project pro ...

  5. NGINX + LUA实现复杂的控制 --源自http://outofmemory.cn/code-snippet/14396/nginx-and-lua

    安装lua_nginx_module 模块 lua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenResty Centos和debian的安装就简单了.. 这里说下freebs ...

  6. bootstrap左右圆角按钮-适配手机页面

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  7. android学习笔记56——Service

    Service四大组件之一,需要在AndroidMainfest.xml中添加相关配置,运行于后台,不与用户进行交换,没有UI... 配置时可通过<intent-filter.../>元素 ...

  8. 解决ftp连接出现 无法从控制 Socket 读取。Socket 错误 = #10054。

    ftp连接会显示以下错误信息 无法从控制 Socket 读取.Socket 错误 = #10054 或者是这样的信息 Opening data channel for directory list.T ...

  9. 配置 Gii 允许访问的 IP 地址

    通过本机以外的机器访问 Gii,请求会被出于安全原因拒绝,在 config/web.php 配置 Gii 为其添加允许访问的 IP 地址: if (YII_ENV_DEV) { // configur ...

  10. 22. Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...