前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >内核线程创建

内核线程创建

作者头像
用户3765803
发布2019-03-05 10:14:08
1.5K0
发布2019-03-05 10:14:08
举报
文章被收录于专栏:悟空被FFmpeg玩

阅读了kernel的start_kernel代码后,学习了一下kernel_thread的使用

#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/sched.h>MODULE_AUTHOR("T-bagwell_CU");MODULE_LICENSE("GPL");static DECLARE_WAIT_QUEUE_HEAD(myevent_waitqueue);extern unsigned int myevent_id;static int example_kernel_thread(void *unused){        DECLARE_WAITQUEUE(wait, current);        daemonize("create_by_T-bag");        allow_signal(SIGKILL);        add_wait_queue(&myevent_waitqueue, &wait);        while(1){                set_current_state(TASK_INTERRUPTIBLE);                schedule();                if(signal_pending(current)){                        break;                }        }        set_current_state(TASK_RUNNING);        remove_wait_queue(&myevent_waitqueue, &wait);        printk(KERN_WARNING "This is in example_kernel_thread\n");        return 0;}static __init int init_hello_kernel_thread(void){        int ret;        ret=kernel_thread(example_kernel_thread, NULL,                                  CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD );        if(unlikely(ret<0)){                printk(KERN_WARNING "kernel_thread create failed \n");        }        else{                printk(KERN_WARNING "kernel_thread create success \n");        }        return 0;}static __exit void cleanup_hello_kernel_thread(void){        printk(KERN_WARNING "kernel_thread exit \n");        return ;}module_init(init_hello_kernel_thread);module_exit(cleanup_hello_kernel_thread);

写一个Makefile来编译这个module

KERNELDIR = /usr/src/kernels/2.6.27.5-117.fc10.i686PWD := $(shell pwd)obj-m := kernel_thread.omodules:            $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:            rm -rf *.o *.ko test_chrdev Module.* module* *.mod.c

编译完成以后,可以看一下结果:

从上图可以看出来,kernel_thread.ko文件已经被insmod进了modules里

接下来可以看一下进程:

这个内核线程也被创建出来了

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2009/09/25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档