Deadlocks - what arer you doing about them
- nitinjavakid
- Member
- Posts: 65
- Joined: Sat Oct 21, 2006 11:28 am
- Location: Exams over!
- Contact:
Deadlocks - what arer you doing about them
I was reading about deadlocks and found it quite a challenging topic. So, the question is; how are you handling deadlocks in your OS? Are you just ignoring them or using some technique?
Regards
Nitin
Nitin
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
If an deadlock occurs in userspace, it is the fault of an application. And unless its a driver written by me its none of my business
In the kernel, I use a combination of the following:
- Wait-free code when supported (i.e. avoid the need for locking)
- Resource ordering (which is imo the easiest deadlock preventer existing)
In the kernel, I use a combination of the following:
- Wait-free code when supported (i.e. avoid the need for locking)
- Resource ordering (which is imo the easiest deadlock preventer existing)
Hi.
In theory if any of deadlock conditions(e.g. mutual exclusion) is not reached deadlock cannot occur.Yet there isn't a universal solution for all kinds of cases.So to face different occasions the solution should be specially designed.Here are some examples:
-For memory allocation:you can disable dynamic(runtime) memory allocation feature.That is,all memory is given when a process is initialized,so the requested size is fixed.In this way,deadlock on memory will not occur(because there's no allocation later during execution).
-For processes which are waiting for each other's signal:better to test it dynamicly using 2 tables for each process.The table contains processes it is waiting for.Regular tests will find out if the processes it's waiting for is also waiting for itself.(I'm not quite sure if others are using this link-like method,and also if this will work for a larger loop).
-General-about preemption:preemption can take place during runtime.If 2 processes are deadlocked for each other's resources,you cn make one suspend to free its resource for the other one to go on.When the other one has finished using the resource,you can hand it back to its original owner.(This can easily work for some kinds of resources.For memory,you can page a process out to an external storage to free the memory it takes up.)
Just for you to catch a glimpse...
In theory if any of deadlock conditions(e.g. mutual exclusion) is not reached deadlock cannot occur.Yet there isn't a universal solution for all kinds of cases.So to face different occasions the solution should be specially designed.Here are some examples:
-For memory allocation:you can disable dynamic(runtime) memory allocation feature.That is,all memory is given when a process is initialized,so the requested size is fixed.In this way,deadlock on memory will not occur(because there's no allocation later during execution).
-For processes which are waiting for each other's signal:better to test it dynamicly using 2 tables for each process.The table contains processes it is waiting for.Regular tests will find out if the processes it's waiting for is also waiting for itself.(I'm not quite sure if others are using this link-like method,and also if this will work for a larger loop).
-General-about preemption:preemption can take place during runtime.If 2 processes are deadlocked for each other's resources,you cn make one suspend to free its resource for the other one to go on.When the other one has finished using the resource,you can hand it back to its original owner.(This can easily work for some kinds of resources.For memory,you can page a process out to an external storage to free the memory it takes up.)
Just for you to catch a glimpse...
- nitinjavakid
- Member
- Posts: 65
- Joined: Sat Oct 21, 2006 11:28 am
- Location: Exams over!
- Contact:
- carbonBased
- Member
- Posts: 382
- Joined: Sat Nov 20, 2004 12:00 am
- Location: Wellesley, Ontario, Canada
- Contact:
Re: Deadlocks - what arer you doing about them
Fixing deadlocks is an entirely huge and variable topic. I agree with m that a targetted approach at fixing each kind is always required.nitinjavakid wrote:I was reading about deadlocks and found it quite a challenging topic. So, the question is; how are you handling deadlocks in your OS? Are you just ignoring them or using some technique?
Finding them is another one which is often difficult -- you're putting your OS through its paces, and all of a sudden it just stops. Why?
I don't know if this will be useful to you, but I've found that a specific debugging property (ie, #ifdef'd code) in my OS has provided me with useful insight. The code is an extremely simple addition to my mutex locks. Translate any indefinite mutex locks (aka, potential deadlocks) to timeout after 30 seconds. If this mutex reaches the timeout state, trace out the error as a deadlocked mutex, and the two thread handles involved (the one that's locked it indefinitly, and the one trying to lock it again).
--Jeff
First, provide and promote methods that don't allow permanently locked mutexes. Secondly, ignore any other deadlocks.
I use a define that makes this code do exactly what you think it would:
It doesn't prevent deadlock (problem of logic) but it does prevent forgetting to unlock it / unlocking twice etc. The programmer should be the one taking care of the logic.
On the other hand, promote use of lockless algorithms. For example, most, if not all, mutexes can be replaced by using a compare-and-swap instruction instead. You load the original value, transform it, and then if it's still the same replace it with the calculated value. If not, you try again.
Programmers that make logic errors should be forced to fix them as much as possible. Programmers that have all their logic set right shouldn't be forced to type two commands when one can suffice.
I use a define that makes this code do exactly what you think it would:
Code: Select all
void something() {
locked(mutexA) {
// do something
}
}
On the other hand, promote use of lockless algorithms. For example, most, if not all, mutexes can be replaced by using a compare-and-swap instruction instead. You load the original value, transform it, and then if it's still the same replace it with the calculated value. If not, you try again.
Programmers that make logic errors should be forced to fix them as much as possible. Programmers that have all their logic set right shouldn't be forced to type two commands when one can suffice.