How to use "sigaction" in Qt?
-
@fd = open("/dev/hpi0",O_RDONLY);
sigaction(SIGIO, sig_handler, NULL);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC);@
This can't work.
How to use "sigaction" in -QT- Qt? Or, other method to achieve "soft interrupt"?
Thanks! -
Maybe this? (it does work for SIGSEGV and SIGINT, I often get those)
@
struct sigaction act, oact;
memset((void*)&act, 0, sizeof(struct sigaction));
memset((void*)&oact, 0, sizeof(struct sigaction));
act.sa_flags = 0;
act.sa_handler = &signalHandler;
sigaction(SIGINT, &act, &oact);
sigaction(SIGKILL, &act, &oact);
sigaction(SIGQUIT, &act, &oact);
sigaction(SIGSTOP, &act, &oact);
sigaction(SIGTERM, &act, &oact);@(...)
@
void signalHandler(int signal)
{
//print incoming signal
switch(signal){
case SIGINT: fprintf(stderr, "SIGINT => "); break;
case SIGKILL: fprintf(stderr, "SIGKILL => "); break;
case SIGQUIT: fprintf(stderr, "SIGQUIT => "); break;
case SIGSTOP: fprintf(stderr, "SIGSTOP => "); break;
case SIGTERM: fprintf(stderr, "SIGTERM => "); break;
case SIGSEGV: fprintf(stderr, "SIGSEGV => "); break;
default: fprintf(stderr, "APPLICATION EXITING => "); break;
}//...
}
@Hope it helps!!
--
-
@class MainWindow : public QMainWindow
{
Q_OBJECT
private slots:
void sigterm_handler(int);
};MainWindow::MainWindow(QWidget parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
struct sigaction action;
fd = open("/dev/hpi0",O_RDONLY);
memset(&action, 0, sizeof(action));
action.sa_handler = sigterm_handler;
action.sa_flags = 0;
sigaction(SIGIO, &action, NULL);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC);
}@
error at "action.sa_handler = sigterm_handler;"
message: error: argument of type 'void (MainWindow::)(int)' does not match 'void ()(int)' -
man sigaction
--
-
Hi,
It's not possible to directly call a non static member function of class through C style signal handler. Try to delegate task of handling signal to an instance of class using a static member function. See code below for example:
@
#include <iostream>
#include <signal.h>using namespace std;
class MyClass {
static MyClass* m;public:
void handleSignal(int num){
cout<<"Signal handled: "<<num<<endl;
}static void setSignalHandlerObject(MyClass* mc) {
m = mc;
}static void callSignalHandler(int num){
m->handleSignal(num);
}
};MyClass* MyClass::m = NULL;
int main(){
signal(SIGINT,MyClass::callSignalHandler);
while(1);
}
@ -
Use the Self Pipe Trick.
http://doc.qt.nokia.com/4.7/unix-signals.html
Apart from this, UNIX signal handlers must be ordinary C functions (in C++ you'll need the extern C linkage).
-
This example explain how to use SIGINT (terminate app using Ctrl+C).