1 2 static char help[] = "Tests signal handling.\n\n"; 3 4 #include <petscsys.h> 5 #include <signal.h> 6 7 typedef struct _handlerCtx { 8 int exitHandler; 9 int signum; 10 } HandlerCtx; 11 12 int handleSignal(int signum, void *ctx) { 13 HandlerCtx *user = (HandlerCtx *)ctx; 14 15 user->signum = signum; 16 if (signum == SIGHUP) user->exitHandler = 1; 17 return 0; 18 } 19 20 int main(int argc, char *args[]) { 21 HandlerCtx user; 22 23 user.exitHandler = 0; 24 25 PetscFunctionBeginUser; 26 PetscCall(PetscInitialize(&argc, &args, (char *)0, help)); 27 PetscCall(PetscPushSignalHandler(handleSignal, &user)); 28 while (!user.exitHandler) { 29 if (user.signum > 0) { 30 PetscCall(PetscPrintf(PETSC_COMM_SELF, "Caught signal %d\n", user.signum)); 31 user.signum = -1; 32 } 33 } 34 PetscCall(PetscPopSignalHandler()); 35 PetscCall(PetscFinalize()); 36 return 0; 37 } 38 39 /*TEST 40 41 build: 42 requires: !defined(PETSC_MISSING_SIGHUP) 43 44 test: 45 TODO: need to send a signal to the process to kill it from the test harness 46 47 TEST*/ 48