Skip to main content

Avoiding Deadlock

Avoiding Deadlock

Requesting a (+) exclusive lock when you hold an existing shared lock is potentially dangerous because it can lead to a situation known as "deadlock". This situation occurs when two processes each request an exclusive lock on a lock name already locked as a shared lock by the other process. As a result, each process hangs while waiting for the other process to release the existing shared lock.

The following example shows how this can occur (numbers indicate the sequence of operations):

Process A Process B

1. LOCK ^a(1)#"S"

Process A acquires shared lock.

 
 

2. LOCK ^a(1)#"S"

Process B acquires shared lock.

3. LOCK +^a(1)

Process A requests exclusive lock and waits for Process B to release its shared lock.

 
 

4. LOCK +^a(1)

Process B requests exclusive lock and waits for Process A to release its shared lock. Deadlock occurs.

This is the simplest form of deadlock. Deadlock can also occur when a process is requesting a lock on the parent node or child node of a held lock.

To prevent deadlocks, request the exclusive lock without the plus sign, which unlocks your shared lock. In the following example, both processes release their prior locks when requesting an exclusive lock to avoid deadlock (numbers indicate the sequence of operations). Note which process acquires the exclusive lock:

Process A Process B

1. LOCK ^a(1)#"S"

Process A acquires shared lock.

 
 

2. LOCK ^a(1)#"S"

Process B acquires shared lock.

3. LOCK ^a(1)

Process A releases shared lock, requests exclusive lock, and waits for Process B to release its shared lock.

 
 

4. LOCK ^a(1)

Process B releases shared lock and requests exclusive lock. Process A immediately acquires its requested shared lock. Process B waits for Process A to release its shared lock.

Another way to avoid deadlocks is to follow a strict protocol for the order in which you issue LOCK + and LOCK - commands. Deadlocks cannot occur as long as all processes follow the same order. A simple protocol is for all processes to apply and release locks in collating sequence order.

To minimize the impact of a deadlock situation, include the timeout argument when using plus sign locks. For example, the LOCK +^a(1):10 operation times out after 10 seconds.

If a deadlock occurs, you can resolve it by using the Management Portal or the ^LOCKTAB utility to remove one of the locks in question. From the Management Portal, open the Manage Locks window, and then select the Remove option for the deadlocked process.

FeedbackOpens in a new tab