简体   繁体   English

在递归函数中使用 fork

[英]Usage of fork in a recursive function

I have a recursive function, in which I make a pipe.我有一个递归函数,我在其中制作了一个管道。 I am assuming that each function call has it's own pipe.我假设每个函数调用都有自己的管道。 Is that the case?是这样吗?

Here is my recursive function.这是我的递归函数。

int sum(tree *&root , tree *&root2, int value1 , int value2 , int result_tree)
{
    if(root->l_child == NULL && root->r_child == NULL)
        return root->data;
    else
    {
        value1 = sum(root->l_child , root2 , value1 , value2 , result_tree);
        value2 = sum(root->r_child , root2 , value1 , value2 , result_tree);

        int fd2[2];
        pipe(fd2);
        if(fork() == 0)
        {
            if(root->data == 43)
                result_tree = value1 + value2;      
            else if(root->data == 45)
                result_tree = value1 - value2;  
            else if(root->data == 42)
                result_tree = value1 * value2;  
            else if(root->data == 47)
                result_tree = value1 / value2;  

            close(fd2[0]);
            write(fd2[1] , &result_tree , sizeof(result_tree));
        }
        else
        {
            close(fd2[1]);
            read(fd2[0] , &result_tree , sizeof(result_tree));
        }

        root->data = result_tree;
        delete_node(root2 , root);
        //cout<<"\n";
        //display_in(root2);
        return result_tree;
    }
}

It gives output as much times as is the number of operands in the expression.For example, if the user enters expression: 3+4+4 then the output is: result:11result11result11它给出与表达式中操作数数量一样多的输出。例如,如果用户输入表达式:3+4+4,则输出为:result:11result11result11

what is wrong init.什么是错误的初始化。

In response to your question回答你的问题

I am assuming that each function call has it's own pipe.我假设每个函数调用都有自己的管道。 Is that the case?是这样吗?

You are calling the function pipe once per invocation of your function sum .每次调用函数sum调用一次函数pipe This creates a unique,anonymous,bidirectional pair of pipes.这创建了一个独特的、匿名的、双向的管道对。 However, you are not checking the return value of pipe .但是,您没有检查pipe的返回值。 It is possible the pipes are not being created because you are ignoring the return value.管道可能没有被创建,因为您忽略了返回值。 The man page states手册页指出

On success, zero is returned.成功时,返回零。

You should check that pipe returns zero when called, and abort if zero is not returned.您应该检查pipe在调用时是否返回零,如果没有返回零则中止。 The same goes for calls to close , write , and read .closewriteread调用也是如此。

If you include in your question your goal and why you think your code does not accomplish it, it is possible to provide a more informed answer.如果您在问题中包含您的目标以及您认为您的代码没有实现它的原因,则可以提供更明智的答案。 Just looking at your code, I am assuming that you understand that when the result of fork is zero, you are in the child process.看看你的代码,我假设你明白当fork的结果为零时,你在子进程中。 It looks like you are writing some results to a pipe, but then you continue executing.看起来您正在将一些结果写入管道,但随后您继续执行。 This probably is not your intent.这可能不是您的意图。 If it was not, you should call exit in the child process to terminate execution after writing to the pipe.如果不是,则应在子进程中调用exit以在写入管道后终止执行。

got the code fixed!修复了代码! I just included exit(0) in child process and it's working well.我只是在子进程中包含了 exit(0) 并且它运行良好。

int sum(tree *&root , tree *&root2, int value1 , int value2 , int result_tree)
{
     if(root->l_child == NULL && root->r_child == NULL)
         return root->data;
     else
     {
         value1 = sum(root->l_child , root2 , value1 , value2 , result_tree);
         value2 = sum(root->r_child , root2 , value1 , value2 , result_tree);

         int fd2[2];
         pipe(fd2);
         if(fork() == 0)
         {
             if(root->data == 43)
                 result_tree = value1 + value2;      
             else if(root->data == 45)
                 result_tree = value1 - value2;  
             else if(root->data == 42)
                 result_tree = value1 * value2;  
             else if(root->data == 47)
                 result_tree = value1 / value2;  

             close(fd2[0]);
             write(fd2[1] , &result_tree , sizeof(result_tree));
             exit(0);                                                       // change
         }
         else
         {
             close(fd2[1]);
             read(fd2[0] , &result_tree , sizeof(result_tree));
         }

         root->data = result_tree;
         delete_node(root2 , root);
         //cout<<"\n";
         //display_in(root2);
         return result_tree;
     }
}

I don't understand this that what actually exit(0) did , so that the code gave correct output.我不明白这实际上 exit(0) 做了什么,所以代码给出了正确的输出。 Can anyone explain this exit(0) thing , in this particular piece of code.任何人都可以在这段特定的代码中解释这个 exit(0) 的事情。 Thankyou!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM