I know how to use it in C (with signal.h), but the <csignal>
library is provided in C++ and I want to know if it includes sigaction? I tried running it but it said not found. I was wondering if I did something wrong?
#include <iostream>
#include <string>
#include <cstdio>
#include <csignal>
namespace {
volatile bool quitok = false;
void handle_break(int a) {
if (a == SIGINT) quitok = true;
}
std::sigaction sigbreak;
sigbreak.sa_handler = &handle_break;
sigbreak.sa_mask = 0;
sigbreak.sa_flags = 0;
if (std::sigaction(SIGINT, &sigbreak, NULL) != 0) std::perror("sigaction");
}
int main () {
std::string line = "";
while (!::quitok) {
std::getline(std::cin, line);
std::cout << line << std::endl;
}
}
But for some reason it doesn't work. EDIT: By "doesn't work", I mean the compiler fails and says there's no std::sigaction function or struct.
sigaction is C POSIX isn't it?
volatile bool
may not be good enough. More on that: set flag in signal handler – Emulsify