4 개의 쓰레드가 있는데, 1 번 쓰레드를 CPU 1, 2 번 쓰레드 2 등에서 실행하려고합니다. 그러나 아래 코드를 실행할 때, 선호도 마스크는 올바른 값을 반환하지만 스레드에서 sched_getcpu()를 수행하면 CPU 4에서 실행 중임을 모두 반환합니다.CPU Affinity Masks (다른 CPU에 쓰레드 넣기)
아무도 내 문제가 무엇인지 알 수 있습니까? 사전에
감사합니다!
#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>
void *pthread_Message(char *message)
{
printf("%s is running on CPU %d\n", message, sched_getcpu());
}
int main()
{
pthread_t thread1, thread2, thread3, thread4;
pthread_t threadArray[4];
cpu_set_t cpu1, cpu2, cpu3, cpu4;
char *thread1Msg = "Thread 1";
char *thread2Msg = "Thread 2";
char *thread3Msg = "Thread 3";
char *thread4Msg = "Thread 4";
int thread1Create, thread2Create, thread3Create, thread4Create, i, temp;
CPU_ZERO(&cpu1);
CPU_SET(1, &cpu1);
temp = pthread_setaffinity_np(thread1, sizeof(cpu_set_t), &cpu1);
printf("Set returned by pthread_getaffinity_np() contained:\n");
for (i = 0; i < CPU_SETSIZE; i++)
if (CPU_ISSET(i, &cpu1))
printf("CPU1: CPU %d\n", i);
CPU_ZERO(&cpu2);
CPU_SET(2, &cpu2);
temp = pthread_setaffinity_np(thread2, sizeof(cpu_set_t), &cpu2);
for (i = 0; i < CPU_SETSIZE; i++)
if (CPU_ISSET(i, &cpu2))
printf("CPU2: CPU %d\n", i);
CPU_ZERO(&cpu3);
CPU_SET(3, &cpu3);
temp = pthread_setaffinity_np(thread3, sizeof(cpu_set_t), &cpu3);
for (i = 0; i < CPU_SETSIZE; i++)
if (CPU_ISSET(i, &cpu3))
printf("CPU3: CPU %d\n", i);
CPU_ZERO(&cpu4);
CPU_SET(4, &cpu4);
temp = pthread_setaffinity_np(thread4, sizeof(cpu_set_t), &cpu4);
for (i = 0; i < CPU_SETSIZE; i++)
if (CPU_ISSET(i, &cpu4))
printf("CPU4: CPU %d\n", i);
thread1Create = pthread_create(&thread1, NULL, (void *)pthread_Message, thread1Msg);
thread2Create = pthread_create(&thread2, NULL, (void *)pthread_Message, thread2Msg);
thread3Create = pthread_create(&thread3, NULL, (void *)pthread_Message, thread3Msg);
thread4Create = pthread_create(&thread4, NULL, (void *)pthread_Message, thread4Msg);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);
return 0;
}
그래서 먼저 pthread_create()해야합니다. 그러나 그것은 이미 지정된 함수를 실행합니다 ... 나는이 개념 전체를 이해할 수 있을지 너무 확신하지 못합니다. 그래서 내 pthread_Message() 함수에 setaffinity 함수를 넣어야합니까? – hahuang65
매우 * long * 동안 스레드를 실행할 필요가 없습니다. 즉, 새로 생성 된 스레드가 가장 먼저 수행 할 수 있습니다. 이것은 새로운 CPU 마스크가 현재 CPU를 포함하지 않는다면 다시 일정이 잡히는 것을 의미합니다. – MarkR
아직도 이해가 안 ... pthread_create(), pthread_create()가 호출하는 함수 또는 pthread_create() 전에 Diffing을 사용하여 선호도 마스크를 설정하고 싶습니다. – hahuang65