- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的示例代码
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
id_t pid;
void handle_sigterm(int sig)
{
printf("handle me \n");
}
void forkexample()
{
// child process because return value zero
pid = fork();
int status = 0;
if (pid == 0)
{
printf("Hello from Child!\n");
char *newargv[] = { "test2", NULL };
char *newenviron[] = { NULL };
newargv[0] = "test2";
execve("test2", newargv, newenviron);
printf("error -> %d", errno);
fflush(stdout);
}
// parent process because return value non-zero.
else
{
struct sigaction psa;
psa.sa_handler = handle_sigterm;
sigaction(SIGTERM, &psa, NULL);
printf("Hello from Parent!\n");
fflush(stdout);
int result = waitpid(pid, &status, 0);
printf("result -> %d\n", result);
fflush(stdout);
}
}
int main()
{
printf("pid -> %d\n", getpid());
forkexample();
return 0;
}
while(true)
。可以说父子进程同时都接收
SIGTERM
,如何让父进程等到子进程终止然后退出呢?我从
documentation读到:
The wait() function shall cause the calling thread to become blocked until status information generated by child process termination is made available to the thread, or until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process
SIGTERM
时,它将退出
wait()
并杀死进程。但是我希望它等到 child 终止然后退出。我该如何实现?
最佳答案
您也可以使用waitpid()等待父级信号处理程序中的子级。这样可以确保即使 parent 接收到信号, parent 也将等待 child 。一些建议如下。
关于c++ - C++:使用waitpid时等待子进程终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61950473/
我正在使用 C 语言开发一个基于 ncurses 的文件管理器。问题是一些子进程可能需要一些时间才能完成,直到发生这种情况,它会因为 waitpid 而卡住。 我不能使用 WNOHANG 标志,因为下
我写了代码来按顺序打印pid parent->g3->c2->g1->g2->c1 . 所以我用了 wait() , 和 waitpid() .但我失败了。 所以我写了“完成”代码来知道什么是问题。
#include #include #include int main() { int status; int pid = fork();assigned to variab
我的管道有问题。我浏览了这些主题,但没有找到任何可以解决我的问题的内容。 我的管道工作正常,但我想知道我的 child 何时终止。所以我想用waitpid来检查我的 child 。但这是行不通的。 我
我有一个函数可以启动一个进程,然后返回标准输出和退出代码。但是我注意到它声称每个进程都返回 1 的退出代码。我控制被调用的可执行文件并将它打印到标准输出退出代码,所以我已经确认当它“失败”时,它实际上
我需要在 Linux 下使用 C 模拟以下 bash 命令(使用 fork、exec、kill、signal、wait、waitpid、dup2、open、sleep、pipe 等)。 [0] ech
我有一个简单的功能 - 它的目的是在覆盖之前将文件复制到 .old。因为我很懒惰(这里的答案是这样建议的)我 fork 并使用 cp 来完成这项工作。 然后我调用 waitpid 并检查返回码。 调用
我正在使用 waitpid(2) 检查和标记作业控制程序的进程状态。我正在使用 WUNTRACED 选项,以在作业控制程序中捕获有用的信号,如 SIGTSTP。 问题是,当 CTRL-Z (SIGTS
我在我的代码中执行以下步骤: fork() execv 在子进程中运行一个外部脚本 在父进程中: While( waitpid(..., WNOHANG) == 0) { //Send
我想知道你是否可以更改参数waitpid() 目前我需要连续变量输出 ( 0.50 ) 来打印。但是,考虑到 waitpid() 在我尝试打印输出时只接受整数,它会给我 0。不确定如何解决这个问题,或
我试图从父进程访问 fork 子进程的退出状态,但我得到了奇怪的答案。有时,我得到 256,有时我得到一个负数,这取决于我指定的是 &status 还是仅指定 status。我知道我真的很接近。有人可
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我有一个父进程和一个子进程通过管道进行通信。 parent 写, child 读。而且,一旦 child 读完了,它就会对读过的数据做一些处理。我需要确保父级等到子级完成处理此数据(而不是终止)。 我
我正在使用 execv 启动一个进程并让它写入一个文件。我同时启动了一个线程来监视文件,以便它的大小不超过使用 stat.st_size 的特定限制。现在,当达到限制时,我为子进程 waitpid ,
我对 waitpid 参数有疑问。我应该在 p[0] 完成后开始 p[1](进程 1)。 这是 p0 的开始: if(p[0] == 0){ process(0,1); //(process,
如果我fork一个子进程,子进程在父进程调用waitpid之前退出,那么就是waitpid #include #include #include #include #include int
来自现有问题 here ,有人给出了这个示例代码: int status; child_pid = fork(); if (child_pid == 0) { // in child; do
如果发生崩溃,我们使用以下函数转储堆栈以获取有关崩溃的更多信息: static void dumpStack() { char buf[64]; pid_t p
我创建了一个模仿 std::thread 的简单 Process 类。它应该只在 linux 上工作。 struct Process { Process(/*...*/) { /* fork
我对 waitpid 函数有点困惑: int main(int argc, char** argv) { if (pid_t pid = fork()) { setpgid(p
我是一名优秀的程序员,十分优秀!