简体   繁体   English

如何正确向子进程发送 SIGTSTP 信号?

[英]How to correctly send SIGTSTP signal to a child process?

I am writing a custom shell program for an assignment where I do fork() and exec().我正在编写一个自定义 shell 程序,用于执行 fork() 和 exec() 的任务。 What I want to do is handle the signal SIGTSPS so that it suspends the child process.我想要做的是处理信号 SIGTSPS 以便它挂起子进程。

I am currently handling crtl z with我目前正在处理 crtl z

 void SIGTSTP_Handler(){

if (child != 0) {
    kill(child, SIGTSTP);
    }
}

and my parent process waitpid with和我的父进程waitpid

if (waitpid(child, &status, WUNTRACED |WCONTINUED ) == -1) {
        err(-1, "Failed to waitpid()");
    }
if(WIFSTOPPED(status))
{

    printf("child stop with code: %d\n", WIFSTOPPED(status));
}

when I press ctrl Z my process stop but it exit with code 1. which means error.当我按 ctrl Z 时,我的进程停止但它以代码 1 退出。这意味着错误。 I want to be able to suspend the child process and resume it if i pass it a CONT signal.如果我向它传递一个 CONT 信号,我希望能够暂停子进程并恢复它。 How do i properly handthe ctrl Z so that it suspend the child process and if I input fg it will put the suspend process back into running?我如何正确处理 ctrl Z 以便它暂停子进程,如果我输入 fg 它将使暂停进程重新运行?

sleep 10
PID 71740: I must be the parent!
^Z
71740child stop with code: 1

Your child process is stopped, not exited.您的子进程已停止,而不是退出。 If it had exited, WIFSTOPPED(status) would be 0 and WIFEXITED(status) would be nonzero.如果它已退出,则 WIFSTOPPED(status) 将为 0,WIFEXITED(status) 将为非零。

To continue the process, when your fg command is executed, send the SIGCONT signal to the child:要继续该过程,请在执行fg命令时向子进程发送SIGCONT信号:

kill(child, SIGCONT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM