How to trigger SIGUSR1 and SIGUSR2?

haunted85 picture haunted85 · May 29, 2011 · Viewed 141.7k times · Source

I'm getting acquainted with signals in C. I can't figure out what kind of signals SIGUSR1 and SIGUSR2 are and how can I trigger them. Can anyone please explain it to me?

Answer

Oliver Charlesworth picture Oliver Charlesworth · May 29, 2011

They are user-defined signals, so they aren't triggered by any particular action. You can explicitly send them programmatically:

#include <signal.h>

kill(pid, SIGUSR1);

where pid is the process id of the receiving process. At the receiving end, you can register a signal handler for them:

#include <signal.h>

void my_handler(int signum)
{
    if (signum == SIGUSR1)
    {
        printf("Received SIGUSR1!\n");
    }
}

signal(SIGUSR1, my_handler);