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 9:10

>>16
nah, this enables or disables blocking. or am I mistaken?
>>20
:( well it is not that important. I will ask students to set their stdout buffers to null.
>>21
it just waits. Childs will be sending simple integers to server so it will be only a few bytes. It is buffered forever.

Weird thing \n does not the flush the buffer. It does while printing to console but not when it is a pipe.

Anyway it does not matter much. Asking students to insert setvbuf(stdout, NULL, _IOLBF, 0); to their code is not that much. Or maybe I can fix source files by hand

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