#include #include #include #include #include #include pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; void logit(const char *fmt, ...) { va_list args; char buf[1024]; int bytes; buf[sizeof(buf)-1] = '\0'; va_start(args, fmt); bytes = vsnprintf(buf, sizeof(buf)-1, fmt, args); va_end(args); pthread_mutex_lock(&log_mutex); printf("%d:", pthread_self()); printf("%s", buf); pthread_mutex_unlock(&log_mutex); } struct TestIoInfo { int Iterations; int Progress; }; void * TestIoThread (void *arg) { struct TestIoInfo *t = (struct TestIoInfo *)arg; int i, j; int fd, fdd; char FileName[255]; FILE *f; logit("IO Thread %d starting...\n", pthread_self()); snprintf(FileName, sizeof(FileName), "/tmp/TestIoThread-%d-%x", getpid(), pthread_self()); sleep(1); for (j=0; jIterations; ++j) { if ((fd = open(FileName, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0) { logit("Error Opening File: %s - %d\n", FileName, errno); return; } fdd = dup(fd); if ((f = fdopen(fdd, "rb")) == NULL) { logit("Can't open descriptor %d - %d\n", fd, errno); return; } fclose(f); close(fd); if (0 == (j%t->Progress)) { logit("IO Thread %d - %d\n", pthread_self(), j); } } unlink(FileName); logit("IO Thread %d done.\n", pthread_self()); return NULL; } main (int argc, char ** argv) { int threadcount = 4; int progress = 1; pthread_t tid[10]; int i; struct TestIoInfo IoInfo; logit("Testing with %d concurrent threads\n", threadcount); logit("Progress indicated every %d operations...\n", progress); IoInfo.Iterations = 200; IoInfo.Progress = progress; for (i=0; i