kthread_create 简单使用

news/2024/7/4 6:11:06 标签: struct, module, thread, null, up, 框架
kthread_create:创建线程。
struct  task_struct  *kthread_create(int  (*threadfn)(void  *data),void  *data,const  char  *namefmt,  ...);
线程创建后,不会马上运行,而是需要将kthread_create()  返回的task_struct指针传给wake_up_process(),然后通过此函数运行线程。
kthread_run  :创建并启动线程的函数:
struct  task_struct  *kthread_run(int  (*threadfn)(void  *data),void  *data,const  char  *namefmt,  ...);
kthread_stop:通过发送信号给线程,使之退出。
int  kthread_stop(struct  task_struct  *thread);
线程一旦启动起来后,会一直运行,除非该线程主动调用do_exit函数,或者其他的进程调用kthread_stop函数,结束线程的运行。
但如果线程函数正在处理一个非常重要的任务,它不会被中断的。当然如果线程函数永远不返回并且不检查信号,它将永远都不会停止。

1.       头文件

#include <linux/sched.h>   //wake_up_process()

#include <linux/kthread.h> //kthread_create()、kthread_run()

#include <err.h> //IS_ERR()、PTR_ERR()



2.       实现

2.1创建线程

kernel thread可以用kernel_thread创建,但是在执行函数里面必须用daemonize释放资源并挂到init下,还需要用completion等待这一过程的完成。为了简化操作kthread_create闪亮登场。

在模块初始化时,可以进行线程的创建。使用下面的函数和宏定义:

struct task_struct *kthread_create(int (*threadfn)(void *data),

                            void *data,

                            const char namefmt[], ...);


kthread_create源码详解见http://blog.sina.com.cn/s/blog_6237dcca0100gq67.html

#define kthread_run(threadfn, data, namefmt, ...)                     /

({                                                            /

    struct task_struct *__k                                        /

           = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); /

    if (!IS_ERR(__k))                                        /

           wake_up_process(__k);                                /

    __k;                                                     /

})


例如:

static struct task_struct *test_task;

static int test_init_module(void)

{

    int err;

    test_task = kthread_create(test_thread, NULL, "test_task");

    if(IS_ERR(test_task)){

      printk("Unable to start kernel thread./n");

      err = PTR_ERR(test_task);

      test_task = NULL;

      return err;

    }

    wake_up_process(test_task);

    return 0;

}

        module_init(test_init_module);


2.2线程函数

在线程函数里,完成所需的业务逻辑工作。主要框架如下所示:

int threadfunc(void *data){

        …

        while(1){

               set_current_state(TASK_UNINTERRUPTIBLE);

               if(kthread_should_stop()) break;

               if(){//条件为真

                      //进行业务处理

               }

               else{//条件为假

                      //让出CPU运行其他线程,并在指定的时间内重新被调度

                      schedule_timeout(HZ);

               }

        }

        …

        return 0;

}

2.3结束线程

在模块卸载时,可以结束线程的运行。使用下面的函数:

int kthread_stop(struct task_struct *k);

例如:

              static void test_cleanup_module(void)

{

            if(test_task){

                kthread_stop(test_task);

                test_task = NULL;

            }

}

module_exit(test_cleanup_module);


3.       注意事项

(1)       在调用kthread_stop函数时,线程函数不能已经运行结束。否则,kthread_stop函数会一直进行等待。

(2)       线程函数必须能让出CPU,以便能运行其他线程。同时线程函数也必须能重新被调度运行。在例子程序中,这是通过schedule_timeout()函数完成的。

4.性能测试

可以使用top命令来查看线程(包括内核线程)的CPU利用率。命令如下:

       top –p 线程号

可以使用下面命令来查找线程号:

       ps aux|grep 线程名

       注:线程名由kthread_create函数的第三个参数指定。

代码:

#include <linux/kthread.h>
#include <linux/module.h>
#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
static struct task_struct * MyThread = NULL;
static int MyPrintk(void *data)
{
char *mydata = kmalloc(strlen(data)+1,GFP_KERNEL);
memset(mydata,'\0',strlen(data)+1);
strncpy(mydata,data,strlen(data));
while(!kthread_should_stop())
{
SLEEP_MILLI_SEC(1000);
printk("%s\n",mydata);
}
kfree(mydata);
return 0;
}
static int __init init_kthread(void)
{
MyThread = kthread_run(MyPrintk,"hello world","mythread");
return 0;
}
static void __exit exit_kthread(void)
{
if(MyThread)
{
printk("stop MyThread\n");
kthread_stop(MyThread);
}
}
module_init(init_kthread);
module_exit(exit_kthread);
MODULE_AUTHOR("YaoGang");

这个内核线程的作用就是每隔一秒打印一个“hello world”
值得一提的是kthread_should_stop函数,我们需要在开启的线程中嵌入该函数并检查此函数的返回值,否则kthread_stop是不起作用的

可以使用top命令来查看线程(包括内核线程)的CPU利用率。命令如下:

       top –p线程号

可以使用下面命令来查找线程号:

       ps aux|grep线程名




http://www.niftyadmin.cn/n/1116644.html

相关文章

互联网中B端客户和C端客户的区别

一般该词在互联网中出现比较多&#xff0c;主要指的是企业端用户&#xff0c;即这里的B端就是说的是B2B&#xff08;Business-to-Business)中的企业之间的网络营销&#xff01; 除了B端客户还有C端客户&#xff0c;A端客户。C代表的是consumer &#xff08;消费者&#xff09;…

IOS开发之无法选择模拟器显示NO Scheme

1. 不是 文件冲突的 看这个链接https://blog.csdn.net/sanpintian/article/details/7377365 2.文件冲突的 打开工程文件。 打开 直接 搜索 <<<< 或者 >>> 然后删除 转载于:https://www.cnblogs.com/qingzZ/p/10233808.html

具有 CSA CCM 证明的 SOC 2 可简化 Windows Azure 客户的安全性评估过程

编辑人员注释&#xff1a;本文章由 Windows Azure 产品市场营销总监 Sarah Fender 撰写。 今天&#xff0c;我们宣布 Microsoft 的公共审计师 Deloitte 已经发布了有关 Windows Azure 安全性、可用性和保密性信赖原则的服务组织控制 (SOC) 2 类型 2 报告。作为“企业就绪”云服…

典型基础面试题目

2019独角兽企业重金招聘Python工程师标准>>> 转载于:https://my.oschina.net/architectliuyuanyuan/blog/3038967

第八部分_客户化JSP标签

EL语言(减少JSP页面中的Java代码) <body><%String username request.getParameter("username");String password request.getParameter("password");%>username: <% out.print(username); %><br>password: <% out.print(pass…

mini2440 dm9000 网卡驱动详解 2

3. platform_driver的remove&#xff0c; suspend和resume的实现 remove函数的功能是把设备从内核中移除&#xff0c;释放内存区域。该函数在卸载模块时被调用。代码清单如下&#xff1a; static int __devexit dm9000_drv_remove(struct platform_device *pdev) { …

linux下实时查看log

1、先切换到&#xff1a;cd usr/local/tomcat5/logs2、tail -f catalina.out3、这样运行时就可以实时查看运行日志了 Ctrlc 是退出tail命令。 顺便讲一下linux中tail命令 tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail…

checkAll全选的一个小例子

function checkAll(tag,flag){//得到所有check var checkboxs $(tag).closest("table").find(":checkbox");for(var i0;i<checkboxs.length;i){if(checkboxs[i].name"importedNetItemId"){if(flag){checkboxs[i].checked tr…