为了让程序拥有更好的性能,有时候需要将进程或线程绑定到特定的CPU,这样可以减少调度的开销和保护关键进程或线程。进程绑定到CPULinux提供一个接口,可以将进程绑定到特定的CPU:#includeint 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);参数pid:进程的id号,如果pid为0,则表示本进程cpusetsize:mask的大小mask:运行进程的CPU,可以通过以下函数操作mask#define CPU_SET(cpu, cpusetp) //设置cpu#define CPU_CLR(cpu, cpusetp) //删除cpu#define CPU_ISSET(cpu, cpusetp) //判断cpu#define CPU_ZERO(cpusetp) //初始化为0示例代码 #include#include#include#include void WasteTime(){ int abc = 10000000; while(abc--) { int tmp = 10000*10000; } sleep(1);}int main(int argc, char **argv){ cpu_set_t mask; while(1) { CPU_ZERO(&mask); CPU_SET(0, &mask); if (sched_setaffinity(0, sizeof(mask), &mask) perror("sched_setaffinity"); } WasteTime(); CPU_ZERO(&mask); CPU_SET(1, &mask); if (sched_setaffinity(0, sizeof(mask), &mask) perror("sched_setaffinity"); } WasteTime(); CPU_ZERO(&mask); CPU_SET(2, &mask); if (sched_setaffinity(0, sizeof(mask), &mask) perror("sched_setaffinity"); } WasteTime(); CPU_ZERO(&mask); CPU_SET(3, &mask); if (sched_setaffinity(0, sizeof(mask), &mask) perror("sched_setaffinity"); } WasteTime(); }} 测试编译之后运行程序,输入命令top -p 进程id,输入f,输入j,输入回车,可以看到进程在cpu0123之间不停切换。线程绑定到CPU不仅仅进程可以绑定到CPU,线程也可以。Linux提供一个接口,可以将线程绑定到特定的CPU:#includeint 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);该接口与进程绑定到CPU的接口的使用方法基本一致。当进程绑定到特定的CPU之后,线程还是可以绑定到其他的CPU的,没有冲突。示例代码 #include#include#include#include#includevoid WasteTime(){ int abc = 10000000; while(abc--) { int tmp = 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) perror("pthread_setaffinity_np"); } WasteTime(); CPU_ZERO(&mask); CPU_SET(2, &mask); if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) 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) perror("pthread_setaffinity_np"); } WasteTime(); CPU_ZERO(&mask); CPU_SET(4, &mask); if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) perror("pthread_setaffinity_np"); } WasteTime(); }} int main(int argc, char *argv[]){ cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0, &mask); if (sched_setaffinity(0, sizeof(mask), &mask) 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);} 测试编译运行之后,输入命令top -p 进程id,输入f,输入j,输入回车,输入H,可以看到主线程一直保持在cpu0,一个线程在cpu12之前切换,另一个线程在cpu34之间切换。 09-30 11:53