Actual source code: ex1.c


  2: static char help[] = "Tests signal handling.\n\n";

  4: #include <petscsys.h>
  5: #include <signal.h>

  7: typedef struct _handlerCtx {
  8:   int exitHandler;
  9:   int signum;
 10: } HandlerCtx;

 12: PetscErrorCode handleSignal(int signum, void *ctx)
 13: {
 14:   HandlerCtx *user = (HandlerCtx *)ctx;

 16:   user->signum = signum;
 17:   if (signum == SIGHUP) user->exitHandler = 1;
 18:   return PETSC_SUCCESS;
 19: }

 21: int main(int argc, char *args[])
 22: {
 23:   HandlerCtx user;

 25:   user.exitHandler = 0;

 27:   PetscFunctionBeginUser;
 28:   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
 29:   PetscCall(PetscPushSignalHandler(handleSignal, &user));
 30:   while (!user.exitHandler) {
 31:     if (user.signum > 0) {
 32:       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Caught signal %d\n", user.signum));
 33:       user.signum = -1;
 34:     }
 35:   }
 36:   PetscCall(PetscPopSignalHandler());
 37:   PetscCall(PetscFinalize());
 38:   return 0;
 39: }

 41: /*TEST

 43:    build:
 44:      requires: !defined(PETSC_MISSING_SIGHUP)

 46:    test:
 47:      TODO: need to send a signal to the process to kill it from the test harness

 49: TEST*/