I want to run a section of my code, but with the option to interrupt it (ctrl-c) before it is fully completed, and resume execution of the rest of my code. (I work on the Linux platform.)
My guess is to create a fork, call a method, and then use signal processing. What steps are needed to process the signals?
void Manager::Run()
{
pid_t pID = fork();
if( pID<0 )
exit(1);
else if( pID==0 ) {
BuildList();
}
else {;}
waitpid(pID,NULL,0);
PrintList();
}
It looks like I would like to use a string similar to a signal (SIGINT, sigint) somewhere in the if / else part. And I would need to define a function like this:
sigint(int param){ signal(param, SIG_DFL);};
In addition, I only want to kill the child process.
Is this the right idea to solve my problem? If so, what signal processing is needed to make this work?
UPDATE:
, , forking. , . , . .
Manager.hh
static void sighandler(int signum)
{
PrintList();
exit(1);
};
.cc
void Manager::Run()
{
signal(SIGINT,sighandler);
BuildList();
signal(SIGINT,SIG_DFL);
PrintList();
}
sighandler , :
error: argument of type 'void (Manager::)(int)' does not match 'void (*)(int)'
signal(SIGINT,sighandler) :: (), .
PrintList() sighandler, :
error: cannot call member function 'void Manager::PrintList()' without object
PrintList(); sighandler().
, , PrintList() ( sighandler), .
error: invalid use of member 'Manager::theList' in static member function
error: invalid use of member 'Manager::it' in static member function
- ?