• kthread_run


    头文件
    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);

    检测

    int kthread_should_stop(void);

    返回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);  
  • 相关阅读:
    js 基于函数伪造的方式实现继承
    js 创建List<Map> 这种格式的集合
    微信get post请求到微信服务器 模版 素材操作
    微信开发学习 问题1: 网页授权问题 “该连接无法访问” 解决方法
    Jackson 高性能的JSON处理 ObjectMapper
    baseDao 使用spring3+hibernate4方式
    PropertiesUtil 读取配置文件工具类
    C语言(函数)学习之strstr strcasestr
    命令行选项解析函数(C语言):getopt()和getopt_long()
    AE插件开发的一些总结
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709830.html
Copyright © 2020-2023  润新知