CS 3733 Operating Systems, Spring 2010 Assignment 5


Due Friday, April 16, 2010


Overview
This is a continuation of Assignment 4. You will solve the same problem using POSIX threads.

Part 1
Make a directory, assign5, for this assignment and copy all of the files from assign4 into this directory. Write the function:
     void *searchForTwoStringsThreadBad(void *pointer);
The parameter will point to a structure containing 3 pointers to strings.
The function will call searchForTwoStrings from Assignment 4 and print a line to standard output.
The line should contain the filename and the return value of searchForTwoStrings.
The function should return NULL.
Add this function in your library file with searchForTwoStrings.

Part 2
Modify search_simple.c from assignment 4 so that it now calls searchForTwoStringsThreadBad instead of searchForTwoStrings.
Since the function you are calling already does the printing, the main program will just call the function in a loop and exit.
At this point we are not creating multiple threads, just calling this as an ordinary function.
You need to add code to set up the appropriate sttructure for the parameter of searchForTwoStringsThreadBad.
Test this as before.

Part 3
Write a new main program, search_thread_bad.c that is similar to search_simple, but it creates a new thread for each file.
The threads must run concurrently. Make sure that the main program does not exit until all threads have completed.

Hint: Be careful. If you do this in what might seem to be the obvious way, and reuse the memory for the parameter for each call to searchForTwoStringsThreadBad, your program might change the values stored there before a thread can use these values.

Part 4
The word "bad" is used in the names above because there is an unprotected critical section.
It is possible, but not likely, that the output for different threads could be interleaved.
Fix this problem by writing searchForTwoStringsThread which uses a POSIX mutex lock to protect the critical section.
Write a new main program search_thread to test this. Your solution must preserve the concurrency of the solution to Part 3.


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. For Part 4, include a paragraph describing how you solved the problem of possibly jumbled output. Use this cover sheet for handing in your assignment. Answer the questions listed on the cover sheet.