[petsc-users] Help with PETSc signal handling

Gard Spreemann gspr at nonempty.org
Tue Nov 7 06:17:23 CST 2017


Hello list,

I seem to be misunderstanding how to install a signal handler with
PETSc. If I send the USR1 signal to a program with the below code, it
exits with a non-zero exit code as if no signal handler were
intalled. I'd appreciate if someone could shed some light on the
matter.

***

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <petscmat.h>
#include <petscsys.h>

PetscErrorCode handler(int signum, void * ctx)
{
  if (signum == SIGUSR1)
  {
    *((sig_atomic_t *) ctx) = 1;
  }
  return 0;
}


int main(int argc, char ** argv)
{
  printf("I am %lu, hear me roar.\n", getpid());

  PetscInitialize(&argc, &argv, NULL, NULL); 
  
  sig_atomic_t flag = 0;
  PetscErrorCode err;
  err = PetscPopSignalHandler(); CHKERRQ(err);
  err = PetscPushSignalHandler(handler, (void *)&flag); CHKERRQ(err);

  for (int i = 0; i < 20; i++)
  {
    printf("I'm awake. Did anything happen?\n");
    if (flag)
    {
      printf("YES!\n");
      return 0;
    }
    else
      printf("No...\n");

    err = PetscSleep(5); CHKERRQ(err);
  }

  PetscFinalize();
  return 0;
}


***


Best regards,
Gard Spreemann



More information about the petsc-users mailing list