linux - redirectioning printf to pipe C -
the code above not give warnings or errors, parent not print string passed pipe. idea?
pid_t pid; int fd[2]; int num_bytes_read; char buffer[80]; pipe (fd); pid=fork(); if (pid == 0) {/*son*/ fclose(stdout); close(fd[0]); dup(fd[1]); printf ("write in pipe\n"); fflush(stdout); } else { /*parent*/ close (fd[1]); num_bytes_read = read (fd[0], buffer, sizeof (buffer)); printf ("the received string is: %s\n", buffer); } return (0);
in child writing file* have closed (i.e. stdout). dup have assigned fd == 0 descriptor of pipe, structure stdout
points remains "closed". either write pipe using write (as suggested @chrk) or close(stdout_fileno)
instead of fclose(stdout)
, or maybe can reassign stdout new value obtained fdopen
.
Comments
Post a Comment