zeus
4 years ago
7 changed files with 337 additions and 0 deletions
@ -0,0 +1,4 @@ |
|||||
|
all: |
||||
|
gcc -pthread ./getfiles.c -o getfiles-simple ; |
||||
|
gcc -pthread ./getfiles-size.c -o getfiles-size ; |
||||
|
gcc -pthread ./longest_path.c -o get-longest-path ; |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,110 @@ |
|||||
|
|
||||
|
#include <dirent.h> |
||||
|
#include <stdio.h> |
||||
|
|
||||
|
#include<string.h> |
||||
|
#include<pthread.h> |
||||
|
#include<stdlib.h> |
||||
|
#include<unistd.h> |
||||
|
#include <sys/stat.h> |
||||
|
|
||||
|
int NTHREADS; |
||||
|
int counter; |
||||
|
unsigned long long size=0; |
||||
|
void *listFilesRecursively(void* path); |
||||
|
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
||||
|
|
||||
|
int main(int argc,char** argv) { |
||||
|
DIR *d; |
||||
|
struct dirent *dir; |
||||
|
char* path=argv[1]; |
||||
|
/*
|
||||
|
d = opendir("node_modules"); |
||||
|
if (d) { |
||||
|
while ((dir = readdir(d)) != NULL) { |
||||
|
if (dir->d_type == DT_DIR) { |
||||
|
printf("D %s\n", dir->d_name); |
||||
|
}else if (dir->d_type == DT_REG) { |
||||
|
printf("F %s\n", dir->d_name); |
||||
|
} |
||||
|
} |
||||
|
closedir(d); |
||||
|
} |
||||
|
*/ |
||||
|
listFilesRecursively(path); |
||||
|
printf("count %d\n", counter); |
||||
|
|
||||
|
printf("size %lld\n", size); |
||||
|
return(0); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
int walk_dir(char *dname, char *pattern, int spec) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/**
|
||||
|
* Lists all files and sub-directories recursively |
||||
|
* considering path as base path. |
||||
|
*/ |
||||
|
void *listFilesRecursively(void *basePath_pass) |
||||
|
{ |
||||
|
char* basePath=(char*) basePath_pass; |
||||
|
char path[1000]; |
||||
|
struct dirent *dp; |
||||
|
DIR *dir = opendir(basePath); |
||||
|
|
||||
|
pthread_t tid[2]; |
||||
|
|
||||
|
int i = 0; |
||||
|
int err; |
||||
|
pthread_t* thread_id=(pthread_t*)malloc(1); |
||||
|
int ret1,ret2; |
||||
|
|
||||
|
NTHREADS = 0; |
||||
|
|
||||
|
// Unable to open directory stream
|
||||
|
if (!dir) |
||||
|
return(0); |
||||
|
|
||||
|
while ((dp = readdir(dir)) != NULL) |
||||
|
{ |
||||
|
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) |
||||
|
{ |
||||
|
//printf("%s\n", dp->d_name);
|
||||
|
if (dp->d_type == DT_DIR) { |
||||
|
printf("D %s\n", dp->d_name); |
||||
|
// Construct new path from our base path
|
||||
|
strcpy(path, basePath); |
||||
|
strncat(path, "/",1); |
||||
|
strcat(path, dp->d_name); |
||||
|
|
||||
|
thread_id=(pthread_t*) realloc(thread_id,(NTHREADS+1)*sizeof(pthread_t)); |
||||
|
err = pthread_create(&thread_id[NTHREADS], NULL, listFilesRecursively, path ); |
||||
|
if (err != 0) |
||||
|
printf("\ncan't create thread :[%s]", strerror(err)); |
||||
|
else |
||||
|
printf("\n Thread created successfully\n"); |
||||
|
pthread_join( thread_id[NTHREADS], NULL); |
||||
|
NTHREADS++; |
||||
|
// listFilesRecursively(path);
|
||||
|
}else if (dp->d_type == DT_REG) { |
||||
|
printf("F %s\n", dp->d_name); |
||||
|
struct stat st; |
||||
|
stat(basePath, &st); |
||||
|
size += st.st_size; |
||||
|
counter++; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
/*
|
||||
|
for(int j=0; j < NTHREADS; j++) |
||||
|
{ |
||||
|
pthread_join( thread_id[j], NULL); |
||||
|
} |
||||
|
*/ |
||||
|
closedir(dir); |
||||
|
//printf("count %d\n", counter);
|
||||
|
} |
||||
|
|
@ -0,0 +1,103 @@ |
|||||
|
|
||||
|
#include <dirent.h> |
||||
|
#include <stdio.h> |
||||
|
|
||||
|
#include<string.h> |
||||
|
#include<pthread.h> |
||||
|
#include<stdlib.h> |
||||
|
#include<unistd.h> |
||||
|
|
||||
|
int NTHREADS; |
||||
|
int counter; |
||||
|
void *listFilesRecursively(void* path); |
||||
|
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
||||
|
|
||||
|
int main(int argc,char** argv) { |
||||
|
DIR *d; |
||||
|
struct dirent *dir; |
||||
|
char* path=argv[1]; |
||||
|
/*
|
||||
|
d = opendir("node_modules"); |
||||
|
if (d) { |
||||
|
while ((dir = readdir(d)) != NULL) { |
||||
|
if (dir->d_type == DT_DIR) { |
||||
|
printf("D %s\n", dir->d_name); |
||||
|
}else if (dir->d_type == DT_REG) { |
||||
|
printf("F %s\n", dir->d_name); |
||||
|
} |
||||
|
} |
||||
|
closedir(d); |
||||
|
} |
||||
|
*/ |
||||
|
listFilesRecursively(path); |
||||
|
printf("count %d\n", counter); |
||||
|
return(0); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
int walk_dir(char *dname, char *pattern, int spec) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/**
|
||||
|
* Lists all files and sub-directories recursively |
||||
|
* considering path as base path. |
||||
|
*/ |
||||
|
void *listFilesRecursively(void *basePath_pass) |
||||
|
{ |
||||
|
char* basePath=(char*) basePath_pass; |
||||
|
char path[1000]; |
||||
|
struct dirent *dp; |
||||
|
DIR *dir = opendir(basePath); |
||||
|
|
||||
|
pthread_t tid[2]; |
||||
|
|
||||
|
int i = 0; |
||||
|
int err; |
||||
|
pthread_t* thread_id=(pthread_t*)malloc(1); |
||||
|
int ret1,ret2; |
||||
|
|
||||
|
NTHREADS = 0; |
||||
|
|
||||
|
// Unable to open directory stream
|
||||
|
if (!dir) |
||||
|
return(0); |
||||
|
|
||||
|
while ((dp = readdir(dir)) != NULL) |
||||
|
{ |
||||
|
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) |
||||
|
{ |
||||
|
//printf("%s\n", dp->d_name);
|
||||
|
if (dp->d_type == DT_DIR) { |
||||
|
printf("D %s\n", dp->d_name); |
||||
|
// Construct new path from our base path
|
||||
|
strcpy(path, basePath); |
||||
|
strncat(path, "/",1); |
||||
|
strcat(path, dp->d_name); |
||||
|
|
||||
|
thread_id=(pthread_t*) realloc(thread_id,(NTHREADS+1)*sizeof(pthread_t)); |
||||
|
err = pthread_create(&thread_id[NTHREADS], NULL, listFilesRecursively, path ); |
||||
|
if (err != 0) |
||||
|
printf("\ncan't create thread :[%s]", strerror(err)); |
||||
|
else |
||||
|
printf("\n Thread created successfully\n"); |
||||
|
pthread_join( thread_id[NTHREADS], NULL); |
||||
|
NTHREADS++; |
||||
|
// listFilesRecursively(path);
|
||||
|
}else if (dp->d_type == DT_REG) { |
||||
|
printf("F %s\n", dp->d_name); |
||||
|
counter++; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
/*
|
||||
|
for(int j=0; j < NTHREADS; j++) |
||||
|
{ |
||||
|
pthread_join( thread_id[j], NULL); |
||||
|
} |
||||
|
*/ |
||||
|
closedir(dir); |
||||
|
//printf("count %d\n", counter);
|
||||
|
} |
||||
|
|
@ -0,0 +1,120 @@ |
|||||
|
|
||||
|
#include <dirent.h> |
||||
|
#include <stdio.h> |
||||
|
|
||||
|
#include<string.h> |
||||
|
#include<pthread.h> |
||||
|
#include<stdlib.h> |
||||
|
#include<unistd.h> |
||||
|
#include <sys/stat.h> |
||||
|
#define PATH_MAX 4096 |
||||
|
|
||||
|
int NTHREADS; |
||||
|
int counter; |
||||
|
//ftiaxnoume metavliti gia to megalytero path me megethos oso to max path size poy sto linux einai 4096
|
||||
|
char longest_path[PATH_MAX+1]; |
||||
|
//ftiaxnoume metavliti gia na apothikeuei to megethos tona arxeion
|
||||
|
unsigned long long size=0; |
||||
|
void *listFilesRecursively(void* path); |
||||
|
//kanoume initialize to mutex gia na kleidosoume tous workers
|
||||
|
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
||||
|
|
||||
|
int main(int argc,char** argv) { |
||||
|
DIR *d; |
||||
|
struct dirent *dir; |
||||
|
//diavazoume to arxeio san parametro
|
||||
|
//prosoxh einai sth thesi 1 kai oxi 0
|
||||
|
char* path=argv[1]; |
||||
|
listFilesRecursively(path); |
||||
|
printf("count %d\n", counter); |
||||
|
|
||||
|
printf("size %lld\n", size); |
||||
|
|
||||
|
printf("longest path is %s\n",longest_path); |
||||
|
return(0); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
int walk_dir(char *dname, char *pattern, int spec) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/**
|
||||
|
* Lists all files and sub-directories recursively |
||||
|
* considering path as base path. |
||||
|
*/ |
||||
|
void *listFilesRecursively(void *basePath_pass) |
||||
|
{ |
||||
|
char* basePath=(char*) basePath_pass; |
||||
|
char path[1000]; |
||||
|
struct dirent *dp; |
||||
|
DIR *dir = opendir(basePath); |
||||
|
|
||||
|
pthread_t tid[2]; |
||||
|
|
||||
|
int i = 0; |
||||
|
int err; |
||||
|
pthread_t* thread_id=(pthread_t*)malloc(1); |
||||
|
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
||||
|
int ret1,ret2; |
||||
|
|
||||
|
NTHREADS = 0; |
||||
|
|
||||
|
// Unable to open directory stream
|
||||
|
if (!dir) |
||||
|
return(0); |
||||
|
|
||||
|
//diavazoume oso yparxoyn katalogoi
|
||||
|
while ((dp = readdir(dir)) != NULL) |
||||
|
{ |
||||
|
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) |
||||
|
{ |
||||
|
if (dp->d_type == DT_DIR) { |
||||
|
printf("D %s\n", dp->d_name); |
||||
|
// Construct new path from our base path
|
||||
|
printf("path %s\n",path); |
||||
|
strcpy(path, basePath); |
||||
|
printf("path %s\n",path); |
||||
|
strncat(path, "/",1); |
||||
|
printf("path %s\n",path); |
||||
|
strcat(path, dp->d_name); |
||||
|
printf("path %s\n",path); |
||||
|
|
||||
|
|
||||
|
printf("path %s\n",path); |
||||
|
printf("base path %s\n",basePath); |
||||
|
|
||||
|
thread_id=(pthread_t*) realloc(thread_id,(NTHREADS+1)*sizeof(pthread_t)); |
||||
|
err = pthread_create(&thread_id[NTHREADS], NULL, listFilesRecursively, path ); |
||||
|
if (err != 0) |
||||
|
printf("\ncan't create thread :[%s]", strerror(err)); |
||||
|
else |
||||
|
printf("\n Thread created successfully\n"); |
||||
|
pthread_join( thread_id[NTHREADS], NULL); |
||||
|
NTHREADS++; |
||||
|
}else if (dp->d_type == DT_REG) { |
||||
|
printf("F %s\n", dp->d_name); |
||||
|
//kaloume stat gia na paroyme to size
|
||||
|
struct stat st; |
||||
|
stat(basePath, &st); |
||||
|
//prosthtoume to size sthn global metavliti
|
||||
|
size += st.st_size; |
||||
|
|
||||
|
//kleidonoyme prin tin sygrisi gia na apofygoyme lathi
|
||||
|
pthread_mutex_lock( &mutex1 ); |
||||
|
//sygrinoyme to current path me to longest path
|
||||
|
//kai an einai megalytero to allazoyme
|
||||
|
if(strlen(longest_path)<=strlen(basePath)) |
||||
|
strcpy(longest_path,path); |
||||
|
//anoigoyme pali to mutex gia na mpoyn oi alloi
|
||||
|
pthread_mutex_unlock( &mutex1 ); |
||||
|
|
||||
|
|
||||
|
counter++; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
closedir(dir); |
||||
|
} |
||||
|
|
Loading…
Reference in new issue