博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kthread_run【转】
阅读量:6080 次
发布时间:2019-06-20

本文共 3133 字,大约阅读时间需要 10 分钟。

转自:

头文件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!\n");      tsk = kthread_run(thread_function, NULL, "mythread%d", 1);      if (IS_ERR(tsk)) {          printk(KERN_INFO "create kthread failed!\n");      }      else {          printk(KERN_INFO "create ktrhead ok!\n");      }      return 0;  }  static void hello_exit(void)  {      printk(KERN_INFO "Hello, exit!\n");      if (!IS_ERR(tsk)){          int ret = kthread_stop(tsk);          printk(KERN_INFO "thread function has run %ds\n", ret);      }  }  module_init(hello_init);  module_exit(hello_exit);

 

本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sky-heaven/p/6226181.html,如需转载请自行联系原作者

你可能感兴趣的文章
本地vs云:大数据厮杀的最终幸存者会是谁?
查看>>
阿里云公共镜像、自定义镜像、共享镜像和镜像市场的区别 ...
查看>>
shadowtunnel v1.7 发布:新增上级负载均衡支持独立密码
查看>>
Java线程:什么是线程
查看>>
mysql5.7 创建一个超级管理员
查看>>
【框架整合】Maven-SpringMVC3.X+Spring3.X+MyBatis3-日志、JSON解析、表关联查询等均已配置好...
查看>>
要想成为高级Java程序员需要具备哪些知识呢?
查看>>
带着问题去学习--Nginx配置解析(一)
查看>>
onix-文件系统
查看>>
java.io.Serializable浅析
查看>>
我的友情链接
查看>>
多线程之线程池任务管理通用模板
查看>>
CSS3让长单词与URL地址自动换行——word-wrap属性
查看>>
CodeForces 580B Kefa and Company
查看>>
开发规范浅谈
查看>>
Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming
查看>>
鼠标增强软件StrokeIt使用方法
查看>>
本地连接linux虚拟机的方法
查看>>
某公司面试java试题之【二】,看看吧,说不定就是你将要做的题
查看>>
BABOK - 企业分析(Enterprise Analysis)概要
查看>>