暂停与SIGTSTP子进程后,外壳没有响应 [英] After suspending child process with SIGTSTP, shell not responding

查看:241
本文介绍了暂停与SIGTSTP子进程后,外壳没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码在C基本外壳,而我的工作,现在暂停子进程。

I'm coding a basic shell in C, and I'm working on suspending a child process right now.

我觉得我的信号处理程序是正确的,我的子进程暂停,但在这之后,终端应该返回到父进程而这没有发生。

I think my signal handler is correct, and my child process is suspending, but after that, the terminal should return to the parent process and that's not happening.

孩子被暂停,但我的壳没有注册任何输入或输出了。 tcsetpgrp()似乎并没有得到帮助。

The child is suspended, but my shell isn't registering any input or output anymore. tcsetpgrp() doesn't seem to be helping.

下面是我的信号处理程序在我的壳code表示SIGTSTP:

Here's my signal handler in my shell code for 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);
}

谁能告诉我什么,我做错了吗?

Can anyone tell me what I'm doing wrong?

我是否中止我与孩子一起外壳,和我需要stdin和stdout返回莫名其妙的壳呢?我会怎么做呢?

Am I suspending my shell along with the child, and do I need to return stdin and stdout to the shell somehow? How would I do this?

谢谢!

推荐答案

tcsetpgrp 是指定什么是前台作业。当你的shell产卵在前台工作(不包括&安培; ),它应该创建一个新的进程组,使该控制终端的前台工作(而不是无论是在STDIN )。然后,在pressing CTRL-Z,这项工作将得到三偏磷酸钠。就这么暂停工作,而不是你的shell终端。你的shell不应陷阱三偏磷酸钠或三偏磷酸钠发送给任何人。

tcsetpgrp is to specify what is the foreground job. When your shell spawns a job in foreground (without &), it should create a new process group and make that the foreground job (of the controlling terminal, not whatever's on STDIN). Then, upon pressing CTRL-Z, that job will get the TSTP. It's the terminal that suspends the job, not your shell. Your shell shouldn't trap TSTP or send TSTP to anyone.

这只是应等待()它催生,当它已经停止检测(和索回前台组和内部暂停标记作业)工作。你的 FG 命令将使作业的再次PGID前台进程组和发送 SIGCONT 来并再次等待它,而 BG 将只发送 SIGCONT

It should just wait() for the job it has spawned and detect when it has been stopped (and claim back the foreground group and mark the job as suspended internally). Your fg command would make the job's pgid the foreground process group again and send a SIGCONT to it and wait for it again, while bg would just send the SIGCONT

这篇关于暂停与SIGTSTP子进程后,外壳没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆