Copyright © tutorialspoint.com
#include <sched.h> |
A CPU affinity mask is represented by the cpu_set_t structure, a "CPU set", pointed to by mask. Four macros are provided to manipulate CPU sets. CPU_ZERO() clears a set. CPU_SET() and CPU_CLR() respectively add and remove a given CPU from a set. CPU_ISSET() tests to see if a CPU is part of the set; this is useful after sched_getaffinity() returns. The first available CPU on the system corresponds to a cpu value of 0, the next CPU corresponds to a cpu value of 1, and so on. The constant CPU_SETSIZE (1024) specifies a value one greater than the maximum CPU number that can be stored in a CPU set.
sched_setaffinity() sets the CPU affinity mask of the process whose ID is pid to the value specified by mask. If pid is zero, then the calling process is used. The argument cpusetsize is the length (in bytes) of the data pointed to by mask. Normally this argument would be specified as sizeof(cpu_set_t).
If the process specified by pid is not currently running on one of the CPUs specified in mask, then that process is migrated to one of the CPUs specified in mask.
sched_getaffinity() writes the affinity mask of the process whose ID is pid into the cpu_set_t structure pointed to by mask. The cpusetsize argument specifies the size (in bytes) of mask. If pid is zero, then the mask of the calling process is returned.
Tag | Description |
---|---|
EFAULT | A supplied memory address was invalid. |
EINVAL | The affinity bitmask mask contains no processors that are physically on the system, or cpusetsize is smaller than the size of the affinity mask used by the kernel. |
EPERM | The calling process does not have appropriate privileges. The process calling sched_setaffinity() needs an effective user ID equal to the user ID or effective user ID of the process identified by pid, or it must possess the CAP_SYS_NICE capability. |
ESRCH | The process whose ID is pid could not be found. |
A child created via fork(2) inherits its parents CPU affinity mask. The affinity mask is preserved across an execve(2).
This manual page describes the glibc interface for the CPU affinity calls. The actual system call interface is slightly different, with the mask being typed as unsigned long *, reflecting that the fact that the underlying implementation of CPU sets is a simple bitmask. On success, the raw sched_getaffinity() system call returns the size (in bytes) of the cpumask_t data type that is used internally by the kernel to represent the CPU set bitmask.
Copyright © tutorialspoint.com