Afinidad de CPU


Existe un método progamático para establecer la afinidad de CPU para un proceso en c/c++ para el sistema operativo linux.

Author: Alnitak, 2008-11-11

4 answers

Necesita usar sched_setaffinity(2).

Por ejemplo, para ejecutar solo en CPU 0 y 2:

#define _GNU_SOURCE
#include <sched.h>

cpu_set_t  mask;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
CPU_SET(2, &mask);
result = sched_setaffinity(0, sizeof(mask), &mask);

(0 para el primer parámetro significa el proceso actual, proporcione un PID si es algún otro proceso que desea controlar).

Véase también sched_getcpu(3).

 43
Author: Alnitak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-03 11:55:07

Use sched_setaffinity a nivel de proceso, o pthread_attr_setaffinity_np para subprocesos individuales.

 8
Author: puetzk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-11 13:57:05

En breve

unsigned long mask = 7; /* processors 0, 1, and 2 */
unsigned int len = sizeof(mask);
if (sched_setaffinity(0, len, &mask) < 0) {
    perror("sched_setaffinity");
}

Busque en CPU Affinity para más detalles

 2
Author: thAAAnos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-11 14:20:56

He hecho muchos esfuerzos para darme cuenta de lo que está sucediendo, así que añado esta respuesta para ayudar a personas como yo (uso gcc compilador en linux mint)

#include <sched.h> 
cpu_set_t  mask;

inline void assignToThisCore(int core_id)
{
    CPU_ZERO(&mask);
    CPU_SET(core_id, &mask);
    sched_setaffinity(0, sizeof(mask), &mask);
}
int main(){
    //cal this:
    assignToThisCore(2);//assign to core 0,1,2,...

    return 0;
}

Pero no olvide agregar estas opciones al comando del compilador: -D _GNU_SOURCE Debido a que el sistema operativo puede asignar un proceso al núcleo en particular, puede agregar este GRUB_CMDLINE_LINUX_DEFAULT="quiet splash isolcpus=2,3" al archivo grub ubicado en /etc/default y ejecutar sudo update-grub en terminal para reservar los núcleos que desea

 0
Author: Martin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-08 08:49:39