我在UNIX shell上写作。
按下CTRL-C
时,会发送SIGINT
信号。 (工作中!)
但是当按下CTRL-Z
时,获取信号的进程会停止,但我无法返回到我的shell。只有当我关闭这个过程时,我才能回来。
这是我的signal_handler()
:
// in the main()
signal(SIGINT, signal_handler);
signal(SIGTSTP, signal_handler);
// when pid == 0, right over execvp()
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
// signal handler
void signal_handler(int signum) {
switch(signum) {
case SIGINT:
cout << "[caught SIGINT]" << endl;
kill(pid, SIGINT);
break;
case SIGTSTP:
cout << "[caught SIGTSTP]" << endl;
kill(pid, SIGTSTP);
break;
default:
break;
}
}
pid_t pid; // is a global variable
// this would be my main()
if((pid = fork()) < 0) {
cout << "Error" << endl;
exit(1);
} else if(pid == 0) { // child
setpgid(0,0);
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
execvp(arguments[0], &arguments[0]);
cout << "Argument not found" << endl;
exit(1);
} else if(checkIfBackground(inputNew) == false) { // mother
int status;
pid_t pid_r;
if(waitpid(pid, &status, 0) < 0) {
cout << "Error" << endl;
}
} else {
cout << "Process is in background" << endl;
}
答案 0 :(得分:0)
您需要检查孩子的状态,看看他们何时停在UNTRACED
标志处:
pid_t child = waitpid(-1, &status, WUNTRACED | WCONTINUED);
if (WIFSTOPPED(status))
... the child has stopped
else if (WIFCONTINUED(status))
... the child has been continued
else if (WIFEXITED(status))
... the child has exited with exit
else if (WIFSIGNALLED(status))
... the child exited with a signal
请注意,如果您不关心切换到运行状态(WIFCONTINUED
情况),则不需要WCONTINUED
标记。