Chapter Contents

Previous

Next
Coprocessing Functions

Advanced Topics: Program Termination

When a program that uses coprocesses terminates, either by a call to exit or by a call to return from the main function, all coprocesses must be terminated. The process by which termination occurs is a complicated one whose details may be relevant to some applications.

By using the blkjmp function, you can intercept coprocess termination when it occurs as the result of coexit or exit , allowing the coprocess to perform any necessary cleanup. Both coexit and exit are implemented by the library as a longjmp to a jump buffer defined by the library; if you intercept an exit or coexit , you can allow coprocess termination to continue by issuing longjmp using the data stored in the jump buffer by blkjmp . (See the blkjmp function description in Chapter 8, "Program Control Functions," in SAS/C Library Reference, Volume 1 for details of this process.)

The atcoexit function also can be used to establish a cleanup routine for a coprocess. Note that this function allows one coprocess to define a cleanup routine for another or even for every terminating coprocess.


Exit Processing for Secondary Coprocesses

When exit is called by a coprocess other than the main one, the library effects a coexit for each non-idle coprocess. More specifically, the following steps are performed:

  1. A longjmp is performed using a library jump buffer to terminate the calling coprocess, allowing the termination to be intercepted by blkjmp . This means that the exit call is treated at first as a coexit .

  2. Any atcoexit routines for the coprocess are called.

  3. The active coprocess is terminated by the library. The coprocess that cocalled the terminated process is then made active. Instead of resuming the cocall function, the exit function is called again. If this coprocess is not the main coprocess, steps 1 and 2 are performed for that coprocess. If this coprocess is the main coprocess, the normal function of exit is performed and the entire program is terminated, as described in Exit Processing for the Main Coprocess.

Note that as a result of the above approach, by the time exit is called by the main coprocess, all remaining coprocesses are idle.


Exit Processing for the Main Coprocess

When the main coprocess calls exit , the following steps are performed:

  1. A longjmp is performed using a library jump buffer to terminate the program, allowing termination to be intercepted by blkjmp .

  2. Any coexit routines and any atcoexit routines for the main coprocess are called.

  3. The main coprocess becomes ended.

  4. If there are any idle coprocesses left, one of them is selected for termination. This coprocess is suspended in a call to the coreturn function. This coprocess becomes active, but the call to coreturn is not completed. Instead, a coexit is performed. (If, on the other hand, all coprocesses are ended, the remainder of program termination processing is performed.)

  5. As usual, if the coprocess has issued blkjmp , it will intercept the coexit and can perform cleanup processing. Note that since there is no calling process to return to, an attempt to call coreturn is treated as an error. It is permissible to call cocall to communicate with other unterminated coprocesses or even to use costart to create new ones at this time.

  6. When coexit completes, the current coprocess is terminated, and control is transferred to step 3.

All coprocesses are terminated before files are closed by the library so that coprocess termination routines can flush buffers or write final messages.


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.