Solved

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

Posted on 2004-09-06
4
483 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 250 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 250 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

628 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