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

本文共 3103 字,大约阅读时间需要 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);

 

转载于:https://www.cnblogs.com/sky-heaven/p/6226181.html

你可能感兴趣的文章
WinForm 之 程序启动不显示主窗体
查看>>
FragmentTransaction.replace() 你不知道的坑
查看>>
模拟退火算法
查看>>
StringUtils方法全集介绍
查看>>
性能调校
查看>>
VMware workstation虚拟网卡类型介绍
查看>>
C# web 更新折腾记
查看>>
IBM主机巡检操作文档
查看>>
zabbix企业应用之Mysql主从监控
查看>>
移动端iphone按下a链接背景颜色会变灰
查看>>
如何识别 MacBook Pro 机型
查看>>
javascript 图标分析工具
查看>>
从结构struct谈到类class(基于C++实现)
查看>>
阿里云负载均衡服务
查看>>
小命令 sysdig
查看>>
IT十八掌作业_java基础第五天_静态代码块、类的继承和接口
查看>>
流程控制-for序列、流程控制-for字典
查看>>
Easy APNs Provider的使用
查看>>
搭建mysql集群
查看>>
Gson工具包使用
查看>>