Reading and writing from pipe after execvp using dup2

26 Views Asked by At

I want to execute a process called "left" which basically gets 2 integer by scanf and prints its sum. So as to get the sum, i redirected the stdout and stdin by using 1 pipe. But scanf doesn't read anything and blocks the process. What's wrong here?

        num2 = 1;
        int filed[2];
        pipe(filed);
        dup2(filed[1],STDOUT_FILENO);
        dup2(filed[0],STDIN_FILENO);
        
        printf("%d\n%d\n",num1,num2); // sending the numbers
        int fide =fork();
        if (fide == 0)
        {
            char *args[] = {"./left",NULL};
            execvp("./left",args);
            
        }
        else
        {
            
            wait(NULL);
            
            close(filed[1]);
            char buf[11];
            read(filed[0],buf,11);
            fprintf(stderr,"my result: %s \n",buf);
           
        }
0

There are 0 best solutions below