当前位置:   article > 正文

Linux kernel-4.19 内核线程开启以及延时_kernel 4.19

kernel 4.19

如何开启Linux内核线程,可以使用 kthread_create

wake_up_process(read_thread);会让线程开始跑

使用schedule_timeout函数,该方法会让需要延迟的任务睡眠到指定的延时时间耗尽后在重新运行。

  1. #include <linux/sched.h>
  2. #include <linux/kthread.h>
  3. #include <linux/delay.h>
  4. static int read_config(void *data)
  5. {
  6. int *i = (int *)data;
  7. while(1){
  8. printk("kernel thread");
  9. schedule_timeout(msecs_to_jiffies(100));
  10. }
  11. return 0;
  12. }
  13. //kthread_create
  14. int i = 0;
  15. struct task_struct *read_thread = kthread_create(read_config, &i, "xs9922_readconfig")
  16. if (IS_ERR(read_thread)) {
  17. printk(KERN_ERR "kthread_create failed\n");
  18. } else {
  19. wake_up_process(read_thread);
  20. }

延时问题: 

schedule_timeout(timeout);:在timeout个jiffies后重新调度task

msecs_to_jiffies(100);

msecs_to_jiffies(const unsigned int m);// ms转换为系统节拍数

        这里代表内核每秒钟会产生100个系统节拍(tick),也就是10ms一次,会产生一次tick中断,每次中断 'jiffies'会 +1 ,定时的时间是基于当前的系统节拍(jiffies),向后延时定时的时间。

 schedule_timeout(msecs_to_jiffies(100));

 所以总接来说msecs_to_jiffies(100) = 10个节拍(一个节拍等于10ms ) schedule_timeout(10);在10个节拍后醒来

以上是我个人观点,有误可以讲

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号