Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this homework, you will use the POSIX thread (Pthread) library to implement multi-threaded text manipulation program. Your program will take a directory name as
In this homework, you will use the POSIX thread (Pthread) library to implement multi-threaded text manipulation program. Your program will take a directory name as an input, read a directory of text files (the files with .txt extension), assign each file to a separate worker thread. Each worker thread will parse all words from the given file and store each parsed unique word into a dynamically allocated array. It should be noted that each file must be read and processed by a separate worker thread. Use the main thread to orchestrate everything. More specifically, the main thread reads the given directory, assigning threads to regular files that it encounters. The main thread also allocates the initial array of character pointers (or array of structs). Program Details a) The main thread is responsible for creating threads and waiting for the completion of them. b) Each worker thread must open only its assigned file. c) The directory to be read is specified as the first command-line argument. d) The number of worker threads is specified as the second command-line argument. e) Instead of storing just words, you must also store which files the words are in. Therefore, you should probably use a struct for this. This memory must be dynamically allocated. f) Worker threads are responsible for re-allocating memory, as necessary. More specifically, if a word is to be stored, but the dynamically allocated array is full, then the worker thread detecting this must call the realloc() function. The new array size should be doubled in that case. Such operations must be synchronized! g) It should be noted that the number of files and the number of threads may differ. However, it is ensured that the number of files will be greater than or equal to the number of threads. h) If the number of files are greater than the number of threads, then each worker thread continues to take a new file given by the MAIN Thread after it completes the first file. i) Please do not use global variables to synchronize various threads. j) Please do not just implement the simple approach of merely having all threads share a single "mutex" variable. k) Instead, implement the following: When a worker thread adds a word, the only part that is synchronized is obtaining the designated index (i.e., such that no worker threads aim to write to the same array slot). Allocating memory and writing to the designated array slot must be able to occur in parallel with other threads. In other words, multiple threads could be writing to different slots of the array simultaneously. When memory for the array needs to be re-allocated (i.e., by doubling its size), only one thread can do this. All other threads must be waited while this re-allocation occurs. 1) Handling Errors Your program must ensure that the correct number of command-line arguments are included. If not, display an error message and usage information to stderr as follows: i. ERROR: Invalid arguments ii. USAGE: ./a.out -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