Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now