了解Pthreads中 [英] Understanding Pthreads

查看:174
本文介绍了了解Pthreads中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到一个概念的高级Linux编程来到。这里是一个链接:指 4.5的GNU / Linux线程执行

I came across a concept in Advanced Linux Programming. Here's a link: refer to 4.5 GNU/Linux Thread Implementation.

我的理念是什么笔者说的清楚,但我很困惑与他所要打印进程ID的解释了线程的程序。

I'm clear with the concept what author say's, but I'm confused with the program he has explained for printing processID's for threads.

下面是code

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void* thread_function (void* arg)
{
    fprintf (stderr, "child thread pid is %d\n", (int) getpid ());
    /* Spin forever. */
    while (1);
    return NULL; 
}

int main ()
{
    pthread_t thread;
    fprintf (stderr, "main thread pid is %d\n", (int) getpid ());
    pthread_create (&thread, NULL, &thread_function, NULL);
    /* Spin forever. */
    while (1);
    return 0;
} 

根据笔者上述code的输出是

The output for the above code according to author is

% cc thread-pid.c -o thread-pid -lpthread
% ./thread-pid &
[1] 14608
main thread pid is 14608
child thread pid is 14610 

我编译时得到的输出是

The output I get when I compile is

[1] 3106
main thread pid is 3106
child thread pid is 3106

据我了解,创建一个线程,linux的内部调用克隆(大多数情况下),相同的系统调用不创建一个进程。唯一的区别是在过程共享同一进程地址空间创建的线程,而过程由一个父进程副本的父进程地址空间创建。所以,我认为是印刷过程中的ID线程产生相同的进程ID。但是,它的不一样导致的书。

I understand that to create a thread, linux internally calls clone(most of the cases), same as fork system call does to create a process. The only difference is thread created in process share the same process address space, while process created by a parent process copies the parent process address space. So, what I think is printing process ID in threads result in the same processID. but, its not the same result in book.

请Tellme公司是什么,他说什么..?答案是错的书/雷..?

Please tellme what is he talking about..? Is the answer wrong in the book/mine..?

推荐答案

我得到包含libc中的书与Linux的相同的结果 libuClibc-0.9.30.1.so (1)

I get the same results of the book with linux that contains the libc libuClibc-0.9.30.1.so (1).

root@OpenWrt:~# ./test
main thread pid is 1151
child thread pid is 1153

和我试图与包含从Ubuntu的libc的一个linux运行这个程序 libc6的 (2)

and I tried to run this program with a linux that contains the libc from ubuntu libc6 (2)

$ ./test
main thread pid is 2609
child thread pid is 2609

libc中的(1)使用的LinuxThreads 实施的pthread

The libc (1) use linuxthreads implementation of pthread

和libc中的(2)使用 NPTL (本地POSIX线程库)实现的pthread

And the libc (2) use NPTL ("Native posix thread library") implementation of pthread

按照 LinuxThreads的常见问题解答(在J.3回答):

According to the linuxthreads FAQ (in J.3 answer):

每个线程确实是一个独特的PID鲜明的过程,
  发送到一个线程的PID信号只能由该线程来处理

each thread is really a distinct process with a distinct PID, and signals sent to the PID of a thread can only be handled by that thread

因此​​,在旧的libc它们使用的LinuxThreads 执行,每个线程都有其独特的PID

So in the old libc which use linuxthreads implementation, each thread has its distinct PID

在新的libc版本,它使用 NPTL 的实施,所有主题的主要过程的相同的PID。

In the new libc version which use NPTL implementation, all threads has the same PID of the main process.

NPTL 是由红帽团队开发。并根据红帽NPTL文件:在一个这是在解决的问题 NPTL 的实现是:

The NPTL was developed by redhat team. and according to the redhat NPTL document: One of the problems which are solved in the NPTL implementation is:

(章:与现有的执行问题,第5页)

具有不同的进程ID,每个线程会导致兼容性
  问题与其他POSIX线程实现。这部分是一个
  争论的焦点,因为信号can'tbe用得非常好,但仍
  值得注意

Each thread having a different process ID causes compatibility problems with other POSIX thread implementations. This is in part a moot point since signals can'tbe used very well but is still noticeable

和解释您的问题。

您正在使用包含新版本的libc在 NPTL (本地POSIX线程库)实现的pthread

You are using the new libc version that contains the NPTL ("Native posix thread library") implementation of pthread

和图书使用旧版本包含的libc 的LinuxThreads 实施的pthread

And the Book use an old version of libc that contains linuxthreads implementation of pthread

这篇关于了解Pthreads中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆