`getpid()` return 在进程的每个线程中是否有不同的值?
Does `getpid()` return different values in each thread of a process?
在 Pthreads 手册页中提到
Calls to getpid(2) return a different value in each thread
在 LinuxThreads 部分。
我创建了两个线程并在其中打印了 PID。但是在两者中,PID 是相同的。
int main ()
{
//pid_t pid;
pthread_t tid[2];
{
printf("In main, PID : %d, PPID : %d\n", getpid(), getppid());
pthread_create(&(tid[0]), NULL, &(f),NULL);
pthread_create(&(tid[1]), NULL, &(g),NULL);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
}
return 0;
}
void *g()
{
printf("My PID in G : %d, PPID : %d\n", getpid(), getppid());
}
void* f()
{
printf("My PID in F : %d, PPID : %d\n", getpid(), getppid());
}
下面是我得到的输出,
在主程序中,PID:5219,PPID:5214
我在 F 中的 PID:5219,PPID:5214
我在 G 中的 PID:5219,PPID:5214
我想知道我是不是理解错了什么。
在解释中提到,
The LinuxThreads implementation deviates from the POSIX.1
specification in a number of ways, including the following:
Calls to getpid(2) return a different value in each thread.
但是您很可能使用 POSIX Threads
,而不是 The LinuxThreads
。在POSIX中,线程属于一个进程,每个线程都具有相同的PID
。
在 Pthreads 手册页中提到
Calls to getpid(2) return a different value in each thread
在 LinuxThreads 部分。
我创建了两个线程并在其中打印了 PID。但是在两者中,PID 是相同的。
int main ()
{
//pid_t pid;
pthread_t tid[2];
{
printf("In main, PID : %d, PPID : %d\n", getpid(), getppid());
pthread_create(&(tid[0]), NULL, &(f),NULL);
pthread_create(&(tid[1]), NULL, &(g),NULL);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
}
return 0;
}
void *g()
{
printf("My PID in G : %d, PPID : %d\n", getpid(), getppid());
}
void* f()
{
printf("My PID in F : %d, PPID : %d\n", getpid(), getppid());
}
下面是我得到的输出,
在主程序中,PID:5219,PPID:5214
我在 F 中的 PID:5219,PPID:5214
我在 G 中的 PID:5219,PPID:5214
我想知道我是不是理解错了什么。
在解释中提到,
The LinuxThreads implementation deviates from the POSIX.1 specification in a number of ways, including the following:
Calls to getpid(2) return a different value in each thread.
但是您很可能使用 POSIX Threads
,而不是 The LinuxThreads
。在POSIX中,线程属于一个进程,每个线程都具有相同的PID
。