c - 取消线程后如何杀死所有使用 pthread_create 创建的子进程?

标签 c linux pthreads

我有以下代码,我制作ps aux | myprogram(我构建的应用程序的名称)的 main() 代码的每个步骤中的 grep myprogram

在开始执行myprogram 时,ps aux | grep myprogram 只显示列表中 myprogram 的 1 次

取消我在 main() 开始时创建的线程后,ps aux | grep myprogram 显示 myprogram 两次,我预计只得到 1 个。

有人可以解释这种行为吗?以及如何回到初始状态(只有1个myprogram)

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_t test_thread;

void *thread_test_run (void *v)
{
    int i=1;
    while(1)
    {
       printf("into thread %d\r\n",i);
       i++; 
       sleep(1);
    }
    return NULL
}

int main()
{
    // ps aux | grep myprogram  ---> show only 1 myprogram

    pthread_create(&test_thread, NULL, &thread_test_run, NULL);

    // ps aux | grep myprogram  ---> show  3 myprogram

    sleep (20);  


    pthread_cancel(test_thread);

    // ps aux | grep myprogram  ---> show 2 myprogram and I expected only 1 !!??

   // other function are called here...

    return 0;
}

编辑

linux使用的libc是libc-0.9.30.1.so

# ls -l /lib/| grep libc
-rwxr-xr-x    1 root     root        16390 Jul 11 14:04 ld-uClibc-0.9.30.1.so
lrwxrwxrwx    1 root     root           21 Jul 30 10:16 ld-uClibc.so.0 -> ld-uClibc-0.9.30.1.so
lrwxrwxrwx    1 root     root           21 Jul 30 10:16 libc.so.0 -> libuClibc-0.9.30.1.so
-rw-r--r--    1 root     root         8218 Jul 11 14:04 libcrypt-0.9.30.1.so
lrwxrwxrwx    1 root     root           20 Jul 30 10:16 libcrypt.so.0 -> libcrypt-0.9.30.1.so
-rw-r--r--    1 root     root       291983 Jul 11 14:04 libuClibc-0.9.30.1.so

最佳答案

我假设您有一些过时的 glibc(版本 2.2 或 2.3),它使用了 pthread 的“linuxthreads”实现。

在这个较旧的库中,库创建了一个额外的线程用于线程管理;它可以在第一次调用 pthread_create 后创建;但大部分时间它会休眠。

在较新的 Linux 中,有带有 NPTL(“本地 posix 线程库”)实现的 glibc。使用的时候,在ps axu中是看不到线程的;使用 ps axum(与 m)查看 native 线程。并且 NPTL 不使用管理线程。

PS 检查 http://pauillac.inria.fr/~xleroy/linuxthreads/faq.html D.5 答案:

D.5: When I'm running a program that creates N threads, top or ps display N+2 processes that are running my program. What do all these processes correspond to?

Due to the general "one process per thread" model, there's one process for the initial thread and N processes for the threads it created using pthread_create. That leaves one process unaccounted for. That extra process corresponds to the "thread manager" thread, a thread created internally by LinuxThreads to handle thread creation and thread termination. This extra thread is asleep most of the time.

PPS:谢谢,Mohamed KALLEL;谢谢,mux:libc-0.9.30.1 是 uClibc,它似乎使用相同的过时的 linuxthreads 实现(已知不完全兼容 posix)。这是变更日志:http://web.archive.org/web/20070609171609/http://www.uclibc.org/downloads/Changelog

0.9.10 21 March 2002

Major new features: o pthreads support (derived from glibc 2.1.3's linuxthreads library) by Stefan Soucek and Erik Andersen

关于c - 取消线程后如何杀死所有使用 pthread_create 创建的子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13290306/

相关文章:

c - 从正则表达式到 NFA 再到 DFA

linux - bash 脚本中的事件指示符

C 套接字和 openssl (RSA)

C 语言 - 将字符串参数插入 sqlite 数据库 %s 不起作用

linux - 如何确定 NTP 通信状态

linux - 关于RPM升级

linux - NPTL 默认堆栈大小问题

c - pthread_mutex 和进程终止

linux - 在 Linux 中等待多线程事件的最佳实践(如 WaitForMultipleObjects)

c - 各个函数的内存分配?