Overview
The last three assignments of the semester will explore the use of
concurrency to solve a simple problem. This assignment will involve processes
(created by forking). Assignment 5 will use POSIX threads, and Assignment 6
will use network communication. Part 1 of this project will be used in all
three assignments, so it is important that you start this early and make sure
that it is fully debugged.
For these assignments, put all the the general purpose functions, such as seachForTwoStrings in a separate library file from your main. You can then reuse this for each part of this assignment and for future assignments.
Part 1
Make a directory, assign4, for this assignment.
Write the function:
int searchForTwoStrings(char *filename, char *s1, char *s2)
It returns the line number of the first line in the file filename
that contains
both s1 and s2 (with case ignored).
The first line in the file is line number 1.
It returns 0 if there is no line that contains both of
these strings and returns -1 and sets errno
if it cannot read the file.
Lines are delimited by the newline character, '\n'.
Do not assume that the entire file will fit in memory, but you may assume a maximum length of a line in the file, say 10K. Return -1 if a line is too long. The readline function from the restart library may be of use here.
Part 2
Write a main program, search_simple.c
that takes two strings and a list of filenames
on the command line. If there are not at least three parameters,
the program should exit immediately with a warning message.
The program prints a descriptive line containing your name and the two strings.
For each of the filenames it calls the function from Part 1
and for each, prints a line giving the filename and the return value
of the above function.
Part 3
Write a new main program, search_fork_bad.c that is similar to
the one above, but
it creates a child process for
each of the filenames. Each child process prints a line with the
required information.
It is required that you solve this
problem in such a way the the children work independently and
concurrently. Each should produce its output when the output is ready.
It is not necessary that the output lines be in the same order as the
list of filenames given by the command line parameters.
If care is not taken, there is a possibility that the output generated by the children will be jumbled. Test your program and see if this will happen. Do not worry about this problem until Part 4.
Part 4
Whether the output of the above program is jumbled or not,
the program is not correct if there is a possibility that it will produce
incorrect output.
Fix this problem by writing a new main program, seach_fork.
Use a synchronization construct to protect the appropriate critical
sections.
Your solutions must preserve the concurrency in determining
of the output lines.
Note that you cannot simply implement this
with a POSIX unnamed semaphore.
If you create such a semaphore before
you fork, each child gets a copy of the semaphore and so each is using
a different semaphore.
One way to solve this problem is to have
each child process write to a pipe and have another process read from
the pipe and output the results.
Write a paragraph indicating
how you solved this problem, and why it works.
For each of parts 2, 3, and 4 you will be producing output that demonstrates that your program is working. Save the output, print it, and include it with your assignment. For each part, write a description of the output generated and why you think it demonstrates that the program is working correctly. Include this description on the same page as the output or on the following page. Use this cover sheet for handing in your assignment. Answer the questions listed on the cover sheet.