Tags: dear, drive, exnternal, implementing, interactively, pipe, program, programming, pure, tcl, usd

Implementing pipe in pure C

On Programmer » TCL

2,431 words with 1 Comments; publish: Thu, 01 May 2008 00:10:00 GMT; (20078.13, « »)

Dear all,

I used to usd the Tcl pipe to interactively drive an exnternal program.

Now, I may need to do it in pure C, without using tcl. Can any one give

me some hints how to do so on windows box? Maybe you can give me some

hints on how the tcl pipe is implemented on Windows box.

Thanks a lot!

Best regards,

Jingzhao

All Comments

Leave a comment...

  • 1 Comments
    • Jingzhao Ou wrote:

      > I used to usd the Tcl pipe to interactively drive an exnternal program.

      > Now, I may need to do it in pure C, without using tcl. Can any one give

      > me some hints how to do so on windows box? Maybe you can give me some

      > hints on how the tcl pipe is implemented on Windows box.

      The source code for Tcl will give you all the hints you want about how

      Tcl implements it, but there might be considerably more overhead there

      than you need.

      Not exactly a Tcl question, but...

      1. Use CreatePipe() three times, once each for stdin, stdout, stderr.

      2. Use DuplicateHandle() with bInheritHandle FALSE and

      DUPLICATE_SAME_ACCESS on each of the pipes.

      3. Close the inheritable (original) versions of the read end of the

      stdout and stderr pipes.

      4. Close the inheritable (original) version of the write end of the

      stdin pipe.

      5. Call CreateProcess() with startup info hStd(InOut)Put * hStdError

      set to the remaining three inheritable handles.

      6. Create three threads, one that will sit doing blocking reads on

      the stdout pipe handle, on the stderr pipe handle, and one that

      will write to the stdin pip handle.

      When "done" with the pipe:

      7. Close the stdin pipe handle. [1]

      8. Close the stdout pipe handle. [1]

      9. Close the stderr pipe handle. [1]

      10. GetExitCodeProcess() the child's hProcess. TerminateProcess()

      it if it's still running. [2]

      11. Wait for threads to terminate, or end them. [3]

      12. Close the process and thread handles to the child. [4]

      [1] There might be reasons to close the stdin pipe handle earlier,

      for example, if a pipeline is expecting stdin to close before it will

      complete and flush output.

      [2] Or not. There are reasons why you may want the child process to

      live on.

      [3] Or not. They might be re-used doing something else.

      [4] Only if you expect it to exit, or desire no more direct control over

      it if it does not.

      HTH.

      MKS

      MKS

      #1; Thu, 01 May 2008 00:11:00 GMT