set_cpus_allowed_ptr()将该线程绑定到特定的CPU上去

6450阅读 0评论2020-07-04 fangdikui
分类:LINUX

set_cpus_allowed_ptr(task0, cpumask_of(0));
---------------------------

点击(此处)折叠或打开

  1. #include<linux/kernel.h>
  2. #include<linux/module.h>
  3. #include<linux/kthread.h>

  4. struct task_struct *task0;
  5. static spinlock_t spinlock;
  6. int val;

  7. int task(void *arg)
  8. {
  9.     printk(KERN_INFO "%s:%d\n",__func__,__LINE__);
  10.     /* To generate panic uncomment following */
  11.     /* panic("softlockup: hung tasks"); */

  12.     while(!kthread_should_stop()) {
  13.         printk(KERN_INFO "%s:%d\n",__func__,__LINE__);
  14.         spin_lock(&spinlock);
  15.         /* busy loop in critical section */
  16.         while(1) {
  17.             printk(KERN_INFO "%s:%d\n",__func__,__LINE__);
  18.         }

  19.         spin_unlock(&spinlock);
  20.     }

  21.     return val;
  22. }

  23. static int softlockup_init(void)
  24. {
  25.     printk(KERN_INFO "%s:%d\n",__func__,__LINE__);

  26.     val = 1;
  27.     spin_lock_init(&spinlock);
  28.     task0 = kthread_run(&task,(void *)val,"softlockup_thread");
  29.     set_cpus_allowed_ptr(task0, cpumask_of(0));

  30.     return 0;
  31. }

  32. static void softlockup_exit(void)
  33. {
  34.     printk(KERN_INFO "%s:%d\n",__func__,__LINE__);
  35.     kthread_stop(task0);
  36. }

  37. module_init(softlockup_init);
  38. module_exit(softlockup_exit)

上一篇:去掉编译内核的优化选项
下一篇:Linux Watchdog 机制