如何获取linux内核线程的pid

  1. 如果是pthread,则使用,
    #include <pthread.h>
    pthread_t pthread_self(void);
  2. 如果不是pthread,即是由内核创建的线程,则使用,
    #include <sys/types.h>
    pid_t gettid(void);
    获取线程所在的进程的id,方法如下:
    #include <sys/types.h>
    #include <unistd.h>
    pid_t getpid(void);
    pid_t getppid(void);
    所以,我们在代码中使用如下的语句打印:
    printf("\ntid=%lu, pid=%lu\n", gettid(), getpid());
    这样就能获取当前代码所在的线程和进程了。
    根据打印出来的进程的pid,获取进程名的方法是:
    ls -lh /proc/pid/exe
    lrwxrwxrwx 1 root root 0 Jan 1 20:48 /proc/pid/exe -> …
    sh-3.2#
    查看thread id的方法有:
  3. sh-3.2# ps -efL | grep process,
    ps命令指定-L命令选项可以用来查看进程下所包含的所有线程。
  4. sh-3.2# ls -l /proc/pid/task/
    查看进程下当前有哪些task,这些task指的就是线程。
发布了0 篇原创文章 · 获赞 0 · 访问量 5516

猜你喜欢

转载自blog.csdn.net/runshui27/article/details/91366355
今日推荐