如何杀死取消一个线程后在pthread_create创建的所有子进程? [英] How to kill all subprocess created with pthread_create after cancelling a thread?
问题描述
我有以下的code和我做的ps aux | grep的myprogram
中的main()code的每一个步骤的 myprogram (我构建应用程序的名称)。
在 myprogram 的的ps aux执行的一开始就| grep的myprogram
显示只有1次在 myprogram 列表
取消我在的main()
的的ps aux的乞讨创建线程之后| grep的myprogram
显示在 myprogram 两次,我希望只得到1。
能有人解释这种现象?以及如何以返回到初始状态(仅1 myprogram)
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&pthreads.h中GT;的pthread_t test_thread;无效* thread_test_run(无效* V)
{
INT I = 1;
而(1)
{
的printf(进线程%d个\\ r \\ n,I);
我++;
睡眠(1);
}
返回NULL
}诠释的main()
{
//的ps aux | grep的myprogram --->只显示1 myprogram 在pthread_create(安培; test_thread,NULL,&放大器; thread_test_run,NULL); //的ps aux | grep的myprogram --->显示3 myprogram 睡眠(20);
pthread_cancel可以(test_thread); //的ps aux | grep的myprogram --->显示2 myprogram和我的预期只有1 !! ?? //其他功能都在这里被称为... 返回0;
}
修改
在Linux中使用的libc中的 libc-0.9.30.1.so
#ls -l命令/ lib中/ | libc中的grep
-rwxr-XR-X 1根根16390 7月11日14:04 ld-uClibc-0.9.30.1.so
lrwxrwxrwx 1根根30年7月21日10:16 LD-uClibc.so.0 - > ld-uClibc-0.9.30.1.so
lrwxrwxrwx 1根根30年7月21日10:16 libc.so.0 - > libuClibc-0.9.30.1.so
-rw-R - R-- 1根根8218年07月11 14:04 libcrypt-0.9.30.1.so
lrwxrwxrwx 1根根30年7月20日10:16 libcrypt.so.0 - > libcrypt-0.9.30.1.so
-rw-R - R-- 1根根291983 7月11日14:04 libuClibc-0.9.30.1.so
我假设你有一些过时的glibc(版本2.2或2.3),它使用了LinuxThreads的实施的pthread的。
在此旧库中的一个额外的线程是由线程管理库中创建;它可以第一次调用pthread_create的后创建的;但它会睡觉时间最多的。
在新的Linux版本有glibc的使用NPTL(本地POSIX线程库)的实施。当它被使用时,你不会看到 PS AXU
线程;使用 PS阿克苏姆
(用 M
)来查看本地线程。而NPTL没有使用管理线程。
PS检查 http://pauillac.inria.fr/~xleroy/linuxthreads/ faq.html常见 D.5答案:
D.5:当我跑步时,创建N个线程,顶部或ps显示N + 2个进程运行我的程序的程序。怎么所有这些进程对应?
由于一般一个进程的每个线程的模式,有一个为初始线程和N流程,它使用pthread_create的创建线程一个进程。这使得一个进程下落不明。额外的处理相当于线程管理器螺纹,由LinuxThreads的内部创建处理线程创建和线程终止一个线程。这种额外的螺纹是睡着的大部分时间。
块引用>PPS:谢谢,穆罕默德KALLEL;谢谢,MUX功能:libc-0.9.30.1是uClibc的,似乎它使用相同的过时的LinuxThreads实现(这被称为是不能完全POSIX兼容)。这里是更新日志:的http://web.archive.org/web/20070609171609/http://www.uclibc.org/downloads/Changelog
2002年0.9.10 3月21日
主要的新特性:
Øpthreads支持,(从glibc的2.1.3的库的LinuxThreads派生)
斯特凡绍切克和埃里克安德森
块引用>I have the following code and I make
ps aux | grep myprogram
in each step of the main() code of myprogram (name of the application I build).At the beggining of the execution of myprogram, the
ps aux | grep myprogram
show only 1 time the myprogram in the listafter cancelling a thread that I created in the begging of the
main()
, theps aux | grep myprogram
show the myprogram twice and I expected to get only 1.Could some one explain this behaviour? and how to return to the initial situation (only 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; }
EDIT
the libc used by the linux is
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
解决方案I'll assume you have some outdated glibc (version 2.2 or 2.3), which used the "linuxthreads" implementation of pthread.
In this older library one additional thread is created by the library for thread management; it can be created after first call to pthread_create; but it will sleep most time.
In newer linuxes there is glibc with NPTL ("Native posix thread library") implementation. When it is used, you will not see threads in
ps axu
; useps axum
(withm
) to see native threads. And NPTL not uses a management thread.PS Check http://pauillac.inria.fr/~xleroy/linuxthreads/faq.html D.5 answer:
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: Thanks, Mohamed KALLEL; thanks, mux: libc-0.9.30.1 is uClibc and seems that it uses same outdated linuxthreads implementation (which is known to be not fully posix-compatible). Here is changelog: 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
这篇关于如何杀死取消一个线程后在pthread_create创建的所有子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!