#include <signal.h>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
void sigHandler(int sigNum){
cout << "\nSignal has been sent." << endl;
}
int main(){
pid_t PID = fork();
if(PID == -1){
perror("fork");
exit(EXIT_FAILURE);
}
if(PID == 0){
cout << "\nChild will now send a signal to the parent process." << endl;
kill(getppid(), SIGINT);
cout << "\nChild will now send a 2nd signal." << endl;
kill(getppid(), SIGINT);
cout << "\nChild will now send a 3rd signal." << endl;
kill(getppid(), SIGINT);
}else{
struct sigaction action;
action.sa_handler = sigHandler;
sigemptyset(&action.sa_mask);
int checkErr = sigaction(SIGINT, &action, NULL);
if(checkErr == -1){
perror("sigaction");
exit(EXIT_FAILURE);
}
wait(NULL);
cout << "\nParent process is now finishing." << endl;
}
}
对于这个特定的程序,我试图从子进程向父进程发送三次信号;但是,当我多次调用kill()时,我的VM会重新启动。
有没有人有解决方案?
谢谢。