For the ubuntu14.04 operating system, you can see the structure of the process control block in the/usr/src/linux-headers-4.4.0-31/include/linux/sched.h file, as follows
structtask_struct {volatile LongState/*-1 unrunnable, 0 runnable, >0 stopped*/ void*Stack; atomic_t usage; unsignedintFlags/*per process flags, defined below*/unsignedintptrace, #ifdef CONFIG_SMPstructLlist_node Wake_entry; inton_cpu; unsignedintwakee_flips; unsignedLongWakee_flip_decay_ts; structTask_struct *Last_wakee; intwake_cpu;#endif..... pid_t. PID; pid_t Tgid; ...... ......}
As you can see, there are two fields defined, PID and Tgid, where PID is the ID of this lightweight process LWP, and Tgid is the ID of the LWP group, which is the ID of the main LWP.
Original: http://blog.csdn.net/u012398613/article/details/52183708
1, Pid,tid, the use of real PID
process pid:getpid () thread tid:pthread_self ()//is unique within the process, but not unique in different processes. Thread Pid:syscall (Sys_gettid)//is unique within the system#include<stdio.h>#include<pthread.h>#include<sys/types.h>#include<sys/syscall.h>structmessage{inti; intJ;};void*hello (structMessage *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);}intMainintargcChar*argv[]) { structmessage test; pthread_t thread_id; TEST.I=Ten; 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 0;}
Getpid () Gets the PID of the process, in the kernel, each thread has its own PID, to get the PID of the thread, must use Syscall (Sys_gettid);
The pthread_self function gets the thread ID, the thread ID is unique in a process, and the thread created in a different process may appear with the same ID value.
#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));}intMainintargcChar*argv[]) {pid_t pid; pthread_t Tid_one,tid_two; if((Pid=fork ()) ==-1) {perror ("Fork"); Exit (Exit_failure); } Else if(pid==0) {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 0;}
2, the use of PID and TID
In Linux, each process has a PID, type pid_t, obtained by Getpid (). The POSIX thread under Linux also has an ID, type pthread_t, obtained by Pthread_self (), which is maintained by the thread and whose ID space is independent of each process (that is, threads in different processes may have the same ID). As you may know, the threads implemented by the POSIX line libraries in Linux are actually a process (LWP), except that the process shares some resources with the main process (the process that initiates the thread), such as code snippets, data segments, and so on.
Sometimes we may need to know the true PID of the thread. For example, if the process P1 to send a signal to a thread in another process P2, neither the PID of the P2 nor the Pthread ID of the thread can be used, but only the real PID of the thread, called the TID, is used.
There is a function gettid () can get the TID, but glibc does not implement the function, only through the Linux system call Syscall to get. Using Syscall to get the TID just one line of code, but in order to deepen the impression of crossing, simply provide the following scenario.
There is a cluster of processes in which another thread is set up in one process. Each process shares a data structure, indicated by shared_ptr, which stores the TID for the thread. During the execution of each process, it is necessary to determine whether the thread exists and (re) create it if it does not exist.
First, the beginning of the thread function, you need to save your TID to the shared memory,
Click (here) to collapse or open
- #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
- }
Determine whether a process exists in each process,
Click (here) to collapse or open
- ~ 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. How to view Pid,tid in Linux system
Thread process will have its own ID, from the operating system, this ID is called PID
Quote Original
The four threads'll has the same PID but only if viewed from above. What do you (as a user) call a PID is isn't what the kernel (looking from below) calls a PID. In the kernel, each of the thread has it's own ID, called a PID (although it would possibly make + sense to call this a TID, O R thread ID) and they also has a tgid (thread group ID) which is the PID of the thread that started the whole process. Simplistically, when a new process was created, it appears as a thread where both the PID and Tgid are the same (new) Numbe R.when a thread starts another thread, that started thread gets its own PID (so the scheduler can schedule it independentl Y) but it inherits the Tgid from the original thread. That's the kernel can happily schedule threads independent of what process they belong to, while processes (thread Grou P IDs) is reported to you.
The diagram for thread inheritance is as follows:
USER VIEW <--pid--<-----------------pid-----------------> +---------+ | process | _| pid=42 |_ _/| tgid=42 | \_ (New thread) _ _ (Fork) _/ +---------+ / +---------+ +---------+ | process | | process | | pid=44 | | pid=43 | | tgid=42 | | tgid=43 | +---------+ +---------+ <--pid---<---------pid--------> <---PID---> KERNEL VIEW
Here you can see clearly that creating a new process will give a new PID and Tgid, and 2 values are the same,
When a new thread is created, a new PID is given, and the tgid is consistent with the process that was started.
Linux uses process to view thread's Method 1). htop
Press T (show process thread nesting relationship) and H (display thread), and then F4 filter the process name. 2). ps -eLf | grep java
(snapshot, with thread command, E is show all process, L is display thread, f full format output) 3). pstree -p <pid>
(Show process tree, no PID display all) 4). top -Hp <pid>
(Real-time) 5). ps -T -p <pid>
Snapshot the recommended degree is the number from small to large.
The relationship between PID, Tid and True PID in C language programming