转自: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. PHP数组函数总结

    array_change_key_case - 返回字符串键名全为小写或大写的数组 array_chunk - 将一个数组分割成多个 array_column - 返回数组中指定的一列 array_c ...

  2. K-Means clusternig example with Python and Scikit-learn(推荐)

    https://www.pythonprogramming.net/flat-clustering-machine-learning-python-scikit-learn/ Unsupervised ...

  3. EmployeeTest

    package fourthf; public class EmployeeTest { public static void main(String[] args) { // TODO Auto-g ...

  4. 浅谈Android样式开发之shape

    引言 在Android开发中我们很多情况都是使用图片来展示相关效果,今天我就来详细介绍下Android下使用Shape来进行简单UI的开发.一方面这些是Android开发的基础,另一方面这方面的知识可 ...

  5. 忘记mysql root 密码修改小技巧

    首先我说一下我的情况,我并不是忘记了我的root密码,只不过是我在使用phpmyadmin的时候更改密码的时候选择了如图1 的这个方法将密码加密并更改了,然后就再次登录的时候登录不上,所以对于菜鸟级的 ...

  6. iptables原理

    1.iptables防火墙简介 Iptables也叫netfilter是Linux下自带的一款免费且优秀的基于包过滤的防火墙工具,它的功能十分强大,使用非常灵活,可以对流入.流出.流经服务器的数据包进 ...

  7. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  8. c++虚析构函数

    虚析构函数的作用主要是当通过基类指针删除派生类对象时,调用派生类的析构函数(如果没有将不会调用派生类析构函数) #include <iostream> using namespace st ...

  9. java中 IndexOf()、lastIndexOf()、substring()的用法

    public int indexof(String str)返回字符串中出现str的第一个位置 public int indexof(String str,int fromIndex)返回字符串中从f ...

  10. [MySQL] MySQL存储过程常用的函数

    一.字符串类 CHARSET(str) //返回字串字符集 CONCAT (string2  [,... ]) //连接字串 INSTR (string ,substring ) //返回substr ...