- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试使用 fork() 创建进程树,以便每个父进程的子进程数在给定数组中,例如,如果数组是 {2,1,3,0,0,0,0}树看起来像这样:
| a |
/ \
| b | | c |
/ / | \
| d | | e | | f | | g |
通过检查 fork() 返回的值是否为 0,我能够创建进程并将父进程与子进程分开。我设法创建了一个流程树,但我设法创建的树是对称的,而不是我真正想要构建的。 sibling 之间的路由过程我想不通,
我如何分别检查每个进程应该为它创建多少子进程,以及在不为其他同级进程创建的情况下为它创建?
这是我到目前为止得到的:
int main() {
int nums[7] = { 2,1,3,0,0,0,0 };
int pid, pid2;
size_t len = sizeof(nums)/sizeof(int);
int childs2;
printf("\nProcess number %d has pid= %d\n", 0, getpid());
int childs = 1;
while( childs <= nums[0] ) {
pid = fork();
if (pid == 0 ) {
printf("Process number %d has pid= %d\n", childs, getpid());
printf("I am Process with pid=%d and my parent pid=%d\n", getpid(), getppid());
waitpid(getppid());
for (int i=1; i<len; i++) {
childs2 = 0;
if (childs2 < nums[i]) {
pid2 = fork();
if (pid2 == 0) {
printf("Process number %d has pid= %d\n", childs, getpid());
printf("I am Process with pid=%d and my parent pid=%d\n", getpid(), getppid());
waitpid(getppid());
break;
} else {
wait(NULL);
childs2++;
}
} else {
childs2++;
}
}
break;
} else {
wait(NULL);
childs++;
}
}
return 0;
}
我必须区分进程才能知道哪个进程是叶进程,哪个进程是父进程。为此,我需要在每个过程中执行不同的操作,但我想不出办法,
我的输出是:
Process number 0 has pid= 98431
Process number 1 has pid= 98432
I am Process with pid=98432 and my parent pid=98431
Process number 1 has pid= 98433
I am Process with pid=98433 and my parent pid=98432
Process number 1 has pid= 98434
I am Process with pid=98434 and my parent pid=98432
Process number 2 has pid= 98435
I am Process with pid=98435 and my parent pid=98431
Process number 2 has pid= 98436
I am Process with pid=98436 and my parent pid=98435
Process number 2 has pid= 98437
I am Process with pid=98437 and my parent pid=98435
树看起来像:
| a |
/ \
| b | | c |
/ \ / \
| d | | e || f | | g |
但我希望输出为:
Process number 0 has pid= 98431
Process number 1 has pid= 98432
I am Process with pid=98432 and my parent pid=98431
Process number 2 has pid= 98433
I am Process with pid=98433 and my parent pid=98431
Process number 3 has pid= 98434
I am Process with pid=98434 and my parent pid=98432
Process number 4 has pid= 98435
I am Process with pid=98435 and my parent pid=98433
Process number 5 has pid= 98436
I am Process with pid=98436 and my parent pid=98433
Process number 6 has pid= 98437
I am Process with pid=98437 and my parent pid=98433
所以树看起来像:
| a |
/ \
| b | | c |
/ / | \
| d | | e | | f | | g |
.
最佳答案
我们需要做的是跟踪我们在列表中的进程以及子进程在列表中的位置。以下代码显示了如何执行此操作。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NumberOf(a) (sizeof (a) / sizeof *(a))
/* Create children for process p.
In a child, return the number of that child process.
In the parent, return -1.
*/
static int CreateChildren(int NumberOfChildren[], int FirstChild[], int p)
{
// Create children for process p.
printf("Process %d has pid %u and parent %u.\n",
p, (unsigned) getpid(), (unsigned) getppid());
for (int i = 0; i < NumberOfChildren[p]; ++i)
{
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0)
/* This is a child process, and it is child i of process p, so
its process number is FirstChild[p] + i. Return that.
*/
return p = FirstChild[p] + i;
}
// Wait for children to finish.
for (int i = 0; i < NumberOfChildren[p]; ++i)
wait(0);
// Tell caller the parent finished.
return -1;
}
int main(void)
{
int NumberOfChildren[] = { 2, 1, 3, 0, 0, 0, 0 };
size_t N = NumberOf(NumberOfChildren);
// Check the NumberOfChildren array for consistency.
{
int sum = 0;
for (size_t n = 0; n < N; ++n)
{
if (NumberOfChildren[n] < 0)
{
fprintf(stderr,
"Error, number of children cannot be negative but is %d.\n",
NumberOfChildren[n]);
exit(EXIT_FAILURE);
}
sum += NumberOfChildren[n];
}
if (sum != N-1)
{
fprintf(stderr,
"Error, the numbers of children sum to %d desecendants "
"of the root, but array has %zu elements after the root "
"element.\n",
sum, N-1);
exit(EXIT_FAILURE);
}
}
/* Compile information about the children -- set FirstChild[n] to the
index of the element in NumberOfChildren that is for the first child
of process n.
*/
int FirstChild[N];
{
int NextChild = 1;
for (int n = 0; n < N; ++n)
{
FirstChild[n] = NextChild;
NextChild += NumberOfChildren[n];
}
}
// This is the root process. Set p to its index.
int p = 0;
/* Create children for process p. When a child is created, it will
return its process number, and we will loop to create children for it.
*/
while (p >= 0)
p = CreateChildren(NumberOfChildren, FirstChild, p);
}
示例输出:
Process 0 has pid 2648 and parent 2641.Process 1 has pid 2649 and parent 2648.Process 2 has pid 2650 and parent 2648.Process 3 has pid 2651 and parent 2649.Process 4 has pid 2652 and parent 2650.Process 5 has pid 2653 and parent 2650.Process 6 has pid 2654 and parent 2650.
关于c - 有没有办法更好地路由流程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55500369/
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
首先是一些背景;我们正在开发一个数据仓库,并对我们的 ETL 过程使用哪些工具进行一些研究。该团队非常以开发人员为中心,每个人都熟悉 C#。到目前为止,我已经看过 RhinoETL、Pentaho (
我需要具有管理员权限的进程。从this问题和答案来看,似乎没有比启动单独进程更好的方法了。因为我宁愿有一个专用于该过程的过程,而不是仅为此方法在第二个过程中启动我的原始应用程序–我以为我会在VS201
我有这个函数来压平对象 export function flattenObject(object: Object, prefix: string = "") { return Object.key
我正在开发一个基于java的Web应用程序,它要求我使用来自SIP( session 启动协议(protocol))消息的输入生成序列图。我必须表示不同电话和相应服务器之间的调用流程。我可以利用任何工
这是我的代码: Process p=Runtime.getRuntime().exec("something command"); String s; JFrame frame = new JFram
我对 istio 的 mTLS 流程有点困惑。在bookinginfo 示例中,我看到服务通过http 而不是https 进行调用。如果服务之间有 mTLS 那么服务会进行 http 调用吗? 是否可
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
之前做过一个简单的纸牌游戏,对程序的整体流程有自己的想法。我最关心的是卡片触发器。 假设我们有一张名为“Guy”的牌,其效果为“每当你打出另一张牌时,获得 2 点生命”。我将如何将其合并到我的代码中?
我有 4 个 Activity 。 A、B、C 和 D。 用户可以从每个 Activity 开始任何 Activity 。 即 Activity A 有 3 个按钮来启动 B、C 和 D。以同样的方式
我做了一个简单的路由器类,简化后看起来像这样 // @flow import { Container } from 'unstated' type State = { history: Objec
我有两个 Activity ,比如 A1 和 A2。顺序为 A1->A2我从 A1 开始 A2 而没有在 A1 中调用 finish() 。在 A2 中按下后退按钮后,我想在 A1 中触发一个功能。但
我正在考虑在我的下一个项目中使用 BPEL。我试用了 Netbeans BPEL 设计器,我对它很满意。但在我决定使用 BPEL 之前,我想知道它对测试驱动开发的适用程度。不幸的是,我对那个话题知之甚
我需要将两个表格堆叠在一起,前后都有内容。我无法让后面的内容正常流动。堆叠的 table 高度可变。 HTML 结构: ... other content ...
我是 Hibernate 的新手。我无法理解 Hibernate 的流程。请澄清我的疑问。 我有“HibernateUtil.java ”和以下语句 sessionFactory = new Anno
早上好 我开始使用 Ruby,想创建一个小工具来获取我的公共(public) IP 并通过电子邮件发送。我遇到了字符串比较和无法处理的 if/else block 的基本问题。 代码非常简单(见下文)
我目前正尝试在我的团队中建立一个开发流程并阅读有关 GitFlow 的信息。它看起来很有趣,但我可以发现一些问题。 让我们假设以下场景: 我们完成了 F1、F2 和 F3 功能,并将它们 merge
我已经使用 git flow 有一段时间了。我很想了解一个特定的用例。 对于我的一个项目,我有一张新网站功能的门票。此工单取决于许多子任务。我想为主工单创建一个功能分支,然后为每个子任务创建一个脱离父
你怎么知道在一个程序中已经发现并解决了尽可能多的错误? 几年前我读过一篇关于调试的文档(我认为这是某种 HOWTO)。其中,该文档描述了一种技术,其中编程团队故意将错误添加到代码中并将其传递给 QA
我目前正在构建一个微服务架构,并从身份验证服务器和客户端着手。我还想确认使用 token 对用户进行身份验证的最佳流程。 在上图中。第 3 步是我开始感到困惑。我想到了2个解决问题的方法。 每个 ap
我是一名优秀的程序员,十分优秀!