2011-03-12 11 views
22

que escribió el código siguiente para crear un hilo del núcleo:¿Cómo detener los hilos del kernel de Linux en rmmod?

#include<linux/init.h> 
#include<linux/module.h> 
#include<linux/kernel.h> 
#include<linux/kthread.h> 
#include<linux/sched.h> 

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); 

En dando el comando insmod, soy capaz de crear un hilo de núcleo denominado "Pradeep" y puedo ver el nuevo hilo con el comando ps -ef de la siguiente manera

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

y su matriz se KThreadd cuyo PID es 2. pero no soy capaz de detener este hilo en dar rmmod comando. Está dando la siguiente salida:

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

¿Alguien puede decirme cómo matar este hilo?

Respuesta

38

Usted debe utilizar solamente uno de kthread_create() o 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;                \ 
}) 

Así que va a crear dos roscas y fugas de uno de ellos:

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

Además, su función del hilo podría ser faltan algunos detalles:

/** 
* 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). 
*/ 

creo que las dos opciones para la terminación de un hilo son:

  1. llamada do_exit() cuando haya terminado.
  2. Devuelve un valor cuando otro hilo llama al kthread_stop().

Afortunadamente, después de solucionar estos dos pequeños problemas, tendrás un creador/segador de hilo funcional.

+0

que corrigen el primer problema, pero yo no entendía la segunda prbblem. ¿Puede por favor explicarme el segundo en detalle? – pradeepchhetri

+1

@pradeepchhetri, ¿resolvió su problema? Lamento haberme perdido esto por casi una semana.Sobre la terminación del hilo, parece que solo tiene dos opciones: llamar 'do_exit()' en su función de hilo (en lugar de caer al final de la función o intentar devolver un valor); o sondear 'kthread_should_stop()' periódicamente: cuando devuelve verdadero, entonces necesita 'devolver 0' (para tener éxito) o' devolver' uno de los códigos de error por un error. – sarnold

+0

Hola, estoy escribiendo un controlador intensivo multi threading en kernel. Como, printk no funciona con los hilos del kernel, ¿cómo puedo registrar la información por hilos del kernel para depurar más fácilmente? –

0

en el código u no necesita utilizar kthread_create API como kthread_run lo hace internamente .. uso ya sea

tarea = kthread_create (& thread_function, (void *) datos "Pradeep");
O
task = kthread_run (& thread_function, (void *) data, "pradeep");

Además, su módulo no tiene licencia de GPL. Esa podría ser una de las causas de tus problemas.

+0

'task = kthread_create (y thread_function, (void *) de datos, "Pradeep");' 'wake_up_process (tarea)' ** O ** 'task = kthread_run (y thread_function, (void *) de datos, "pradeep"); ' – Jayzcode

5

espero que el siguiente programa resuelve el problema .... pulgar hacia arriba :-)

`#include<linux/init.h> 
#include<linux/module.h> 
#include<linux/kernel.h> 
#include<linux/kthread.h> 
#include<linux/sched.h>` 

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"); 
Cuestiones relacionadas