简体   繁体   中英

Using signal, handler, kill, and getpid in C

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;
}

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.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM