/* gcc -W -Wall -o hog hog.c */ #include #include #include #include int main(void) { int pages, pagesize, i; unsigned char *mem; struct timeval tv; pages = sysconf(_SC_PHYS_PAGES); if (pages < 0) { perror("_SC_PHYS_PAGES"); return EXIT_FAILURE; } pages = (3 * pages) / 4; pagesize = sysconf(_SC_PAGESIZE); if (pagesize < 0) { perror("_SC_PAGESIZE"); return EXIT_FAILURE; } mem = malloc(pages * pagesize); if (!mem) { fprintf(stderr, "out of memory\n"); return EXIT_FAILURE; } for (i = 0; i < pages; i++) mem[i * pagesize] = 0; gettimeofday(&tv, NULL); srand((unsigned int)tv.tv_sec); while (1) { struct timeval start; getchar(); gettimeofday(&start, NULL); for (i = 0; i < 2 * pages; i++) mem[(rand() / (RAND_MAX / pages + 1)) * pagesize] = 0; gettimeofday(&tv, NULL); timersub(&tv, &start, &tv); printf("%lu.%lu\n", tv.tv_sec, tv.tv_usec); } return EXIT_SUCCESS; }