Thread or process to bind to a specific cpu

Common macro definitions are:

1) cpu sets of initialization, it is set to the empty set

void CPU_ZERO(cpu_set_t *set);

2) cpu specified concentration was added to the cpu

void CPU_SET(int cpu, cpu_set_t *set);

3) will be deleted from the specified cpu focus

void CPU_CLR(int cpu, cpu_set_t *set);

4) whether the query cpu concentrated in Non-zero value; not, returns 0

int CPU_ISSET(int cpu, cpu_set_t *set);

 

Commonly used functions are:

#include <unistd.h>

1) number of cores that can be used queries

int sysconf(_SC_NPROCESSORS_CONF);

Returns the number of cores may be used in the system, but it will contain the value of the number of nuclei in the system is disabled, so that the current value does not represent the number of cores available in the system

2) number of cores may be used in the current system

int sysconf(_SC_NPROCESSORS_ONLN);

Truly representative of the currently available audit system

 

In addition to the above two functions similar to the two functions as follows:

#include <sys/sysinfo.h>

1) to obtain the number of available cores

int get_nprocs_conf(void);

2) Get the number of current real core available

int get_nprocs(void);

 

Binding process and cpu

#include <sched.h>

1) Set binding process with the cpu

int sched_setaffinity(pid_t pid, size_t cpusize, const cpu_set_t *mask);

2) Check the binding process with the cpu

int sched_getaffinity(pid_t pid, size_t cpusize, const cpu_set_t *mask);

 

Cpu binding thread

1) Set binding thread cpu

int pthread_setaffinity_np(pthread_t thread, size_t cpuszie, const cpu_set_t *mask);

2) Check the binding thread cpu

int pthread_getaffinity_np(pthread_t thread, size_t cpusize, const cpu_set_t *mask);

 

View the current system cpu-related information:

cat /proc/cpuinfo

Guess you like

Origin www.cnblogs.com/rohens-hbg/p/11224865.html