Question

A spin lock acquire() can be implemented with a test-and- set instruction as follows: while (test-and-set(&lock->held)...

A spin lock acquire() can be implemented with a test-and- set instruction as follows:

while (test-and-set(&lock->held) == 1); // spin

Recall that test-and-set instruction returns the old value at the address while atomically setting it to 1.

A test-and-set instruction consumes more CPU cycles than an instruction that simply compares the values of memory variables. Thus, the test-and-set instruction is said expensive.

Now, suppose we implement a new lock acquire() as follows:

1: while (1) {
2: while (lock->held > 0)
3: ; // spin
4: if (test-and-set(&lock->held) == 0)
5: return;
6: }

a) Does the above new lock acquire() implementation work? Explain how the new lock acquire() operation works.
b) Is the modified lock implementation more efficient than the first one? Justify your answer.

Homework Answers

Answer #1

a) Does the above new lock acquire() implementation work? Explain how the new lock acquire() operation works.

Answer:

Yes, the above new lock acquire() implementation works.

Working:

The lock spins until it finds the lock is free.

This is done at line 2 to line 3:

while (lock->held > 0)
// spin

Then it uses the atomic test-and-set for acquiring the lock. If the atomic lock acquisition fails, it goes back to the outer loop for trying again.


b) Is the modified lock implementation more efficient than the first one? Justify your answer.

Answer:

The behaviour of the lock has changed as follows:

This implementation will call “test-and-set” less frequently as compared to the first implementation. It spins only until it considers that the lock is free.

Therefore, this implementation is more efficient.

This implementation matches the actual implementation in the real-world locks.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement...
1.Ford initially tried to use vertical integration to control all aspects of the production and sale...
1.Ford initially tried to use vertical integration to control all aspects of the production and sale of its automobiles. Later, Ford abandoned vertical integration and adopted a strategy of partnerships with key suppliers. The partnership arrangements were expected to cut costs for Ford but still permit the suppliers to enjoy profits at the level of the industry standard. How would it be possible for a supplier’s profits to be preserved while Ford’s costs decreased? Select one: a. Ford would agree...
The Business Case for Agility “The battle is not always to the strongest, nor the race...
The Business Case for Agility “The battle is not always to the strongest, nor the race to the swiftest, but that’s the way to bet ’em!”  —C. Morgan Cofer In This Chapter This chapter discusses the business case for Agility, presenting six benefits for teams and the enterprise. It also describes a financial model that shows why incremental development works. Takeaways Agility is not just about the team. There are product-management, project-management, and technical issues beyond the team’s control. Lean-Agile provides...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...