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

Source: Internet
Author: User
Tags session id

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.

1#include <unistd.h>2#include <sys/syscall.h>3#include <stdio.h>4#include <pthread.h>5 6 #defineGettidv1 () syscall (__nr_gettid)//New Form7 #defineGettidv2 () syscall (Sys_gettid)//traditional Form8 9 void*ThreadFunc1 ()Ten { Oneprintf"The pthread_1 ID is%ld\n", Pthread_self ()); Aprintf"The thread_1 ' s Pid is%d\n", Getpid ()); -printf"The Lwpid/tid of Thread_1 is:%ld\n", (Long int) Gettidv1 ()); - pause (); the  -         return 0; - } -  + void*ThreadFunc2 () - { +printf"The pthread_2 ID is%ld\n", Pthread_self ()); Aprintf"The thread_2 ' s Pid is%d\n", Getpid ()); atprintf"The Lwpid/tid of Thread_2 is:%ld\n", (Long int) Gettidv1 ()); - pause (); -  -         return 0; - } -  in intMainintargcChar*argv[]) - { to pid_t tid; + pthread_t pthread_id; -  theprintf"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 ());Panax Notoginseng  -         //Create 2 Threads thePthread_create (&pthread_id, NULL, THREADFUNC2, NULL); +Pthread_create (&pthread_id, NULL, THREADFUNC1, NULL); A pause (); the  +         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:

# ./threadtestthe Master Thread's pthread ID is 140154481125184The master thread's Pid is 20992The lwpid of Master thread is:20992The pthread_1IDIs140154464352000The thread_1's Pid is 20992The Lwpid/tid of Thread_1 is:20994The pthread_2IDIs140154472744704The thread_2's Pid is 20992The 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 $; if ($8~/threadtest/) print $}'PID TID LWP tgid pgrp SID tpgid COMMAND20992 20992 20992 20992 20992 30481 20992./ThreadTest20992 20993 20993 20992 20992 30481 20992./ThreadTest20992 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 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 can print the relationship between 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

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

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.