Operating Systems - 3
Operating Systems - 3
(Page: EX-17)
6.8 Race conditions are possible in many computer systems. Consider an online auction system
where the current highest bid for each item must be maintained. A person who wishes to bid on an
item calls the bid(amount) function, which compares the amount being bid to the current highest bid.
If the amount exceeds the current highest bid, the highest bid is set to the new amount. This is
illustrated below: (10 points)
Describe how a race condition is possible in this situation and what might be done to prevent the
race condition from occurring.
6.11 One approach for using compare_and_swap() for implementing a spinlock is as follows:
You have been given the job of creating a word count program for a major book publisher. After
processing all the words in a book, you should produce the number of distinct words used in the
book as well as what they are. Your only stated interface is get_words(char *word[]), which takes as
its parameter an array of character strings (words) and on return places in the array the book’s next
1000 words to be counted. If 1000 words are successfully retrieved, the function returns 1, otherwise,
0. Each call of the function produces a new set of 1000 words. The main work each thread will do
should look like this:
while (get_words(word)) {
/* list is a string array storing all the distinct words that have been encountered so far
*/
Write a multithreaded program to complete the work. While the problem allows you a lot of
flexibility to write a correct program, you must attempt to write an efficient one that minimizes
space and synchronization overhead. For example, a thread should not hold a mutual exclusion lock
during the entire period when it searches the list to check if a word is in it. In addition to write the
program, you should describe your design for improving efficiency and why it works. A pseudo
code with sufficient detail to reveal how the synchronization is applied suffice, though you are
encouraged (but not required) to write real code and test it on a computer.