Actual source code: ex1.c

petsc-3.6.1 2015-08-06
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;

 14: int handleSignal(int signum, void *ctx)
 15: {
 16:   HandlerCtx *user = (HandlerCtx*) ctx;

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

 25: int main(int argc, char *args[])
 26: {
 27:   HandlerCtx     user;

 30:   user.exitHandler = 0;

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