[TriEmbed] catching SIGINT/SIGHUP

Pete Soper pete at soper.us
Mon Sep 28 11:27:26 CDT 2015


This code looks fine to me. There are a few other signals that might 
nail the process, but isn't clear they would be possible for your 
situation. But it would be harmless to set them up. I'm thinking of 
SIGHUP, SIGQUIT, and (with gross program misbehavior) SIGILL, ABRT, and 
SEGV. The one last detail is to tell everybody using this program to 
*not* do a "kill -9" on the process, as this will blow it away without 
it being able to catch the signal (it is uncatchable).

-Pete

On 09/28/2015 11:55 AM, Alex Davis via TriEmbed wrote:
> Anyone have experience with unix c/c++ coding and catching interrupt
> signals in the code?
>
> In my instance, I am using the rpi-rgb-led-matrix (led-matrix.h) library
> on a Pi2. You don't want to just exit a program using it, as you
> 'freeze' the matrix and it ends up at the last line scanned looking very
> bright in a way which must be overdriving the LEDs.
>
> I'm using LIRC to fire off different matrix utilities and sometimes I've
> got to to kill one before running another.
>
> I'm no unix code expert, but I found an example using <signal.h> which
> at least works. Here's a simple example:
>
> #include <signal.h>
>
> volatile bool interrupt_received = false;
> static void InterruptHandler(int signo) {
>      interrupt_received = true;
> }
>
> int main(int argc, char *argv[]) {
>
>      signal(SIGTERM, InterruptHandler);
>      signal(SIGINT, InterruptHandler);
>
>      while (!interrupt_received)
>      {
>          // do your rgb_matrix::DrawText(canvas, font, x, y +
>          font.baseline(), color, line) here
>          // or whatever
>      }
>      fprintf(stderr, "Caught signal. Exiting.\n");
>
>      // shut down the matrix
>      // or whatever your cleanup tasks need be
>      matrix->Clear();
>      delete matrix
>
>      exit(0);
>      return 0;
> }
>
> Is there a better way to do this? I think this is the easiest/laziest
> but I'm open to doing it better.
>





More information about the TriEmbed mailing list