Resources, such as saved or cached data, may be locked when accessed by a specific process within a program. Locking the data prevents other processes from overwriting the data prematurely. If a process or query needs to access locked data, but the process locking the data won't let it go, a deadlock may occur.
For example, the following situation will cause a deadlock between two processes:
- Process 1 requests resource B from process 2.
- Resource B is locked while process 2 is running.
- Process 2 requires resource A from process 1 to finish running.
- Resource A is locked while process 1 is running.
The result is that process 1 and process 2 are waiting for each other to finish. Since neither process can continue until the other one completes, a deadlock is created.
Developers can prevent deadlocks by avoiding locking conditions in their programming logic. For example, instead of having two processes rely on each other, the source code can be written so that each thread finishes before another thread needs its resources. By ensuring data is accessible when needed, programmers can protect their applications from hanging or crashing.
NOTE: Deadlocks may also occur when two or more queries are run on a database. Transactional databases lock active records, preventing other queries from accessing them. If a process cannot access a locked record, a database deadlock may occur.