C语言编程中pid, tid以及真实pid的关系(转)
add by zhj: 下面是我对pid,tgid,ppid的个人理解
对于ubuntu14.04操作系统,可以在/usr/src/linux-headers-4.4.0-31/include/linux/sched.h文件中看到进程控制块的结构体,如下
struct task_struct {
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
void *stack;
atomic_t usage;
unsigned int flags; /* per process flags, defined below */
unsigned int ptrace; #ifdef CONFIG_SMP
struct llist_node wake_entry;
int on_cpu;
unsigned int wakee_flips;
unsigned long wakee_flip_decay_ts;
struct task_struct *last_wakee; int wake_cpu;
#endif
......
......
pid_t pid;
pid_t tgid;
......
......
}
可以看到,里面定义了两个字段,pid和tgid,其中pid就是这个轻量级进程lwp的id,而tgid是轻量级进程组的id,当创建进程时,我们可以为自己指定
进程的tgid字段,貌似可以随便指定,只要存在就行,另外在父进程中,可以为子进程设置进程组id,如果没有指定,它会继承父进程的进程组id。
还有一个概念ppid,我没在这个结构体中找到,但操作系统肯定是会记录的,在Python中,通过os.get_ppid()就可以获取当前进程的父进程。tgid与ppid,
这两者其实没有任何关系,因为tgid是可以自己来指定的,平时基本不用,不用管它。
原文:http://blog.csdn.net/u012398613/article/details/52183708
1、pid,tid,真实pid的使用
进程pid: getpid() // 相当于os.getpid()
线程tid: pthread_self() //进程内唯一,但是在不同进程则不唯一。相当于thread.get_ident()
线程pid: syscall(SYS_gettid) //系统内是唯一的。python中没有现成的方法,需要手动调用动态链接库ctypes.CDLL('libc.so.6').syscall(xx) #include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h> struct message
{
int i;
int j;
}; void *hello(struct message *str)
{
printf("child, the tid=%lu, pid=%d\n",pthread_self(),syscall(SYS_gettid));
printf("the arg.i is %d, arg.j is %d\n",str->i,str->j);
printf("child, getpid()=%d\n",getpid());
while();
} int main(int argc, char *argv[])
{
struct message test;
pthread_t thread_id;
test.i=;
test.j=;
pthread_create(&thread_id,NULL,hello,&test);
printf("parent, the tid=%lu, pid=%d\n",pthread_self(),syscall(SYS_gettid));
printf("parent, getpid()=%d\n",getpid());
pthread_join(thread_id,NULL);
return ;
}
getpid()得到的是进程的pid,在内核中,每个线程都有自己的PID,要得到线程的PID,必须用syscall(SYS_gettid);
pthread_self函数获取的是线程ID,线程ID在某进程中是唯一的,在不同的进程中创建的线程可能出现ID值相同的情况。
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h> void *thread_one()
{
printf("thread_one:int %d main process, the tid=%lu,pid=%ld\n",getpid(),pthread_self(),syscall(SYS_gettid));
} void *thread_two()
{
printf("thread two:int %d main process, the tid=%lu,pid=%ld\n",getpid(),pthread_self(),syscall(SYS_gettid));
} int main(int argc, char *argv[])
{
pid_t pid;
pthread_t tid_one,tid_two;
if((pid=fork())==-)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if(pid==)
{
pthread_create(&tid_one,NULL,(void *)thread_one,NULL);
pthread_join(tid_one,NULL);
}
else
{
pthread_create(&tid_two,NULL,(void *)thread_two,NULL);
pthread_join(tid_two,NULL);
}
wait(NULL);
return ;
}
2、pid与tid的用途
Linux中,每个进程有一个pid,类型pid_t,由getpid()取得。Linux下的POSIX线程也有一个id,类型pthread_t,由pthread_self()取得,该id由线程维护,其id空间是各个进程独立的(即不同进程中的线程可能有相同的id)。你可能知道,Linux中的POSIX线程库实现的线程其实也是一个进程(LWP),只是该进程与主进程(启动线程的进程)共享一些资源而已,比如代码段,数据段等。
有时候我们可能需要知道线程的真实pid。比如进程P1要向另外一个进程P2中的某个线程发送信号时,既不能使用P2的pid,更不能使用线程的pthread id,而只能使用该线程的真实pid,称为tid。
有一个函数gettid()可以得到tid,但glibc并没有实现该函数,只能通过Linux的系统调用syscall来获取。使用syscall得到tid只需一行代码,但为了加深各位看官的印象,简单提供下面场景。
有一簇进程,其中一个进程中另外启了一个线程。各进程共享一个数据结构,由shared_ptr指明,其中保存有线程的tid。在各个进程的执行过程中,需要判断线程是否存在,若不存在则(重新)创建。
首先,在线程函数的开始,需要将自己的tid保存至共享内存,
点击(此处)折叠或打开
- #include <sys/syscall.h>
- #include <sys/types.h>
- void*
- thread_func(void *args)
- {
- //~ lock shared memory
- shared_ptr->tid = syscall(SYS_gettid); //~ gettid()
- //~ unlock shared memory
- //~ other stuff
- }
在各进程中判断进程是否存在,
点击(此处)折叠或打开
- //~ lock shared memory
- pthread_t id;
- if (shared_ptr->tid == 0) { //~ tid is initialized to 0
- pthread_create(&id, NULL, thread_func, NULL);
- } else if (shared_ptr->tid > 0) {
- int ret = kill(shared_ptr->tid, 0); //~ send signal 0 to thread
- if (ret != 0) { //~ thread already died
- pthread_create(&id, NULL, thread_func, NULL);
- }
- }
- //~ unlock shared memory
3、linux 系统中查看pid,tid的方法
线程进程都会有自己的ID,从操作系统来讲,这个ID就叫做PID
C语言编程中pid, tid以及真实pid的关系(转)的更多相关文章
- [C] 在 C 语言编程中实现动态数组对象
对于习惯使用高级语言编程的人来说,使用 C 语言编程最头痛的问题之一就是在使用数组需要事先确定数组长度. C 语言本身不提供动态数组这种数据结构,本文将演示如何在 C 语言编程中实现一种对象来作为动态 ...
- R语言编程中的常见错误
R语言编程中的常见错误有一些错误是R的初学者和经验丰富的R程序员都可能常犯的.如果程序出错了,请检查以下几方面. 使用了错误的大小写.help().Help()和HELP()是三个不同的函数(只有第 ...
- Linux 编程中的API函数和系统调用的关系【转】
转自:http://blog.chinaunix.net/uid-25968088-id-3426027.html 原文地址:Linux 编程中的API函数和系统调用的关系 作者:up哥小号 API: ...
- Linux下C语言编程中库的使用
零.问题 1. 为什么要用到库? 2. 我要用一个库,但是,尼玛命令行上该怎么写呢?或者说库文件如何使用? 3. Linux的库在那些地方? 4. 什么是静态库,什么是动态库,二者有啥区别? 5. 常 ...
- C语言编程中函数指针的定义及使用
C语言中函数指针的定义: typedef int (*funcPtr)(int, int)表示定义了一个函数指针funcPtr,这个函数指针只能指向如下: int add(int, int).int ...
- 【转】 嵌入式C语言编程中Inline函数的应用
源地址:https://blog.csdn.net/vigour1000/article/details/9622037 有一段儿时间没写写经验笔记了,哎,也是自己这一段时间以来(其实最近一直是这个状 ...
- Go语言编程中字符串切割方法小结
1.func Fields(s string) []string,这个函数的作用是按照1:n个空格来分割字符串最后返回的是[]string的切片 复制代码代码如下: import ( "fm ...
- c语言编程中%g是什么格式
%g用来输出实数,它根据数值的大小,自动选f格式或e格式(选择输出时占宽度较小的一种),且不输出无意义的0.即%g是根据结果自动选择科学记数法还是一般的小数记数法 printf("%g\n& ...
- C 语言项目中.h文件和.c文件的关系
http://blog.csdn.net/xingkong_678/article/details/38639847 关于两者以前的关系,要从N年以前说起了~ long long ago,once a ...
随机推荐
- Java JPA小记
什么是JPA JPA之于ORM(持久层框架,如MyBatis.Hibernate等)正如JDBC之于数据库驱动. JDBC是Java语言定义的一套标准,规范了客户端程序访问关系数据库(如MySQL.O ...
- qt在GUI显示时,将调试信息输出到控制台的设置
1. 在.pro文件中添加一下设置: CONFIG += console 2. 项目的[构建和运行]中,需要勾选[Run in terminal]:
- C语言学习之路
c语言学习 初识c语言 c语言数据类型.运算符和表达式(整数浮点数) 字符型数据/字符串 算术运算符和算术表达式(优先级,结合性等) 顺序程序设计(运算符之类内容,字符输入输出等) C/C++ 查看数 ...
- Java开发中Maven Jar包管理建议
Jar包管理规范 基于使用Git做版本控制,使用Jenkins做持续集成,以及Git-flow分支管理策略的情况: 带-SNAPSHOT为快照版本,例如1.0.0-SNAPSHOT 正式发布版本只有版 ...
- Mac vim“装逼”配置
配置c++ 等编程语言补全等 from blog http://www.cnblogs.com/xiaobo-Linux/p/8909402.html 1. 安装 macvim brew instal ...
- OSG描边特效osgFX::Outline的修改
对一个三维场景中的物体实现描边特效,可以参考osg范例osgoutline 这个描边特效使用了模板缓存Stencil来实现,参见源代码osgFX/Outline.cpp 使用了两个Pass 第一个Pa ...
- Kafka 1.1新功能:数据的路径间迁移
经常有小伙伴有这样的疑问:为什么线上Kafka机器各个磁盘间的占用不均匀,经常出现“一边倒”的情形? 这是因为Kafka只保证分区数量在各个磁盘上均匀分布,但它无法知晓每个分区实际占用空间,故很有可能 ...
- 使用Curator操作ZooKeeper
Curator是Netflix公司开源的一个ZooKeeper client library,用于简化ZooKeeper客户端编程.它包含如下模块: Framework:Framework是ZooKe ...
- Python变量访问权限控制
oop1.py文件代码 # user/bin/python class Foo: def bar(self): print('ok') def hello(self, name): print(&qu ...
- js 基础知识总结
1.switch结构 switch语句部分和case语句部分,都可以使用表达式. switch(1 + 3) { case 2 + 2: f(); break; default: neverhappe ...