- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我需要从同一父级创建 n 个子级,并让它们在父级无限请求信号发送给某个子级时运行。我让父级创建了那些 n 个子级,但是他们执行完了,所以我让他们进入了一个 while(1) 循环。问题是,当我试图杀死任何 child 时,它变成了一个僵尸进程,而不是真正终止它的执行。我猜那是因为 parent 仍在等待 child 终止执行并且他们不发送退出状态。所以......首先,我真的需要让 children 进入一个无限循环,让他们在 parent 要求信号时运行吗?如果我这样做,我该如何避免这个“永不终止执行”的问题?我需要找到一种方法让 children 退出那个 while 循环并发送实际的“完成执行”信号,因为我认为我不能使用 wait()
因为 children 从未真正完成运行,他们刚刚被 parent 终止。
谢谢。PS:在 Linux 中运行。这是我的代码
int main(){
int i;
pid_t parent = getpid();
pid_t pid[4];
printf("parent with pid %d\n", parent);
for(i = 0; i < 5; i++){
pid[i] = fork();
if(pid[i] < 0){
perror("Error in fork.");
exit(1);
} else if(pid[i] == 0 && getppid() == padre) {
printf("Number: %d pid %d, parent: %d\n", i, getpid(), getppid());
while(1);
}
}
if(getpid() == padre){
while(1){
printf("Enter pid and signal:\n");
int x, y;
scanf("%d", &x); // pid
scanf("%d", &y); // signal
printf("you did: kill(%d, %d)\n", x, y);
kill(x, y);
}
}
return 0;
}
编辑:实现答案的最终代码:https://github.com/sebasura/sistope
最佳答案
The problem is, when I try to kill any child, it becomes a zombie process instead of actually terminating its execution.
好吧,是的,不是。成为僵尸通常是在进程终止时发生的,直到该进程被其父进程回收为止。僵尸占用进程表中的一些空间,但它不运行,因此不消耗 CPU。
I'm guessing that's because the parent is still waiting for the children to terminate execution and they don't send the exit status.
不,这是因为 kill()
只是发送了一个信号。您需要使用 wait()
函数之一——也许是 waitpid()
——来实际收集已终止的子进程。
So... First of all, do I really need to make the children enter an infinite while loop to make them be running while the parent is asking for the signals?
没有。 child 可以使用 sigwait()
或其变体之一来等待来自指定集合的信号,或者使用 pause()
暂停执行等待接收到任何终止信号进程或触发信号处理函数。但是请注意,有些信号在默认情况下不执行这两种操作,因此 sigwait()
可能是更好的选择。
If I do, how do I avoid this "never terminating execution" problem?
child 必须因收到信号而终止。这已经发生在你身上,因为 children 正在变成僵尸。对于您当前的代码,它可能取决于您发送的信号,但是,有些代码的默认处理不会终止进程。
关于使用 fork() 创建 "background running" child 并用信号杀死他们中的每一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43144777/
有3个 repo : 有 OpenAI Baselines 存储库:https://github.com/openai/baselines . 我有它的 fork :https://github.co
我试图了解在调用 fork() 后复制文件描述符的含义及其对争用的可能影响。 在“Linux 编程接口(interface)”24.2.1 (p517) 中: When a fork() is per
我对 systemd 如何跟踪主进程存在后仍然存在的主进程的子进程感兴趣? 最佳答案 Systemd 为此使用了 cgroups。在启动任何使用服务文件定义的可执行文件之前,systemd 会创建一个
这是输出--- 家长:我的pid是4525 parent :我 parent 的 pid 是 3350 parent 开始- 4525 3350 fork 前 fork 前 child 4526 45
我之前 fork 了 jockm/vert.x 并向他发送了拉取请求。现在我想 fork vert-x/vert.x (jockm/vert.x 的上游)并向他们发送不同的拉取请求。但是,当我单击“F
我想控制从 Perl 脚本派生的进程的名称 a。理想情况下它会像这样: ./forker.pl | ... | | fork("forked.pl"); |\ | \ | `--------\ |
我知道 fork() 在更高级别上做什么。我想知道的是这个—— 一旦有 fork 调用,trap 指令就会跟随并且控制跳转以执行 fork “处理程序”。现在,这个创建子进程的处理程序如何通过创建另一
我正在研究操作系统测验,但我不知道输出什么 if(fork()) fork() 会产生。有人可以解释吗? 我不明白这一行: if(fork()) 编辑: 我所说的“输出”是指如果执行此代码,将
这个问题在这里已经有了答案: Why does this program print "forked!" 4 times? (6 个答案) 关闭 3 年前。 在 C 中,fork() 函数将为父进程
有什么方法可以区分程序中不同 fork() 函数创建的子进程。 global variable i; SIGCHLD handler function() { i--; } handle() {
我正在重新开发一个系统,该系统将通过 http 向多个供应商之一发送消息。原来是perl脚本,重新开发很可能也会用perl。 在旧系统中,同时运行多个 perl 脚本,每个供应商运行 5 个。当一条消
Git 的新手,仍然有点困惑。我在 github 上 fork 了一个项目,想将项目所有者最近对原始项目所做的一些更改引入/merge 到我的 fork 中。这可能吗?该项目是只读的,但基本上,我想让
根据维基百科(可能是错误的) When a fork() system call is issued, a copy of all the pages corresponding to the par
我需要帮助了解如何在 Go 中妖魔化进程。 package main import ( "fmt" "os" ) func start() { var procAttr os.Pro
我已经执行了这段代码。我知道消息的顺序是任意顺序的(因为我明确没有使用信号量)我的程序流程如何?为什么? 父级被执行,因此“baz”被打印一次。有人可以解释为什么不打印“bar”吗?为什么我得到“fo
这个问题已经有答案了: Why does this program print "forked!" 4 times? (6 个回答) 已关闭 5 年前。 我对 fork 进程有疑问。我有一个代码是 i
我在弄清楚如何使用在不同进程之间创建的列表时遇到了麻烦。我所拥有的是: FileList.h - 我创建的列表 #include "Node.h" typedef struct FileList {
好吧,所以我一直在 stackoverflow 上查找这个问题,并且肯定在谷歌上搜索了半个小时,但我得到的答案似乎与我真正想做的事情几乎没有任何关系,希望有人能提供帮助我在这方面,代码如下: int
我正在尝试了解 fork-join 的工作原理。 维基百科有以下合并排序示例,其中左半部分被 fork ,右半部分由当前线程处理。 mergesort(A, lo, hi): if lo t
下面的代码输出了一系列的过程,它到底做了什么: _PARENT_ / \ /
我是一名优秀的程序员,十分优秀!