SHARING Compiler Option
Although multiple client programs can use the same server library at the same time, they are not necessarily using the same instance of the library. You can use the compiler control option SHARING to specify whether multiple client processes access the same instance of the library. The possible values of this option are PRIVATE, SHAREDBYALL, SHAREDBYRUNUNIT, and DONTCARE.
SHARING = PRIVATE
The operating system initiates a separate instance of the server library program for each client process that links to the library. Values assigned to global objects in the library by a particular client process are visible only to that client process. Private libraries always default to the TEMPORARY option when they are frozen.
SHARING = SHAREDBYALL
All client processes share the same instance of the library. If one client process changes the value of a global object in the library, the next client process that interrogates the global object receives the changed value. The SHAREDBYALL option can be useful if the service provided by the library involves combining information from several clients or sharing resources among several clients.
SHARING = SHAREDBYRUNUNIT
The same instance of the server library is shared by each linkage that originates, either directly or indirectly, from the same client process. Multiple linkages can originate from the same client process in situations such as the following:
-
A single client process contains multiple library declarations that link to the same library program.
-
A client process imports one or more library objects provided indirectly. Though a client process might import objects from two separate libraries (LIB1 and LIB2), both of those libraries, in turn, might link to the same library (LIB3) to provide these objects. If LIB3 is SHAREDBYRUNUNIT, then the objects are imported from the same instance of library LIB3.
The SHAREDBYRUNUNIT option makes it possible for you to implement the ANSI COBOL concept of a run unit. A run unit consists of a program, all SHAREDBYRUNUNIT libraries it initiates directly, and any SHAREDBYRUNUNIT libraries initiated when code from another frozen library runs on the program's stack. To meet the ANSI definition, if the client process links to the same library multiple times, the linkage must be to the same instance of that library. A SHAREDBYRUNUNIT library will remain frozen until its client process terminates.
However, the SHAREDBYRUNUNIT option, by itself, does not guarantee that a client process and its libraries will form a single run unit. To ensure a single run unit, you must use the following design rules:
-
The client must not use any library objects from a control procedure or from offspring processes.
-
The libraries used by the client, directly or indirectly, must all have a sharing option of SHAREDBYRUNUNIT.
-
The libraries must not link to any other libraries before or after freezing.
The following example illustrates what can happen if rule 3 is not followed:
-
A client process CP1 implicitly links to a SHAREDBYRUNUNIT library named LIB1 by invoking a procedure in that library.
-
The client process CP1 also implicitly links to a SHAREDBYRUNUNIT library named LIB2 by invoking procedure X in that library.
-
Before LIB2 freezes, it links to LIB1. Because LIB2 is not yet frozen, LIB2 is not considered part of the run unit of CP1. Therefore, the system links LIB2 to a new instance of LIB1.
In this example, the client process gets access to a second instance of LIB1, which is not part of the ANSI run unit. If library LIB2 had frozen before linking to LIB1, then the linkage would have accessed the existing instance of LIB1, which is part of the run unit.
A run unit is not the same as a process family. For example, tasks initiated by the client process are not part of the run unit. Any other client processes linked to the library are also considered to be separate run units and receive their own instances of the library.
SHARING = DONTCARE
In most programming languages, this option is a nonpreferred synonym for SHAREDBYALL. However, in C and COBOL85, this option is a nonpreferred synonym for SHAREDBYRUNUNIT.
Default Values for SHARING
If a server library program does not include the SHARING compiler control option, then the compiler assigns a default SHARING option to the library. The default value of the SHARING option is SHAREDBYALL in ALGOL, and FORTRAN77; and SHAREDBYRUNUNIT in C, COBOL74, COBOL85, and Pascal.
Sharing in COBOL74
Sharing is handled in a special way for COBOL74 libraries. If a library written in this language has a sharing value of SHAREDBYALL or SHAREDBYRUNUNIT, then multiple client processes can link to the same instance of the library. However, the operating system ensures that only one client process can be executing the procedure exported by this library at any given time. If another client process invokes the procedure while it is in use, the operating system causes this client process to wait until the procedure becomes available.
Shared Library Declarations
Note that the library sharing option affects only the relationship between library declarations and library instances. The sharing option cannot prevent multiple client processes from accessing the same server library instance through a common library declaration. For example, the outer block of an ALGOL program might include a library declaration. If this ALGOL program initiates two internal tasks, the library declaration is visible to both tasks. The two tasks could use this library declaration to access the same library instance, even if the library sharing option is PRIVATE.

