#include #include #include #include long count1; long count2; volatile int stop; void *thr_routine(void *arg) { while (!stop) { (*(long *)arg)++; pthread_yield(); } return (0); } void alarm_handler(int sig) { stop = 1; } #define TIMEOUT 20 #define SIZE 100 pthread_t tdarray[SIZE]; long count[SIZE]; int main(int argc, char *argv[]) { pthread_attr_t attr; long i; long total; printf("testing scope process context switch speed...\n"); pthread_setconcurrency(1); signal(SIGALRM, alarm_handler); pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS); for (i = 0; i < SIZE; ++i) { pthread_create(&tdarray[i], &attr, thr_routine, &count[i]); } alarm(TIMEOUT); for (i = 0; i < SIZE; ++i) { pthread_join(tdarray[i], NULL); } total = 0; for (i = 0; i < SIZE; ++i) { printf("%ld,", count[i]); total += count[i]; } printf("\ntotal: %ld, context switches per thread:%ld/s\n", total, total/SIZE/TIMEOUT); #if 0 printf("testing scope system context switch speed...\n"); for (i = 0; i < SIZE; ++i) count[i] = 0; stop = 0; pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); for (i = 0; i < SIZE; ++i) { pthread_create(&tdarray[i], &attr, thr_routine, &count[i]); } alarm(TIMEOUT); for (i = 0; i < SIZE; ++i) { pthread_join(tdarray[i], NULL); } total = 0; for (i = 0; i < SIZE; ++i) total += count[i]; printf("context switches:%ld/s\n", total/SIZE/TIMEOUT); #endif return (0); }