简体   繁体   English

已退出但可连接线程不在/ proc / [pid] / task下吗?

[英]Is exited but joinable threads not under /proc/[pid]/task?

As far as I know, threads created by pthread_create with NULL thread attribute will make the thread joinable after the thread exits if its host process is still alive. 据我所知,如果线程的宿主进程仍处于活动状态,则由pthread_create创建的具有NULL thread属性的线程将在线程退出后使该线程可连接。 So, I wrote a demo to verify this: 因此,我编写了一个演示来验证这一点:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void* thread_function(void* argv)
{
    printf("thread_function invoked\n");
    return NULL;
}

int main(int argc, char* argv[])
{
    pthread_t tid;
    pthread_create(&tid, NULL, thread_function, NULL);
    pause();
    return 0;
}

Compile it: 编译:

gcc -o 01_detach 01_detach.c -pthread gcc -o 01_detach 01_detach.c -pthread

Then run: 然后运行:

./01_detach ./01_detach
thread_function invoked 调用thread_function

Find the process: 查找过程:

ps -aux | ps -aux | grep 01_detach grep 01_detach
liucong 5856 0.0 0.0 14708 1032 pts/7 S+ 22:59 0:00 ./01_detach liucong 5856 0.0 0.0 14708 1032 pts / 7 S + 22:59 0:00 ./01_detach

Find the joinable thread: 查找可连接的线程:

ls /proc/5856/task ls / proc / 5856 / task
5856 5856

I think there should be two entries in /proc/5856/task , one for main thread and the other for the joinable thread which exited. 我认为/ proc / 5856 / task中应该有两个条目,一个用于主线程,另一个用于退出的可连接线程。 How to explain this? 怎么解释呢? Is exited threads always not under /proc/[pid]/task? 是否退出的线程始终不在/ proc / [pid] / task下? How to find out the exited but joinable threads in Linux? 如何找出Linux中已退出但可连接的线程?

Platform: 平台:

Linux liucong-vaio 4.4.0-98-generic #121~14.04.1-Ubuntu SMP Wed Oct 11 11:54:55 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux Linux liucong-vaio 4.4.0-98-generic#121〜14.04.1-Ubuntu SMP Wed Oct 11 11:54:55 UTC 2017 x86_64 x86_64 x86_64 GNU / Linux

gcc version: gcc版本:

gcc version 4.8.5 (Ubuntu 4.8.5-2ubuntu1~14.04.1) gcc版本4.8.5(Ubuntu 4.8.5-2ubuntu1〜14.04.1)

As far as I know, threads created by pthread_create with NULL thread attribute will make the thread joinable after the thread exits if its host process is still alive. 据我所知,如果线程的宿主进程仍处于活动状态,则由pthread_create创建的具有NULL thread属性的线程将在线程退出后使该线程可连接。

Pretty much yes: by default, POSIX threads are joinable until and unless they are explicitly detached. 可以肯定的是:默认情况下,POSIX线程是可联接的,直到并且除非将它们显式分离。

I think there should be two entries in /proc/5856/task , one for main thread and the other for the joinable thread which exited. 我认为/ proc / 5856 / task中应该有两个条目,一个用于主线程,另一个用于退出的可连接线程。

You seem to be jumping to a conclusion there. 您似乎正在那里得出结论。 The Linux manual page for /proc says: /proc的Linux手册页显示

This is a directory that contains one subdirectory for each thread in the process. 该目录包含进程中每个线程的一个子目录。

You have assumed that a joinable thread that has terminated but not been joined still counts as a "thread in the process" for /proc purposes. 您已假定已终止但尚未加入的可连接线程出于/proc目的仍被视为“进程中的线程”。 That would be plausible, but the docs don't lend much support to it. 这似乎是合理的,但是文档并没有对此提供太多支持。

How to explain this? 怎么解释呢? Is exited threads always not under /proc/[pid]/task? 是否退出的线程始终不在/ proc / [pid] / task下?

You have pretty much answered your own question: no, your testing shows that they are not. 您已经回答了几乎自己的问题:不,您的测试表明事实并非如此。 This is presumably covered somewhere in the kernel docs, but it is not stated explicitly in any of the docs that I turned up in a few searches. 大概在内核文档中的某处已涵盖了该方法,但是我在几次搜索中找到的任何文档中均未明确说明。 It is suggestive, however, that the same manual page I already referenced says this, too: 但是,暗示性的是,我已经参考过的同一手册页也说明了这一点:

In a multithreaded process, the contents of the /proc/[pid]/task directory are not available if the main thread has already terminated. 在多线程进程中,如果主线程已经终止,则/proc/[pid]/task目录的内容不可用。

Note well that the main thread having terminated does not imply that the whole process has terminated. 请注意,主线程已终止并不意味着整个过程已终止。

How to find out the exited but joinable threads in Linux? 如何找出Linux中已退出但可连接的线程?

I am unaware of any general-purpose mechanism to extract that information. 我不知道任何用于提取该信息的通用机制。 Certainly the pthreads API does not provide for it -- as far as pthreads is concerned, the process that wants to know about threads belonging to it is expected keep track of that information itself. 当然,pthreads API并未提供它-就pthreads而言,希望了解属于该线程的进程会跟踪该信息本身。

But all of your efforts seem to be extremely roundabout. 但是,您的所有努力似乎都非常round回。 If you want to demonstrate that a thread launched with default thread attributes is joinable, then why not just join it? 如果要演示使用默认线程属性启动的线程是可联接的,那么为什么不联接它呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM