简体   繁体   English

如何杀死 Linux Kernel 线程?

[英]How to kill Linux Kernel Thread?

I am developing Linux kernel module.我正在开发 Linux kernel 模块。 This module is communicating with my user-space C application.该模块正在与我的用户空间 C 应用程序进行通信。 In this module, I am creating a thread.在这个模块中,我正在创建一个线程。 this is a part of my module where I am facing the problem:这是我面临问题的模块的一部分:

static struct task_struct *thread1;

static ssize_t read_pid(struct file *pfile, char __user *buffer, size_t length, loff_t *offset)
{

unsigned int rand;
get_random_bytes(&rand, sizeof(rand));
rand = rand % 250;
char our_thread[20];
sprintf(our_thread, "thread%u", rand);

if(thread1==NULL)
{
thread1 = kthread_create(read_in_thread,&args,our_thread);
    if((thread1))
        {
            printk(KERN_INFO "Thread is created\n");
            printk("thread name %s\n", our_thread);
            // lancement du thread
            wake_up_process(thread1);
            printk(KERN_INFO "Thread is awake\n");
        }
}

else

printk("THREAD1 IS NOT NULL!!! CAN NOT CREATE THREAD!!!\n");

}

When I execute my user-space program for the first time, everything is working well and it prints for me on the console:当我第一次执行我的用户空间程序时,一切都运行良好,它会在控制台上为我打印:

Thread is created 
thread name
Thread is awake 

But when I press Ctrl+C to exit the running user space code and execute it again, it prints:但是当我按Ctrl+C退出正在运行的用户空间代码并再次执行时,它会打印:

THREAD1 IS NOT NULL!!! CAN NOT CREATE THREAD!!!

I think that *thread1 is not free or the thread is not killed.我认为*thread1不是免费的或者线程没有被杀死。 I think that I need to kill the thread under else statement in order I can create another one when I re-run my program.我认为我需要在else语句下终止线程,以便在重新运行程序时创建另一个线程。

Could anyone help me?有人可以帮我吗? Thank you.谢谢你。

plz try Cntrl+Z to kill the thread.请尝试Cntrl+Z杀死线程。 or below process might help if you dont care about the dependencies of thread.如果您不关心线程的依赖关系,或以下过程可能会有所帮助。 ps -e -T | grep myp | grep -v grep ps -e -T | grep myp | grep -v grep kill pid# ps -e -T | grep myp | grep -v grep kill pid#

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

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