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 ...
随机推荐
- selenium 页面截图并保存
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org. ...
- CSS3的filter用法
最近到处看到有人在说CSS3的filter一直没有时间自己去测试这效果.今天终于抽出时间学习这个CSS3的Filter.不整不知道呀,一整才让我感到吃惊,太强大了.大家先来看个效果吧: 我想光看上面的 ...
- iOS8新特性(1)-UIPopoverPresentationController使用
从iOS 8开始,苹果提出新的 UIPopoverPresentationController代替UIPopoverController: 新的UIPopoverPresentationControl ...
- 【linux使用】bash shell命令行常用快捷键 (转载)
移动: Ctrl + A: 移动到当前编辑的命令行首, Ctrl + E: 移动到当前编辑的命令行尾, Ctrl + F 或 ->:按字符右移(往命令行尾部方向,前移) Ctrl + B 或 & ...
- 用Java来写常见的排序算法
随着校招的临近 算法是校招中很重要的一个部分 总结了常见几种排序算法,各种算法的时间复杂度和空间复杂度大家也需要多了解下 package com.huwei.sort; /** * 各种排序算法 * ...
- linux防火墙开启端口
1.使用命令查看端口开启情况(下图为安装时未选择开启防火墙) [root@fullstack ~]# iptables -L -n Chain INPUT (policy ACCEPT) target ...
- dotNet平台模板列中的单选无效的解决方案
最近在grid里添加一个单选列,最开始直接创建一个模板列,然后在模板列里放一个radiobutton.并指定其GroupName.这是radiabutton最常用的方法.但是在Grid里,这样却毫无效 ...
- file xxx from install of xxx conflicts with file from xxx
执行安装 rpm -ivh lib64stdc++6-4.6.1-2-mdv2011.0.x86_64.rpm 时提示以下错误: warning: lib64stdc++6-4.6.1-2-mdv20 ...
- python中不同包之间调用方法、
在pycharm中... 当两个py文件在同一个文件夹下的时候.直接from 文件名 import * 即可 当两个文件在不同的文件夹下的时候.需要在文件中加入 _init_.py 文件.里面可以什 ...
- C# 构建动态树
public class Tree { public Guid Id { get; set; } public string Url { get; set; } public Guid? Parent ...