获取进程的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. html+css二级菜单制作!

    二级菜单!!<!DOCTYPE html<html lang="e<head> <meta charset="UTF-8"> < ...

  2. libtcc使用问题一二

    问题来由: powersniff(参考博客的文章,在qq群下载最新版本)目前使用lua作为分析插件,但熟练lua的人不多.所以,移植python和tcc两类语言作为插件. tcc(即tiny c),h ...

  3. Elasticsearch查询

    Elasticsearch支持两种类型的查询:基本查询和复合查询. 基本查询,如词条查询用于查询实际数据. 复合查询,如布尔查询,可以合并多个查询, 然而,这不是全部.除了这两种类型的查询,你还可以用 ...

  4. gulp.js简单操作

    一.安装gulp 1.深入设置任务之前,需先安装gulp: $ npm install gulp -g 2.这会将gulp安装到全域环境下,让你可以存取gulp的CLI.接著,需要在本地端的专案进行安 ...

  5. Provisional, Temporary 和Interim 的区别

    1 Provisional  adj. 临时的.暂时的.暂定的:n. 临时邮票 强调在一定时期内暂时的.双方同意的但还不是最终确定的决定或者条约等. Such as例如: Provisional go ...

  6. Hadoop学习14--Hadoop之一点点理解yarn

    yarn是一个分布式的资源管理系统. 它诞生的原因是原来的MapReduce框架的一些不足: 1.JobTracker单点故障隐患 2.JobTracker承担的任务太多,维护Job状态,Job的ta ...

  7. Linq系列

    LINQ 图解 Linq中的Select——投影 Linq学习资源 Expert C# 5.0中的Linq部分

  8. Jmeter安装

    安装下载方法:http://www.jb51.net/softjc/266834.html 下载地址:http://jmeter.apache.org/download_jmeter.cgi Wind ...

  9. 【转】Samba配置文件详解

    一. 客户命令: 1. smbclient smbclient 命令用来存取远程 samba 服务器上的资源,它的界面到目前为止还是文本方式的,命令形式和 ftp 类似. smbclient 命令的语 ...

  10. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...