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 ...
随机推荐
- 聊一聊PV和并发
最近和几个朋友,聊到并发和服务器的压力问题.很多朋友,不知道该怎么去计算并发?部署多少台服务器才合适? 所以,今天就来聊一聊PV和并发,还有计算web服务器的数量 的等方法.这些都是自己的想法加上一些 ...
- 调用Interop.zkemkeeper.dll无法使用解决方案
调用Interop.zkemkeeper.dll无法使用 已经注册dll成功但是还是报错 检索 COM 类工厂中 CLSID 为 {00853A19-BD51-419B--2DABE57EB61F} ...
- remove name="ProxyModule“会导致重复执行
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访 ...
- 命令查询网站是否开启CDN加速
1.使用ping命令 不一定准确 运行-CMD 直接输入ping 域名 C:\Users\6503>ping www.netnic.com.cn 正在 Ping u999.v.qingcdn ...
- linux下epoll实现机制
linux下epoll实现机制 原作者:陶辉 链接:http://blog.csdn.net/russell_tao/article/details/7160071 先简单回顾下如何使用C库封装的se ...
- java的锁机制
一段synchronized的代码被一个线程执行之前,他要先拿到执行这段代码的权限,在Java里边就是拿到某个同步对象的锁(一个对象只有一把锁): 如果这个时候同步对象的锁被其他线程拿走了,他(这个线 ...
- 用R做逻辑回归之汽车贷款违约模型
数据说明 本数据是一份汽车贷款违约数据 application_id 申请者ID account_number 账户号 bad_ind 是否违约 vehicle_year ...
- 20155217-杨笛-c与java
命是弱者的借口,运是强者的谦词 你有什么技能比大多数人(超过90%以上)更好? 针对这个技能的获取你有什么成功的经验? 与老师博客中的学习经验有什么共同之处? 其实我经常会去想自己有什么拿得出手的优点 ...
- mysql中distinct的用法
本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...
- tp中ueditor编辑器的使用
1/引入三个文件 <script type="text/javascript" charset="utf-8" src="{$Think.con ...