#include<stdio.h>
#include<stdlib.h>
#include <sys/prctl.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/syscall.h>
void* thread1(void* arg)
{
pthread_setname_np(pthread_self(),"thread1");
while(1)
{
printf("thread1 %d\n", gettid());
sleep(1000);
}
}
pid_t gettid(void)
{
return (pid_t)syscall(SYS_gettid);
}
void thread2(void)
{
while(1)
{
printf("thread2 %d\n",gettid());
sleep(1000);
}
}
int main()
{
pthread_setname_np(pthread_self(),"myThread");
pthread_t th1,th2;
void* retval;
pthread_create(&th1,NULL,thread1,NULL);
pthread_create(&th2,NULL,thread2,NULL);
pthread_setname_np(th1,"myThread1");
pthread_setname_np(th2,"myThread2");
printf("main thread %d\n",gettid());
sleep(1000);
pthread_join(&th1,&retval);
pthread_join(&th2,&retval);
}
./a.out
thread1 5259
thread2 5260
main thread 5258
ps -e -T |grep myThread
PID SPID TTY TIME CMD
5258 5258 pts/22 00:00:00 myThread
5258 5259 pts/22 00:00:00 myThread1
5258 5260 pts/22 00:00:00 myThread2
ps -Lf -p 5258
UID PID PPID LWP C NLWP STIME TTY TIME CMD
test123 5258 5067 5258 0 3 14:59 pts/22 00:00:00 ./a.out
test123 5258 5067 5259 0 3 14:59 pts/22 00:00:00 ./a.out
test123 5258 5067 5260 0 3 14:59 pts/22 00:00:00 ./a.out
ps auxww -L |grep 5258
USER PID LWP %CPU NLWP %MEM VSZ RSS TTY STAT START TIME COMMAND
test123 5258 5258 0.0 3 0.0 88444 712 pts/22 Sl+ 14:59 0:00 ./a.out
test123 5258 5259 0.0 3 0.0 88444 712 pts/22 Sl+ 14:59 0:00 ./a.out
test123 5258 5260 0.0 3 0.0 88444 712 pts/22 Sl+ 14:59 0:00 ./a.out
top -H -p 5258
top - 15:12:19 up 5:08, 5 users, load average: 0.00, 0.00, 0.00
Threads: 3 total, 0 running, 3 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.1 sy, 0.0 ni, 99.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 10230620 total, 495796 free, 2944780 used, 6790044 buff/cache
KiB Swap: 4191228 total, 4189820 free, 1408 used. 6890420 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5258 test123 20 0 88444 712 632 S 0.0 0.0 0:00.00 myThread
5259 test123 20 0 88444 712 632 S 0.0 0.0 0:00.00 myThread1
5260 test123 20 0 88444 712 632 S 0.0 0.0 0:00.00 myThread2