我正在读一本关于unix的书,.It在linux中说,线程有不同的pid。并给出下面的代码来打印pid和线程id。我使用SUSE和gcc.However,我得到相同的pid.Can,有人告诉我为什么?谢谢。
#include "pthread.h"
pthread_t ntid;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid = %u tid = %u (0x%x)\n",s,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid);
}
void *thr_fn(void *arg)
{
printids("new thread :");
return (void *)0;
}
int main()
{
int err;
err = pthread_create(&ntid,NULL,thr_fn,strerror(err));
if(err != 0)
err_quit("can't create new thread :%s\n",strerror(err));
printids("main thread :");
sleep(1);
exit(0);
}但是,我明白:
main thread : pid = 2945 tid = 3075803392 (0xb7550900)
new thread : pid = 2945 tid = 3075799872 (0xb754fb40)发布于 2013-08-02 13:47:25
在Linux中,当从用户空间查看时,线程具有相同的线程,在视图空间中,每个线程都有一个单独的PID。
发布于 2013-08-02 13:45:36
线程是同一个进程中的一个概念,它们总是具有相同的pid。只有在处理进程时,pid才会更改;在这种情况下,子进程的pid将与父进程不同。
https://stackoverflow.com/questions/18018419
复制相似问题