#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/delay.h>
static int thread_func(void *data)
{
int i = 0;
while (1) {
if (kthread_should_stop()) {
return 1;
}
ssleep(2);
}
}
struct task_struct *task;
int __init hello_init_module(void)
{
task = kthread_run(&thread_func, NULL, "test_thread");
return 0;
}
void __exit hello_cleanup_module(void)
{
kthread_stop(task);
}
module_init(hello_init_module);
module_exit(hello_cleanup_module);
MODULE_LICENSE("GPL");
На ядре 2.6.9 (CentOS) все работает, на 2.6.27 (Arch) rmmod намертво виснет, видимо ждет завершения потока.
Кто-нибудь знает как теперь правильно завершать поток?
читай внимательно Linux Device Drivers, глава 7.
ты отправляешь поток спать на 2 сек и устанавливаешь состояние задачи в TASK_UNINTERRUPTIBLE.
попробуй так:
static int thread_func(void *data)
{
int i = 0;
while (1) {
ssleep(2);
set_current_state(TASK_INTERRUPTIBLE);
schedule();
if (kthread_should_stop()) {
return 1;
}
}
}