/** Test Case: OpenDir, Get status and close it. */ #include #include #include #include #include #include #include #include #define TEST_DIR "/tmp/thread" #define MAX_TEST_THREAD (80) #define MAX_TEST_FILE 5000 static unsigned long *result[MAX_TEST_THREAD]; static int stop = 0; static void* case_function(void *para) { int id = (int)(long)para; DIR *pDir; struct stat f_stat; struct dirent *entry=NULL; char path[256]; char cmd[512]; int filecnt = 0; int dircnt = 0; int filetotalsize = 0; unsigned long myresult = 0; int f = 0; result[id] = &myresult; /* Goto my path and construct empty file */ sprintf(path, "%s/%d", TEST_DIR, id); printf("Creating temp file at %s....\n", path); sprintf(cmd, "mkdir %s", path); system(cmd); chdir(path); for (f = 0; f < MAX_TEST_FILE; f++) { char name[256]; sprintf(name, "%s/%d", path, f); int t = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); if (t != -1) close(t); else { printf("Errno = %d.\n", errno); exit(errno); } } again: if ((pDir = opendir(path)) == NULL) { printf("打开 %s 错误:没有那个文件或目录\n", TEST_DIR); goto err; } while ((entry = readdir(pDir)) != NULL) { struct stat buf; if (entry->d_name[0] == '.') continue; //f = open(entry->d_name, 0); f = stat(entry->d_name, &buf); if (f) close(f); myresult++; //printf("Filename %s, size %10d",entry->d_name, f_stat.st_size); } closedir(pDir); /* Need to stop */ if (!stop) goto again; return 0; err: ; } void main() { int i; pthread_t thread; system("mkdir "TEST_DIR); for (i = 0; i < MAX_TEST_THREAD; i++) { pthread_create(&thread, NULL, case_function, (void*)(long)i); } while (1) { sleep(1); printf("Statistics:\n"); for (i = 0; i < MAX_TEST_THREAD; i++) { printf("%d\t", *result[i]); } printf("\n"); for (i = 0; i < MAX_TEST_THREAD; i++) *result[i] = 0; } }