Solution Found!
A multithreaded web server wishes to keep track of the numberof requests it services
Chapter 5, Problem 5.19(choose chapter or problem)
A multithreaded web server wishes to keep track of the number of requests it services (known as hits). Consider the two following strategies to prevent a race condition on the variable hits. The first strategy is to use a basic mutex lock when updating hits:
int hits;
mutex_lock hit_lock;
hit_lock.acquire();
hits++;
hit_lock.release();
A second strategy is to use an atomic integer:
atomic_t hits;
atomic_inc(&hits);
Explain which of these two strategies is more efficient.
Questions & Answers
QUESTION:
A multithreaded web server wishes to keep track of the number of requests it services (known as hits). Consider the two following strategies to prevent a race condition on the variable hits. The first strategy is to use a basic mutex lock when updating hits:
int hits;
mutex_lock hit_lock;
hit_lock.acquire();
hits++;
hit_lock.release();
A second strategy is to use an atomic integer:
atomic_t hits;
atomic_inc(&hits);
Explain which of these two strategies is more efficient.
ANSWER:Step 1 of 3
A race condition in an operating system is a situation when multiple processes access and manipulate the same data at the same time. The output in such a case depends upon the order of access that takes place. So, to avoid a race condition, mutual exclusion is used using either mutex locks or atomic integer.