重新运行程序:在修复可能的错误后,重新运行程序以查看sched_setaffinity()是否成功。 检查返回值:检查sched_setaffinity()的返回值,如果返回0,则表示成功;如果返回-1,则检查errno以确定错误原因。 使用系统工具:使用如top、htop等系统监控工具来观察线程的CPU使用情况,验证线程是否被正确地绑定到了指定的CPU核心上。5...
(sched_setaffinity(0, sizeof(cpu_set_t), cpusetp) != 0) printf("failed to call sched_setaffinity\n"); else printf("success to set process affinity\n"); CPU_FREE(cpusetp); exit(EXIT_SUCCESS); } 2. Build it by: gcc a.c -o a -ltcmalloc or gcc a.c -o a -ltcmalloc_...
cpu_set_t cpuset; // 定义一个cpu_set_t类型的变量cpuset,用于表示CPU集合 size_t cpusetsize = sizeof(cpu_set_t); // 获取cpu_set_t类型的大小,用作sched_setaffinity和sched_getaffinity的参数 // 验证并打印当前进程的CPU亲和性 cpu_set_t get_cpuset; // 定义一个变量get_cpuset用于获取当前进程...
初始化CPU集合,将所有位清零CPU_ZERO(&cpuset);// 设置CPU亲和性,将CPU 0和CPU 1的位设置为1,表示希望将进程绑定到这两个CPU上CPU_SET(0,&cpuset);CPU_SET(1,&cpuset);// 尝试设置当前进程的CPU亲和性if(sched_setaffinity(0,cpusetsize,&cpuset)==-1){perror("sched_setaffinity failed");// 如果设...
/* sched_setaffinity returns 0 in success */ if( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 ) { printf("WARNING: Could not set CPU Affinity, continuing...\n"); } 如果程序可以执行到这儿,那么我们的线程就已经设置了自己的亲和性(affinity)。调用sched_setaffinity会设置由pid所引用...
24 if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) 25 { 26 printf("set affinity np ERROR!\n"); 27 return -1; 28 } 29 30 return 0; 31 } 32 33 34 void *thread1(void *param) 35 { 36 attach_cpu(0); ...
In addition to thetasksetcommand, processor affinity can also be set using thesched_setaffinity()system call. The following code excerpt retrieves the CPU affinity information for a specified PID. If the PID passed to it is 0, it will return...
24 if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) 25 { 26 printf("set affinity np ERROR!\n"); 27 return -1; 28 } 29 30 return 0; 31 } 32 33 34 void *thread1(void *param) 35 { 36 attach_cpu(0); ...
14if (cpu_index < 0 || cpu_index >= cpu_num)15 { 16 printf("cpu index ERROR!\n");17return -1;18 } 19 20 cpu_set_t mask;21 CPU_ZERO(&mask);22 CPU_SET(cpu_index, &mask);23 24if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)25...
*/ if (check && (p->flags & PF_NO_SETAFFINITY)) { ret = -EINVAL; goto out; } if (cpumask_equal(p->cpus_ptr, new_mask)) goto out; /* * Picking a ~random cpu helps in cases where we are changing affinity * for groups of tasks (ie. cpuset), so that load balancing i...