tags:

views:

54

answers:

2

I have to execute any other program from my C program which is continuouslly giving messages (intermediate result) on stdout.These messages(intermediate result) i can retrieve after exec finishes its execution(other program is terminated) but my problem is that if execution of exec command is going on ,then how to retrieve these messages(intermediate result) line by line? Based on these Intermediate results i have to provide some inputs too to exec command(provide input to other program) ,how to do this?

+2  A: 

See pipe(2). Use the input file descriptor for the stdin of the child process which calls exec.

To move the file descriptor, do

dup2( my_pipe[0], stdin);

in the child after the fork.

Or, use popen(3) as Chao suggests.

Potatoswatter
i have done similar way(used pipe for stdout) ,but in this case i am able to read from pipe only when exec command finishes(other program execution finishes) but i am not able to retrieve intermediate results parallely while exec command execution going on
joseph
@joseph, You need to `fork` your process so the parent process can send and receive information to the `exec`'d process. Or am I misunderstanding you?
strager
i have fork my process, in child process first i using pipe to duplicate stdout( dup2( my_pipe[1], stdout);) then i am executing exec commandand in parent process i am reading intermediate result in loop(using read command on my_pipe[0]) while debugging i found thatread does not give any o/p until exec finishes(it gets blocked on read command if exec has not finished) but i need these intermediate result parallely(as soon as exec gives this intermediate result)
joseph
@Joseph: Sorry then, I guess I got the question backward. I reversed the sense of the answer. You might be looking for two pipes in opposite directions mapped to `stdin` and `stdout`, which is essentially what `popen` gets you anyway.
Potatoswatter
This is the code which i have written as per requirment which gets hangs at fgets() please see what modificatication or where my approach is going on?pid = fork (); if (pid == (pid_t) 0) {//*********chid process code********************************** close (output[0]); dup2 (output[1], STDOUT_FILENO); close (input[1]); dup2 (input[0], STDIN_FILENO); execvp("./a.out",NULL); }
joseph
//***************parent process code*******************************else { close (input[0]); FILE* stream; close (output[1]); stream = fdopen (output[0], "r"); char *lMssg=new char[MAX_LENGTH]; int lBufferLength=MAX_LENGTH; while(fgets(lMssg,lBufferLength,stream)) { cout<<"Message is:"<<lMssg<<endl; write(input[1],"23",strlen("23")); } close(input[1]); close (output[0]); waitpid (pid, NULL, 0); }
joseph
This is the sample program which i have to execute using exec command and retrieve intermediate result and provide input to this program#include <stdio.h>int main(){int i; printf ("enter your message\n"); scanf("%d", printf("you have entered :%d \n",i);printf ("again enter your message \n"); scanf("%d", printf("you have entered second time %d\n",i);printf ("again enter your message \n"); scanf("%d", printf("you have entered third time %d\n",i);}
joseph
@joseph: I think the problem is that the parent fails to "press return" after "typing" `23`. Add a `\n` to the `write` call.
Potatoswatter
In this case atleast line Message is:<message>(please enter yor message) should be printed and then it should be hanged at write() commandbut this line is also not getting printes it means this program hangs on read() command
joseph
@joseph: You need to flush the output. C++ iostreams does this at every `endl`, but in C you have to call `fflush(stdout);` or `fflush(NULL)` to send the data from the buffer to the pipe.
Potatoswatter
where to put fflush(stdout); in the code ? i mean to ask after which line i should put fflush(stdout);
joseph
@joseph: Every time you need data written by `printf` sent to the other process. Looks like here that is after each time printing "enter your message."
Potatoswatter
+4  A: 

popen(3) will start a program, giving you back a FILE* you can use to read the process's output.

cHao