对于ubuntu14.04操作系统,可以在/usr/src/linux-headers-4.4.0-31/include/linux/sched.h文件中看到进程控制块的结构体,如下
structtask_struct {volatile long state; /*-1 unrunnable, 0 runnable, >0 stopped*/
void *stack;
atomic_t usage;
unsignedint flags; /*per process flags, defined below*/unsignedintptrace;
#ifdef CONFIG_SMPstructllist_node wake_entry;inton_cpu;
unsignedintwakee_flips;
unsignedlongwakee_flip_decay_ts;struct task_struct *last_wakee;intwake_cpu;#endif......
......
pid_t pid;
pid_t tgid;
......
......
}
可以看到,里面定义了两个字段,pid和tgid,其中pid就是这个轻量级进程lwp的id,而tgid是lwp组的id,即主lwp的id
1、pid,tid,真实pid的使用
进程pid: getpid()
线程tid: pthread_self()//进程内唯一,但是在不同进程则不唯一。
线程pid: syscall(SYS_gettid) //系统内是唯一的
#include#include#include#include
structmessage
{inti;intj;
};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(1);
}int main(int argc, char *argv[])
{structmessage test;