繁体   English   中英

使用C中的管道和fork重定向标准输出

[英]Redirect stdout with pipes and fork in C

我有关于ptrace和管道的练习。 以下代码是整个程序的一部分。 在主要部分之前制作管道,this_trace.s_out为1。主要父亲创建一个孩子,这个孩子为stdout制作了自己的孩子。 当程序运行ls时,它将在屏幕上打印并且不写入文件。 怎么了?

if(pid == 0)
{
char buf0[BUFFSIZE], buf1[BUFFSIZE], buf2[BUFFSIZE];
int length0, length1, length2;


if(this_trace.s_out == 1)   //For stdout redirection
{
    if((pid1=fork()) == -1)
    {
        perror("fork");
        exit(1); 
    }

    if(pid1 == 0) //child for stdout redirect
    {//sleep(2);
        if(fd1 = open(this_trace.out_name,O_WRONLY | O_CREAT | O_TRUNC, 0666) == -1)
        {
            perror("create stdout file");
            exit(1);
        }


        close(p_out[WRITE]);
        close(p_in[READ]);
        close(p_in[WRITE]);
        close(p_err[READ]);
        close(p_err[WRITE]);

        do{
            if((length1 = read(p_out[READ],buf1,BUFFSIZE)) == -1)
            {
                perror("Read for stdout redirection");
                exit(1);
            }
            write(fd1, buf1, length1);
        }while(length1 > 0);


        close(fd1);
        //close(p_out[READ]);
        return 0;
        //break;

    }
    else if(pid1 > 0)//child from main father
    {
        close(p_out[READ]);
        close(p_in[READ]);
        close(p_in[WRITE]);
        close(p_err[READ]);
        close(p_err[WRITE]);

        dup2(p_out[WRITE], 1);
    }

}




ptrace(PTRACE_TRACEME, 0, NULL, NULL);  


//execv(argv[1],NULL);
execl("/bin/ls","ls",NULL);


}

对不起,我的英语不好。

目前尚不清楚为什么会有这么多流程。 您有以下问题:

if (fd1 = open(this_trace.out_name,O_WRONLY | O_CREAT | O_TRUNC, 0666) == -1)

这将为fd1分配0或1,而与打开的文件描述符无关。 它应该是:

if ((fd1 = open(this_trace.out_name,O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1)

使用dup2() (或dup() )将文件描述符重定向到标准通道后,应关闭原始文件描述符。 因此,在dup2(p_out[WRITE], 1); 您需要close(p_out[WRITE]);

您应该从execl()检测到故障并进行处理; 如果execl()返回,则失败。

您将显示未使用的变量buf0buf2 (以及相应的length0length2 )。

暂无
暂无

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

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