mardi 17 novembre 2020

signal handler doesn't work - signal(SIGINT, handler) - Linux, C++11

I'm trying to build a small shell using c++11 on Linux Ubuntu 18 VM.

void ctrlCHandler(int sig_num) {
  cout << "smash: got ctrl-C" <<endl;
}

int main(int argc, char* argv[]) {
    if(signal(SIGTSTP , ctrlZHandler)==SIG_ERR) {
        perror("smash error: failed to set ctrl-Z handler");
    }
    if(signal(SIGINT , ctrlCHandler)==SIG_ERR) {
        perror("smash error: failed to set ctrl-C handler");
    }
    

    SmallShell& smash = SmallShell::getInstance();
    while(true) {
        std::cout << smash.promptName  << "> "; 
        std::string cmd_line;
        std::getline(std::cin, cmd_line);
        smash.executeCommand(cmd_line.c_str());
    }
    return 0;

When I try to run a command like 'sleep 10' for example and then click ctrl+C, the output of the signal handler is not being executed and the shell itself is being terminated.

I also try using sigaction(), unfortunately, it didn't work as well.

Aucun commentaire:

Enregistrer un commentaire