Understanding Linux processes, threads, Pid,lwp,tid,tgid

Source: Internet
Author: User
Tags session id postgresql

In the Linux top and PS commands, the default is to see the PID (Process id), perhaps you can see the LWP (thread id) and Tgid (thread group ID for the thread group leader), and so on, while the Li Nux library functions and system calls maybe you noticed the Pthread ID and the TID, and so on. There are more IDs, such as PGRP (process group ID), sid (session ID for the session leader) and Tpgid (TTY process group ID for the process GR OUP leader). Too many concepts may be faint, but as long as you have an accurate understanding of the basic concepts of Linux processes and threads, the meanings of these IDs are solved. The core concepts of processes and threads are described below, and the relationships between these IDs are validated with a sample program.





Processes and threads for Linux


Linux processes and threads have a lot of similarities and differences, can be under Google. But as long as you can clearly understand the points, it is enough to understand the meaning of the various IDs in Linux.


    • A process is the basic unit of resource allocation, and a thread is the basic unit of Dispatch
    • A process is a collection of resources , including memory address space, file descriptors, and so on, in which multiple threads in a process share these resources.
    • When the CPU dispatches a task, the basic unit of dispatch (Dispatchable entity) is a thread . If there are no other threads in a process, it can be understood that there is only one main thread in the process, and that the main process is exclusive to all resources in the process.
    • The individual processes are completely independent, and the threads are interdependent and share resources. In a multi-process environment, the termination of any one process does not affect other non-child processes. In a multithreaded environment, the parent thread terminates and all child threads are forced to terminate (without resources).


The 1th note above is the most basic and most important.






Initial understanding of the various IDs. Basically, from high to low, IDs below the split line are less important.


    • PID: Process ID.
    • LWP: Thread ID. A common display in user-configured commands such as PS.
    • tid: Thread ID, equal to LWP. TID is more commonly used in system-provided interface functions, such as Syscall (Sys_gettid) and Syscall (__nr_gettid).
    • tgid: Thread group ID, which is the process ID of thread group leader, equals PID.
    • ------Split Line------
    • pgid: Process group ID, which is the process ID of the process group leader.
    • pthread ID: The ID provided by the Pthread library, the effective range is not at the system level and can be ignored.
    • SID: Session ID for the session leader.
    • tpgid: TTY process group ID for the process group leader.


As you can see from the list above, the various IDs are finally attributed to the PID and LWP (TID). So understanding the various IDs ultimately boils down to understanding the connections and differences between PID and LWP (TID) .






The following figure is a diagram that describes the relationship between a parent-child process and a thread.






A good description of user view and kernel view (kernel view) See the difference between threads:


    • From the user's perspective, the tid 44 thread generated in PID 42 belongs to Tgid (the process ID of the thread group leader). Even with the default parameters of Ps and top, you cannot see the TID 44 thread.
    • From the kernel perspective,tid 42 and Tid 44 are independent dispatch units that can be viewed as "pid 42" and "pid 44".


It is important to note that sometimes the process and thread distinctions in Linux are not very strict. Even if the threads and processes are mixed, the PID and TID mix, depending on the context, can still clearly distinguish the meaning of the other person wants to express. , from the kernel perspective to see the PID 44, is from the point of view of the dispatch unit, but in the top or PS command, you can absolutely not find a PID 44 process, only see an LWP (TID) is a 44 thread.





Sample program for understanding PID and LWP (TID)


A sample program is used to further understand the PID and LWP (TID) and to print out various IDs using the formatted PS command. The following program creates 2 sub-threads in the main function, plus the main thread, which has a total of 3 threads. Verify the relationship between the PID and the LWP (TID) by printing the Pthread ID, PID, and LWP (TID) separately in 3 threads.


#include <unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <pthread.h>

#define gettidv1() syscall(__NR_gettid) // new form
#define gettidv2() syscall(SYS_gettid)  // traditional form

void *ThreadFunc1()
{
        printf("the pthread_1 id is %ld\n", pthread_self());
        printf("the thread_1's Pid is %d\n", getpid());
        printf("The LWPID/tid of thread_1 is: %ld\n", (long int)gettidv1());
        pause();

        return 0;
}

void *ThreadFunc2()
{
        printf("the pthread_2 id is %ld\n", pthread_self());
        printf("the thread_2's Pid is %d\n", getpid());
        printf("The LWPID/tid of thread_2 is: %ld\n", (long int)gettidv1());
        pause();

        return 0;
}

int main(int argc, char *argv[])
{
        pid_t tid;
        pthread_t pthread_id;

        printf("the master thread's pthread id is %ld\n", pthread_self());
        printf("the master thread's Pid is %d\n", getpid());
        printf("The LWPID of master thread is: %ld\n", (long int)gettidv1());

        // Create 2 threads         pthread_create(&pthread_id, NULL, ThreadFunc2, NULL);
        pthread_create(&pthread_id, NULL, ThreadFunc1, NULL);
        pause();

        return 0;
} 


Note that when compiling, you use-l to specify the library parameters.







gcc threadtest.c-o threadtest-l pthread


Execute the procedure with the following results:


# ./threadTest
the master thread's pthread id is 140154481125184
the master thread's Pid is 20992
The LWPID of master thread is: 20992
the pthread_1 id is 140154464352000
the thread_1's Pid is 20992
The LWPID/tid of thread_1 is: 20994
the pthread_2 id is 140154472744704
the thread_2's Pid is 20992
The LWPID/tid of thread_2 is: 20993


The above results indicate that the Pthread ID is the ID provided by the Pthread library and has no meaning at the system level. The PID is the process ID of the thread group leader, which is 20992. The LWP (TID) is the thread ID, which is 20993 and 20994, respectively.






While using PS to view the results, note that PS defaults to printing only process-level information and requires the-l option to view the thread's basic information.







# ps -eo pid,tid,lwp,tgid,pgrp,sid,tpgid,args -L | awk '{if(NR==1) print $0; if($8~/threadTest/) print $0}'
  PID   TID   LWP  TGID  PGRP   SID TPGID COMMAND
20992 20992 20992 20992 20992 30481 20992 ./threadTest
20992 20993 20993 20992 20992 30481 20992 ./threadTest
20992 20994 20994 20992 20992 30481 20992 ./threadTest


From the above results, you can see:


    • pid=tgid:20992
    • tid=lwp:20993 or 20994
    • As for sid,30481 is the process ID of the bash shell.




Linux User-state command View Thread top


The default top displays the number of tasks, which is the process.






You can switch to a thread by tapping "H". As you can see, there are actually 96 threads. You can also use the top-h command directly to print thread conditions directly.








Ps


The-l option of PS can be used to see threads and can usually print out information about LWP and NLWP. You can view the thread information as follows:







PS -elf




Pidstat


pidstat-t [-P PID number] can print the relationship between the threads.





Htop


To enable thread viewing in Htop, turn on htop and press <F2> to enter the Htop settings menu. Select display options under the Settings bar, and then turn on the tree view and show custom thread names options. Press <F10> to exit settings.
Note: Mac's F2 press FN+F2.





Reference


The difference between a Linux process and a thread


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.