2

I am a beginner in C and system programming. I wrote a program and it should display the following: Caught SIGUSR1 Caught SIGUSR2 Caught SIGINT

However, when I do "./test.c", the only thing I see is "Caught SIGINT" when I type Ctrl-C. How can I fix my code so my program displays the messages above? Sorry if my question is dumb. Your help is greatly appreciated. Thanks for reading.

EDITED:

#include <signal.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

static void sigHandler_sigusr1(int sig)
{
    //sig contains the signal number that was received
    printf("Caught SIGUSR1, %d\n", getpid());
    //kill(getpid(), SIGUSR1);
}

static void sigHandler_sigusr2(int sig)
{
    //sig contains the signal number that was received
    printf("Caught SIGSR2, %d\n", getpid());
    //kill(getpid(), SIGUSR2);
}

static void sigHandler_sigint(int sig)
{
    //sig contains the signal number that was received
    printf("Caught SIGINT, Existing, %d\n", getpid());
    //kill(getpid(), SIGINT);
    exit(EXIT_SUCCESS);
}

int main(int argc, char *argv[])
{

    if (signal(SIGUSR1, sigHandler_sigusr1) == SIG_ERR)
        printf("Unable to create handler for SIGUSR1\n");

    if (signal(SIGUSR2, sigHandler_sigusr2) == SIG_ERR)
        printf("Unable to create handler for SIGUSR2\n");

    if (signal(SIGINT, sigHandler_sigint) == SIG_ERR)
        printf("Unable to create handler for SIGINT\n");

    kill(getpid(), SIGUSR1);
    kill(getpid(), SIGUSR2);
    kill(getpid(), SIGINT);

    while (1)
    {
        sleep(1);
    }

    return 0;
}
user2203774
  • 609
  • 4
  • 13
  • 25
  • 1
    my suggestion is [avoid using printf in a signal handler](http://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler/16891065#16891065) – Grijesh Chauhan Jul 17 '13 at 14:48

3 Answers3

2

In order to active the signal handler function you need to send signal to the procces. It is missing from your code.

This is how you send signal to yourself:

kill(getpid(), SIGUSR1);

You need to do it for SIGUSR1 and SIGUSR2.

The reason you can see SIGINT message is that when you press ctrl+c you actually sending a SIGINT singal to your procces.

Ran Eldan
  • 1,350
  • 11
  • 25
  • Hi. Does that mean I should put kill(getpid(), SIGUSR1) in my main method? – user2203774 Jul 17 '13 at 15:02
  • Yes. Also `kill(getpid(), SIGUSR2);` in order to active both. And if you want to make `SIGINT` without pressing `crtl+c` you should do `kill(getpid(), SIGINT);` – Ran Eldan Jul 17 '13 at 15:05
  • Hi. I just try that. It doesn't work. I'll post my new code here. Do you mind to take a look and let me know what I'm missing? Thanks for your time. – user2203774 Jul 17 '13 at 15:08
1

I can be wrong, but SIGUSR1 and SIGUSR2 are user specified signals.

When you do "Ctrl-C", you do an interrupt, caught by SIGINT handler.

In order to caught SIGUSR1 ans SIGUSR2 you have to throw them yourself :

kill(pid, SIGUSR1);

More informations

Community
  • 1
  • 1
Flaëndil
  • 118
  • 6
1

That all looks good.

With SIGUSR's you have to explicitly call them in the program. You can't trigger them with ctrl-z or ctrl-c.

You didn't show any code where you try and trigger the signals.

Scotty Bauer
  • 1,277
  • 9
  • 14