CPU与线程绑定

CPU与线程绑定


2024年6月19日发(作者:)

CPU与线程绑定

线程与CPU绑定

2008-07-24 17:591. 在用户空间进行线程与CPU绑定

(1)头文件

#include <sched.h>

(2)函数原型

int sched_setaffinity(pid_t pid, unsigned int len, unsigned long *mask);

int sched_getaffinity(pid_t pid, unsigned int len, unsigned long *mask);

sched_setaffinity, sched_getaffinity – set and get a process’s

CPU affinity mask

(3)程序实例

cpu_set_t mask;

__CPU_ZERO(&mask);

__CPU_SET(0, &mask);

sched_setaffinity(0,sizeof(cpu_set_t),&mask);

此段代码可将线程绑定在0号CPU上。

2. 在内核空间进行线程与CPU绑定

(1)头文件

#include <linux/sched.h>

(2)函数原型

int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask);

Change a given task‘s CPU affinity

(3)程序实例

#ifdef CONFIG_SMP

static cpumask_t apm_save_cpus(void)

{

cpumask_t x = current->cpus_allowed;

/* Some bioses don‘t like being called from CPU != 0 */

set_cpus_allowed(current, cpumask_of_cpu(0));

BUG_ON(smp_processor_id() != 0);

return x;

}

static inline void apm_restore_cpus(cpumask_t mask)

{

set_cpus_allowed(current, mask);

}

#else

/*

* No CPU lockdown needed on a uniprocessor

*/

#define apm_save_cpus() (current->cpus_allowed)

#define apm_restore_cpus(x) (void)(x)

#endif


发布者:admin,转转请注明出处:http://www.yc00.com/web/1718795828a2752655.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信