Skip to content

Instantly share code, notes, and snippets.

@ncode
Forked from gleicon/check.c
Created June 29, 2011 04:17
Show Gist options
  • Save ncode/1053103 to your computer and use it in GitHub Desktop.
Save ncode/1053103 to your computer and use it in GitHub Desktop.
file check w/ threads
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#define NUM_THREADS 5
#define MAXPATHSIZE 1024
#define MAXFILENAMESIZE MAXPATHSIZE + 32
//gcc check.c -o check -lpthread -lrt
struct _filedata {
long tid;
char path[MAXPATHSIZE];
};
struct timespec timediff(struct timespec start, struct timespec end){
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
void *create_remove_file(void *_fdata) {
int temp;
FILE *fp;
unsigned char fname[MAXFILENAMESIZE];
unsigned char buf[256];
struct timespec time1, time2, result, fhash;
struct _filedata *fd = (struct _filedata *) _fdata;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &fhash);
sprintf(fname, "%s/%ld-%ld", fd->path, fhash.tv_nsec, fd->tid);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
fp = fopen(fname, "wb+");
fprintf(fp, "tstamp + thread id #%ld!\n", fd->tid);
fclose(fp);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
result = timediff(time1,time2);
fprintf(stdout, "\tFile: %s Creation: seconds %ld, nanoseconds %ld\n", fname, result.tv_sec, result.tv_nsec);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
fp = fopen(fname, "rb+");
fread(buf, sizeof(buf), 1, fp);
fclose(fp);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
result = timediff(time1,time2);
fprintf(stdout, "\tFile: %s Reading: seconds %ld, nanoseconds %ld\n", fname, result.tv_sec, result.tv_nsec);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
unlink(fname);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
result = timediff(time1,time2);
fprintf(stdout, "\tFile: %s Deletion: seconds %ld, nanoseconds %ld\n", fname, result.tv_sec, result.tv_nsec);
pthread_exit(NULL);
}
int _dir_exists(char *path) {
struct stat st;
int i = 0;
fprintf(stderr, "\tchecking: %s", path);
i = stat(path, &st);
if (i == -1) {
fprintf(stderr, " dont exist (%d)\n", i);
} else {
fprintf(stderr, " exists (%d)", i);
}
return i;
}
int main (int argc, char *argv[]) {
long t = 0;
char **mp = argv;
char *c = NULL;
if (argc < 2) {
fprintf(stderr, "usage %s <path> <path> .... <path>\n", argv[0]);
exit(-1);
}
mp++;
fprintf(stderr, "Check enabled on:\n");
for (c; (c = *(mp)) != NULL; mp++) {
if (_dir_exists(c) > -1) {
pthread_t pt;
int r;
struct _filedata fd;
fd.tid = t;
strncpy(fd.path, c, strlen(c)+1);
r = pthread_create(&pt, NULL, create_remove_file, (void *) &fd);
fprintf(stderr, " thread created\n");
if (r) {
fprintf(stderr, "ERROR: return code from pthread_create() is %d\n", r);
exit(-1);
}
pthread_join(pt, NULL);
t++;
}
}
pthread_exit(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment