Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

hey

Name: Anonymous 2011-10-25 10:51

Hey /prog/. I am trying to access stdin and stdout of a child program. Here is what I did.

This works for stdin of child, I can access it and send strings from parent to child. however reverse does not work.

#include  <stdio.h>
#include  <string.h>
#include  <sys/types.h>

void  main(void){
     pid_t  pid;
     int    i;

     char str[40];
     int     fd1[2];
     int     fd2[2];
     pipe(fd1);
     pipe(fd2);

     pid = fork();

     if(pid == 0){

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

       dup2( fd2[0], 0 );
       close(fd2[1]);
       execl("./c","c", 0);
     }
    

     close(fd1[1]);
     close(fd2[0]);

     FILE *out = fdopen (fd2[1], "w");
     FILE *inp = fdopen (fd1[0], "r");
    
     for(i=0; i<10; i++){
       fprintf(out, "hello%d\n", i);
       printf("sent\n");
       fscanf(inp, "%s", str);
       printf("cli:%s\n", str);
     }
     close(fd2[1]);
     close(fd1[0]);
}


and ./c is


#include <stdio.h>

void  main(){
  int i;
  char t[20];


  for(i=0; i<10; i++){
      scanf("%s", t);
      printf("!%s!\n", t);
  }
}

Name: Anonymous 2011-10-26 17:40

>>28

Yes that's expected.. If it's a terminal it's line buffered

Again, you are a stupid fucker. And I quote pages 135 and 136 in "Advanced Programming in the Unix Environment" by Stevens and Rago.

"Line buffering is typically used on a stream when it refers to a terminal: standard input and standard output for example.

Line buffering comes with two caveats. First, the size of the buffer that the standard I/O library is using to collect each line is fixed, so I/O might take place if we fill the buffer before a newline. Second, whenever input is requested through the standard I/O library from either (a) an unbuffered stream or (b) a line-buffered stream."

Do you see anything that requires a terminal, when used as a predicate, to be true? Because I sure a hell don't.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List