0
void print_tree(tree_node *thestruct,int* fd,int numKids)
{    
    FILE *fo = freopen("output2.txt","w",stdout);
    printf("%s",thestruct->line);
    fflush(stdout);
    int i = 0;
    char buff[20];

    for(i=1; i < numKids+3; i++)
    {  
        bzero(buff,20);
        close(fd[2*i+1]);
        read(fd[2*i], buff, 20);
        close(fd[2*i]);
        printf("%s",buff);
        fflush(stdout);
    }   
}

基本上在这行代码中,我试图在进程之间进行管道传输时从读写中打印缓冲区。但是最后一个打印语句没有打印到文件,而是打印到标准输出?我也尝试过使用 fprintf 并且最后一个打印语句仍然显示为

mgl70@ece29:~/ComputerSystems$ ./a.out processTree
D 0

当我想要运行可执行文件时在终端中

D 0

打印到文件中

这是写的

else{//child process stuff 
     close(fd[2*procNum]);
     char* line = strdup(initNode->line); 
     write(fd[2*procNum+1],line,20);
  } 
4

0 回答 0