Signal parent process from child thread using C

For my master’s thesis I had to use signals with C language.

In my code, I used SIGUSR1 as the signal I wait for and the handle_my_custom_signal function which will execute once this signal is received in the parent process.

This piece of code allows you to put your program listening for a signal.

signal(SIGUSR1, handle_my_custom_signal);

This is how I trigger the signal:

raise(SIGUSR1);

SIGINT is used by Ctrl+C. So if you for this signal, your function will be triggered, when you do Ctrl+C.

There are two signals that can be used as we want, without having to waste the SIGINT that is used by Ctrl+C, such as SIGUSR1 and SIGUSR2. In this post you can see these and other miscellaneous signals.

This GitHub Gist has the main code of an experiment I did to try this out. You can see the full repository, which contains the Makefile for this: isabelcosta/testing-signals-with-c.

I actually used this on my Master project, more specifically this line here.