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 ...
随机推荐
- PHP数组函数总结
array_change_key_case - 返回字符串键名全为小写或大写的数组 array_chunk - 将一个数组分割成多个 array_column - 返回数组中指定的一列 array_c ...
- K-Means clusternig example with Python and Scikit-learn(推荐)
https://www.pythonprogramming.net/flat-clustering-machine-learning-python-scikit-learn/ Unsupervised ...
- EmployeeTest
package fourthf; public class EmployeeTest { public static void main(String[] args) { // TODO Auto-g ...
- 浅谈Android样式开发之shape
引言 在Android开发中我们很多情况都是使用图片来展示相关效果,今天我就来详细介绍下Android下使用Shape来进行简单UI的开发.一方面这些是Android开发的基础,另一方面这方面的知识可 ...
- 忘记mysql root 密码修改小技巧
首先我说一下我的情况,我并不是忘记了我的root密码,只不过是我在使用phpmyadmin的时候更改密码的时候选择了如图1 的这个方法将密码加密并更改了,然后就再次登录的时候登录不上,所以对于菜鸟级的 ...
- iptables原理
1.iptables防火墙简介 Iptables也叫netfilter是Linux下自带的一款免费且优秀的基于包过滤的防火墙工具,它的功能十分强大,使用非常灵活,可以对流入.流出.流经服务器的数据包进 ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- c++虚析构函数
虚析构函数的作用主要是当通过基类指针删除派生类对象时,调用派生类的析构函数(如果没有将不会调用派生类析构函数) #include <iostream> using namespace st ...
- java中 IndexOf()、lastIndexOf()、substring()的用法
public int indexof(String str)返回字符串中出现str的第一个位置 public int indexof(String str,int fromIndex)返回字符串中从f ...
- [MySQL] MySQL存储过程常用的函数
一.字符串类 CHARSET(str) //返回字串字符集 CONCAT (string2 [,... ]) //连接字串 INSTR (string ,substring ) //返回substr ...