Linux内核驱动学习(五)KThread学习总结

举报
小麦大叔 发表于 2021/12/29 23:59:01 2021/12/29
2.2k+ 0 0
【摘要】 文章目录 简介例程运行结果参考 简介 使用内核线程需要包含头文件#include <linux/kthread.h>,下面整理了一下常用的api接口,如下表格所示; 函数...

简介

使用内核线程需要包含头文件#include <linux/kthread.h>,下面整理了一下常用的api接口,如下表格所示;

函数 功能
struct task_struct * kthread_create(threadfn, data, namefmt, arg...) 创建一个线程
struct task_struct * kthread_run(threadfn, data, namefmt, ...) 创建线程并运行
int kthread_stop(struct task_struct *k) 停止线程

例程

下面的代码简单实现了创建一个线程,循环60秒,每秒打印count的数值和传入线程执行函数的参数our_thread,所以预期结果是该模块会打印1 thread_func:thread1字符串的数据。

#include <linux/module.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/time.h>

#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/delay.h>

static struct task_struct *thread_body;
static char  our_thread[8]="thread1";

static int thread_func(void *buff) {

	static int count = 0;
	char *data = (char*)buff;
	unsigned long j0, j1;
	int delay = 60*HZ;

	printk(KERN_INFO "In thread 1");
	
	j0 = jiffies;
	j1 = j0 + delay;
	
	while (time_before(jiffies, j1)){
		schedule();
		msleep(1000);
		printk("%d thread_func:%s \n",++count,data);
	}

	return 0;
}

static int __init demo_thread_init(void){
    
    printk(KERN_INFO "in demo_thread_init\n");
	
    //这里可以也使用 kthread_run ,kthread_run中已经包含了wake_up_process操作
    thread_body = kthread_create(thread_func, (char*)our_thread, "thread1");
	
    if((thread_body))
    {
        wake_up_process(thread_body);
    }

	return 0;
}

module_init(demo_thread_init);

static void __exit demo_thread_exit(void){
	int ret;
	ret = kthread_stop(thread_body);
	if(!ret){
		printk(KERN_INFO "Thread stopped\n"); 
	}
}
module_exit(demo_thread_exit);

MODULE_LICENSE("GPL");	

  
 

运行结果

[    4.496344] 1 thread_func:thread1
[    5.499766] 2 thread_func:thread1
...
[   57.673065] 54 thread_func:thread1
[   58.676418] 55 thread_func:thread1
[   59.679734] 56 thread_func:thread1
[   60.683070] 57 thread_func:thread1
...

  
 

参考

https://www.programering.com/a/MDN4IjMwATk.html

http://tuxthink.blogspot.com/2011/02/kernel-thread-creation-1.html

文章来源: great.blog.csdn.net,作者:小麦大叔,版权归原作者所有,如需转载,请联系作者。

原文链接:great.blog.csdn.net/article/details/88178376

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。