Note that there are some explanatory texts on larger screens.

plurals
  1. POdup2() and exec()
    text
    copied!<pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; int main( int argc, char **argv) { int pfds[ 2], i; size_t pbytrd; pid_t childpid; char buffer[ 200]; pipe( pfds); if( ( childpid = fork() ) == -1) { perror( "fork error: "); exit( 1); } else if( childpid == 0) { close( pfds[ 0]); dup2( pfds[1], 1); close( pfds[ 1]); for( i = 0; i &lt; 10; i++) printf("Hello..."); execlp( "xterm","xterm","-e","./sample_a", (char *) 0); exit( 0); } else { close( pfds[ 1]); for( ; ; ) { if( ( pbytrd = read( pfds[ 0], buffer, sizeof( buffer) ) ) == -1) { perror(" read error: "); exit( 1); } else if( pbytrd == 0) { write( fileno( stdout), "Cannot read from pipe...\n", strlen( "Cannot read from pipe...\n") ); exit( 0); } else { write( fileno( stdout), "MESSAGE: ", strlen( "MESSAGE: ") ); write( fileno( stdout), buffer, pbytrd); } } } return( 0); </code></pre> <p>}</p> <p>My sample_a.c code is below: </p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; int main( int argc, char **argv) { int i; for( i= 0; i&lt; 10; i++) write( fileno( stdout), "Hello...", strlen( "Hello...") ); return( 0); } </code></pre> <p>In the above code, what I really want to do is: 1) Redirect output from the child process to pipe and have parent read from the pipe and print it out to stdout.</p> <p>I am able to redirect child process output( "printf") from stdout to the pipe but I am not able to redirect execlp's child process, xterm, output to the pipe.</p> <p>Can anybody help me this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload