1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* This method will create threads, then bind each to its own cpu. */ bool do_cpu_stress(int numthreads) { int ret = TRUE; int created_thread = 0; /* We need a thread for each cpu we have... */ while ( created_thread < numthreads - 1 ) { int mypid = fork(); if (mypid == 0) /* Child process */ { printf("\tCreating Child Thread: #%i\n", created_thread); break; } else /* Only parent executes this */ { /* Continue looping until we spawned enough threads! */ ; created_thread++; } } /* NOTE: All threads execute code from here down! */ |
1 2 3 4 5 6 7 8 9 10 | cpu_set_t mask; /* CPU_ZERO initializes all the bits in the mask to zero. */ CPU_ZERO( &mask ); /* CPU_SET sets only the bit corresponding to cpu. */ CPU_SET( created_thread, &mask ); /* sched_setaffinity returns 0 in success */ if( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 ) { printf("WARNING: Could not set CPU Affinity, continuing...\n"); } |
1 2 3 4 5 6 7 8 | void CPU_ZERO (cpu_set_t *set) 这个宏对 CPU 集 set 进行初始化,将其设置为空集。 void CPU_SET (int cpu, cpu_set_t *set) 这个宏将 cpu 加入 CPU 集 set 中。 void CPU_CLR (int cpu, cpu_set_t *set) 这个宏将 cpu 从 CPU 集 set 中删除。 int CPU_ISSET (int cpu, const cpu_set_t *set) 如果 cpu 是 CPU 集 set 的一员,这个宏就返回一个非零值(true),否则就返回零(false)。 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* Now we have a single thread bound to each cpu on the system */ int computation_res = do_cpu_expensive_op(41); cpu_set_t mycpuid; sched_getaffinity(0, sizeof(mycpuid), &mycpuid); if ( check_cpu_expensive_op(computation_res) ) { printf("SUCCESS: Thread completed, and PASSED integrity check!\n", mycpuid); ret = TRUE; } else { printf("FAILURE: Thread failed integrity check!\n", mycpuid); ret = FALSE; } return ret; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |