Actual source code: ex1.c

petsc-3.13.6 2020-09-29
Report Typos and Errors

  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: int handleSignal(int signum, void *ctx)
 13: {
 14:   HandlerCtx *user = (HandlerCtx*) ctx;

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

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

 26:   user.exitHandler = 0;

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


 42: /*TEST

 44:    build:
 45:      requires: !define(PETSC_MISSING_SIGHUP)

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

 50: TEST*/