按比例吃CPU
前几天测试软件在多核上的性能,需要按照比例吃各个CPU,查了查资料,撸了下面一小段代码; 1 #include <unistd.h> 2 #include <stdlib.h> 3 #include <stdio.h> 4 #include <time.h> 5 6 #define __USE_GNU 7 #include <pthread.h> 8 #include <sched.h> 9 10 //CPU ID号 11 #define CPU_0 0x0 12 #define CPU_1 0x01 13 #define CPU_2 0x02 14 #define CPU_3 0x03 15 16 //总时间和运行时间 17 #define FULL_TIME 100 18 #define RUN_TIME 80 19 20 //时钟HZ数 21 static clock_t clktck; 22 23 //用户参数输入的吃CPU百分比 24 static int eat_cpu_percent; 25 26 //线程绑定CPU 27 int attach_cpu(int cpu_index) 28 { 29 int cpu_num = sysconf(_SC_NPROCESSORS_CONF); 30 if (cpu_index < 0 || cpu_index >= cpu