Being Bit(ten) and Byting Back

Monday, May 30, 2005

Something to remember about deadlocks

Somebody once said to me that "In theory, theory and practice go together. In practice, they rarely do.".

Yesterday, while working on interfacing module for a digital radiography panel we use in our product (Yes, I was working over long weekend. And I need some sympathy please!:'( ), I ran into one of those rare situations where forgetting your OS theory is detrimental to your long-term sanity.

I ran into a very subtle situation where two of the three threads were deadlocking and freezing up the application. I don't want to rehash the whole story here, the memory is too raw and painful right now. I figured out in the end what was going on after a couple of hours when I saw some undergrad notes on an OS course on some CS department on deadlocks. And I cursed myself for not remembering those. OS theory was always something that bored me, and yesterday I wished I had paid more attention in class.

So what I'm going to do now is to write those conditions on my blog, in the faint hope that I can remember them the next time I'm working on a long weekend thinking about the beautiful day outside. The three conditions for a deadlock to occur are: [drum roll here] i) Mutual Exclusion: Only one resource may use a resource at a time. ii) Hold-and-Wait: A process may hold allocated resources while awaiting assignment of others iii) No Pre-emption: No resource can be forcibly removed from a process holding it.

Now, in a multithreaded application, these conditions are good thing, however, if the fourth condition is satisfied, then you're locked in the deadly embrace of death

iv) Circular Wait: A closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain.

On another note, I've always wondered if there is a better way to design a multithreaded application in OO context. A thread is a thread of execution and somehow that's always smelled of a procedural function to me. The question I have is that in a well-designed OO design, should'nt all objects be active, and all messages asynchronous?

0 Comments:

Post a Comment

<< Home