czmafi00
czmafi00

Reputation: 31

Continuos Signal handle

I am building an algorithm for handling signal received from RS232 port. The basic idea is to receive a signal from RS232 and then grab image with camera.

Let's ignore camera and RS232 for now. My short program should enter the while loop, and ask for "cin" command. If command == 'b', signal should be raised and signal_handler() function activated. For now it works fine.

What I would like to do is change the program to get back into while loop and continue. My program is terminated after interupt.

When I remove exit(signum), program returns into while loop and ask for the command. And if I enter 'b' again Program ends.

Where did I go wrong? Wrong signal or entire entire approach?
Thanks!

void signalHandler(int signum) {
    cout << "Interrupt signal (" << signum << ") received.\n";
}

int main(){
    char Command = 'a';

    signal(SIGINT, signalHandler);

    while (true){
        cout << "Command: ";
        cin >> Command;
        if (Command == 'b'){
            raise(SIGINT);
        }
    }

    return 0;
}

Upvotes: 2

Views: 129

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409356

The problem you're having is because one the signal is caught, the signal handler is reset to its default, which in the case of SIGINT is to terminate the process.

You should be using sigaction instead, which lets you specify the behavior (check the section about the SA_RESETHAND flag).

Upvotes: 1

Related Questions