Kristian Evensen ha scritto:
Hello again,
Thank you very much for all your help with kernel threads, I have now
developed a couple of test modules which use them and feel like I start to
understand them. However, there is one thing I have yet to figure out how to
solve "properly".
I am working on a module that communicates with userspace using Netlink in a
separate thread. The way I have designed the module I want to use a blocking
call (skb_recv_datagram) to receive the packets. The problem comes when I
want to kill this thread. Currently, I signal it using send_sig, but
compared to the kthread_*-functions this seems a bit "dirty".
My questions are:
- Is it ok to kill a thread using signals?
- After reading up on kthread_stop, I now see that it does not send a
signal. Are there any other kthread_* or similar functions which does this,
or will at least unblock the thread?
- Are there any other, recommended ways of killing threads containing
blocking calls?
Thanks for any help,
Kristian
Hi Kristian,
see this example:
static struct task_struct *my_task;
static DECLARE_WAIT_QUEUE_HEAD(myevent_waitqueue);
static int my_flag;
static int my_thread(void *unused)
{
do {
/* Relinquish the processor until the event occurs */
my_flag =0;
wait_event_interruptible(myevent_waitqueue, my_flag ||
kthread_should_stop());
PDEBUG("Thread awakened\n");
/* Work to do */
...
...
} while (!kthread_should_stop());
return 0;
}
static int my_init(void)
{
my_task = kthread_run(my_thread, NULL, "my_threadd");
if (IS_ERR(my_task)) {
printk(KERN_ERR "my_thread: Failed to start my_threadd, error\n");
return -1;
}
return 0; // success
}
static void my_exit(void)
{
kthread_stop(my_task);
}
If you want the thread exit with SIGKILL (kill -9) you must add at the
beginning of mykthread:
/* Request delivery of SIGKILL */
allow_signal(SIGKILL);
and modify this :
if (wait_event_interruptible(myevent_waitqueue, my_flag ||
kthread_should_stop()) == -ERESTARTSYS)
break;
WARNING: if you kill the thread, when you call the function
kthread_stop() in my_exit(), it enters UNINTERRUPTIBLE sleep and (if you
compiled the code as a module) rmmod doesn't return!
So IMHO killing a thread this way is not the right thing to do.
regards
Luca
--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ