Вопрос

I am trying to rewrite a c program that do something like ls|wc|wc , I already did it for ls|wc , it worked fine, but I can't figure out why my program stops at the child in the indicated line. please help!

int main (void)
{
    pid_t pid_fils, pid_pfils;

    int fd[2], fd2[2];

    if(pipe(fd)==-1 || pipe(fd2)==-1)
    {
        printf("pipe failed!");
        return 1;
    }

    printf("program started\n");
    pid_fils=fork();
    if(pid_fils==0)
    {
        pid_pfils=fork();
        if(pid_pfils==0)
        {
            //action3
            printf("I am the grandson\n");
            close(fd[0]);//close read side
            dup2(fd[1],1);//connect write with stdout
            close(fd[1]);//close write side
            execlp("ls","ls",(char*)0);
            //execvp("ls",argv3);
            return 0;/*actions grandson*/
        }
        else
        {
            //action2
            printf("I am the son\n");
            wait();
            printf("son, wait ok\n");
            >close(fd[1]);  //close read side
            >dup2(fd[0],0); //connect write with stdin
            >close(fd[0]);  //close read side

            ///////pipe2////
           > close(fd2[0]);  //close read side
            >dup2(fd2[1],1); //connect write with stdout/*it stops here -can't display "ok!"*/
            printf("ok!\n");    
            >close(fd2[1]);  //close write side

            execlp("wc","wc",(char*)0);
            printf("error exec returned!\n");    
            return 0;
        }
    }
    else
    {
        ///action1
        printf("I am the parent\n");
        wait();
        printf("parent,wait ok\n");
        close(fd2[1]);  //close write side, 
        dup2(fd2[0],0); //connect read with stdin
        close(fd2[0]);  //close read side
        execlp("wc","wc",(char*)0);
        return 0;/*the parent*/
    }
    return 1;
}
Это было полезно?

Решение

Make sure you close all unused descriptors. In your case, the easiest solution is to move the creation of pipe(fd) into the first if block (in the first sub-process). The problem is that as long as any process could possibly write to the pipe, the reader won't get EOF and so won't terminate.

if(pipe(fd2)==-1)
{
    printf("pipe failed!");
    return 1;
}

printf("program started\n");
pid_fils=fork();
if(pid_fils==0)
{
    if(pipe(fd)==-1)
    {
        printf("pipe failed!");
        return 1;
    }
    pid_pfils=fork();

I should also mention that you may want to reconsider the wait calls. Not sure what you are intending to do with them but you don't want the "ls" process to block on output because the reader hasn't been started yet.

Другие советы

dup2(fd2[1],1);

Above line will first close the file at descriptor 1 and then duplicate the decriptor from fd2[1] into 1.

1 is stdout. Meaning that call closed stdout.

printf prints to stdout, meaning printf prints to 1 which is now assigned to the pipe fd2

So your ok went into the pipe and not on the screen.

try

        //action2
        printf("I am the son\n");
        wait();
        printf("son, wait ok\n");
        close(fd[1]);  //close read side
        dup2(fd[0],0); //connect write with stdin
        close(fd[0]);  //close read side

        ///////pipe2////
        int my_terminal_out = dup(1);
        close(fd2[0]);  //close read side
        dup2(fd2[1],1); //connect write with stdout/*it stops here -can't display "ok!"*/
        fprintf(my_terminal_out, "ok!\n");    
        close(fd2[1]);  //close write side

Not tested. Also you should test the rest of your code for similar missteps.

+What DrC said.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top