Pithikos
Pithikos

Reputation: 20300

Is there a way to sigaction() to a signal handler with multiple parameters?

I am using sigaction() to perform an action every time SIGINT is received. All tutorials I have seen use this prototype as a signal handler

void sig_handler(int sig);

Is there a way somehow to make this to take more parameters so it suits my needs? So for example

void sig_handler(char* surname, int age);

This is my code:

void sig_handler(int sig) {
    printf("SIGINT(%d) received\n", sig);
}

int main( ){
    struct sigaction act;
    act.sa_handler=sig_handler;

    sigaction(SIGINT, &act, NULL);

    while(1){};
    return 0 ;
}

Upvotes: 2

Views: 4465

Answers (2)

LowTechGeek
LowTechGeek

Reputation: 441

Not directly, but you could set a global variable that tells your sig_handler() what to do.

int ACTION = 0;

void sig_handler(int sig) {
    if (sig == SIGINT) {
        switch (ACTION) {
          case 0: other_function(char* surname, int age);
          break;
        // more cases
        default:
          ;
        }
    } else if ( ....  // more signals
    }
}

int main( ){
    struct sigaction act;
    act.sa_handler=sig_handler;

    sigaction(SIGINT, &act, NULL);

    while(1){};
    return 0 ;
}

Upvotes: 2

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43508

You can't do this with signals. How would these parameters be supplied and delivered? A signal is just a predefined numeric code that causes the process to execute the handler asynchronously, by interrupting the main flow.

You can, however, use a Unix socket, a pipe, or a fifo file for this.

Upvotes: 1

Related Questions