kthread_run【转】

摘要:
IS_错误){va_listargs;va_start;vsnprintf;va_end;}returncreate。结果;}EXPORT_符号;结束intkthread_stop;1检测intkthread_ should_ stop;1返回should_Stop标志。它用于检查创建的线程的结束标志,并决定是否退出示例staticinthello_init{printk(KERN_INFO)“你好,世界!

转自:http://blog.csdn.net/zhangxuechao_/article/details/50876397

头文件
include/linux/kthread.h
创建并启动
/**
 * kthread_run - create and wake a thread.
 * @threadfn: the function to run until signal_pending(current).
 * @data: data ptr for @threadfn.
 * @namefmt: printf-style name for the thread.
 *
 * Description: Convenient wrapper for kthread_create() followed by
 * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
 */
#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;                                       
})

创建
/**
* kthread_create - create a kthread.
* @threadfn: the function to run until signal_pending(current).
* @data: data ptr for @threadfn.
* @namefmt: printf-style name for the thread.
*
* Description: This helper function creates and names a kernel
* thread. The thread will be stopped: use wake_up_process() to start
* it. See also kthread_run(), kthread_create_on_cpu().
*
* When woken, the thread will run @threadfn() with @data as its
* argument. @threadfn can either call do_exit() directly if it is a
* standalone thread for which noone will call kthread_stop(), or
* return when 'kthread_should_stop()' is true (which means
* kthread_stop() has been called). The return value should be zero
* or a negative error number; it will be passed to kthread_stop().
*
* Returns a task_struct or ERR_PTR(-ENOMEM).
*/
struct task_struct *kthread_create(int (*threadfn)(void *data),
       void *data,
       const charnamefmt[],
       ...)
{
    structkthread_create_info create;
    DECLARE_WORK(work, keventd_create_kthread, &create);
    create.threadfn =threadfn;
    create.data =data;
    init_completion(&create.started);
    init_completion(&create.done);
    /*
    * The workqueue needs to start up first:
    */
    if (!helper_wq)
       work.func(work.data);
    else{
       queue_work(helper_wq, &work);
       wait_for_completion(&create.done);
    }
    if (!IS_ERR(create.result)) {
       va_list args;
       va_start(args, namefmt);
       vsnprintf(create.result->comm, sizeof(create.result->comm),
         namefmt, args);
       va_end(args);
    }
    returncreate.result;
}
EXPORT_SYMBOL(kthread_create);

结束
int kthread_stop(struct task_struct *k);
    1
检测
int kthread_should_stop(void);
    1
返回should_stop标志。它用于创建的线程检查结束标志,并决定是否退出
举例
static int hello_init(void)  
{  
    printk(KERN_INFO "Hello, world!
");  
    tsk = kthread_run(thread_function, NULL, "mythread%d", 1);  
    if(IS_ERR(tsk)) {  
        printk(KERN_INFO "create kthread failed!
");  
    }  
    else{  
        printk(KERN_INFO "create ktrhead ok!
");  
    }  
    return 0;  
}  
static void hello_exit(void)  
{  
    printk(KERN_INFO "Hello, exit!
");  
    if (!IS_ERR(tsk)){  
        int ret =kthread_stop(tsk);  
        printk(KERN_INFO "thread function has run %ds
", ret);  
    }  
}  
module_init(hello_init);  
module_exit(hello_exit);  

免责声明:文章转载自《kthread_run【转】》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇window.opener方法的使用 js跨域在CMD上用telnet远程登录发送邮件测试记录下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Vlmcsd(KMS)激活服务器程序

vlmcs-Windows-x64.exe127.0.0.1检查服务器是否正常联通,端口1688。vlmcs-Windows-x64.exe-X127.0.0.1显示支持的激活app类型。...

如何让bat文件已管理员身份运行

某些批处理需要高权限才能执行行,以实现所需的Zhi效果。您可以以管理员身份运行,方法是将以下代码放在批处理的顶部。...

CAS单点登录------未认证授权服务

问题背景:之前我使用的127.0.0.1进行CAS直接url进行过滤!后来我用nginx进行反向代理出现问题:如下图第一眼,就在内心想,草这什么鬼!调试了五分钟发现还是不行!  网上各种教程!  半小时过去了!我吧配置文件自己看看!    出现这样问题:原因是CAS 的服务认真之前先有个REgx的正则判断,目录如上看这个正则时候,我瞬间明白了,我下面这个问题...

JRebel 6 破解版及使用方法

2.解压下载的jrebel6.0.0-crack.zip、jrebel6.0 jar包和破解文件。假设文件在D:/jrebel步骤:1中解压缩。eclipse下载jrebe插件,可以在市场上下载。2.打开eclipse的窗口首选项jrebel,打开优势选项卡,并将jar包的路径指向D:/jrebel/jrebel.jar。用CMD打开DOS窗口,输入cd/d...

关于WINFORM中输入法的设置

关于WINFORM(转移到)John Suna的专栏开发中输入方法的设置,它碰巧遇到了这种问题。网络真的很好:)这是文本集。感谢作者的辛勤工作给您带来的便利。在WINFORM中,我们经常遇到这样的问题:文本输入框中的输入法被禁用或总是更改为全宽输入法。查阅相关数据后,总结如下:(1)Control.ImeMode属性:获取或设置控件的输入方法编辑器模式。此模...

Oracle 12c新特性(For DBA)

2: Oracle12cIn-MemoryOracle12cIn-Memory提供了一种独特的双格式体系结构,它可以使用传统的行格式和新的内存列格式同时在内存中存储表。与其他NOSQL分片结构相比,OracleSharding提供了优异的运行时性能和更简单的生命周期管理。OracleSharding使用GDS体系结构自动部署和管理分片和复制技术。GDS还提供...