c中的叉树取决于特定参数
Fork tree in c depending on specific parameters
所以我必须用叉子制作一棵树,然后我需要使用 pstree 来显示它。
这棵树是基于一些参数的,这里有一个选项:2 0 3 0 1 0 3。
含义:原进程需要fork 2children,左边什么都不做,右边fork 3children。左边一个和右边一个什么都不做,中间一个forks 1个进程那个进程比forks 3个多children。
这是一个方案:
o
/ \
o o
/|\
o o o
|
o
/|\
o o o
我不知道如何开始这个,我只有这个:(记住我不需要硬编码)
编辑:我不知道如何确定要从哪个 child 分叉。 (我怎么知道这 3 个 child 中的哪个是中间那个?)
char c; int n;
int *array = (int*)malloc(20*sizeof(int));
int i = 0;
//reads the cmdline arguements
while ((scanf("%d", &n))!=EOF) {
array[i]=n;
//printf("%d ", array[i]);
i++;
}
//gets pid of original process and makes a string to call pstree
int d=getpid();
char str[50] = "pstree -c ";
char str1[20];
sprintf(str1, "%d", d);
strcat(str,str1);
快一天了,不知道评论有没有用。
这是一个建议的实现 - 我知道您想先尝试自己做,所以首先是提示。如果您需要一些线索,请查看底部的 C 程序。
假设:
- 字符串由许多数字组成,每个数字至少由 space 分隔。
- 字符串结束后,指令相当于“0 fork”
- 和限制(在底部)。
首先,您可能会注意到一个递归模式(但同样的算法也可能以 迭代 方式开发)
- fork n次
- 让 children 1 .. n 分叉 x1 .. xn 次,同时用下一个操作喂养他们
- 每个child重复相同的算法
提示
- 创建两个函数,一个用于获取字符串中的下一个数字,一个用于跳过字符串中的 n 个数字
- 创建一个有两个参数的递归函数
- 分叉次数
- 字符串中给出下一个叉子指令的部分,如果有的话,特定 child(在所有 children 中,只有一个 should for again)
- 如果没有分叉,函数稍等
- 如果必须执行N次分叉,
- 获得一个指向字符串的 S,在所有 that 调用的 children 被跳过之后(例如,“2 0 3 4 0 0”分叉两次, 0和3是children要分叉的次数,第2个child得到一个指向“4 0 0”的S。
- 做 N 个分叉,children 用参数递归调用函数(例如 2 个分叉的相同示例 0 和 3)
- child端,return递归调用后
- parent过程(这个一个)每次都让S指向下一位,前提是child至少创建了一个分叉。
- 最后等待上面创建的所有进程
就是这样。
限制:此处建议的算法假定操作字符串为每组分叉仅生成一个活动分支 - 您提供的字符串“2 0 3 0 1 0 3”根据该限制(否则算法会更难;例如,对于“2 2 3 ...”,您将有 2 个平行的活动分支,它们有自己的生命并且必须从字符串中获取!意思是在string 哪个部分要给深度 D fork F...)。
你应该得到类似的东西(是的,我的程序名称是 "x"),通过 pstree -c
可见大约一分钟
-bash───x─┬─x
└─x─┬─x
├─x───x─┬─x
│ ├─x
│ └─x
└─x
实施建议
- 首先是 2 个辅助函数
要到达下一个数字(或 0 即 '\0')是字符串
char *nextdigit(char *s) {
while (*s && (*s<'0' || *s>'9')) s++; // skip non digits
return s;
}
跳过n位
char *skipndigits(int n, char *s) {
while (n--) {
s = nextdigit(s);
if (*s) s++;
}
return s;
}
接下来是应该如何调用递归函数(例如从 main()),具有 forkering() 的名称函数
char *op = "2 0 3 0 1 0 3";
forkering(atoi(op), op+1);
最后是递归函数。
#define WAITING 60
void forkering(int n, char *op) {
printf("forkering %d, with %s\n", n, op);
if ( ! n) {
sleep(WAITING); // no forking, wait WAITING seconds
return;
}
char *nextop = skipndigits(n, op); // next op (for children)
while(n--) {
op = nextdigit(op);
int nop = atoi(op); // number of forks that child has to do
if ( ! fork()) {
forkering(nop, nextop);
return;
}
if (nop) nextop = skipndigits(1, nextop); // only skip if non zero
op = skipndigits(1, op); // for our children
}
while (wait(NULL) > 0); // wait all processes created above
}
如您所见,该函数非常短。
所以我必须用叉子制作一棵树,然后我需要使用 pstree 来显示它。 这棵树是基于一些参数的,这里有一个选项:2 0 3 0 1 0 3。 含义:原进程需要fork 2children,左边什么都不做,右边fork 3children。左边一个和右边一个什么都不做,中间一个forks 1个进程那个进程比forks 3个多children。
这是一个方案:
o / \ o o /|\ o o o | o /|\ o o o
我不知道如何开始这个,我只有这个:(记住我不需要硬编码) 编辑:我不知道如何确定要从哪个 child 分叉。 (我怎么知道这 3 个 child 中的哪个是中间那个?)
char c; int n;
int *array = (int*)malloc(20*sizeof(int));
int i = 0;
//reads the cmdline arguements
while ((scanf("%d", &n))!=EOF) {
array[i]=n;
//printf("%d ", array[i]);
i++;
}
//gets pid of original process and makes a string to call pstree
int d=getpid();
char str[50] = "pstree -c ";
char str1[20];
sprintf(str1, "%d", d);
strcat(str,str1);
快一天了,不知道评论有没有用。
这是一个建议的实现 - 我知道您想先尝试自己做,所以首先是提示。如果您需要一些线索,请查看底部的 C 程序。
假设:
- 字符串由许多数字组成,每个数字至少由 space 分隔。
- 字符串结束后,指令相当于“0 fork”
- 和限制(在底部)。
首先,您可能会注意到一个递归模式(但同样的算法也可能以 迭代 方式开发)
- fork n次
- 让 children 1 .. n 分叉 x1 .. xn 次,同时用下一个操作喂养他们
- 每个child重复相同的算法
提示
- 创建两个函数,一个用于获取字符串中的下一个数字,一个用于跳过字符串中的 n 个数字
- 创建一个有两个参数的递归函数
- 分叉次数
- 字符串中给出下一个叉子指令的部分,如果有的话,特定 child(在所有 children 中,只有一个 should for again)
- 如果没有分叉,函数稍等
- 如果必须执行N次分叉,
- 获得一个指向字符串的 S,在所有 that 调用的 children 被跳过之后(例如,“2 0 3 4 0 0”分叉两次, 0和3是children要分叉的次数,第2个child得到一个指向“4 0 0”的S。
- 做 N 个分叉,children 用参数递归调用函数(例如 2 个分叉的相同示例 0 和 3)
- child端,return递归调用后
- parent过程(这个一个)每次都让S指向下一位,前提是child至少创建了一个分叉。
- 最后等待上面创建的所有进程
就是这样。
限制:此处建议的算法假定操作字符串为每组分叉仅生成一个活动分支 - 您提供的字符串“2 0 3 0 1 0 3”根据该限制(否则算法会更难;例如,对于“2 2 3 ...”,您将有 2 个平行的活动分支,它们有自己的生命并且必须从字符串中获取!意思是在string 哪个部分要给深度 D fork F...)。
你应该得到类似的东西(是的,我的程序名称是 "x"),通过 pstree -c
-bash───x─┬─x
└─x─┬─x
├─x───x─┬─x
│ ├─x
│ └─x
└─x
实施建议
- 首先是 2 个辅助函数
要到达下一个数字(或 0 即 '\0')是字符串
char *nextdigit(char *s) {
while (*s && (*s<'0' || *s>'9')) s++; // skip non digits
return s;
}
跳过n位
char *skipndigits(int n, char *s) {
while (n--) {
s = nextdigit(s);
if (*s) s++;
}
return s;
}
接下来是应该如何调用递归函数(例如从 main()),具有 forkering() 的名称函数
char *op = "2 0 3 0 1 0 3";
forkering(atoi(op), op+1);
最后是递归函数。
#define WAITING 60
void forkering(int n, char *op) {
printf("forkering %d, with %s\n", n, op);
if ( ! n) {
sleep(WAITING); // no forking, wait WAITING seconds
return;
}
char *nextop = skipndigits(n, op); // next op (for children)
while(n--) {
op = nextdigit(op);
int nop = atoi(op); // number of forks that child has to do
if ( ! fork()) {
forkering(nop, nextop);
return;
}
if (nop) nextop = skipndigits(1, nextop); // only skip if non zero
op = skipndigits(1, op); // for our children
}
while (wait(NULL) > 0); // wait all processes created above
}
如您所见,该函数非常短。