#include #include #include #include #include #include #include int run_test(int count) { int fd; FILE* f; char buf[1024]; char record[1024 * 128]; size_t len; int running = -1; int running1 = -1; char* pos; char* spos; int result = -1; fd = open("/proc/stat", O_RDONLY); if (fd < 0) { perror("Can't open /proc/stat????\n"); exit(1); } len = read(fd, record, 1024 * 128); pos = strstr(record, "procs_running "); if (pos == NULL) { printf("procs_running not found in block read\n"); close(fd); return -1; } if (sscanf(pos, "procs_running %u", &running) != 1) { printf("sscanf failed on block read '%s'\n", pos); return -1; } lseek(fd, 0, SEEK_SET); f = fdopen(fd, "r"); while (fgets(buf, 1024, f) != NULL) { if ((spos = strstr(buf, "procs_running")) != NULL) { if (sscanf(spos, "procs_running %u", &running1) != 1) { printf("Found procs_running but sscanf failed\n\t'%s'\n", buf); result = -1; break; } result = 1; if (running != running1) { printf("%d: results unequal %d != %d\n", count, running, running1); result = 0; } break; } } fclose(f); close(fd); return result; } main(int argc, char* argv[]) { int count = 0; int failures = 0; int goods = 0; int uneqs = 0; struct timespec delay; printf("udef_test start...\n"); delay.tv_sec = 0; delay.tv_nsec = 10000000l; while (count < 10000) { int i = run_test(count); if (i == 0) uneqs++; else if (i < 0) failures++; else goods++; nanosleep(&delay, 0); count ++; } printf("failures=%d goods=%d uneqs=%d\n", failures, goods, uneqs); printf("... udev_test end\n"); }