6.2. Using the sched_setaffinity() System Call to Set Processor Affinity
Este contenido no está disponible en el idioma seleccionado.
6.2. Using the sched_setaffinity() System Call to Set Processor Affinity
In addition to the taskset command, processor affinity can also be set using the sched_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 the affinity information for the current process:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>
int main(int argc, char **argv)
{
int i, online=0;
ulong ncores = sysconf(_SC_NPROCESSORS_CONF);
cpu_set_t *setp = CPU_ALLOC(ncores);
ulong setsz = CPU_ALLOC_SIZE(ncores);
CPU_ZERO_S(setsz, setp);
if (sched_getaffinity(0, setsz, setp) == -1) {
perror("sched_getaffinity(2) failed");
exit(errno);
}
for (i=0; i < CPU_COUNT_S(setsz, setp); i++) {
if (CPU_ISSET_S(i, setsz, setp))
online++;
}
printf("%d cores configured, %d cpus allowed in affinity mask\n", ncores, online);
CPU_FREE(setp);
}
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>
int main(int argc, char **argv)
{
int i, online=0;
ulong ncores = sysconf(_SC_NPROCESSORS_CONF);
cpu_set_t *setp = CPU_ALLOC(ncores);
ulong setsz = CPU_ALLOC_SIZE(ncores);
CPU_ZERO_S(setsz, setp);
if (sched_getaffinity(0, setsz, setp) == -1) {
perror("sched_getaffinity(2) failed");
exit(errno);
}
for (i=0; i < CPU_COUNT_S(setsz, setp); i++) {
if (CPU_ISSET_S(i, setsz, setp))
online++;
}
printf("%d cores configured, %d cpus allowed in affinity mask\n", ncores, online);
CPU_FREE(setp);
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Note
For more information, or for further reading, the following man page is related to the information given in this section:
Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.
Hacer que el código abierto sea más inclusivo
Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.
Acerca de Red Hat
Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.