Static / Dynamic Linking in IBM COBOL
Using linking, we can have one program call another program that is not contained in the source code of the calling program. Depending on the linkage options, the object module of the calling program is linked with the object module of the called program “before or during” execution – referred to as static or dynamic linking.
Appropriately choosing the linkage as static / dynamic depending on the application requirement (and not a single technique for the entire application) can result in benefits related to performance, memory usage and maintenance.
In the static CALL statement, the COBOL program and all called programs are part of the same load module (self-contained). When control is transferred, the called program already resides in storage, and a branch to it takes place. Subsequent executions of the CALL statement make the called program available in its last-used state, unless the called program has the INITIAL attribute.
A static call occurs:
1. When the CALL literal statement is used in a program that is compiled using the NODYNAM and NODLL compiler options.. With these options, all calls of the CALL literal format are handled as static calls.
Because a statically called program is link-edited into the same load module as the calling program, a static call is faster than a dynamic call (though the load modules are larger).
The key disadvantage is that to change the behavior of the called program, relinking is necessary. As statically called programs cannot be deleted (using CANCEL), static calls might take more main storage. Also if more than one calling program accesses the called program, duplicate copies of the programs have to be loaded into memory.
In the dynamic CALL statement, the called COBOL subprogram is not link-edited with the main program, but is instead link-edited into a separate load module, and, at run time, is loaded only when it is required (that is, when called).
When a dynamic CALL statement calls a subprogram that is not resident in storage, the subprogram is loaded from secondary storage into the region or partition containing the main program and a branch to the subprogram is performed. The first dynamic call to a subprogram within a run unit obtains a fresh copy of the subprogram. Subsequent calls to the same subprogram (by either the original caller or any other subprogram within the same run unit) result in a branch to the same copy of the subprogram in its last-used state, provided the subprogram does not possess the INITIAL attribute.
When a CANCEL statement is issued for a subprogram, the storage occupied by the subprogram is freed, and a subsequent call to the subprogram functions as though it were the first call. A subprogram can also be cancelled from a program other than the original caller.
A Dynamic Call occurs:
1. When the CALL literal statement is used in a program that is compiled using the DYNAM and the NODLL compiler options (or)
2. when the CALL identifier statement is used in a program. CALL identifier is always dynamic, even if the NODYNAM compiler option is used.
The program name in the PROGRAM-ID paragraph must be identical to the corresponding load module name or load module alias of the load module that contains it.
Typically, dynamic CALL statement is used in the following circumstances:
- For ease of maintenance. Applications do not have to be link-edited again when dynamically called subprograms are changed.
- The subprograms called are used infrequently or are very large. If the subprograms are called on only a few conditions, dynamic calls can bring in the subprogram only when needed. If the subprograms are very large or there are many of them, using static calls might require too much main storage. Less total storage might be required to call and cancel one, then call and cancel another, than to statically call both.
The key points to be noted are:
- CALL identifier is always dynamic
- With NODYNAM compiler option, all calls of the CALL literal format are handled as static calls.
- When a static CALL statement and a dynamic CALL statement to the same subprogram are issued within one program, a second copy of the subprogram is loaded into storage.