Linux内核:kthread_create(线程)、SLEEP_MILLI_SEC
转自:http://blog.csdn.net/guowenyan001/article/details/39230181
一、代码
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/types.h>
- #include <linux/kthread.h>
- #include <linux/err.h>
- MODULE_VERSION("1.0.0_0");
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("gwy");
- #ifndef SLEEP_MILLI_SEC
- #define SLEEP_MILLI_SEC(nMilliSec) \
- do { \
- long timeout = (nMilliSec) * HZ /1000; \
- while (timeout > 0) \
- { \
- timeout = schedule_timeout(timeout); \
- } \
- }while (0);
- #endif
- struct task_struct* thread = NULL;
- void thread_proc(void* arg)
- {
- struct timespec ts;
- //set_current_state(TASK_UNINTERRUPTIBLE);
- while(!kthread_should_stop())
- {
- ts = current_kernel_time();
- printk("thread_proc:%s. time:%ld\n", (char*)arg, ts.tv_sec);
- SLEEP_MILLI_SEC(1000);
- }
- }
- static int init_marker(void)
- {
- int err;
- printk("init marker.\n");
- thread = kthread_create(thread_proc, "thread arg", "my_thread%d", 0);
- if (IS_ERR(thread))
- {
- err = PTR_ERR(thread);
- printk("kthread_create fail.\n");
- return err;
- }
- wake_up_process(thread);
- printk("thread create.\n");
- return 0;
- }
- static void exit_marker(void)
- {
- if (thread)
- {
- kthread_stop(thread);
- thread = NULL;
- printk("thread stop.\n");
- }
- printk("exit marker.\n");
- }
- module_init(init_marker);
- module_exit(exit_marker);
二、输出结果
三、注意
1)在调用kthread_stop函数时,线程函数不能已经运行结束。否则,kthread_stop函数会一直进行等待。
2)线程函数必须能让出CPU,以便能运行其他线程。 同时线程函数也必须能重新被调度运行。
3)注意线程函数的使用,参见“kthread_create的简单使用”。
参考资料:
kthread_create的简单使用:http://blog.csdn.net/newnewman80/article/details/7050090
kthread_create与kerne_thread的区别:http://blog.sina.com.cn/s/blog_5a1e1b770100jfc0.html
Linux内核:kthread_create(线程)、SLEEP_MILLI_SEC的更多相关文章
- linux内核中创建线程方法
1.头文件 #include <linux/sched.h> //wake_up_process() #include <linux/kthread.h> //kthread_ ...
- linux内核中创建线程方法【转】
本文转载自:https://www.cnblogs.com/Ph-one/p/6077787.html 1.头文件 #include <linux/sched.h> //wake_up_p ...
- Linux内核学习笔记-2.进程管理
原创文章,转载请注明:Linux内核学习笔记-2.进程管理) By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...
- Linux设备驱动开发详解-Note(5)---Linux 内核及内核编程(1)
Linux 内核及内核编程(1) 成于坚持,败于止步 Linux 2.6 内核的特点 Linux 2.6 相对于 Linux 2.4 有相当大的改进,主要体现在如下几个方面. 1.新的调度器 2.6 ...
- 【读书笔记】《Linux内核设计与实现》进程管理与进程调度
大学跟老师做嵌入式项目,写过I2C的设备驱动,但对Linux内核的了解也仅限于此.Android系统许多导致root的漏洞都是内核中的,研究起来很有趣,但看相关的分析文章总感觉隔着一层窗户纸,不能完全 ...
- Linux内核线程kernel thread详解--Linux进程的管理与调度(十)
内核线程 为什么需要内核线程 Linux内核可以看作一个服务进程(管理软硬件资源,响应用户进程的种种合理以及不合理的请求). 内核需要多个执行流并行,为了防止可能的阻塞,支持多线程是必要的. 内核线程 ...
- Linux内核线程创建
本文旨在简单介绍一下Linux内核线程: 先举个例子: 不插U盘,在Linux命令行中输入:ps -el:然后插上U盘,再次输入:ps -el 会发现多出了下面一行(当然还会有其他的,比如scsi相关 ...
- Linux内核多线程实现方法 —— kthread_create函数【转】
转自:http://blog.csdn.net/sharecode/article/details/40076951 Linux内核多线程实现方法 —— kthread_create函数 内核经常需要 ...
- Linux内核线程kernel thread详解--Linux进程的管理与调度(十)【转】
转自:http://blog.csdn.net/gatieme/article/details/51589205 日期 内核版本 架构 作者 GitHub CSDN 2016-06-02 Linux- ...
- 常见linux内核线程说明
ps进程名有方括号的是内核级的进程,执行辅助功能(比如将缓存写入到磁盘):所有其他进程都是使用者进程.您会注意到,就算是在您新安装的(最小化的)系统中,也会有很多进程在运行. 在文档kernel-pe ...
随机推荐
- Struts2之开山篇
①Struts2是什么? Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图 ...
- node-glob学习
node的glob模块允许你使用 *等符号, 来写一个glob规则,像在shell里一样,获取匹配对应规则的文件. 这个glob工具基于javascript.它使用了 minimatch 库来进行匹配 ...
- 解决httpd: Could not reliably determine the server's fully qualified domain name
解决方案: 用记事本打开 httpd.conf 将里面的 #ServerName localhost:80 注释去掉即可. 再执行 httpd 然后可以通过浏览器访问 http://localhost ...
- Request.Form接收不到post数据.
Request.Form接收不到post数据. https://q.cnblogs.com/q/62635/ Content-Type 有没有设置为 application/x-www-form-ur ...
- 转载:MySQL 语句大全:创建、授权、查询、修改等
本文转载>这里 一.用户创建.权限.删除 1.连接MySql操作 连接:mysql -h 主机地址 -u 用户名 -p 用户密码 (注:u与root可以不用加空格,其它也一样)断开:exit ( ...
- java时间类型操作
java中Date类型与字符串相互转化 Date date = new Date(); System.out.println(date); //DateFormat类为一个 ...
- 【Sorting Collection】
排序集锦 各种排序算法,总结一下,一直在遗忘...... [冒泡排序] 就是下面这个鬼啦: c实现代码(升序): #include<stdio.h> void BubbleSort(int ...
- 如何做出透明背景的flash动画
最近碰到了一个问题,做动画时怎么设置透明背景,字是白色的,网上搜了一下,发现可以这样做 在插入的flash代码中加入这一行代码就可以实现了 <param name="wmode&quo ...
- Deep Residual Learning
最近在做一个分类的任务,输入为3通道车型图片,输出要求将这些图片对车型进行分类,最后分类类别总共是30个. 开始是试用了实验室师姐的方法采用了VGGNet的模型对车型进行分类,据之前得实验结果是训练后 ...
- Maven pom.xml 元素配置说明(一)
部分来源: Maven中 dependencies 节点和 dependencyManagement 节点的区别 dependencies与dependencyManagement的区别 maven ...