这里使用了msleep(50); printk 开启其实挺大的,当我使用msleep(10);机器直接卡死了;

另外ISERR不能判断结构体的,只能判断 空指针

#cat hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/err.h> MODULE_LICENSE("Dual BSD/GPL"); static struct task_struct *tsk; static int thread_function(void *data)
{
int time_count = 0;
do {
printk(KERN_INFO "thread_function: %d times", ++time_count);
msleep(50);
}while(!kthread_should_stop());
return time_count;
} static int hello_init(void)
{
printk(KERN_INFO "Hello, world!\n"); tsk = kthread_run(thread_function, NULL, "mythread%d", 1);
if (IS_ERR(tsk)) {
printk(KERN_INFO "create kthread failed!\n");
}
else {
printk(KERN_INFO "create ktrhead ok!\n");
}
return 0;
} static void hello_exit(void)
{
printk(KERN_INFO "Hello, exit!\n");
if (IS_ERR(tsk)){
printk(KERN_INFO "thread function already dispear");
}else {
printk(KERN_INFO "thread function is there,let's use kthread_stop to stop it\n");
int ret = kthread_stop(tsk);
printk(KERN_INFO "stop over,thread function has run %ds\n", ret);
}
} module_init(hello_init);
module_exit(hello_exit);

[root@xxx /home/ahao.mah/main]
#cat Makefile
obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd) all:
$(MAKE) -C $(KDIR) M=$(PWD) modules clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean

make编译

#make
make -C /lib/modules/3.10.0-327.ali2000.alios7.x86_64/build M=/home/ahao.mah/main modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-327.ali2000.alios7.x86_64'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/kernels/3.10.0-327.ali2000.alios7.x86_64'

安装模块

#insmod hello.ko

卸载模块

[root@rt2m09617.sqa.tbc /home/ahao.mah/main]
#rmmod hello

使用kthread内核线程的内核模块的更多相关文章

  1. linux常见进程与内核线程

    发现大量jdb2进程占用io资源.jdb2进程是一个文件系统的写journal的进程 kthreadd:这种内核线程只有一个,它的作用是管理调度其它的内核线程.它在内核初始化的时候被创建,会循环运行一 ...

  2. Linux内核线程的思考与总结

    1.内核线程,只是一个称呼,实际上就是一个进程,有自己独立的TCB,参与内核调度,也参与内核抢占. 这个进程的特别之处有两点,第一.该进程没有前台.第二.永远在内核态中运行. 2.创建内核线程有两种方 ...

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

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

  4. Linux进程管理 (篇外)内核线程简要介绍

    关键词:kthread.irq.ksoftirqd.kworker.workqueues 在使用ps查看线程的时候,会有不少[...]名称的线程,这些有别于其它线程,都是内核线程. 其中多数内核线程从 ...

  5. Linux内核线程创建

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

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

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

  7. 常见linux内核线程说明

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

  8. Linux内核线程之深入浅出【转】

    转自:http://blog.csdn.net/yiyeguzhou100/article/details/53126626 [-] 线程和进程的差别 线程的分类 1     内核线程 2     轻 ...

  9. Linux进程管理 (篇外)内核线程简要介绍【转】

    转自:https://www.cnblogs.com/arnoldlu/p/8336998.html 关键词:kthread.irq.ksoftirqd.kworker.workqueues 在使用p ...

随机推荐

  1. C# 利用反射查看类的信息

    using System; using System.Collections; using System.Collections.Generic; using System.Reflection; u ...

  2. (转)基于企业级证书的IOS应用打包升级功能介绍

    IOS应用程序升级流程介绍:IOS手机端应用程序需要升级时,打开服务器端html文件(本文为ucab.html文件)->点击在线安装->打开plist文件(本文中为ucab.plist文件 ...

  3. 【微信H5支付】微信公众号里H5网页点击调取微信支付

    最近在公众号里开发了下单支付H5网页,需要在H5里调用微信支付界面.开发思路和代码整理如下: todo...

  4. swift 创建tableView 并实现协议

    import UIKit class ViewController2: UIViewController,UITableViewDelegate,UITableViewDataSource{      ...

  5. Linux进程实时监控 - htop

    htop 是一个 Linux 下的交互式的进程浏览器,top的增强版 htop:                        进入:htop        退出:按q键 常用操作:          ...

  6. boost之ThreadPool

    threadpool是基于boost库实现的一个线程池子库,但线程池实现起来不是很复杂.我们从threadpool中又能学到什么东西呢? 它是基于boost库实现的,如果大家对boost库有兴趣,看看 ...

  7. Mysql相关操作

    1. 如何更改系统环境变量PATH?vim /etc/profile  加入 PATH=$PATH:/usr/local/mysql/bin2. 默认mysql安装好后,并没有root密码,如何给ro ...

  8. 广播接收者 BroadcastReceiver 示例-2

    BaseActivity /**所有Activity的基类*/ public class BaseActivity extends Activity {     @Override     prote ...

  9. C# Interface显式实现和隐式实现

    c#中对接口的实现方式有两种:隐式实现和显式实现,之前一直没仔细看过,今天查了些资料,在这里整理一下. 隐式实现的例子 interface IChinese { string Speak(); } p ...

  10. PagerSlidingTabStrip的使用

    布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...