Operating Systems - 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSE 3320 Homework Assignment 3 (Fall 2021)

Due date: Noon 10/6 (Wednesday)


SGG Book (the 10th Edition)

(Page: EX-17)

Problem 1: (20 points)

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.

Problem 2: (20 points)

6.11 One approach for using compare_and_swap() for implementing a spinlock is as follows:

A suggested alternative approach is to use the “compare_and_compare_and-swap” idiom, which


checks the status of the lock before invoking the compare_and_swap() operation. (The rationale
behind this approach is to invoke compare_and_swap() only if the lock is currently available.) This
strategy is shown below:

Does this “compare_and_compare_and_swap” idiom work appropriately for implementing


spinlocks? If so, explain. If not, illustrate how the integrity of the lock is compromised.
Problem 3: (60 points)

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)) {

for (i = 0; i < 1000; i++) {

/* list is a string array storing all the distinct words that have been encountered so far

You can sequentially search the list to see if there is a match

*/

if word[i] is not in the list {

increment its count by one;

add word[i] to list at the end of the list;

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.

You might also like