linux下线程绑定内核,多线程 – 无法将内核线程绑定到CPU

我编写了这段代码来将2个内核线程绑定到不同的CPU:

#include

#include

#include

#include

struct task_struct *task1;

struct task_struct *task2;

int cpu, data;

int thread_function_one(void *data)

{

int ret = 10;

printk(KERN_INFO "IN THREAD FUNCTION 1 \n");

while(!kthread_should_stop()){

cpu = get_cpu();

put_cpu();

printk("t1 cpu = %d\n",cpu);

msleep(5000);

}

printk(KERN_INFO "EXIT from thread function 1\n");

return ret;

}

int thread_function_two(void *data)

{

int ret = 10;

printk(KERN_INFO "IN THREAD FUNCTION 2 \n");

while(!kthread_should_stop()){

cpu = get_cpu();

put_cpu();

printk("t2 cpu = %d\n",cpu);

msleep(6000);

}

printk(KERN_INFO "EXIT from thread function 2\n");

return ret;

}

static int kernel_init(void)

{

printk(KERN_INFO "module_init\n");

cpu = get_cpu();

put_cpu();

printk("main thread cpu = %d \n",cpu);

task1 = kthread_create(&thread_function_one,(void *)&data,"one");

kthread_bind(task1, cpu);

wake_up_process(task1);

cpu = 5;

task2 = kthread_create(&thread_function_two,(void *)&data,"two");

kthread_bind(task2, cpu);

wake_up_process(task2);

return 0;

}

static void kernel_exit(void)

{

kthread_stop(task1);

kthread_stop(task2);

printk(KERN_INFO "module_exit\n");

}

module_init(kernel_init);

module_exit(kernel_exit);

MODULE_AUTHOR("K_K");

MODULE_LICENSE("GPL");

但是当我在thread_function_one和thread_function_two中打印cpu id时,两者都打印与主线程相同的CPU.

我做错了吗?

这个编程的输出:

kernel: [ 956.816510] module_init

kernel: [ 956.816515] main thread cpu = 8

kernel: [ 956.816743] IN THREAD FUNCTION 1

kernel: [ 956.816748] t1 cpu = 8

kernel: [ 956.817057] IN THREAD FUNCTION 2

kernel: [ 956.817062] t2 cpu = 8

kernel: [ 961.815160] t1 cpu = 8

kernel: [ 962.814649] t2 cpu = 8

kernel: [ 966.816760] t1 cpu = 8

kernel: [ 968.815711] t2 cpu = 8

kernel: [ 971.818307] EXIT from thread function 1

kernel: [ 974.816813] EXIT from thread function 2

kernel: [ 974.816872] module_exit

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值