sched_setaffinity(2) System Calls Manual sched_setaffinity(2) sched_setaffinity, sched_getaffinity - LIBRARY Standard C library (libc, -lc) #define _GNU_SOURCE /* feature_test_macros(7) */ #include int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask); int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask); , . . , (.., ) . , , . cpu_set_t, << >>, mask. CPU_SET(3) . sched_setaffinity() mask , ID pid. pid , . cpusetsize ( ), mask. sizeof(cpu_set_t). , pid, , mask, , mask. sched_getaffinity() cpu_set_t, mask, , ID pid. cpusetsize mask ( ). pid , . On success, sched_setaffinity() and sched_getaffinity() return 0 (but see "C library/kernel differences" below, which notes that the underlying sched_getaffinity() differs in its return value). On failure, -1 is returned, and errno is set to indicate the error. EFAULT . EINVAL mask , , , cpuset cgroups <>, cpuset(7). EINVAL (sched_getaffinity() and, before Linux 2.6.9, sched_setaffinity()) cpusetsize is smaller than the size of the affinity mask used by the kernel. EPERM (sched_setaffinity()) . ID ID ID , pid, CAP_SYS_NICE pid. ESRCH pid . Linux. Linux 2.5.8, glibc 2.3. Initially, the glibc interfaces included a cpusetsize argument, typed as unsigned int. In glibc 2.3.3, the cpusetsize argument was removed, but was then restored in glibc 2.3.4, with type size_t. sched_setaffinity() , , mask , . , , <>, cpuset(7). , , . : /proc/cpuinfo; sysconf(3) _SC_NPROCESSORS_CONF _SC_NPROCESSORS_ONLN; cpu /sys/devices/system/cpu/. sched(7) Linux. , . pid , gettid(2). pid 0 , , getpid(2), ( POSIX pthread_setaffinity_np(3) sched_setaffinity()). isolcpus , . -- sched_setaffinity() cpuset(7). Documentation/admin-guide/kernel-parameters.txt. , isolcpus ( ). , fork(2), . execve(2). C glibc . : mask unsigned long *, , . sched_getaffinity() mask ; cpusetsize ( ) cpumask_t, . ( unsigned long *) . , cpu_set_t, glibc, 128 , 1023. 1024, : sched_getaffinity(pid, sizeof(cpu_set_t), &mask); EINVAL; , mask, cpusetsize, ( , , ). , mask ( CPU_ALLOC(3)). -- sched_getaffinity() ( EINVAL). , CPU_ALLOC(3) , ( , sizeof(long)). , sched_getaffinity() , . , , CPU_COUNT(3) ( ). , , . , - . , . : , , . , , , . lscpu(1) , (x86) : $ lscpu | egrep -i 'core.*:|socket' Thread(s) per core: 2 Core(s) per socket: 2 Socket(s): 1 : ; ; . $ time -p ./a.out 0 0 100000000 real 14.75 user 3.02 sys 11.73 $ time -p ./a.out 0 1 100000000 real 11.52 user 3.98 sys 19.06 $ time -p ./a.out 0 3 100000000 real 7.89 user 3.29 sys 12.07 #define _GNU_SOURCE #include #include #include #include #include #include int main(int argc, char *argv[]) { int parentCPU, childCPU; cpu_set_t set; unsigned int nloops; if (argc != 4) { fprintf(stderr, "Usage: %s parent-cpu child-cpu num-loops\n", argv[0]); exit(EXIT_FAILURE); } parentCPU = atoi(argv[1]); childCPU = atoi(argv[2]); nloops = atoi(argv[3]); CPU_ZERO(&set); switch (fork()) { case -1: /* Error */ err(EXIT_FAILURE, "fork"); case 0: /* Child */ CPU_SET(childCPU, &set); if (sched_setaffinity(getpid(), sizeof(set), &set) == -1) err(EXIT_FAILURE, "sched_setaffinity"); for (unsigned int j = 0; j < nloops; j++) getppid(); exit(EXIT_SUCCESS); default: /* Parent */ CPU_SET(parentCPU, &set); if (sched_setaffinity(getpid(), sizeof(set), &set) == -1) err(EXIT_FAILURE, "sched_setaffinity"); for (unsigned int j = 0; j < nloops; j++) getppid(); wait(NULL); /* Wait for child to terminate */ exit(EXIT_SUCCESS); } } . lscpu(1), nproc(1), taskset(1), clone(2), getcpu(2), getpriority(2), gettid(2), nice(2), sched_get_priority_max(2), sched_get_priority_min(2), sched_getscheduler(2), sched_setscheduler(2), setpriority(2), CPU_SET(3), get_nprocs(3), pthread_setaffinity_np(3), sched_getcpu(3), capabilities(7), cpuset(7), sched(7), numactl(8) Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . sched_setaffinity(2)