通过
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
必须加,不能编译不过.