1 使用kthread_create创建线程:
    struct task_struct *kthread_create(int (*threadfn)(void *data),

void *data,
                                                                   const char *namefmt, ...);
这个函数可以像printk一样传入某种格式的线程名
线程创建后,不会马上运行,而是需要将kthread_create() 返回的task_struct指针传给wake_up_process(),然后通过此函数运行线程。

2. 当然,还有一个创建并启动线程的函数:kthread_run
   struct task_struct *kthread_run(int (*threadfn)(void *data),
                                    void *data,
                                    const char *namefmt, ...);
3. 线程一旦启动起来后,会一直运行,除非该线程主动调用do_exit函数,或者其他的进程调用kthread_stop函数,结束线程的运行。
    int kthread_stop(struct task_struct *thread);
kthread_stop() 通过发送信号给线程。
如果线程函数正在处理一个非常重要的任务,它不会被中断的。当然如果线程函数永远不返回并且不检查信号,它将永远都不会停止。
参考:Kernel threads made easy

--

  1. #include <linux/kthread.h>
  2. static struct task_struct * _task;
  3. static struct task_struct * _task2;
  4. static struct task_struct * _task3;
  5. static int thread_func(void *data)
  6. {
  7. int j,k;
  8. int timeout;
  9. wait_queue_head_t timeout_wq;
  10. static int i = 0;
  11. i++;
  12. j = 0;
  13. k = i;
  14. printk("thread_func %d started/n", i);
  15. init_waitqueue_head(&timeout_wq);
  16. while(!kthread_should_stop())
  17. {
  18. interruptible_sleep_on_timeout(&timeout_wq, HZ);
  19. printk("[%d]sleeping..%d/n", k, j++);
  20. }
  21. return 0;
  22. }
  23. void my_start_thread(void)
  24. {
  25. //_task = kthread_create(thread_func, NULL, "thread_func2");
  26. //wake_up_process(_task);
  27. _task = kthread_run(thread_func, NULL, "thread_func2");
  28. _task2 = kthread_run(thread_func, NULL, "thread_func2");
  29. _task3 = kthread_run(thread_func, NULL, "thread_func2");
  30. if (!IS_ERR(_task))
  31. {
  32. printk("kthread_create done/n");
  33. }
  34. else
  35. {
  36. printk("kthread_create error/n");
  37. }
  38. }
  39. void my_end_thread(void)
  40. {
  41. int ret = 0;
  42. ret = kthread_stop(_task);
  43. printk("end thread. ret = %d/n" , ret);
  44. ret = kthread_stop(_task2);
  45. printk("end thread. ret = %d/n" , ret);
  46. ret = kthread_stop(_task3);
  47. printk("end thread. ret = %d/n" , ret);
  48. }

在执行kthread_stop的时候,目标线程必须没有退出,否则会Oops。原因很容易理解,当目标线程退出的时候,其对应的task结构也变得无效,kthread_stop引用该无效task结构就会出错。

为了避免这种情况,需要确保线程没有退出,其方法如代码中所示:

thread_func()

{

// do your work here

// wait to exit

while(!thread_could_stop())

{

wait();

}

}

exit_code()

{

kthread_stop(_task);   //发信号给task,通知其可以退出了

}

这种退出机制很温和,一切尽在thread_func()的掌控之中,线程在退出时可以从容地释放资源,而不是莫名其妙地被人“暗杀”。

kthread_stop引起的OOP的更多相关文章

  1. c#面向对象基础技能——学习笔记(二)基于OOP思想研究对象的【属性】

    字段(成员变量): 字段只能从对象中访问实例字段,无法直接从类中访问(换言之,不创建实例就不能访问),可以理解为:字段一般用在内部数据交互使用,当需要为外部提供数据时,(要优先使用自动实现的属性而不是 ...

  2. 一个简单oop的changeTab

    好多地方都会用到这样一个效果“点击tab切换内容页”,根据自己的想法实现了一下,写了个简单的插件.以前写代码都是标准的函数式编程,现在觉得面向对象编程看起来比较爽,并且更容易维护,于是就用oop的思想 ...

  3. Python OOP(面向对象编程)

    一OOP的作用 在Python中,类是面向对象设计(OOP)的主要工具.通过使用类这种工具,OOP可以: 1.分解代码,最小化代码的冗余. 2.通过定制现有的代码,来编写新的程序,而不用在原处进行修改 ...

  4. OOP,WEB开发实用小技巧

    偶然读到一篇博客,记录一下心得.这种设计对于新手来说一般是想不到的,它充分的发挥了OOP语言的特性,让代码专用而清爽.这是不是重构的思想呢? 我们在写业务层的时候,有很多方法是重复功能的,我们就可以使 ...

  5. 从OOP的角度看Golang

    资料来源 https://github.com/luciotato/golang-notes/blob/master/OOP.md?hmsr=toutiao.io&utm_medium=tou ...

  6. 玩转JavaScript OOP[2]——类的实现

    概述 当我们在谈论面向对象编程时,我们在谈论什么?我们首先谈论的是一些概念:对象.类.封装.继承.多态.对象和类是面向对象的基础,封装.继承和多态是面向对象编程的三大特性. JavaScript提供了 ...

  7. Atitit 面向对象编程(OOP)、面向组件编程(COP)、面向方面编程(AOP)和面向服务编程(SOP)的区别和联系

    Atitit 面向对象编程(OOP).面向组件编程(COP).面向方面编程(AOP)和面向服务编程(SOP)的区别和联系 1. 面向组件编程(COP) 所以,组件比起对象来的进步就在于通用的规范的引入 ...

  8. iOS - 对OOA、OOD、OOP的理解

    很多人在求职的时候,会遇到一个这样的问题:“对OOD/OOP有较深的理解”,这个时候有人就会问OOD.OOP是什么呢?那么今天咱们就一块来看一下OOA.OOD.OOP到底是什么! (一)OOA--面向 ...

  9. OOP感悟

    行内讲的最多的就是出来一年内基本靠copy,一年后才基本懂得如何去写代码,而理解领悟oop,需要的时间却不定. 我工作中,我发现很多人拿着面向对相当的语言作者做着面向过程的事情,不需要知其所以然,只要 ...

随机推荐

  1. Baidu和Google搜索引擎使用技巧(转)

    转自:Baidu和Google搜索 http://www.douban.com/note/261208979/ 百度搜索一:基本搜索   二:高级搜索   谷歌搜索一:基本搜索1)可部分匹配也可完全匹 ...

  2. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...

  3. java编译错误:varargs 方法的非 varargs 调用

    转自:http://www.blogjava.net/ideame/archive/2007/03/23/105849.html 警告: 最后一个参数使用了不准确的变量类型的 varargs 方法的非 ...

  4. mq_notify

    NAME mq_notify - 通知进程可以接收一条消息 (REALTIME) SYNOPSIS #include <mqueue.h> int mq_notify(mqd_t mqde ...

  5. mysql 复杂的查询语句,工作中用到的记录下

    1  去重查询 select distinct id from user_info where xxxxxx 2 group by 分组查询中排序 group by本身没有排序功能,这可能是mysql ...

  6. ubuntu common

    系统信息 # uname -a              # 查看内核/操作系统/CPU信息 # cat /etc/issue        # 查看操作系统版本 #cat /proc/version ...

  7. iOS symbolicatecrash崩溃日志分析

    1.保留发布程序的 .app文件 和 .dSYM文件 连同.crash文件放在同一个文件家里面. 2.在/Applications/Xcode.app/Contents/Developer/Platf ...

  8. Android 等比例缩放图片

    // 缩放图片 public static Bitmap zoomImg(String img, int newWidth ,int newHeight){ // 图片源 Bitmap bm = Bi ...

  9. 常用WinPE

    微PE工具箱:http://www.wepe.com.cn/ 绝对PE工具箱:http://dl.pconline.com.cn/download/64736.html 通用PE工具箱:http:// ...

  10. 【流媒体】 Android 实时视频编码—H.264硬编码

    [流媒體] Android 实时视频编码—H.264硬编码 SkySeraph Apr 4th 2012 Email:skyseraph00@163.com 1  硬编码 & 软编码 硬编码: ...