gpt4 book ai didi

c - C程序的奇怪打印 - Linux终端

转载 作者:行者123 更新时间:2023-11-30 17:23:58 25 4
gpt4 key购买 nike

我正在编写一个程序来在 C 和 Linux 环境中试验 fork 和信号。

该程序以各种方式创建许多子进程,每个进程在其开头和结尾打印一条特定的消息,而大多数进程自己创建子进程。 parent 总是在终止之前等待所有的 child ,并且在终止之前和 child 终止之后休眠固定的时间。

所以我执行程序,在终端中,立即打印了一些消息,但随后出现了终端的插入提示,就好像程序已经终止并且终端准备好接受进一步的命令,但是程序执行后将继续打印该点之后的其余消息。

我可以在程序打印最后一条消息后立即编写任何新命令,而不会出现任何新提示。可能是什么原因造成的?

编辑:发现问题 - 我需要在“main”中的“if”分支末尾添加以下代码:

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 定义了一个树状结构,它总体定义了必须创建的进程的“树”。

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 */

最佳答案

我认为父进程可能已经终止,而子进程没有终止。在您的情况下,主函数可能在子进程之前终止。

关于c - C程序的奇怪打印 - Linux终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27357494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com