- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的目标:我有一个简单的 c 程序,它应该用我自己的覆盖默认的 SIGTSTP 处理程序,并且只向子进程发送 SIGTSTP。
我的问题:SIGTSTP 处理程序中的 kill 调用会停止父进程,并退出我的程序(不仅仅是子进程)。我做错了什么?
编辑: 这个问题似乎只有在我使用以下 make 命令编译和运行代码时才会发生:gcc -o program *.c -lreadline && ./program
。似乎(make
过程?)已终止,因为我的输出包含 ctrl-z 时的以下行:gcc -o sandbox *.c -lreadline && ./sandbox
有没有办法既让我的程序具有所需的功能又使用 make?
我的代码:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <readline/readline.h>
int child;
void handler();
static void SIGTSTP_Handler()
{
if (child != 0) {
kill(child, SIGTSTP);
}
}
int main(void)
{
signal(SIGTSTP, SIGTSTP_Handler);
child = fork();
if (child == 0) {
setpgid(0, getpid());
printf("CHILD's PID ::: [ %d ]\n", getpid());
printf("CHILD's GROUP ::: %d\n", getpgrp());
execlp("sleep", "sleep", "30", NULL);
}
else {
setpgid(child, child);
int status;
printf("CHILD's PID (From Parent Perspective) ::: [ %d ]\n", child);
printf("PARENT's PID ::: %d\n", getpid());
printf("PARENT's GROUP ::: %d\n", getpgrp());
waitpid(child, &status, WUNTRACED | WCONTINUED);
}
while (1);
}
最佳答案
这个问题是因为我启动程序时使用的 make
命令被 ctrl-z
终止了。要解决此问题,您可以:
老有问题的 Make 命令:
gcc -o program *.c && ./program
可能的解决方案:
(1) 从 make 命令中删除 && ./program
行
(2) 在不使用 make 的情况下编译并运行你的程序
如果您希望在 SIGTSTP
信号的情况下保持主程序运行,我不确定是否仍然可以使用 make 启动程序
关于c - SIGTSTP 处理程序终止父进程和子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52394094/
我的目标:我有一个简单的 c 程序,它应该用我自己的覆盖默认的 SIGTSTP 处理程序,并且只向子进程发送 SIGTSTP。 我的问题:SIGTSTP 处理程序中的 kill 调用会停止父进程,并退
我正在尝试为正在运行的前台进程安装 CTRL-Z (SIGTSTP) 处理程序。 我在 sigaction 之前设置了处理程序 ( wait )在父级。这是正确的地方吗?好像不太行。。 编辑: 我正在
我用 C 写了一个程序,我需要处理 ctrl + z 和相应的信号 SIGTSTP 和 SIGCONT。 接收到信号后我的变量和进程会发生什么变化? 通过信号处理程序 signal(SIGCONT,
我正在尝试编写一个 fork 的程序,子进程执行一个命令,然后将控制权返回给父进程。不过,我无法让 SIGTSTP (C-z) 信号按预期工作......我希望 parent 忽略它,但 child
我正在研究一个问题,以查找给定链表中循环(如果有)中存在的节点数。下面是接受节点头的函数,使用 Floyds 循环算法检查循环,如果找到,则给出循环中的节点数。在运行程序时,它会给出运行时错误 SIG
我正在尝试向子进程发送 SIGTSTP 信号。我面临的问题是向子进程发送 SIGTSTP 会停止我的整个程序,并且调用者无法继续执行程序的其余部分。这是我的代码 cmd := exec.Command
所以我正在尝试在子进程中为 SIGTSTP 信号实现信号处理程序。 基本上我想要实现的是: 启动子进程 让父进程等待子进程 在子进程上调用 Sleep x 秒。 在 sleep 结束执行之前,我想发送
只是想知道 SIGSTOP 和 SIGTSTP 信号之间的区别。 最佳答案 这两个信号都旨在挂起一个进程,该进程最终将通过 SIGCONT 恢复。它们之间的主要区别是: SIGSTOP 是以编程方式发
我用 C 编写了一个 shell,它确实具有一些基本功能。我在我的程序中实现了“仅前景模式”,该模式通过 SIGTSTP 或 CTRL-Z 进行切换。但是,我能够捕获 CTRL-Z 信号,但不能捕获
我对它们中的每一个都有两个处理程序(SIGTSTP、SIGCHLD),问题是当我使用 SIGTSTP 暂停进程时,SIGCHLD 的处理函数也会运行。我应该怎么做才能防止这种情况发生。 信号处理程序:
我正在制作一个小的shell程序。我想在sigtstp_handler中暂停前台程序。和sigchld_handler来获取zombie 。但是当我输入 ctrl z 时,即使我没有前台程序,sigc
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is EOF in the C programming language? 当我学习 K&R C
我在学习中遇到了这个示例代码: #include #include #include char* nums[5] = {"One", "Two", "Three", "Four", "Five"
简而言之,问题陈述:LSF 中是否有一种方法可以将信号 SIGCONT/SIGTSTP 传递给作业中运行的所有进程? 我有一个在 LSF(版本 9.1.2)上运行的 Perl 包装器脚本,并在与 Pe
我正在用 C 编写一个基本的 shell,我现在正致力于暂停一个子进程。 我认为我的信号处理程序是正确的,我的子进程正在挂起,但在那之后,终端应该返回到父进程,而这并没有发生。 child 被挂起,但
我正在用 C 编写一个基本的 shell,我现在正致力于暂停一个子进程。 我认为我的信号处理程序是正确的,我的子进程正在挂起,但在那之后,终端应该返回到父进程,而这并没有发生。 child 被挂起,但
我在 gdb 中运行 vlc 并使用 ^Z 中断它(因为 ^C 不起作用)。在检查了一些堆栈帧和变量后,我尝试使用“cont”来恢复程序,但它一直给我 Program received signa
在 Linux 中有两种不同的信号可用于暂停进程,SIGSTOP 和 SIGTSTP。两者均未由 HotSpot 虚拟机 处理,SIGSTOP 因为无法捕获和SIGTSTP because is no
我有一个安装和卸载设备的 bash 脚本,它在两者之间执行一些读取操作。由于设备非常慢,脚本大约需要 15 秒才能完成(挂载至少需要 5-6 秒)。由于挂载此设备会导致其他问题,因此我不希望此脚本被中
我正在赶时间完成一项编程任务,在这项任务中,我必须同时在同一台机器上运行同一程序的多个实例。目前,我一次启动一个实例,按 Ctrl+z 暂停它们,然后执行 'bg %#' 以在后台恢复执行。 每次我需
我是一名优秀的程序员,十分优秀!