JavaMan
JavaMan

Reputation: 5034

Creating Child Process With StdOut and StdErr Redirected

I want to create a program that can create a process for any arbitrary exe file while having that exe file's stderr and stdout captured i.e. redirected to my program. The sequence of output from the target arbitrary exe file process MUST BE PRESERVED. i.e. if it sends 10 char to stdout, then 10 char to stderr, and then 10 char back to stdout, my program needs to recognized the fact that the child is alternating between stderr and stdout rather than simply treating stderr and stdout as 2 independent streams. Or like this:

stdout 10 bytes, stderr 10 bytes, stdout 10 bytes, stderr 3 bytes,.........

I know the standard way to redirect stdout and stderr is to create 2 anonymous pipes and having that 2 pipes inherited by the created child process. But the problem is that anonymous pipe is blocking i/o. If I use 2 threads in the parent, one for reading the child's stdout and another for reading the child's stderr, I cannot tell the fact that the child is alternating between stdout and stderr i.e. the sequence information listed above is lost.

EDIT:

I am writing a proxy for cl.exe, lib.exe and link.exe. The build system bundled with Mozilla open source projects (NSPR) use some very unique ways to build the source files in order to make it portable across multiple platforms and autodetect the capability of the target compiler (i.e. autoconf and the configure script). It dynamically generates some .h files and #define when executing "make". It even invokes some python scripts to invoke the cl.exe.

I have no idea what the Mozilla build system is doing nor why it needs to use some python scripts to invoke cl.exe (which apparently analyze the stdout/stderr of the cl.exe command). I don't want to spend time studying it. But all I know is that it must invokes cl.exe, lib.exe and link.exe in the end and the interfaces to these commands must be command line arguments, env variables and stdout/stderr. So I need to write a proxy cl.exe which calls the real cl.exe and supplies my own command line arg's and returns whatever stderr and stdout to the caller as transparent as possible.

The characteristics of cl.exe is that it may return error messages in stderr and normal messages in stdout. I just want a transparent way to insert a proxy between the real cl.exe and the build system.

Upvotes: 1

Views: 692

Answers (1)

Slartibartfast
Slartibartfast

Reputation: 8805

I'm not sure why you want to do that, but what you asked can be done like this:

You have a thread per stream, both threads share common queue or something, once they read byte from a stream, they acquire a lock and put some sort of object into common queue. Even this wouldn't guarantee correctness, but I guess it would be good enough.

This approach would have huge overhead, because it would create object per read byte. Depending what you are expecting as output, maybe waiting for entire line would have more sense.

You might want to expand your question with more specifics of what you are trying to achieve?

EDIT: Why don't you just output child's stdout and std err to your own?

Upvotes: 1

Related Questions