转自:http://blog.csdn.net/guowenyan001/article/details/39230181

一、代码

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/types.h>
  4. #include <linux/kthread.h>
  5. #include <linux/err.h>
  6. MODULE_VERSION("1.0.0_0");
  7. MODULE_LICENSE("GPL");
  8. MODULE_AUTHOR("gwy");
  9. #ifndef SLEEP_MILLI_SEC
  10. #define SLEEP_MILLI_SEC(nMilliSec) \
  11. do { \
  12. long timeout = (nMilliSec) * HZ /1000; \
  13. while (timeout > 0) \
  14. { \
  15. timeout = schedule_timeout(timeout); \
  16. } \
  17. }while (0);
  18. #endif
  19. struct task_struct* thread = NULL;
  20. void thread_proc(void* arg)
  21. {
  22. struct timespec ts;
  23. //set_current_state(TASK_UNINTERRUPTIBLE);
  24. while(!kthread_should_stop())
  25. {
  26. ts = current_kernel_time();
  27. printk("thread_proc:%s. time:%ld\n", (char*)arg, ts.tv_sec);
  28. SLEEP_MILLI_SEC(1000);
  29. }
  30. }
  31. static int init_marker(void)
  32. {
  33. int err;
  34. printk("init marker.\n");
  35. thread = kthread_create(thread_proc, "thread arg", "my_thread%d", 0);
  36. if (IS_ERR(thread))
  37. {
  38. err = PTR_ERR(thread);
  39. printk("kthread_create fail.\n");
  40. return err;
  41. }
  42. wake_up_process(thread);
  43. printk("thread create.\n");
  44. return 0;
  45. }
  46. static void exit_marker(void)
  47. {
  48. if (thread)
  49. {
  50. kthread_stop(thread);
  51. thread = NULL;
  52. printk("thread stop.\n");
  53. }
  54. printk("exit marker.\n");
  55. }
  56. module_init(init_marker);
  57. 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的更多相关文章

  1. linux内核中创建线程方法

    1.头文件 #include <linux/sched.h> //wake_up_process() #include <linux/kthread.h> //kthread_ ...

  2. linux内核中创建线程方法【转】

    本文转载自:https://www.cnblogs.com/Ph-one/p/6077787.html 1.头文件 #include <linux/sched.h> //wake_up_p ...

  3. Linux内核学习笔记-2.进程管理

    原创文章,转载请注明:Linux内核学习笔记-2.进程管理) By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...

  4. Linux设备驱动开发详解-Note(5)---Linux 内核及内核编程(1)

    Linux 内核及内核编程(1) 成于坚持,败于止步 Linux 2.6 内核的特点 Linux 2.6 相对于 Linux 2.4 有相当大的改进,主要体现在如下几个方面. 1.新的调度器 2.6 ...

  5. 【读书笔记】《Linux内核设计与实现》进程管理与进程调度

    大学跟老师做嵌入式项目,写过I2C的设备驱动,但对Linux内核的了解也仅限于此.Android系统许多导致root的漏洞都是内核中的,研究起来很有趣,但看相关的分析文章总感觉隔着一层窗户纸,不能完全 ...

  6. Linux内核线程kernel thread详解--Linux进程的管理与调度(十)

    内核线程 为什么需要内核线程 Linux内核可以看作一个服务进程(管理软硬件资源,响应用户进程的种种合理以及不合理的请求). 内核需要多个执行流并行,为了防止可能的阻塞,支持多线程是必要的. 内核线程 ...

  7. Linux内核线程创建

    本文旨在简单介绍一下Linux内核线程: 先举个例子: 不插U盘,在Linux命令行中输入:ps -el:然后插上U盘,再次输入:ps -el 会发现多出了下面一行(当然还会有其他的,比如scsi相关 ...

  8. Linux内核多线程实现方法 —— kthread_create函数【转】

    转自:http://blog.csdn.net/sharecode/article/details/40076951 Linux内核多线程实现方法 —— kthread_create函数 内核经常需要 ...

  9. Linux内核线程kernel thread详解--Linux进程的管理与调度(十)【转】

    转自:http://blog.csdn.net/gatieme/article/details/51589205 日期 内核版本 架构 作者 GitHub CSDN 2016-06-02 Linux- ...

  10. 常见linux内核线程说明

    ps进程名有方括号的是内核级的进程,执行辅助功能(比如将缓存写入到磁盘):所有其他进程都是使用者进程.您会注意到,就算是在您新安装的(最小化的)系统中,也会有很多进程在运行. 在文档kernel-pe ...

随机推荐

  1. jQuery文本框中的事件应用

    jQuery文本框中的事件应用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...

  2. jquery1.7.2的源码分析(四)$.Deferred(2)

    jQuery.Callbacks = function( flags ) { // Convert flags from String-formatted to Object-formatted // ...

  3. Maven的安装及更改下载仓库

    之前在MyEclipse这个IDE中配置Maven,完成配置后启动Maven时出现-Dmaven.multiModuleProjectDirectory system propery is not s ...

  4. 用Canvas实现动画效果

    1.清除Canvas的内容 clearRect(x,y,width,height)函数用于清除图像中指定矩形区域的内容 <!doctype html> <html> <h ...

  5. Mysql基础(二)

    学习路线:数据约束-> 数据库的设计过程-> 存储过程的相关知识-> 触发器-> 权限管理 (一)数据约束 1.1.默认值的设置 创建员工表emp 将默认地址设置为'中国'my ...

  6. [Machine Learning & Algorithm]CAML机器学习系列1:深入浅出ML之Regression家族

    声明:本博客整理自博友@zhouyong计算广告与机器学习-技术共享平台,尊重原创,欢迎感兴趣的博友查看原文. 符号定义 这里定义<深入浅出ML>系列中涉及到的公式符号,如无特殊说明,符号 ...

  7. [转]MySQL索引背后的数据结构及算法原理

    摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...

  8. Python-pandas

    Python-pandas Python 中处理时间序列的主要工具是 pandas 库. 1.pannas 基础 1.1使用 DataFrame 类的第一步 #!/etc/bin/python #co ...

  9. iptables过滤设置服务端口

    1.为SSH跟换连接端口 修改SSH配置文件:/etc/ssh/sshd_config #找到Port 22,这里是标识默认使用22端口,修改为想要的端口. Port Port 2.查看iptable ...

  10. 如何在openresty里解析域名

    转:原文:http://hambut.com/2016/09/09/how-to-resolve-the-domain-name-in-openresty/?utm_source=tuicool&am ...