Chapter Contents

Previous

Next
cosignal

cosignal



Define a Global Signal Handler


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTIONS
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <coproc.h>
#include <signal.h>

   /* This typedef is in <coproc.h>. */
typedef __remote void(*_COHANDLER)(int);
_COHANDLER cosignal(int signum, _COHANDLER handler);


DESCRIPTION

The cosignal function defines a global handler for a signal. A global handler can be called during the execution of any coprocess except one that has used the signal or sigaction function to define a local handler for the same signal. The signum argument is the number of the signal, which should be specified as a symbolic signal name.

The handler argument specifies the function to be called when the signal occurs. The handler argument can be specified as one of the symbolic values SIG_IGN or SIG_DFL to specify that the signal should be ignored or that the default action should be taken, respectively.

The handler always executes as part of the coprocess interrupted by the signal.


RETURN VALUE

cosignal returns the address of the handler for the signal established by the previous call to cosignal or SIG_DFL if cosignal has not been previously called. cosignal returns the special value SIG_ERR if the request cannot be honored.


CAUTIONS

Use of cosignal has no effect on the handling of a signal that is discovered during execution of a coprocess that has defined a local handler (other than SIG_DFL ) using the signal function. However, when SIG_DFL is defined as the local handler, any global handler will be called.

Handlers established with cosignal should not call longjmp without verifying that the coprocess executing is the one that called setjmp to define the target. Attempting to use longjmp to transfer control in one coprocess to a target established by another causes the program to terminate abnormally.

cosignal is more appropriate than signal for handling asynchronous signals in a multiple coprocess program because asynchronous signals can occur at any time, regardless of transfers of control between processes. Alternately, because each coprocess has its own mask of blocked signals, you can use sigblock and sigsetmask to prevent signals from being discovered except during the execution of a process set up to handle them.


EXAMPLE

The following example shows how a coprocess can be defined to get control every n seconds. The coprocess uses cosignal to define a global handler forthe SIGALRM signal. This handler then uses cocall to give control to the coprocess.

#include <stddef.h>
#include <coproc.h>
#include <signal.h>
#include <lclib.h>

coproc_t tmr_proc;        /* timer coprocess ID                    */
char *tmr_wait(char *);
int delay = 10;           /* delay time in seconds                 */

main()
{
      /* Create the timer coprocess.                               */
   tmr_proc = costart(&tmr_wait,NULL);
      /* Allow timer coprocess to initialize.                      */
   cocall(tmr_proc, NULL);
      /* Set time until first signal.                              */
   alarm(delay);
   .
   .
   .
}

void tmr_hndl(int signum)
{
   /* This is the global SIGALRM handler, which uses cocall to     */
   /* activate the timer coprocess. After the timer coprocess has  */
   /* handled the signal, alarm is called to establish the next    */
   /* interval.                                                    */

   cocall(tmr_proc, NULL);
   alarm(delay);             /* Start new timer interval.          */
}

char *tmr_wait(char *unused)
{
   /* This function is a coprocess that establishes a global       */
   /* signal handler with cosignal to gain control every time      */
   /* SIGALRM is raised. The handler is responsible for calling    */
   /* alarm to re-establish the interval after handling is         */
   /* complete. Note that this technique assures that SIGALRM will */
   /* not be raised while this coprocess is active.                */
   for (;;) {                         /* Do until program exit.    */
      cosignal(SIGALRM, &tmr_hndl);    /* Define global handler.   */
      coreturn(NULL);                  /* Let rest of program run. */
      .
      .           /* Alarm has gone off -- do periodic cleanup.    */
      .

   }
}


RELATED FUNCTIONS

sigaction , signal


SEE ALSO

See Chapter 5, "Signal-Handling Functions," in the SAS/C Library Reference, Volume 1.


Chapter Contents

Previous

Next

Top of Page

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