Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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. …
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.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

20 Experts available now in Live!

Get 1:1 Help Now