/* Test the idea of whether a bunch of threads continuously calling * pthread_yield() actually raises the system load or not. */ /* Oh, yes, it certainly does. */ #include #include #include #include #define __USE_GNU void *only_yield(void *arg) { for (;;) pthread_yield(); } int main(int argc, char **argv) { int i, threads; pthread_t tids[256]; double avg[3]; if (argc == 2) threads = atoi(argv[1]); else threads = 5; if (getloadavg(avg, 3) < 0) { fprintf(stderr, "Can't get load average?\n"); exit(1); } printf("Initial load averages for %d threads are %.2lf %.2lf %.2lf\n", threads, avg[0], avg[1], avg[2]); for (i = 0; i < threads; i++) { if (pthread_create(&tids[i], NULL, only_yield, NULL)) { fprintf(stderr, "Failed to create thread number %d\n", i + 1); exit(1); } } for (i = 0; i < 60; i++) { sleep(15); if (getloadavg(avg, 3) < 0) { fprintf(stderr, "Can't get load average?\n"); exit(1); } printf("After %.2f minute%s, for %d threads, the load averages are %.2lf %.2lf %.2lf\n", (i + 1) / 4.0, i != 3 ? "s" : "", threads, avg[0], avg[1], avg[2]); } exit(0); }