Ruh roh! Daniel has jumbled up all the lines between his files! He wants to search for the number of instances of a specific line in his files, but due to severe time restrictions, he must use multi-threading to go sonic speed!
Write a program count_lines.c
which counts the number of times a certain line appears as a line across a library of files. The line and the files will be given as command line arguments. For example:
./count_lines "Jastavo Fring" a.txt b.txt c.txt
would search for the number of times lines matching exactly Jastavo Fring
occurs across a.txt
, b.txt
, and c.txt
combined.
You program must create a separate thread to count occurrences for each file. Be careful to avoid race conditions, as the autotests will likely miss them! Feel free to ask one of the friendly helpers if you aren't sure whether your code includes any race-conditions or deadlocks :smile:.
The output from your program should look exactly like this:
$ rm count_file_one count_file_two count_file_three
$ printf '%s\n' 'additional supply depots' 'additional supply depots ' 'additional supply depots required' 'additional supply depots' >> count_file_one
$ printf '%s\n' 'additional supply depots' >> count_file_two
$ printf '%s\n' '' 'additional supply depots' '' '' >> count_file_three
$ dcc -pthread count_lines.c -o count_lines
$ ./count_lines "additional supply depots" count_file_one count_file_three
3 lines matching "additional supply depots" found across 2 files.
1000
characters.When you think your program is working, you can use CSE autotest to test your solution.
$ 1521 csesoc-autotest count_lines
You can view the solution code to this problem here.