Question: Searching files using pthreads in Linux A group of threads will be used to mimic the behavior of the bash command grep. grep searches the

Searching files using pthreads in Linux

A group of threads will be used to mimic the behavior of the bash command grep. grep searches the named input FILEs for lines containing a match to the given PATTERN. By default, grep prints the matching lines. $ grep PATTERN FILE1 FILE2 ... An example is: $ grep stdout hw1.html to stdout) or you can pass the name of the file to be processed via the command line. The code

In our version of grep, my_grep, each file listed at the command line will be searched by a different thread for the string given by PATTERN. Each thread will print out a line containing PATTERN to standard output as FILE1: ...PATTERN... FILE2: PTTERN...PATTERN... ... In addition, each thread will print out before it exits the number of lines in the file containing PATTERN. The main() thread (assuming it is the last thread) should print out the total number of times that lines containing PATTERN was found in all the threads.

There are two shared resources in my_grep: 1. standard output and 2. the value of PATTERN . Your code needs to use some form of synchronization to protect the shared resources.

How to organize the code

The main thread receives at least two (2) command line arguments. The first is PATTERN, the string to search for in each file. The additional command line arguments are the file names.

The number of command line arguments must be greater than 2. The code can limit the number of files that are processed.

The value of PATTERN (which is a char *) should be stored in a global variable (shared resource) that each thread can access.

The main() thread then calls pthread_create() for each file to be searched passing the filename as the argument. A cast will be needed.

Each thread runs a function, void *searchfile(void *), which opens the external file whose file name was passed in as a parameter for reading. You may assume that the file is in the current directory.

Assuming that the open() is successful, the function uses fgets() or getline() to read each line.

Either strstr() is used to search for the PATTERN, a string class function is used or write you own string searching function.

The function searchfile() writes out each matching line to standard output, with the filename as the first entry on the line.

Synchronization is used to access the shared resource, standard output.

When the whole file has been processed, the function writes to standard output the number of matching lines.

The function returns the number of matching lines.

If the file can not be opened, the function should write an informative message and return 0.

The main() thread joins all the other threads, storing their return values.

When all the other threads have terminated, the main() thread writes to standard output the total number of matching lines.

Test your code carefully to make sure that it can search for PATTERN in more than one file.

What to submit

Please put all your functions in a file called my_grep.c and send it to me.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!