Overview
Step 1
In this lab we will create a simple multi-threaded program that merges two files. The usage of the program is:1
./merge <fi161> <file2> <mergedfife>
The program will create 2 threads, one for file1 and one for file2. Each thread will read a line from the file (use getline()) and write it to the merged file in the following format:
filename: line_number: line_of_text
Here is an example of what the merged file should look like:1
2
3
4
5
6
7
8
9
10
11tami@cs:0~]./merge roses.txt knock.txt merged.txt
tami@cs:0~]cat merged.txt
roses.txt: 1: Roses are red.
knock.txt: 1: Knock, knock!
roses.txt: 2: Violets are blue.
knock.txt: 2: Who's there?
roses.txt: 3: O/S is hard.
knock.txt: 3: Art.
roses.txt: 4: This is so true!
knock.txt: 4: Art who?
knock.txt: 5: R2-D2.
Please notice how the threads alternate in sequence until the end of a file is reached. In the example above, roses.txt has 4 lines and knock.txt has 5 lines (merged.txt thus has 9 lines). The threads and the program will terminate when it is complete. You may not use sleep(), busy waiting, or IPC to synchronise the threads and instead, must use semaphores.
Please note that NOTHING is output to the monitor. When the program functions correctly, a merged file (as displayed above) is created and nothing is printed to stdout.
Step 2
Please ensure that the following requirements are met:
Your code must exist in your home directory on Bash in a file named1
$HOME/lab8/merge.c
The file specified in item 1 must compile to create an executable that properly merges two files of any length. The command to compile the program is:1
gcc -Wall -Wextra -Werror -pthread -o merge merge.c
- Your program must perform reasonable error checking. It should be as robust as possible and detect all errors.
- You must use threads and semaphores to accomplish the task.
- Your code must be well formatted, attractive, readable, and maintainable. Good programming practice requires that your code is properly formatted and commented at all times, not just when it is “done” and submitted for marking.
Notes
Please create your own test files to thoroughly test your program on a Wide variety of different input conditions. The C code you write must adhere to good programming practices. It must be indented, contain suitable comments, use preprocessor directives, and have intuitive variable names. Code that merely works and does not follow good practice DOES NOT meet the requirements. Marking and help during office hours Will only be performed on code that executes on Bash.