如何杀死与C管理线程? [英] How to kill the management thread with C?

查看:119
本文介绍了如何杀死与C管理线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code。构建应用程序是myprogram。

I have the following code. the build application is myprogram.

如果我启动myprogram然后killall myprogram紧接着,我又推出再myprogram崩溃myprogram

If I launch myprogram and then killall myprogram and immediately after that I launch again myprogram then myprogram crash.

坠机原因是由于由第一次发射创建的线程管理没有正确的第二次发射前清零。

the crash cause is due to that the management thread created by the first launch is not properly cleared before the second launch.

所以在第二次发射了when myprogram尝试创建与pthread的和旧的线程管理线程尚未去除,因此导致崩溃。

so in the second launch whent myprogram try to create thread with the pthread and the old thread management is not removed yet so it causes a crash.

有没有一种方法来杀死管理线程我第一次发射结束时或在我的第二次​​发射的用C

Are there a way to kill the management thread at the end of my first launch or at the beginning of my second launch with C?

#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 1 myprogram (1 for the main application)

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

    // ps aux | grep myprogram  ---> show 3 myprogram
    // (1st for the main application)
    // (2nd for the management thread. thread which manage all created thread)
    // (3rd for the created thread)

    sleep (20);  


    pthread_cancel(test_thread);

    // ps aux | grep myprogram  ---> show 2 myprogram and
    // (1st for the main application)
    // (2nd for the management thread. thread which manage all created thread)

    sleep(100);
    // in this period (before the finish of myprogram)
    // I execute killall to kill myprogram 
    // and then immediately I re-launch myprogram and then the program crash
    // because the management thread is not immediately killed

}

BTW:

在linux下使用 libuClibc-0.9.30.1.so ,并根据这个问题,<一个href=\"http://stackoverflow.com/questions/13290306/how-to-kill-all-subprocess-created-with-pthread-create-after-cancelling-a-thread\">How取消线程后杀死在pthread_create创建的所有子进程? 的pthread 这个libc中采用Linux线程实现,不与NPTL(本地POSIX线程库使用libc的)实施
所以管理线程将被用于这种情况下的libc只创建。

the linux use libuClibc-0.9.30.1.so and according to this question How to kill all subprocess created with pthread_create after cancelling a thread? this libc use linux thread implementation of pthread and does not use libc with NPTL ("Native posix thread library") implementation so the management thread will be created only for this case of libc.

推荐答案

我想你,因为你使用 killall <杀的线程管理器的有这个问题/ code>根据的本地POSIX线程库Linux的
从红帽
纸:

I think you're having this problem because you're killing the thread manager by using killall according to The Native POSIX Thread Library for Linux paper from Redhat:

如果经理线程被杀害的过程的其余部分是在
  国家必须手动清理。

If the manager thread gets killed the remainder of the process is in a state which must be manually cleaned up.

和相比也 Linux线程模型:

And also Linux threading models compared:

一个致命的信号可以杀死所有的线程。在这方面的设计LinuxThreads的是一贯的。 一旦进程接收到一个
  致命信号,线程管理器杀死所有其他线程
  (进程)相同的信号

A fatal signal is able to kill all the threads. The LinuxThreads design on this front has been consistent. Once a process receives a fatal signal, the thread manager kills all the other threads (processes) with the same signal.

这意味着,如果你杀线程管理器,它不会有机会杀死其他线程,所以你应该只使用杀主进程杀-p PID 不是 killall

Which means that if you kill the thread manager, it won't get a chance to kill the other threads, so you should only kill the main process using kill -p pid not killall

我想,如果主流程通常存在,或接收信号,线程管理器也将被最终当它这样做不过杀戮和等待其他线程,杀死,它也提到,如果调用了pthread_exit 所有其他进程将返回到主面前宰了:

I think if the main process exists normally, or receives a signal, the thread manager will be killed too eventually when it's done killing and waiting on other threads, however, it also mentions that if you call pthread_exit all other processes will be killed before returning to main:

如果主进程调用了pthread_exit(),过程中不
  终止。主线程进入睡眠状态,它是的工作
  管理线程来唤醒主线程在所有其他线程具有
  被杀害了。

If the main process calls pthread_exit(), the process is not terminated. The main thread goes to sleep, and it is the job of the manager thread to wake up the main thread when all other threads have been killed.

这篇关于如何杀死与C管理线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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