Under linux the process / thread is bound to a specific cpu core run

https://blog.csdn.net/guotianqing/article/details/80958281

 

http://www.xiaoguo123.com/p/linux-taskset/

 

 taskset -cp 0,1 2181 - bound to cpu0 and cpu1

 

 

Linux platform can process is bound to a CPU, load balancing.
Taskset can be achieved through system commands can also be invoked coding system interface functions to achieve (their own and other processes can be specified) by.

1. The system command is implemented (the taskset)

1) Check the number of cpu core
cat / proc / cpuinfo | grep " physical id" | sort | uniq | wc -l

2) Check the binding process in which cpu
taskset -cp process PID

3) the binding process to the CPU
taskset -cp the CPU core number process PID 4) again, the process has been bound to the 2:
Binding process to CPU

Process in which CPU View

2. C ++ code to achieve

1) to obtain the number of CPU cores

#include
// get the total number of cpu core, the index starting from 0
int getCpuCoreCount ()
{
return the sysconf (_SC_NPROCESSORS_CONF);
}

2) the process is bound to CPU

#include
// the processes assigned to a cpu (from zero, is less than the number of cpu core -1)
// successful than 0
int cpuSetByPID (PID int, unsigned int cpu)
{
cpu_set_t mask;
CPU_ZERO (& mask); / / empty set (mask)
CPU_SET (cpu, & mask); // number added to the collection of the cpu
@ pid run on the mask set the CPU
int RET = sched_setaffinity (pid, the sizeof (mask), mask &);
the printf ( "sched_setaffinity (% D,% D)% RET = D \ n-", PID <= 0 getpid (): PID, CPU, RET?);
// successful return value is 0
return RET;
}

Guess you like

Origin blog.csdn.net/llxx1234/article/details/94741583