转自: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. MySQL的if,case语句使用总结

    原文地址: http://outofmemory.cn/code-snippet/1149/MySQL-if-case-statement-usage-summary

  2. js原生dom方法总结

    1.document document方法getElementById (Node)返回指定节点的引用getElementsByTagName (NodeList)返回文档中所有匹配元素的集合quer ...

  3. spring ioc 源码解析

    什么是ioc? 通俗的解释是:(spring)框架中,完成对象的创建和注入的容器. springIOC体系结构: spring IOC的创建是典型的工厂模式,这一系列的bean工厂如上所示. 其核心是 ...

  4. JAVA语法基础作业——动手动脑以及课后实验性问题 (八)

    一.动手动脑 运行AboutException.java示例,了解Java中实现异常处理的基础知识. 1)源代码 import javax.swing.*; class AboutException ...

  5. 对于C语言复杂指针类型的分析

    转载自:http://www.slyar.com/blog/complicated-point-type.html int p; p是一个普通的整型变量. int *p; 1.p与*结合,说明p是一个 ...

  6. OC与c混编实现Java的String的hashcode()函数

    首先,我不愿意大家需要用到这篇文章里的代码,因为基本上你就是被坑了. 起因:我被Java后台人员坑了一把,他们要对请求的参数增加一个额外的字段,字段的用途是来校验其余的参数是否再传递过程中被篡改或因为 ...

  7. Python中两种处理错误方法的比较

    我所说的处理错误的方法,其实是try:,except和raise这两种. 首先抛出一个实例, dictt={'a':1,'b':2,'c':3} try: if dictt['d']>1: #字 ...

  8. MySQL 锁问题

    一.MySQL中不同的存储引擎支持不同的锁机制 (A) MyISAM 和 MEMORY 支持表级锁 (B) BDB 支持页面锁,也支持表级锁 (C) InnoDB 支持行级锁,也支持表级锁,默认是行级 ...

  9. SPOJ FTOUR2 - Free tour II

    Description 有些黑点,问你选择不超过 \(k\) 个黑点的路径,路径权值最大是多少. Sol 点分治. 这是qzc的论文题,不过我感觉他的翻译好强啊...我还是选择了自己去看题目... 点 ...

  10. Python之美--Decorator深入详解

    转自:http://www.cnblogs.com/SeasonLee/archive/2010/04/24/1719444.html 一些往事 在正式进入Decorator话题之前,请允许我讲一个小 ...