2

I have written the following program to understand how pipes work in C. But it is not producing the expected output. I have used pipe to connect the parent process's stdout to child process's stdin. I have used printf and scanf for this purpose. But the child program seems to wait for input instead of reading it using scanf. The code follows the question.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(){

    int pipefd[2];  
    pipe(pipefd);   
    int pid = fork();   // --- fork here ---    

    if (pid == 0){  // child process
        printf("child process\n");  

        int a=0, b=0;   

        close(pipefd[1]);
        dup2(pipefd[0],0);
        close(pipefd[0]);

        scanf("%d%d", &a, &b);
        printf("%d%d", a, b);
        exit(0);    
    }

    else{   // parent process
        printf("parent process\n");

        close(pipefd[0]);
        dup2(pipefd[1],1);
        close(pipefd[1]);

        printf("%d%d", 1, 2);

        wait(NULL);
    }
    return 0;
}

Expected output : parent process child process 12

Actual output : parent process child process

2
  • 1
    Did you try adding a newline when printing from the parent?
    – harmic
    Feb 1, 2014 at 11:43
  • add fflush(stdout); after printf in the parent and nondigit characters into the printf after each number. Well now I see your ` scanf("%d%d",...)` which also will create problem. You can not have two consecutive numbers without separator. It would be scanned as a single big number.
    – Marian
    Feb 1, 2014 at 11:51

1 Answer 1

3

You need to flush stdout for it the buffer to be send over the pipe. You can do that by adding fflush(stdout):

printf("%d%d", 1, 2);
fflush(stdout);

Then you also need to add some whitespace (doesn't matter which one) to the end of the output, because scanf will wait for a whitespace or EOF:

printf("%d%d ", 1, 2);
fflush(stdout);

Another way to flush the output and add the whitespace in one go might be adding a newline to the output:

printf("%d%d\n", 1, 2);

While this seems to work, I am not entirely sure this is guaranteed to flush the pipe.

However this will still not work as expected, because the printed output is 12, which is interpreted by scanf as one integer and scanf will again wait infinitely for the second integer. So add a delimiter whitespace:

printf("%d %d ", 1, 2);
fflush(stdout);
2
  • If I write printf("parent process") instead of prinf("parent process\n"), the message is not printed onto the terminal screen. But if I write printf("child process") instead of printf("child process\n"), the message is still printed onto the terminal. So why is it that sometimes stdout gets flushed even without writing "\n"?
    – nnori
    Feb 1, 2014 at 13:06
  • @nnori In the case of printf("child process") the process ends afterwards at exit(0). At that point every stream is flushed and closed, so you see it printed. In the case of printf("parent process"), since you do not flush before redirecting, the full buffer containing parent process1 2 is sent to the child process instead of being printed to the terminal.
    – user3072164
    Feb 1, 2014 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.