linux - 使用了内核线程的内核模块不能卸载
阿神
阿神 2017-04-17 16:48:20
[Linux讨论组]

在内核模块中创建内核线程,内核线程执行完以后,使用rmmod卸载模块时,提示“已杀死”,然后使用lsmod查看,发现内核模块并没有被卸载,占用为-1。
但是如果在内核线程正在执行的情况下使用rmmod,就可以成功卸载这个模块。

下面是代码,内核线程的作用是每隔一秒printk一次。

struct task_struct *thr = NULL;

static int counting(void *data) {
    
    int i = 0;
    for(i=0; !kthread_should_stop() && i < 10; i++) {

        printk("i = %d\n",i);
        msleep(1000); /* delay 1 sec */
    }
    return 0;
}

static int __init print_init(void) {

    printk("kernel start !\n");

    thr = kthread_run(counting, NULL, "counting-thread");
    if(!thr) {
        printk("create kthread error ! \n");
        return -ECHILD;
    }
    return 0;
}

static void __exit print_exit(void) {

    if(!IS_ERR(thr)) {
        kthread_stop(thr);
    }
    printk("module exit! \n");
}

module_init(print_init);
module_exit(print_exit);
阿神
阿神

闭关修行中......

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号