Reputation: 5040
On unix, a process is stuck, you doubt that it may be deadlock, find the reasons for deadlocking and how to remove and avoid it ?
I know the 4 conditions for deadlock:
Mutual exclusion: A resource can be assigned to at most one process at a time (no sharing).
Hold and wait: A processing holding a resource is permitted to request another.
No preemption: A process must release its resources; they cannot be taken away.
Circular wait: There must be a chain of processes such that each member of the chain is waiting for a resource held by the next member of the chain.
But, they are theoretical, how to determine a deadlock on unix practically ? Only by seeing that a process does not make progress ? How to find which part of the code cause the deadlock and the reasons ? If you are allowed to use tools, what can be used ?
thanks
Upvotes: 3
Views: 5098
Reputation: 393487
There is also Valgrind's Helgrind tool: Helgrind: a thread error detector
Helgrind is a Valgrind tool for detecting synchronisation errors in C, C++ and Fortran programs that use the POSIX pthreads threading primitives.
The main abstractions in POSIX pthreads are: a set of threads sharing a common address space, thread creation, thread joining, thread exit, mutexes (locks), condition variables (inter-thread event notifications), reader-writer locks, spinlocks, semaphores and barriers.
Helgrind can detect three classes of errors, which are discussed in detail in the next three sections:
Problems like these often result in unreproducible, timing-dependent crashes, deadlocks and other misbehaviour, and can be difficult to find by other means.
Helgrind is aware of all the pthread abstractions and tracks their effects as accurately as it can. On x86 and amd64 platforms, it understands and partially handles implicit locking arising from the use of the LOCK instruction prefix.
Helgrind works best when your application uses only the POSIX pthreads API. However, if you want to use custom threading primitives, you can describe their behaviour to Helgrind using the ANNOTATE_* macros defined in helgrind.h. This functionality was added in release 3.5.0 of Valgrind, and is considered experimental.
Upvotes: 5
Reputation: 16266
Use debugger.
Quick tip for debugging deadlocks
or this simple one:
how to find deadlock scenario using gdb
Upvotes: 1