如何在rmmod上停止Linux内核线程?

我编写了以下代码来创建内核线程:

#include #include #include #include #include struct task_struct *task; int data; int ret; int thread_function(void *data) { int var; var = 10; return var; } static int kernel_init(void) { data = 20; printk(KERN_INFO"--------------------------------------------"); task = kthread_create(&thread_function,(void *)data,"pradeep"); task = kthread_run(&thread_function,(void *)data,"pradeep"); printk(KERN_INFO"Kernel Thread : %s\n",task->comm); return 0; } static void kernel_exit(void) { ret = kthread_stop(task); } module_init(kernel_init); module_exit(kernel_exit); 

在给出insmod命令后,我能够创建一个名为“pradeep”的内核线程,我可以使用ps -ef命令查看新线程,如下所示

 root 6071 2 0 10:21 ? 00:00:00 [pradeep] 

并且它的父级是kthreadd,其PID为2.但是我无法在给出rmmod命令时停止此线程。 它提供以下输出:

 ERROR: Removing 'pradeep': Device or resource busy. 

有人可以告诉我如何杀死这个post?

您应该只使用kthread_create()kthread_run()

 /** * kthread_run - create and wake a thread. * @threadfn: the function to run until signal_pending(current). * @data: data ptr for @threadfn. * @namefmt: printf-style name for the thread. * * Description: Convenient wrapper for kthread_create() followed by * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM). */ #define kthread_run(threadfn, data, namefmt, ...) \ ({ \ struct task_struct *__k \ = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \ if (!IS_ERR(__k)) \ wake_up_process(__k); \ __k; \ }) 

所以你要创建两个线程并泄漏其中一个:

 task = kthread_create(&thread_function,(void*) &data,"pradeep"); task = kthread_run(&thread_function,(void*) &data,"pradeep"); 

此外,您的线程函数可能缺少一些细节:

 /** * kthread_create - create a kthread. * @threadfn: the function to run until signal_pending(current). * @data: data ptr for @threadfn. * @namefmt: printf-style name for the thread. * * Description: This helper function creates and names a kernel * thread. The thread will be stopped: use wake_up_process() to start * it. See also kthread_run(). * * When woken, the thread will run @threadfn() with @data as its * argument. @threadfn() can either call do_exit() directly if it is a * standalone thread for which noone will call kthread_stop(), or * return when 'kthread_should_stop()' is true (which means * kthread_stop() has been called). The return value should be zero * or a negative error number; it will be passed to kthread_stop(). * * Returns a task_struct or ERR_PTR(-ENOMEM). */ 

我认为终止线程的两个选择是:

  1. 完成后调用do_exit()
  2. 当另一个线程调用kthread_stop()时返回一个值。

希望在解决这两个小问题之后,你将拥有一个function线程创建者/收割者。

我希望下面的程序解决你的问题….竖起大拇指:-)

 `#include #include #include #include #include` struct task_struct *task; int data; int ret; int thread_function(void *data) { int var; var = 10; printk(KERN_INFO "IN THREAD FUNCTION"); while(!kthread_should_stop()){ schedule(); } /*do_exit(1);*/ return var; } static int kernel_init(void) { data = 20; printk(KERN_INFO"--------------------------------------------"); /*task = kthread_create(&thread_function,(void *)data,"pradeep");*/ task = kthread_run(&thread_function,(void *)data,"pradeep"); printk(KERN_INFO"Kernel Thread : %s\n",task->comm); return 0; } static void kernel_exit(void) { kthread_stop(task); } module_init(kernel_init); module_exit(kernel_exit); MODULE_AUTHOR("SHRQ"); MODULE_LICENSE("GPL"); 

在代码中你不需要使用kthread_create api,因为kthread_run在内部执行它。使用其中之一

task = kthread_create(&thread_function,(void *)data,“pradeep”);
要么
task = kthread_run(&thread_function,(void *)data,“pradeep”);

此外,您的模块不受GPL许可。 这可能是您的问题的一个原因。