kernel_thread hello world 内核线程创建

本文介绍了一个简单的 Linux 内核线程实现示例,通过 kernel_thread 接口创建了一个常驻内核线程,该线程会等待特定事件并响应信号。同时提供了一个 Makefile 用于编译内核模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

阅读了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.i686
PWD := $(shell pwd)
obj-m := kernel_thread.o

modules:
            $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
            rm -rf *.o *.ko test_chrdev Module.* module* *.mod.c


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

 
从上图可以看出来,kernel_thread.ko文件已经被insmod进了modules里
接下来可以看一下进程:
 
这个内核线程也被创建出来了
首先,需要编写内核模块的代码。以下是一个简单的例子,只包含一个初始化函数和一个退出函数。 ```c #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/workqueue.h> #include <linux/slab.h> #include <linux/timer.h> MODULE_LICENSE("GPL"); static struct workqueue_struct *queue; static struct delayed_work hello_work; static void hello_func(struct work_struct *work) { printk(KERN_INFO "Hello World!\n"); schedule_delayed_work(&hello_work, msecs_to_jiffies(1000)); } static int __init hello_init(void) { queue = create_singlethread_workqueue("hello_queue"); INIT_DELAYED_WORK(&hello_work, hello_func); schedule_delayed_work(&hello_work, msecs_to_jiffies(1000)); return 0; } static void __exit hello_exit(void) { cancel_delayed_work_sync(&hello_work); destroy_workqueue(queue); } module_init(hello_init); module_exit(hello_exit); ``` 以上代码使用了工作队列和延迟工作来实现周期性地打印 “Hello World!”。在初始化函数 `hello_init()` 中,创建了一个单线程的工作队列,并初始化了一个延迟工作 `hello_work`,并将其加入到工作队列中,设置了延迟时间为 1000 毫秒。在 `hello_func()` 函数中,打印了一条信息,并再次将 `hello_work` 加入到工作队列中,以达到周期性执行的目的。在退出函数 `hello_exit()` 中,取消了 `hello_work`,并销毁了工作队列。 编译内核模块的命令如下: ```sh make -C /lib/modules/$(uname -r)/build M=$PWD modules ``` 加载内核模块的命令如下: ```sh sudo insmod hello.ko ``` 可以通过 `dmesg` 命令查看内核日志,看到周期性打印的 “Hello World!”。卸载内核模块的命令如下: ```sh sudo rmmod hello ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值