Controlling forked processes stdin/stdout

Controlling forked processes stdin/stdout

Post by Michael E. Warr » Thu, 11 Jan 1996 04:00:00



Is popen() the only way to interface with a forked processes stdin and stdout?
I'd like to be able for fork/exec a process, feed its stdin and capture its
stdout.  It looks to me like popen() will only let you do one or the other.
Thanks for the advice.

Mike Warren
HRB Systems
State College, PA

 
 
 

Controlling forked processes stdin/stdout

Post by Erik O'Shaughnes » Fri, 12 Jan 1996 04:00:00


|
|   Is popen() the only way to interface with a forked processes stdin
|   and stdout?  I'd like to be able for fork/exec a process, feed its
|   stdin and capture its stdout.  It looks to me like popen() will
|   only let you do one or the other.  Thanks for the advice.
|
|   Mike Warren
|   HRB Systems
|   State College, PA
|

Rather than popen, you may consider using pipe(2) to accompish IPC
between a parent and child.  The following is a *very* rough sketch
of how to accomplish it.  No attempt to catch errors has been made
and I very definitely recommend you RTFriendlyM to learn where include
files and libraries for your particular OS reside.  Finally, this
code has not seen the inside of a compiler, so no guarantees.

/* major code fragment follows.. YMMV */
enum { READ_P,WRITE_P };

pid_t pid;
int input[2],output[2];
FILE *in,*out;

pipe(input);                    /* create two pipes, on for talking to */
pipe(output);                   /* the child, the other for listening */

if(pid=fork()){                 /* i'm the parent */

    close(input[READ_P]);       /* close the read side of this pipe */
    in = fdopen(input[WRITE_P],"w"); /* make it a FILE * for ease of use */

    close(output[WRITE_P]);     /* close the write side of this pipe */
    out = fdopen(output[READ_P],"r"); /* ditto */
    ...
    fprintf(in,"%s",data);    /* write data to the child using stdio */
    ...
    fgets(buf,sizeof(buf)-1,out); /* read from the child again with stdio */
    ...
    fclose(in);                 /* clean up after yourself */
    fclose(out);

Quote:}

else{                           /* i'm the child */

    close(input[WRITE_P]);      /* close this end, not going to be used */
    dup2(input[READ_P],STDIN_FILENO); /* now the read side of the pipe */
                                      /* is the child's stdin  */

    close(output[READ_P]);      /* close this end, not going to be used */
    dup2(output[WRITE_P],STDOUT_FILENO); /* stdout and stderr are now */
    dup2(output[WRITE_P],STDERR_FILENO); /* redirected back at the parent */
                                         /* thru the output pipe. */

    /* exec your command here */

    perror("your commmand");  /* if we get this far, exec failed */

Quote:}

...
--

Disclaimer: You know the drill.. all opinions are mine.. blah blah blah.
                 echo "Have a nice day!" >> /dev/kmem

 
 
 

Controlling forked processes stdin/stdout

Post by Marcio Eduardo Delamar » Fri, 12 Jan 1996 04:00:00


Look at "pipe" sys call man page. With it you can
control childern's input and output.

Delamaro