Linux中如何将线程绑定至特定的CPU上运行

通过

pthread_attr_t attr;
	int s = pthread_attr_init(&attr);
	if (s != 0) {
		perror("error");
		return 1;
	}
	pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

设置线程属性的scope为PTHREAD_SCOPE_SYSTEM。

然后在具体的线程开始代码处做如下设置:

//we can set one or more bits here, each one representing a single CPU
	cpu_set_t cpuset;
	//the CPU we whant to use
	int cpu = 2;
	CPU_ZERO(&cpuset);       //clears the cpuset
	CPU_SET(cpu, &cpuset); //set CPU 2 on cpuset

	/*
	 * cpu affinity for the calling thread
	 * first parameter is the pid, 0 = calling thread
	 * second parameter is the size of your cpuset
	 * third param is the cpuset in which your thread will be
	 * placed. Each bit represents a CPU
	 */
	sched_setaffinity(0, sizeof(cpuset), &cpuset);

上面设置当前的线程在cpu 2上运行 。

完整的代码如下:

#define _GNU_SOURCE
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sched.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <errno.h>
#include <assert.h>
#include <malloc.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/types.h>

/**
 * https://man7.org/linux/man-pages/man3/mkfifo.3.html
 *
 * must use  sudo  to  run
 *
 *
 */

#define NAME "/dev/t01"

#define COUNT 100000

void* calls(void *ptr) {
	//we can set one or more bits here, each one representing a single CPU
	cpu_set_t cpuset;
	//the CPU we whant to use
	int cpu = 2;
	CPU_ZERO(&cpuset);       //clears the cpuset
	CPU_SET(cpu, &cpuset); //set CPU 2 on cpuset

	/*
	 * cpu affinity for the calling thread
	 * first parameter is the pid, 0 = calling thread
	 * second parameter is the size of your cpuset
	 * third param is the cpuset in which your thread will be
	 * placed. Each bit represents a CPU
	 */
	sched_setaffinity(0, sizeof(cpuset), &cpuset);

//	sleep(2);
	printf("calls 1 \n");
	char a[] = "i am the sender";
	int fd = open(NAME, O_WRONLY);
	printf("fd:%d\n", fd);
	if (fd < 0) {
		perror("open failed");
		return NULL;
	}
	for (int i = 0; i < COUNT; i++) {
		ioctl(fd, 0, 100);
	}
	close(fd);
	return ptr;
}

void* calls2(void *ptr) {
//	sleep(5);
	printf("calls 1 \n");
	char a[] = "i am the sender";
	int fd = open(NAME, O_WRONLY);
	printf("fd:%d\n", fd);
	if (fd < 0) {
		perror("open failed");
		return NULL;
	}
	for (int i = 0; i < 10000; i++) {
		ioctl(fd, 0, 100);
	}
	close(fd);
	return ptr;
}

int main() {
	pthread_attr_t attr;
	int s = pthread_attr_init(&attr);
	if (s != 0) {
		perror("error");
		return 1;
	}
	pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

	printf("main\n");
	pthread_t thread;
	pthread_t thread2;
	pthread_t thread3;
	pthread_t thread4;
	pthread_t thread5;
	pthread_t thread6;
	pthread_t thread7;
	pthread_t thread8;
	pthread_create(&thread, &attr, calls, NULL);
	pthread_create(&thread2, &attr, calls, NULL);
	pthread_create(&thread3, &attr, calls, NULL);
	pthread_create(&thread4, &attr, calls, NULL);
//	pthread_create(&thread5, NULL, calls2, NULL);
//	pthread_create(&thread6, NULL, calls, NULL);
//	pthread_create(&thread7, NULL, calls2, NULL);
//	pthread_create(&thread8, NULL, calls2, NULL);
	pthread_join(thread, NULL);
//	pthread_join(thread2, NULL);
//	pthread_join(thread3, NULL);
//	pthread_join(thread4, NULL);
//	pthread_join(thread5, NULL);
//	pthread_join(thread6, NULL);
//	pthread_join(thread7, NULL);
//	pthread_join(thread8, NULL);
	printf("456\n");
	return EXIT_SUCCESS;
}


注意第一行的

#define _GNU_SOURCE

必须加,不能编译不过.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值