Linux 新线程的pid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10543710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pid for new thread
提问by kai
I have a quick question about new thread created by pthread_create():
我有一个关于 pthread_create() 创建的新线程的快速问题:
When I print the pid (get from getpid()) of main thread and the child thread, they are the same while when I using htop linux utility to show pid, they are different. Can any one explain this to me?? Thanks!!
当我打印主线程和子线程的 pid(从 getpid() 获取)时,它们是相同的,而当我使用 htop linux 实用程序显示 pid 时,它们是不同的。谁能给我解释一下这个??谢谢!!
kai@kai-T420s:~/LPI$ ./pthr_create
--------------------------------------
main thread: pid: 4845, ppid: 3335
child thread: pid: 4845, ppid: 3335
htop shows:
htop 显示:
采纳答案by dwalter
Linux implements pthreads()
as Light-Weight-Processes, therefor they get a PID assigned.
Linux 实现pthreads()
为轻量级进程,因此它们会获得分配的 PID。
Further information can be found at http://www.linuxforu.com/2011/08/light-weight-processes-dissecting-linux-threads/
更多信息可以在http://www.linuxforu.com/2011/08/light-weight-processes-dissecting-linux-threads/找到
there is also an example how to get the LWP-Pid for your thread.
还有一个示例如何为您的线程获取 LWP-Pid。
#include <stdio.h>
#include <syscall.h>
#include <pthread.h>
int main()
{
pthread_t tid = pthread_self();
int sid = syscall(SYS_gettid);
printf("LWP id is %d\n", sid);
printf("POSIX thread id is %d\n", tid);
return 0;
}
回答by Andy Ross
Threads have both a process ID, returned from the getpid() syscall, and a thread ID, returned by gettid(). For the thread executing under main(), these will be identical. I don't know off hand which one htop is reporting, you should check the docs.
线程具有从 getpid() 系统调用返回的进程 ID 和由 gettid() 返回的线程 ID。对于在 main() 下执行的线程,这些将是相同的。我不知道哪个 htop 正在报告,你应该查看文档。