kernel thread简单使用

news/2024/5/19 0:13:51 标签: thread, signal, makefile, module, null, shell
 

本节介绍下kernel thread简单使用的例子实验.

 

我的系统:

 

joseph:/usr/src/linux-2.6.23/joseph# uname -a

Linux joseph 2.6.23 #1 SMP PREEMPT Fri May 6 18:02:45 CST 2011 i686 GNU/Linux

 

 

文件:

 

├── hello.c 

└── Makefile

 

 

1. hello.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kthread.h>
MODULE_LICENSE("Dual BSD/GPL");

//static DECLARE_WAIT_QUEUE_HEAD(myevent_waitqueue);
//rwlock_t myevent_lock;
static int mykthread(void* unused)
{
		daemonize("josephThread");
	
		// reuest delivery of SIGKILL
		allow_signal(SIGKILL);
		for(;;) {
				//relinquish the processor until the event occurs
				set_current_state(TASK_INTERRUPTIBLE);
				schedule();//allow other parts of the kernel to run
				//Die if I receive SIGKILL
				if (signal_pending(current)) break;
		}
	
		printk("josephThread out <------<<<<< /n");
		set_current_state(TASK_RUNNING);
		return 0;
}
static int hello_init(void)
{
		printk("Hello, I am a test module/n");
		//create my kernel thread
		
		printk("Create mythread<---------<<<<<<<</n");
		kernel_thread(mykthread, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD);
		return 0;
}
static void hello_exit(void)
{
		printk("Bye, my dear!/n Cruel world/n");
}


 

2. Makefile

 

obj-m := hello.o

3. 编译 
#make -C /usr/src/linux-2.6.23 M=`pwd` modules
// /usr/src/linux-2.6.23 是我的源码路径
会生成 hello.ko
4. 清除记录的信息
# echo "" > /var/log/messages
5. 插入模块
# insmod  hello.ko
6. 查看信息
# cat /var/log/messages
May  7 20:24:10 joseph kernel: [ 4281.427586] Hello, I am a test module
May  7 20:24:10 joseph kernel: [ 4281.427758] Create mythread<---------<<<<<<<<
查看我们的内核线程
#ps -ef
...
root      4030     2  0 20:24 ?        00:00:00 [josephThread]
...
7. 结束内核线程
#kill -s 9 4030
查看信息
# cat /var/log/messages 
May  7 20:24:10 joseph kernel: [ 4281.427586] Hello, I am a test module
May  7 20:24:10 joseph kernel: [ 4281.427758] Create mythread<---------<<<<<<<<
May  7 20:27:22 joseph kernel: [ 4472.245694] josephThread out <------<<<<< 
8. 卸载模块
#rmmod hello
查看信息
#cat /var/log/messages
May  7 20:24:10 joseph kernel: [ 4281.427586] Hello, I am a test module
May  7 20:24:10 joseph kernel: [ 4281.427758] Create mythread<---------<<<<<<<<
May  7 20:27:22 joseph kernel: [ 4472.245694] josephThread out <------<<<<< 
May  7 20:28:43 joseph kernel: [ 4553.589958] Bye, my dear!
May  7 20:28:43 joseph kernel: [ 4553.589960]  Cruel world

 

另一个例子:

 

 

#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


 

 

 

 


http://www.niftyadmin.cn/n/1116699.html

相关文章

quart 定时器介绍详解

https://blog.csdn.net/china_shrimp/article/details/52138355

拆了小米的小台灯,手贱,多图

6个大功率LED&#xff0c;那两个像芯片样的三极管&#xff0c;应该是恒流电路驱动&#xff0c;铝基板PCB是亮点&#xff0c;散热很好。 转载于:https://www.cnblogs.com/rongfangliu/p/4617528.html

[个人翻译]GitHub指导文件(GitHub Guides[Hello World])

[个人翻译]GitHub指导文件&#xff08;GitHub Guides[Hello World]&#xff09; Mirage_j个人翻译&#xff0c;欢迎转载&#xff0c;最好标明出处http://www.cnblogs.com/mirageJ/ 原文地址https://guides.github.com/activities/hello-world/ Hello World是电脑编程史上悠久而…

glusterFS

1.准备工作 准备三台机器&#xff08;物理机或者虚拟机均可&#xff09;用于安装和测试GlusterFS&#xff0c;其中两台用作服务器&#xff0c;一台用作客户端&#xff0c;主机名分别为&#xff1a; node1 10.0.21.241 node2 10.0.21.242 Clinet.zhaogang.int 关闭iptables和sel…

模块编译KO文件Makefile 2.6内核通用

#######hello 单文件编译############## obj-m : hello.oKDIR : /lib/modules/$(shell uname -r)/buildPWD : $(shell pwd)default:$(MAKE) -C $(KDIR) M$(PWD) modulesclean:rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions *.order *symvers *Module.markers ##############多…

action mq 使用

觉的他写的非常好,访问地址如下: https://blog.csdn.net/flysun3344/article/details/52906132

RDD、DataFrame、Dataset三者三者之间转换

转化&#xff1a;RDD、DataFrame、Dataset三者有许多共性&#xff0c;有各自适用的场景常常需要在三者之间转换 DataFrame/Dataset转RDD&#xff1a;这个转换很简单val rdd1testDF.rdd val rdd2testDS.rddRDD转DataFrame&#xff1a;import spark.implicits._ val testDF rdd.…

普通GPIO口线模拟I2C

ARM编程:ARM普通GPIO口线模拟I2C 请教个问题&#xff1a; 因为需要很多EEPROM进行点对点控制&#xff0c;所以我现在要用ARM的GPIO模拟I2C&#xff0c;管脚方向我设 置的是向外的。我用网上的RW24C08的万能程序修改了一下&#xff0c;先进行两根线的模拟&#xff0c;SDA6&…