我为进程写了一个信号处理程序,然后fork(),信号处理程序将应用于父进程和子进程.如果我用"exec"替换子进程,则信号处理程序不再存在.
我知道发生这种情况是因为"exec"调用将使用它自己覆盖子进程地址空间.我只是想知道即使在"exec"调用之后是否有办法让信号处理程序工作?
我正在用C编写一个基本shell,我正在努力暂停子进程.
我认为我的信号处理程序是正确的,并且我的子进程正在挂起,但在此之后,终端应该返回到父进程并且没有发生.
孩子被暂停,但我的shell没有注册任何输入或输出.tcsetpgrp()似乎没有帮助.
这是我的SIGTSTP shell代码中的信号处理程序:
void suspend(int sig) {
pid_t pid;
sigset_t mask;
//mpid is the pgid of this shell.
tcsetpgrp(STDIN_FILENO, mpid);
tcsetpgrp(STDOUT_FILENO, mpid);
sigemptyset(&mask);
sigaddset(&mask, SIGTSTP);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
signal(SIGTSTP, SIG_DFL);
//active.pid is the pid of the child currently in the fg.
if (active.pid != 0) {
kill(active.pid, SIGTSTP);
}
else{
//if this code is being run in the child, child calls SIGTSTP on itself.
pid = getpid();
if (pid != 0 && pid != mpid){
kill(pid, SIGTSTP);
} …Run Code Online (Sandbox Code Playgroud)