SCHED_SETAFFINITY(2)

NAME
CPU_AND, CPU_CLR, CPU_COUNT, CPU_EQUAL, CPU_ISSET, CPU_OR, CPU_SET, CPU_XOR, CPU_ZERO, sched_setaffinity, sched_getaffinity - set and get a process's CPU affinity mask

LIBRARY
libc

SYNOPSIS
#include <sys/types.h>
#include <sys/sched.h>
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_ZERO(&mask);
CPU_SET(cpu, &mask);
CPU_CLR(cpu, &mask);
CPU_ISSET(cpu, &mask);
CPU_COUNT(&mask);
CPU_AND(&dstmask, &srcmask1, &srcmask2);
CPU_OR(&dstmask, &srcmask1, srcmask2);
CPU_XOR(&dstmask, &srcmask1, &srcmask2);
CPU_EQUAL(&mask1, &mask2);

DESCRIPTION
The sched_setaffinity() sets the CPU affinity mask of the process, which is identified by the pid to the value specified by the mask. The 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 pid is zero, then the pid of the calling process is used. All threads of the process identified by pid are affected. If the calling thread is one of the target process's threads, then the calling thread will be migrated immediately, if necessary, while other threads of the targt process will be migrated by the scheduler later.

A process created by fork(2), vfork(2), and rfork(2) inherits the calling thread's CPU affinity mask. Same applies to the threads created by lwp_create(2), and the threads created by pthread_create(3).

sched_getaffinity() writes the affinity mask of the process identified by the pid to the cpu_set_t structure pointed to by mask. If the pid is zero, then the pid of the calling process is used. If the process is multithreaded, then the CPU affinity mask of the first thread will be returned.

The cpu_set_t data structure represents a set of CPUs. 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 and so forth. The constant CPU_SETSIZE specifies a value one greater than the maximum CPU number that can be stored in cpu_set_t. The cpu_set_t should be treated as opaque data structure; all manipulation of cpu_set_t should be done via the following macros:

CPU_ZERO()
    Reset the mask, so that it contains no CPUs.

CPU_SET()
    Add the cpu to the mask.

CPU_CLR()
    Remove the cpu from the mask.

CPU_ISSET()
    Test to see whether the cpu is a member of the mask.

CPU_COUNT()
    Return the number of CPUs in the mask.

CPU_AND()
    Store the intersection of the srcmask1 and the srcmask2 in the dstmask. The dstmask could be one of the source masks.

CPU_OR()
    Store the union of the srcmask1 and the srcmask2 in the dstmask. The dstmask could be one of the source masks.

CPU_XOR()
    Store the XOR of the srcmask1 and the srcmask2 in the dstmask. The dstmask could be one of the source masks.

CPU_EQUAL()
    Test whether the mask1 and the mask2 contain the same CPUs.

RETURN VALUES
These system calls return -1 on error and 0 upon successful completion.

ERRORS
The sched_setaffinity() and sched_getaffinity() will fail if:

[EFAULT]
    The mask parameter is outside the process's allocated address space.

[EINVAL]
    The pid parameter is negative.

[ESRCH]
    The process identified by the pid does not exist.

[EPERM]
    The mask does not contain a valid CPU.

SEE ALSO
fork(2), lwp_create(2), lwp_getaffinity(2), lwp_setaffinity(2), rfork(2), vfork(2), pthread_create(3), pthread_getaffinity_np(3), pthread_setaffinity_np(3)

HISTORY
The sched_setaffinity() and sched_getaffinity() functions first appeared in DragonFly 4.7.