Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed image text in transcribed

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 -n Be sure that all dynamically allocated memory is freed via free(). Required Output When you execute your program, each worker thread must preface its output with its thread ID, which can be obtained via pthread_self() function. Further, each worker thread should display the word that it processed, as well as at which index it places the word. As an example, if you are given a directory called exampleDirectory that contains the files filenamel.txt and abc.txt with the following contents: The content of abc.txt is: I love CSE333 course a lot. The content of filenamel.txt is: I like Pthread programming. Hello

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago

Question

Compute the derivative f(x)=1/ax+bx

Answered: 1 week ago

Question

What is job enlargement ?

Answered: 1 week ago