An interlock condition happens when two tasks are waiting for each
other's completion, but neither task can get the resource it needs
to complete. Figure 1 shows an example of an
interlock. Task A has exclusive access to resource M, and task B
has exclusive access to resource N. When task B requests exclusive
access to resource M, B is placed in a wait state because task A has
exclusive control of resource M.
The interlock becomes complete when task A requests exclusive control
of resource N. The same interlock would have occurred if task B issued
a single request for multiple resources M and N prior to task A's
second request. The interlock would not have occurred if both tasks
had issued single requests for multiple resources. Other tasks requiring
either of the resources are also in a wait condition because of the
interlock, although in this case they did not contribute to the conditions
that caused the interlock.
Figure 1. Interlock
Condition Task A Task B
ENQ (M,A,E,8,SYSTEM)
ENQ (N,B,E,8,SYSTEM)
ENQ (M,A,E,8,SYSTEM)
ENQ (N,B,E,8,SYSTEM)
The above example involving two tasks and two resources is a simple
example of an interlock. The example could be expanded to cover many
tasks and many resources. It is imperative that you avoid interlock.
The following procedures indicate some ways of preventing interlocks.
- Do not request resources that you do not need immediately. If
you can use the serially reusable resources one at a time, request
them one at a time and release one before requesting the next.
- Share resources as much as possible. If the requests in the lists
shown in Figure 1 had been for shared control, there would
have been no interlock. This does not mean you should share a resource
that you will modify. It does mean that you should analyze your requirements
for the resources carefully, and not request exclusive control when
shared control is enough.
- If you need concurrent use of more than one resource, use the
ENQ macro to request control of all such resources at the same time.
The requesting program is placed in a wait condition until all of
the requested resources are available. Those resources not being
used by any other program immediately become exclusively available
to the waiting program. For example, instead of coding the two ENQ
macros shown in Figure 2, you could code the
one ENQ macro shown in Figure 3. If all requests
were made in this manner, the interlock shown in Figure 1 could not occur. All of the requests from one task would
be processed before any of the requests from the second task. The
DEQ macro can release a resource as soon as it is no longer needed;
multiple resources requested in a single ENQ invocation can be released
individually through separate DEQ instructions.
Figure 2. Two
Requests For Two ResourcesENQ (NAME1ADD,NAME2ADD,E,8,SYSTEM)
ENQ (NAME3ADD,NAME4ADD,E,10,SYSTEM)
Figure 3. One
Request For Two ResourcesENQ (NAME1ADD,NAME2ADD,E,8,SYSTEM,NAME3ADD,NAME4ADD,E,10,SYSTEM)
- If the use of one resource always depends on the use of a second
resource, then you can define the pair of resources as one resource.
On the ENQ and DEQ
macros, define the pair with a single rname and qname. You can use
this procedure for any number of resources that are always used in
combination. However, the control program cannot then protect these
resources if they are also requested independently. Any requests
must always be for the set of resources.
- If there are many users of a group of resources and some of the
users require control of a second resource while retaining control
of the first resource, it is still possible to avoid interlocks.
In this case, each user should request control of the resources in
the same order. For instance, if resources A, B, and C are required
by many tasks, the requests should always be made in the order of
A, B, and then C. An interlock situation will not develop, since
requests for resource A will always precede requests for resource
B.
- When multiple resources are ENQed, all users must acquire the
resources in the same manner. If one user gets resources individually,
all users must acquire the resources individually. If one user gets
the resources through a single list, all users must acquire the resources
in a single list, but the sequence of resource names within the lists
can be different.