For this assignment you will extend countnames.c (from assignment #1) to compute the name counts of one or more input files in a parallel fashion using multiple processes. For example, the ith child process will process the ith file.
You will develop a program countnames_parallel that takes one or more input files on the command line. You will need to compute the count of names over all files similar to assignment #1.
Assume that you have several files with names separated by newlines. You want to read in and count the names from all the files. Same as in assignment #1 you can assume the files contain up to 100 different names with up to 30 characters in each.
If a file appears multiple times on the command line, those names will be processed multiple times.
You should use fork() to start a process for each of the files, such that we can compute a count for each file individually in parallel. To communicate between processes you may use pipe(). You need to wait for the processes to finish using wait().
You must be able to handle files with any number of names (including no names). You can assume that the files contain only valid ASCII characters separated by newlines (and possibly an occasional empty line, same as in assignment 1). A name can include spaces, for example “Tom Wu” is one name.
The code/ipc directory contains examples of using fork, pipe and wait, which you can consult for help.
For example, running on the names1.txt
files will output:
Download names1.txtand names2.txt
Download names2.txt
$ ./countnames_parallel names1.txt names2.txt
which will output to stdout:
Tom Wu: 4
Jenn Xu: 2
In the example above, if an input file contained empty lines you should also print to stderr messages like (same as assignment 1, empty lines in a file don’t count and can be ignored for counting purposes):
Warning - file names1.txt line 3 is empty.
$ ./countnames_parallel names1.txt names2.txt names2.txt
will output the following:
Tom Wu: 5
Jenn Xu: 4