gpt4 book ai didi

c - SIGTSTP 和 SIGCHLD 之间的关系是什么

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:47 28 4
gpt4 key购买 nike

我对它们中的每一个都有两个处理程序(SIGTSTP、SIGCHLD),问题是当我使用 SIGTSTP 暂停进程时,SIGCHLD 的处理函数也会运行。我应该怎么做才能防止这种情况发生。

信号处理程序:

void signalHandler(int signal) {
int pid, cstatus;
if (signal == SIGCHLD) {
susp = 0;
pid = waitpid(-1, &cstatus, WNOHANG);
printf("[[child %d terminated]]\n", pid);
DelPID(&JobsList, pid);
}
}

void ctrlZsignal(int signal){
kill(Susp_Bg_Pid, SIGTSTP);
susp = 0;
printf("\nchild %d suspended\n", Susp_Bg_Pid);
}

Susp_Bg_Pid 用来保存暂停的进程id。
susp 指示“粉碎”父进程是否挂起的状态。

最佳答案

使用 sigactionSA_NOCLDSTOP 设置您的 SIGCHLD 处理程序。

来自sigaction(2)

SA_NOCLDSTOP - If signum is SIGCHLD, do not receive notification when child processes stop (i.e., when they receive one of SIGSTOP, SIGTSTP, SIGTTIN or SIGTTOU) or resume (i.e., they receive SIGCONT)(see wait(2)). This flag is only meaningful when establishing a handler for SIGCHLD.

更新

void signalHandler(int sig)
{
//...
}

struct sigaction act;

act.sa_handler = signalHandler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NOCLDSTOP;

if (sigaction(SIGCHLD, &act, 0) == -1)
{
perror("sigaction");
exit(1);
}

如果您不熟悉 sigaction,您应该仔细阅读它,因为它有几个不同的选项和行为,这些选项和行为远远优于 signal,但要付出代价在弄清楚如何使用它之前,先了解复杂性和混淆性。我对你似乎想做的事情做了我最好的猜测,但你需要尽快了解这一点。

关于c - SIGTSTP 和 SIGCHLD 之间的关系是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19883512/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com