我有一个关于pthread_create()创建的新线程的快速问题:
当我打印主线程和子线程的pid(来自getpid()),它们是相同的,而当我使用htop linux实用程序来显示pid时,它们是不同的.任何人都可以向我解释一下吗?谢谢!!
kai@kai-T420s:~/LPI$ ./pthr_create
--------------------------------------
main thread: pid: 4845, ppid: 3335
child thread: pid: 4845, ppid: 3335
Run Code Online (Sandbox Code Playgroud)
htop显示:

dwa*_*ter 20
Linux实现pthreads()轻量级进程,因此它们获得了PID分配.
更多信息请访问http://www.linuxforu.com/2011/08/light-weight-processes-dissecting-linux-threads/
还有一个示例如何为您的线程获取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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11138 次 |
| 最近记录: |