Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C Program: Get both stdin and stdout descriptors of sub-process started by a process

Posted on 2004-09-06
4
Medium Priority
?
489 Views
Last Modified: 2013-12-26
I have an existing program that uses stdin/stdout to communicate.  Let's call it v.exe. I do not have the code, just the executable.
v.exe is an interactive program, so until the stdin received is QUIT, v.exe takes the stdin, does some processing, then writes more data to stdout.

I need to write a new program, called log.exe, that once inititated, calls v.exe.  
The program log.exe will display to its stdout the stdout of v.exe.
Similarly, any stdin received by log.exe will be passed to the stdin of v.exe.

If from within log.exe a command like

fd = popen("v.exe", "w");  

is used, fd will be the stdout of v.exe.  How do I get a file descriptor for stdin of v.exe?  
0
Comment
Question by:pheiss
4 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 1000 total points
ID: 11994899
The way I would do this is to create a pipe() before fork(), then in the child, do a dup() of one end of the pipe to 0 (stdin). Then exec() the child process. Anything written to the pipe by the parent process will become stdin for the child...

// code snippet... (untested but should get you pointing in the right direction)
int mpipe[2];
pid_t pid;
FILE *fp;

(void)pipe( mpipe );
pid = fork();

if( pid != 0 ) {
    dup2( mpipe[1], 0 );
    exec();
}

// parent code here...
fp = fdopen( mpipe[0], "w" );
fprintf( fp, "hello child\n" ); // should get sent to child process as STDIN
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 1000 total points
ID: 11995261
cjjclifford is right .... you need to create a bidirectional pipe between the two processes ... popen allows only a unidirectional communication channel

here is an excellent graphical tutorial with some code to use
http://pandonia.canberra.edu.au/OS/l9_1.html
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question