(c / c ++) trying to get the EOF from the parent process to send input to the child process

I have a very simple c / C ++ program that forces a child process to execute another program and then sends some data to this child program and waits for a response.

the child program reads from stdin and waits for EOF to continue.

My problem is that the child program receives the original input from the channel record, but never sees EOF (even if I close the channel), so it waits forever.

I'm not sure why closing a pipe does not mean EOF for a child stdin?

here is the code:

http://gist.github.com/621210

+3
source share
3 answers

, , EOF . - , :

int fds[2];
pipe(fds);  // open a pipe
if (fork()) {
    // parent process
    write(fds[1], ...  // write data
    close(fds[1]); // close it
} else {
    // child process
    while (read(fds[0], ....) > 0) {
        // read until EOF

, - , . , EOF .

, , - close(fds[1]);, . , , EOF .

Edit

, , : , stdout. stdout , . stdout ( /dev/null )

Edit

:

int tochild[2], fromchild[2];
pipe(tochild); pipe(fromchild);
if (fork()) {
    close(tochild[0]);
    close(fromchild[1]);
    //write to tochild[1] and read from fromchild[0]
} else {
    dup2(tochild[0], 0);
    dup2(fromchild[1], 1);
    close(tochild[0]); close(tochild[1]);
    close(fromchild[0]); close(fromchild[1]);
    exec(...
}

, , , , , , ( , , ). , , , ( ) .

+5

, : EOF. , read(). 0, EOF. EOF .

, . . , , , 4 8 .

, . write().

. 4000 . . .

, .

+3

Source: https://habr.com/ru/post/1769051/


All Articles