Major locking errors, Operating System

Assignment Help:

When programming with threads, there are three very common mistakes that programmers often
make:

1. locking twice (depending on the system and type of lock, can cause crashes, hangs, or do bizarre things);

2. locking and not unlocking (i.e. failure to unlock);

3. deadlock (see next lecture).

4. Priority inversion - This is not an error per se, but an important issue that occurs Of these problems, locking twice is probably the easiest type of error to detect. Here's one example:

function f() { function g() {
lock(L); lock(L);
g(); // access shared data
unlock(L); unlock(L);
} }

So-called "recursive" locks can deal with this situation correctly, though normal locks will cause this thread to wait forever when the function g(), when called fromf(), then calls lock(L) on a previously-held lock. Dealing with this can lead to a common code pattern with certain functions designed only to be called with locks held:

function f(){

function g() {

function g_internal() {
lock(L); lock(L); // locks must be held here!
g_internal(); g_internal(); // access shared data
unlock(L); unlock(L); }
} }

Failure to unlock is slightly more dif?cult to detect. It can occur, for example, if the programmer forgets to release the lock in one of the possible execution branches of the function:

function f() {
lock();
if (x==0) {
// should also unlock here before returning!
return;
}
// do something
unlock();
return;
}

One way to deal with this is just to remember to unlock() at each possible return. Another is to have every return path go through the same section of code (and in C, goto is sometimes useful for this, despite its bad reputation).


Related Discussions:- Major locking errors

Describe the differences among short-term and medium-term, Describe the dif...

Describe the differences among short-term, medium-term, and long-term scheduling.   Short-term (CPU scheduler)-selects from jobs in memory those jobs that are ready to execu

Implementation of modern memory management system , Central to implementati...

Central to implementation of a modern memory management system is the page replacement algorithm. Modern virtual memory systems break memory up into pages and map (via a page table

Explain threads in details?, What are threads? A thread - sometimes c...

What are threads? A thread - sometimes called as an implementation context or a lightweight process - is a single sequential flow of control within a program. We use threads

What is dispatcher? explain, Questiion 1 What is RTOS? What are its requir...

Questiion 1 What is RTOS? What are its requirements? Questiion 2 Explain the structural elements of a real time system mode Questiion 3 What is kernel? Explain abo

What is banker''s algorithm, What is banker's algorithm? Banker's algor...

What is banker's algorithm? Banker's algorithm is a deadlock avoidance algorithm that is applicable to a resource-allocation system with multiple examples of each resource type

How free-space is managed using bit vector implementation, How free-space i...

How free-space is managed using bit vector implementation? The free-space list is implemented as a bit map or bit vector. Each block is shown by 1 bit. If the block is free, th

Memory organization and decoding, In a  p system each part (RAM, ROM, I/O) ...

In a  p system each part (RAM, ROM, I/O) has a unique set of numbers. The allocation of these numbers is usually stated in the form of a memory map. This is a plot of data bus agai

What are the advantages and disadvantages of multithreading, What are the a...

What are the advantages and disadvantages of multithreading?  Advantages : Since by multithreading multiple executions are carried out in the same process environment t

Describe two segment-replacement algorithms, Q. Segmentation is alike to p...

Q. Segmentation is alike to paging but uses variable-sized "pages". Describe two segment-replacement algorithms based on FIFO and LRU page replacement schemes. Remember that since

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd