我正在用 C 编写一个基本的 shell,我现在正在暂停一个子进程。
应该有两种方法可以做到这一点。
一种是当我
ps&
输入输入&
时,最后用 a 调用kill
(pid
,child_pid
),然后我将孩子pid
放入进程数组中bg
。第二个是当我按CTRL-Z在当前正在运行的子进程上。
我认为我的信号处理程序是正确的,并且我的子进程正在挂起,但是在那之后,终端应该返回到父进程并且没有发生这种情况。
孩子被挂起,但我的外壳不再注册任何输入或输出。tcsetpgrp()
似乎没有帮助。
这是我的 shell 代码中的信号处理程序SIGTSTP
:
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);
}
}
signal(SIGTSTP, suspend);
}
谁能告诉我我做错了什么?
我是否将我的贝壳与孩子一起悬挂,我是否需要以某种方式返回stdin
贝壳stdout
?我该怎么做?
谢谢!