簡體   English   中英

C程序的Bizzare打印-Linux終端

[英]Bizzare printing of C program - Linux terminal

我正在編寫一個程序,以在C和Linux環境中試驗分叉和信號。

該程序以各種方式創建許多子進程,每個進程在其開始和結束時都打印一條特定的消息,而大多數進程自己創建子進程。 父母總是在等待所有孩子離婚前等待,並且在他們離婚之前和孩子離婚之后還要睡一段固定的時間。

因此,我執行了該程序,並在終端中立即打印了一些消息,但是隨后出現終端的插入提示,好像程序已終止並且終端已准備好接受其他命令,但是正在執行的程序仍在繼續在此之后打印其余消息。

我能夠在程序最后一條消息之后立即編寫任何新命令,而不會出現任何新提示。 是什么原因造成的?

編輯:發現問題-我需要在“ 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 */

我認為父進程可能已終止,而子進程未終止。 在您的情況下,main函數可能在子進程之前終止。

fork在后台啟動一個進程。 父進程完成后,控制權將返回到Shell並顯示新的提示。

要讓父母等到孩子完成后,可以執行類似的操作:

else {
    printf("Parent process is terminating...\n");
    wait(NULL);
    return 0;
}

我只是注意到了問題。 DAD,第一個過程,不等待樹的根。 因此,它在所有子級和子級子返回之前退出。 添加必要的代碼以等待樹的根進程即可解決該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM