Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux pthreads\u setaffinity\u np:参数无效?_Linux_Pthreads_Multiprocessing_Setthreadaffinitymask - Fatal编程技术网

Linux pthreads\u setaffinity\u np:参数无效?

Linux pthreads\u setaffinity\u np:参数无效?,linux,pthreads,multiprocessing,setthreadaffinitymask,Linux,Pthreads,Multiprocessing,Setthreadaffinitymask,我已经设法让我的pthreads程序正常工作了。基本上,我试图手动设置4个线程的关联性,以便线程1在CPU 1上运行,线程2在CPU 2上运行,线程3在CPU 3上运行,线程4在CPU 4上运行 编译后,我的代码只适用于几个线程,其他线程则不适用(好像线程1从来都不起作用),但在不同的时间运行同一个编译程序会得到不同的结果 例如: hao@Gorax:~/Desktop$./a.out 线程3正在CPU 3上运行 pthread\u setaffinity\u np:参数无效 线程2正在CPU

我已经设法让我的pthreads程序正常工作了。基本上,我试图手动设置4个线程的关联性,以便线程1在CPU 1上运行,线程2在CPU 2上运行,线程3在CPU 3上运行,线程4在CPU 4上运行

编译后,我的代码只适用于几个线程,其他线程则不适用(好像线程1从来都不起作用),但在不同的时间运行同一个编译程序会得到不同的结果

例如:
hao@Gorax:~/Desktop$./a.out
线程3正在CPU 3上运行
pthread\u setaffinity\u np:参数无效
线程2正在CPU 2上运行
hao@Gorax:~/Desktop$./a.out
线程2正在CPU 2上运行
pthread\u setaffinity\u np:参数无效
pthread\u setaffinity\u np:参数无效
线程3正在CPU 3上运行
线程3正在CPU 3上运行
hao@Gorax:~/Desktop$./a.out
线程2正在CPU 2上运行
pthread\u setaffinity\u np:参数无效
线程4正在CPU 4上运行
线程4正在CPU 4上运行
hao@Gorax:~/Desktop$./a.out
pthread_setaffinity_np:参数无效

我的问题是“为什么会发生这种情况?还有,为什么邮件有时会打印两次?”

代码如下:

#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>

#define handle_error_en(en, msg) \
               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

void *thread_function(char *message)
{
    int s, j, number;
    pthread_t thread;
    cpu_set_t cpuset;

    number = (int)message;
    thread = pthread_self();    
    CPU_SET(number, &cpuset);

    s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0)
    {
        handle_error_en(s, "pthread_setaffinity_np");
    }

    printf("Thread %d is running on CPU %d\n", number, sched_getcpu());

    exit(EXIT_SUCCESS);
}

int main()
{
    pthread_t thread1, thread2, thread3, thread4;
    int thread1Num = 1;
    int thread2Num = 2;
    int thread3Num = 3;
    int thread4Num = 4;
    int thread1Create, thread2Create, thread3Create, thread4Create, i, temp;

    thread1Create = pthread_create(&thread1, NULL, (void *)thread_function, (char *)thread1Num);
    thread2Create = pthread_create(&thread2, NULL, (void *)thread_function, (char *)thread2Num);
    thread3Create = pthread_create(&thread3, NULL, (void *)thread_function, (char *)thread3Num);
    thread4Create = pthread_create(&thread4, NULL, (void *)thread_function, (char *)thread4Num);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    pthread_join(thread4, NULL);

    return 0;
}
定义GNU源
#包括
#包括
#包括
#包括
#包括
#定义句柄\u错误\u en(en,msg)\
do{errno=en;perror(msg);exit(exit_FAILURE);}while(0)
void*thread_函数(char*message)
{
int s,j,数字;
pthread\u t线程;
cpu\u设置\u t cpuset;
number=(int)消息;
thread=pthread_self();
CPU_设置(编号和cpuset);
s=pthread\u setaffinity\u np(线程、大小f(cpu\u set\t)和cpuset);
如果(s!=0)
{
handle_error_en(s,“pthread_setaffinity_np”);
}
printf(“线程%d正在CPU%d\n上运行”,编号,sched_getcpu());
退出(退出成功);
}
int main()
{
pthread_t thread1、thread2、thread3、thread4;
int thread1Num=1;
int-thread2Num=2;
int-thread3Num=3;
int thread4Num=4;
int thread1创建,thread2创建,thread3创建,thread4创建,i,temp;
thread1Create=pthread_create(&thread1,NULL,(void*)thread_函数,(char*)thread1Num);
thread2Create=pthread_create(&thread2,NULL,(void*)thread_函数,(char*)thread2Num);
thread3Create=pthread_create(&thread3,NULL,(void*)thread_函数,(char*)thread3Num);
thread4Create=pthread_create(&thread4,NULL,(void*)thread_函数,(char*)thread4Num);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
pthread_join(thread3,NULL);
pthread_join(thread4,NULL);
返回0;
}
第一个CPU是CPU 0而不是CPU 1。因此,您需要更改threadNums:

int thread1Num = 0;
int thread2Num = 1;
int thread3Num = 2;
int thread4Num = 3;
您应该通过以下方式使用CPU_ZERO()宏初始化
cpuset

CPU_ZERO(&cpuset);
CPU_SET(number, &cpuset);
另外,不要从线程调用exit(),因为它将停止整个进程及其所有线程:

exit(EXIT_SUCCESS); return 0; // Use this instead or call pthread_exit() 退出(退出成功); 返回0;//改为使用此函数或调用pthread_exit()
谢谢,我会在几个小时内尝试:)