Please find the sample program below for the cpu proximity of a specific pthread.
Add the appropriate libraries.
double waste_time(long n) { double res = 0; long i = 0; while (i <n * 200000) { i++; res += sqrt(i); } return res; } void *thread_func(void *param) { unsigned long mask = 1; if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) <0) { perror("pthread_setaffinity_np"); } printf("result: %f\n", waste_time(2000)); mask = 2; if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) <0) { perror("pthread_setaffinity_np"); } printf("result: %f\n", waste_time(2000)); } int main(int argc, char *argv[]) { pthread_t my_thread; if (pthread_create(&my_thread, NULL, thread_func, NULL) != 0) { perror("pthread_create"); } pthread_exit(NULL); }
Compile the above program with the flag -D_GNU_SOURCE.
YugiReddy Apr 26 '12 at 22:25 2012-04-26 22:25
source share