Skip to content

Instantly share code, notes, and snippets.

@gleicon
Created June 28, 2011 18:52
Show Gist options
  • Save gleicon/1051883 to your computer and use it in GitHub Desktop.
Save gleicon/1051883 to your computer and use it in GitHub Desktop.
file check w/ threads
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#define NUM_THREADS 5
#define MAXPATHSIZE 1024
#define MAXFILENAMESIZE MAXPATHSIZE + 32
struct _filedata {
long tid;
char path[MAXPATHSIZE];
};
void *create_remove_file(void *_fdata) {
FILE *fp;
unsigned char fname[MAXFILENAMESIZE];
unsigned char buf[256];
struct _filedata *fd = (struct _filedata *) _fdata;
sprintf(fname, "%s/%s", fd->path, "temp_fname");
fp = fopen(fname, "wb+");
fprintf(fp, "tstamp + thread id #%ld!\n", fd->tid);
fclose(fp);
fp = fopen(fname, "rb+");
fread(buf, sizeof(buf), 1, fp);
fclose(fp);
unlink(fname);
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;
strcmp(fd.path, c);
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);
}
t++;
}
}
pthread_exit(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment