getpid 与 gettid 与 pthread_self
获取进程的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的更多相关文章
- gettid和pthread_self区别
http://blog.csdn.net/rsyp2008/article/details/45150621 1 线程ID获取方法 Linux下获取线程有两种方法: 1)gettid或者类似getti ...
- gettid 和pthread_self的区别
转: Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库 ...
- gettid()和pthread_self()的区别
Linux中,每个线程有一个tid,类型long,由sys_gettid()取得. Linux内核中并没有实现线程,而是由glibc线程库实现的POSIX线程.每个线程也有一个id,类型 pthrea ...
- Linux2.6内核实现的是NPTL
NPTL是一个1×1的线程模型,即一个线程对于一个操作系统的调度进程,优点是非常简单.而其他一些操作系统比如Solaris则是MxN的,M对应创建的线程数,N对应操作系统可以运行的实体.(N<M ...
- linux内核——进程,轻量级进程,线程,线程组
1.进程.轻量级进程.线程.线程组之间的关系 2.及它们的标识相关说明 一.进程.轻量级进程.线程.线程组之间的关系 借助上图说明: 进程P0有四条执行流,即线程, 主线程t0是它的第一个线程,且与进 ...
- linux服务器开发二(系统编程)--线程相关
线程概念 什么是线程 LWP:Light Weight Process,轻量级的进程,本质仍是进程(在Linux环境下). 进程:独立地址空间,拥有PCB. 线程:也有PCB,但没有独立的地址空间(共 ...
- 20155211 课下测试ch12补做
20155211 课下测试ch12补做 有关线程图,下面说法正确的是() A.图的原点表示没有任何线程完成一条指令的初始状态 B.向右向上是合法的转换 C.向左向下是合法的转换 D.对角线是合法的转换 ...
- 补交课下测试(ch12并发编程) 08.第八周
有关线程图,下面说法正确的是() A .图的原点表示没有任何线程完成一条指令的初始状态 B . 向右向上是合法的转换 C .向左向下是合法的转换 D .对角线是合法的转换 E .一个程序执行的历史被模 ...
- 20155332 补交ch12课下作业
20155332 补交ch12课下作业 课下测试提交晚了,我课后补做了一遍,答对13题,答错3题. 试题内容如下所示: 课本内容 1.并发(Concurrency) 访问慢I/O设备:就像当应用程序等 ...
随机推荐
- UVA 10474 大理石在哪 lower_bound
题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...
- 基于MVC4+EasyUI的Web开发框架形成之旅--基类控制器CRUD的操作
在上一篇随笔中,我对Web开发框架的总体界面进行了介绍,其中并提到了我的<Web开发框架>的控制器的设计关系,Web开发框架沿用了我的<Winform开发框架>的很多架构设计思 ...
- android中的AIDL进程间通信
关于IPC应该不用多介绍了,Android系统中的进程之间不能共享内存,那么如果两个不同的应用程序之间需要通讯怎么办呢?比如公司的一个项目要更新,产品的需求是依附于当前项目开发一个插件,但是呢这个插件 ...
- 3D Touch集成过程整理
1.集成App图标按压快速打开某个功能 在AppDelegate.m中加入以下三个东西 在启动方法里加入3D Touch菜单 - (BOOL)application:(UIApplication *) ...
- 12 Linux下crontab详解
1. 概述: crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进 ...
- [ActionScritp 3.0] 使用LocalConnection建立通信
包 flash.net 类 public class LocalConnection 继承 LocalConnection → EventDispatcher → Object 语言版本: Acti ...
- finder的隐藏文件&IOS虚拟机地址
在终端里输入下面命令即可让它们显示出来. defaults write com.apple.finder AppleShowAllFiles -bool true 如果想恢复隐藏,可以用这个命令: ...
- 1. Longest Palindromic Substring ( 最长回文子串 )
要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...
- 无法连接远程SQL Server 数据库的原因
1. tcp协议开启 2. 1433端口是否添加防火墙例外
- Windows下利用Windbg 分析dump
概述: 注册生成dump文件的函数. 当程序收到没有捕获的异常时,调用上述函数,生成dump文件. 利用Windbg结合编译程序时生成的pdb和代码来分析dump文件,定位问题. 如下代码生成dump ...