|
以下是我的测试代码,很简单,就是在模块加载初始化的时候创建一个任务,一直打印,模块卸载时停止任务。现在的问题是停止不起作用,在打印will stop tst1后就不动了,也不能再退回shell。开始创建的任务仍旧不停打印。刚接触这些,不懂的太多,望知情者给些指点,万分感谢!
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/smp_lock.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/netlink.h>
#include <linux/kthread.h>
#include <linux/err.h>
int test_task(void * data);
struct task_struct *tst1;
int test_a = 3;
static int __init mod_dev_init(void)
{
int err;
char *str1 = "test_task1";
printk(KERN_INFO "usr module entries driver\n");
tst1 = kthread_create(test_task,NULL,str1);
printk("test_task create ok!\n");
if(IS_ERR(tst1))
{
printk("Unable to start kernel thread.\n");
err = PTR_ERR(test_task);
tst1 = NULL;
return err;
}
wake_up_process(tst1);
return 0;
}
int test_task(void * data)
{
int b = 4,c = 0;
unsigned long t = 1000;
printk("this is test_task starting!\n");
while(1)
{
if(kthread_should_stop()) break;
c = test_a + b;
printk("test_a = %d,b = %d,c = %d\n",test_a,b,c);
current->state = TASK_INTERRUPTIBLE;
schedule_timeout(t);
}
return 0;
}
static void __exit mod_dev_exit(void)
{
int ret1;
printk(KERN_INFO "usr module exit driver\n");
if(tst1)
{
printk(KERN_INFO "will stop tst1\n");
ret1 = kthread_stop(tst1);
printk(KERN_INFO "ret1 is %d\n", ret1);
}
}
MODULE_DESCRIPTION("Test module example");
MODULE_LICENSE("GPL");
EXPORT_SYMBOL(test_task);
EXPORT_SYMBOL(test_a);
EXPORT_SYMBOL(tst1);
module_init(mod_dev_init);
module_exit(mod_dev_exit); |
|