zoukankan      html  css  js  c++  java
  • kthread_run【转】

    转自: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 char namefmt[],
           ...)
    {
        struct kthread_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);
        }
    
        return create.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);  
  • 相关阅读:
    Hadoop概述与配置
    组件间通讯及钩子函数
    react组件
    JSX
    Node.js二分查找
    链表
    微信支付V3
    在有序数组中查找某一特定元素的索引
    给定一个只包括 ‘(‘,‘)‘,‘{‘,‘}‘,‘[‘,‘]‘ 的字符串,判断字符串是否有效。
    我的前半生
  • 原文地址:https://www.cnblogs.com/sky-heaven/p/6226181.html
Copyright © 2011-2022 走看看