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?
views:
54answers:
2
+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
2010-08-14 04:04:50
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
2010-08-14 04:15:44
@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
2010-08-14 04:18:28
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
2010-08-14 04:27:44
@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
2010-08-14 04:33:44
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
2010-08-14 05:11:48
//***************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
2010-08-14 05:12:18
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
2010-08-14 05:17:36
@joseph: I think the problem is that the parent fails to "press return" after "typing" `23`. Add a `\n` to the `write` call.
Potatoswatter
2010-08-14 06:12:15
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
2010-08-14 06:17:23
@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
2010-08-14 07:37:42
where to put fflush(stdout); in the code ? i mean to ask after which line i should put fflush(stdout);
joseph
2010-08-14 09:20:19
@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
2010-08-14 16:09:13