Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux command "kill" can send signal to threads

I created 3 thread using pthread_create utility. I could send signal to thread using
kill -SIGUSR1 thread-id.

I got thread id using ps -eLF command ( LWP field ).

I am wondering:

  1. Why we need kill?

  2. How can I use kill command to send a signal to thread group id.? How do I find the group id ?

like image 415
user1776817 Avatar asked Feb 14 '26 10:02

user1776817


1 Answers

The thread group identifier (TGID) is actually the task identifier of the main thread of the process. And the main thread's task identifier is the process identifier of the whole process. This is the value returned by getpid() from any thread in the same process. In other words, gettid() returns the same value as getpid() in the main thread.

With the ps command, the thread group identifier is obtained with the tgid format specifier. With the following ps command, we can see that the lines with pid = tgid = tid is the main thread of the processes:

$ ps -eLo pid,tgid,tid,comm
    PID    TGID     TID COMMAND
    890     890     890 rsyslogd     <-- Main thread = thread group id
    890     890     915 in:imuxsock
    890     890     916 in:imklog
    890     890     917 rs:main Q:Reg
    891     891     891 snapd        <-- Main thread = thread group id
    891     891     934 snapd
    891     891     935 snapd
    891     891     936 snapd
    891     891     937 snapd
    891     891     938 snapd
    891     891    1000 snapd
    891     891    1006 snapd
    891     891    1007 snapd
    891     891    1009 snapd
    891     891    1010 snapd
    891     891    1042 snapd
    891     891    1043 snapd
    891     891    1062 snapd
    891     891    1063 snapd
    891     891    1064 snapd
    891     891    1542 snapd
    891     891    1544 snapd
[...]

like image 80
Rachid K. Avatar answered Feb 17 '26 12:02

Rachid K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!