Question
(C Language) implement(modify) code into use multi-threaded recursive file search. Update it to use 4 threads that each perform a recursive search on a given
(C Language) implement(modify) code into use multi-threaded recursive file search. Update it to use 4 threads that each perform a recursive search on a given directory. Split up the workload so each thread is doing something different, and something useful. For example, in main you could "discover" all the files in the provided starting directory, and start 4 threads that recurse on each of those discovered files(re-using threads as needed once they finish). You will use the pthread library to implement multi-threading; Do not use the Pthread wrappers. And please comment the code!
#include
#include
#include
#include
#include
#include
#include
//takes a file/dir as argument, recurses,
// prints name if empty dir or not a dir (leaves)
void recur_file_search(char *file);
//share search term globally (rather than passing recursively)
const char *search_term;
int main(int argc, char **argv)
{
if(argc != 3)
{
printf("Usage: my_file_search
printf("Performs recursive file search for files/dirs matching\
exit(1);
}
//don't need to bother checking if term/directory are swapped, since we can't
// know for sure which is which anyway
search_term = argv[1];
//open the top-level directory
DIR *dir = opendir(argv[2]);
//make sure top-level dir is openable (i.e., exists and is a directory)
if(dir == NULL)
{
perror("opendir failed");
exit(1);
}
//start timer for recursive search
struct timeval start, end;
gettimeofday(&start, NULL);
recur_file_search(argv[2]);
gettimeofday(&end, NULL);
printf("Time: %ld ", (end.tv_sec * 1000000 + end.tv_usec)
- (start.tv_sec * 1000000 + start.tv_usec));
return 0;
}
//This function takes a path to recurse on, searching for mathes to the
// (global) search_term. The base case for recursion is when *file is
// not a directory.
//Parameters: the starting path for recursion (char *), which could be a
// directory or a regular file (or something else, but we don't need to
// worry about anything else for this assignment).
//Returns: nothing
//Effects: prints the filename if the base case is reached *and* search_term
// is found in the filename; otherwise, prints the directory name if the directory
// matches search_term.
void recur_file_search(char *file)
{
//check if directory is actually a file
DIR *d = opendir(file);
//NULL means not a directory (or another, unlikely error)
if(d == NULL)
{
//opendir SHOULD error with ENOTDIR, but if it did something else,
// we have a problem (e.g., forgot to close open files, got
// EMFILE or ENFILE)
if(errno != ENOTDIR)
{
perror("Something weird happened!");
fprintf(stderr, "While looking at: %s ", file);
exit(1);
}
//nothing weird happened, check if the file contains the search term
// and if so print the file to the screen (with full path)
if(strstr(file, search_term) != NULL)
printf("%s ", file);
//no need to close d (we can't, it is NULL!)
return;
}
//we have a directory, not a file, so check if its name
// matches the search term
if(strstr(file, search_term) != NULL)
printf("%s/ ", file);
//call recur_file_search for each file in d
//readdir "discovers" all the files in d, one by one and we
// recurse on those until we run out (readdir will return NULL)
struct dirent *cur_file;
while((cur_file = readdir(d)) != NULL)
{
//make sure we don't recurse on . or ..
if(strcmp(cur_file->d_name, "..") != 0 &&\
strcmp(cur_file->d_name, ".") != 0)
{
//we need to pass a full path to the recursive function,
// so here we append the discovered filename (cur_file->d_name)
// to the current path (file -- we know file is a directory at
// this point)
char *next_file_str = malloc(sizeof(char) * \
strlen(cur_file->d_name) + \
strlen(file) + 2);
strncpy(next_file_str, file, strlen(file));
strncpy(next_file_str + strlen(file), \
"/", 1);
strncpy(next_file_str + strlen(file) + 1, \
cur_file->d_name, \
strlen(cur_file->d_name) + 1);
//recurse on the file
recur_file_search(next_file_str);
//free the dynamically-allocated string
free(next_file_str);
}
}
//close the directory, or we will have too many files opened (bad times)
closedir(d);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started