1

I'm learning C and system programming. I need to create three signal handlers and once they are created, the program should send the signals to itself in the following order: SIGUSR1, SIGUSR2, and SIGINT. Below is my code. It is not working (for example, when I compile in the terminal - I type ./prog1 and click control-C, the program should print "Caught SIGINT, Exiting" and exit. I did not get any errors, but it just returns to the next line with nothing. Can someone take a look and point me to the right direction? Thank you so much for your help!

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

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

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

static void sigHandler_sigint(int sig)
{
    //sig contains the signal number that was received
    printf("Caught SIGINT, Exiting\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 SIGUSR1\n");

    return 0;
}
Casey
  • 41,449
  • 7
  • 95
  • 125
user2203774
  • 609
  • 4
  • 13
  • 25
  • 1
    did you try to put a while(1); before the return ? – Alexis Jul 17 '13 at 06:07
  • 1
    And also add %d for printing getpid(). – Jeyaram Jul 17 '13 at 06:09
  • "*... program should send the signals to itself in the following order: ...*" to have it perform like this `main()` should do something to initiate this, currently it doesn't. – alk Jul 17 '13 at 11:24

2 Answers2

1

Your main program did not wait to accept the signal. what happen is main program started and check if condition and immediately return 0;

For understanding

enclose while(1) in main program, and now hit ctr+C. You will get the expected output.

#include <signal.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
static void sigHandler_sigusr1(int sig)
{
    //sig contains the signal number that was received
    printf("Caught SIGUSR1\n");
    kill(getpid(), SIGUSR1);
}
static void sigHandler_sigusr2(int sig)
{
    //sig contains the signal number that was received
    printf("Caught SIGSR2\n");
    kill(getpid(), SIGUSR2);
}
static void sigHandler_sigint(int sig)
{
    //sig contains the signal number that was received
    printf("Caught SIGINT, Existing\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 SIGUSR1\n");
        while(1){
         /*some work*/
        }

    return 0;
}

Don't use printf within signal handler. see this post for further details.

Community
  • 1
  • 1
sujin
  • 2,813
  • 2
  • 21
  • 33
1

Modified main() will works.

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 SIGUSR1\n");

   while(1)
   {
         sleep(1);
   }

    return 0;
}

In your code, after registering signal handler, execution is completed. while(1) is added to keep your code running, so when CTRL + C is pressed handler will be invoked.

Also add #include <unistd.h> for getpid() function. And add %d in

printf("Caught SIGSR2\n", getpid());
Jeyaram
  • 9,158
  • 7
  • 41
  • 63