我正在制作一个程序,在C和Linux环境中试验分叉和信号。
程序以各种方式创建许多子进程,每个进程在其开头和结尾处打印特定的消息,而其中大多数都自己创建子进程。父母在终止之前总是等待所有孩子,并且在孩子终止之前和孩子终止之后也会睡一段固定的时间。
所以我执行程序,并在终端中立即打印一些消息,但随后出现终端的插入提示,好像程序已经终止并且终端已准备好接受进一步的命令,但程序被执行后继续打印该点之后的其余消息。
我可以在程序打印的最后一条消息之后立即编写任何新命令,而不会出现任何新提示。可能是什么造成的?
else{
int status;
waitpid(id,&status,0)
}
终端输出(示例):
user@oslab:~/workspace/Ex2.1 $ ./a.out ./forktree/proc.tree
Hello I am process A and I just got forked by DAD!
Hello I am process B and I just got forked from A!
Hello I am process C and I just got forked from A!
Hello I am process D and I just got forked from A!
Hello I am process E and I just got forked from B!
Hello I am process F and I just got forked from B!
user@oslab:~/workspace/Ex2.1 $ I am process C, I finished creating 0 children, and now I am exiting.Farewell!
I am process D, I finished creating 0 children, and now I am exiting.Farewell!
I am process E, I finished creating 0 children, and now I am exiting.Farewell!
I am process F, I finished creating 0 children, and now I am exiting.Farewell!
I am process B, I finished creating 2 children, and now I am exiting.Farewell!
I am process A, I finished creatind 3 children, and now I am exiting.Farewell!
以下是整个代码,可以进行任何你想要的研究 tree.h定义了一个树状结构,它整体定义了" Tree"必须进行的过程。
DAD是第一个进程,位于所述树的根目录之下。
以下是我的计划:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "forktree/tree.h"
#include "forktree/tree.c"
#define SLEEP_TIME 5
//BY NO MEANS READY TO RUN
//update:most functionality is now ready.
int first_of_the_year(struct tree_node *base);//Will only be used by the root of the tree,to make recursion easier
int recurse_me(struct tree_node *base,char *parent_name);//will be used by every other forked process
int main(int argc, char *argv[]){
struct tree_node *base = get_tree_from_file(argv[1]);
//struct tree_node *rocket = NULL;
char *parent_name = NULL;
pid_t id = fork();
if(id<0) {
perror("fork");
} else if (id == 0) {
first_of_the_year(base);
}
return 0;
}
//forked recursions will return -1, while shallow recursions resulting in a successful fork from parents will retun 0.
//that will prevent the children from re-creating multiple copies of other processes, while going up (returning from) the recursion.
int first_of_the_year(struct tree_node *base){
printf("Hello I am process %s and I just got forked by DAD!\n",base->name);
int i = 0;
int flag = 0;
while((base->nr_children>i)&&(flag !=-1)){
flag = recurse_me(base->children + i,base->name);
i++;
}
if (flag==0){
int count = base->nr_children;
int status;
for (i = 0; i < count; i++) {
wait(&status);
}
sleep(SLEEP_TIME);
printf("I am process %s, I finished creatind %u children, and now I am exiting.Farewell!\n",base->name,base->nr_children);
}
return 0;
}
int recurse_me(struct tree_node *base,char *parent_name){
int id;
id = fork();
if(id<0) {
perror("fork");
} else if (id == 0) {
printf("Hello I am process %s and I just got forked from %s!\n",(base- >name),parent_name);
int i=0;
int flag=0;
while((base->nr_children>i)&&(flag !=-1)){
flag = recurse_me(base->children + i,base->name);
i++;
}
if (flag==0){
int count = base->nr_children;
int status;
for (i = 0; i < count; i++) {
wait(&status);
}
sleep(SLEEP_TIME);
printf("I am process %s, I finished creating %u children, and now I am exiting.Farewell!\n",base->name,base->nr_children);
}
return -1;
}
return 0;
}
这是tree.h:
#ifndef TREE_H
#define TREE_H
/******************************************************************************
* Data structure definitions
*/
#define NODE_NAME_SIZE 16
/* tree node structure */
struct tree_node {
unsigned nr_children;
char name[NODE_NAME_SIZE];
struct tree_node *children;
};
/******************************************************************************
* Helper Functions
*/
/* returns the root node of the tree defined in a file */
struct tree_node *get_tree_from_file(const char *filename);
void print_tree(struct tree_node *root);
#endif /* TREE_H */
答案 0 :(得分:0)
我认为父进程可能已终止子进程未终止的位置。在您的情况下,主要功能可能在子进程处理之前终止。
答案 1 :(得分:0)
fork
在后台启动一个进程。父进程完成后,控制权将返回到shell,并显示一个新提示。
要让父母等到孩子完成,你可以做类似的事情:
else {
printf("Parent process is terminating...\n");
wait(NULL);
return 0;
}
答案 2 :(得分:0)
我刚刚注意到了这个问题。 DAD是第一个进程,它不等待树的根。所以它在所有孩子和子孩子返回之前退出。添加必要的代码以等待树的根进程解决了这个问题。