Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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…
Suggested Courses

730 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