Linuxa process or thread is bound toCPU
In order for the program to have better performance, it is sometimes necessary to bind a process or thread to a specific CPU, which can reduce the overhead of scheduling and protect critical processes or threads.
Process is bound toCPU
Linux provides an interface that binds a process to a specific CPU:
#include <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);
Parameters
PID: the ID number of the process, if the pid is 0, indicates that the process
Cpusetsize: the size of mask
Mask: The CPU running the process can be manipulated by the following function mask
#define CPU_SET (CPU, CPUSETP)// set CPU
#define CPU_CLR (CPU, CPUSETP)// remove CPU
#define CPU_ISSET (CPU, CPUSETP)// judging CPU
#define Cpu_zero (CPUSETP)// initialized to 0
Sample code
#include <stdio.h>#include<unistd.h>#include<math.h>#include<sched.h>voidWastetime () {intABC =10000000; while(abc--) { intTMP =10000*10000; } Sleep (1);}intMainintargcChar**argv) {cpu_set_t mask; while(1) {Cpu_zero (&mask); Cpu_set (0, &mask); if(Sched_setaffinity (0,sizeof(mask), &mask) <0) {perror ("sched_setaffinity"); } wastetime (); Cpu_zero (&mask); Cpu_set (1, &mask); if(Sched_setaffinity (0,sizeof(mask), &mask) <0) {perror ("sched_setaffinity"); } wastetime (); Cpu_zero (&mask); Cpu_set (2, &mask); if(Sched_setaffinity (0,sizeof(mask), &mask) <0) {perror ("sched_setaffinity"); } wastetime (); Cpu_zero (&mask); Cpu_set (3, &mask); if(Sched_setaffinity (0,sizeof(mask), &mask) <0) {perror ("sched_setaffinity"); } wastetime (); }}
Test
After compiling, run the program, enter the command top-p process ID, enter f, enter J, enter a return, you can see the process in Switch between cpu0123.
Thread is bound toCPU
Not only can processes be bound to CPUs, but threads can. Linux provides an interface that can bind threads to specific CPUs:
#include <pthread.h>
int pthread_setaffinity_np (pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset);
int pthread_getaffinity_np (pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset);
This interface is basically consistent with the way the process binds to the CPU 's interface.
When a process is bound to a specific CPU , the thread can still bind to the other CPU without a conflict.
Sample code
#include <stdio.h>#include<math.h>#include<pthread.h>#include<unistd.h>#include<sched.h>voidWastetime () {intABC =10000000; while(abc--) { intTMP =10000*10000; } Sleep (1);}void*thread_func (void*param) {cpu_set_t mask; while(1) {Cpu_zero (&mask); Cpu_set (1, &mask); if(PTHREAD_SETAFFINITY_NP (Pthread_self (),sizeof(mask),&mask) <0) {perror ("pthread_setaffinity_np"); } wastetime (); Cpu_zero (&mask); Cpu_set (2, &mask); if(PTHREAD_SETAFFINITY_NP (Pthread_self (),sizeof(mask),&mask) <0) {perror ("pthread_setaffinity_np"); } wastetime (); }} void*THREAD_FUNC1 (void*param) {cpu_set_t mask; while(1) {Cpu_zero (&mask); Cpu_set (3, &mask); if(PTHREAD_SETAFFINITY_NP (Pthread_self (),sizeof(mask),&mask) <0) {perror ("pthread_setaffinity_np"); } wastetime (); Cpu_zero (&mask); Cpu_set (4, &mask); if(PTHREAD_SETAFFINITY_NP (Pthread_self (),sizeof(mask),&mask) <0) {perror ("pthread_setaffinity_np"); } wastetime (); }} intMainintargcChar*argv[]) {cpu_set_t mask; Cpu_zero (&mask); Cpu_set (0, &mask); if(Sched_setaffinity (0,sizeof(mask), &mask) <0) {perror ("sched_setaffinity"); } pthread_t My_thread; if(Pthread_create (&my_thread, NULL, THREAD_FUNC, NULL)!=0) {perror ("pthread_create"); } if(Pthread_create (&my_thread, NULL, THREAD_FUNC1, NULL)!=0) {perror ("pthread_create"); } while(1) {wastetime ();} Pthread_exit (NULL);}
Test
After the compilation runs, enter the command top-p process ID, enter f, enter J, enter H , you can see that the main thread has remained cpu0 , a thread in Cpu12 before switching, the other thread cpu34 switch between them.
Linux processes or threads bound to CPUs