Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中使用管道和叉重定向stdout_C_Pipe - Fatal编程技术网

在C中使用管道和叉重定向stdout

在C中使用管道和叉重定向stdout,c,pipe,C,Pipe,我有一个关于ptrace和pipes的练习。下面的代码是整个程序的一部分。 管道是在主体部分之前制作的,此s为1。主要的父亲创造了孩子,这个孩子为stdout创造了自己的孩子。当程序运行ls时,它会在屏幕上打印,而不会写入文件。怎么了 if(pid == 0) { char buf0[BUFFSIZE], buf1[BUFFSIZE], buf2[BUFFSIZE]; int length0, length1, length2; if(this_trace.s_out == 1) //F

我有一个关于ptrace和pipes的练习。下面的代码是整个程序的一部分。 管道是在主体部分之前制作的,此s为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)
这会将0或1分配给
fd1
,而与打开的文件描述符无关。应该是:

if ((fd1 = open(this_trace.out_name,O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1)
使用
dup2()
(或
dup()
)将文件描述符重定向到标准通道后,应关闭原始文件描述符。因此,在
dup2之后(p_out[WRITE],1)你需要
关闭(p_out[WRITE])

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


您将显示未使用的变量
buf0
buf2
(以及相应的
length0
length2
)。

谢谢。过了很长时间,有些东西很难看到。有些变量用于其他重定向。之所以有这么多的过程,是因为教授给了我们练习。我刚刚做了你的改变,一切都很好。谢谢你,我这么做了,但是如果没有ctrl-c do{if((length0=read(fd0,buff0,bufsize))=-1{perror(“读取stdin重定向”);退出(1);}写入(p_in[write],buf0,length0);}而(length0>0);嗯,你应该避免做。。。while(…)
尽可能循环。我当然想写:
while((length0=read(fd0,buf0,sizeof(buf0)))>0)write(p_in[write],buf0,length0)可能正在检查
write()
是否成功。如果没有你完整的计划,我只能猜测你在干什么。您需要做的首要事情是确保所有进程都关闭所有管道的所有杂散端。通常,这样一个永无止境的循环是因为列表中的某个进程打开了输入管道的另一端进行写入(可能是当前进程)。