how to wait until a child blocks?
- From: Christof Warlich <cwarlich@xxxxxx>
- Date: Wed, 14 Jan 2009 12:36:35 +0100
Hi,
the following few lines are doing what I want, i.e.
printing a prompt _before_ waiting for new input, but
_after_ the content of "someFile" has been printed:
#include <unistd.h>
#include <malloc.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
int main(void) {
char *line = 0;
size_t n = 0;
FILE *pp = popen("cat someFile -", "w");
timespec ts = {0, 10000000};
nanosleep(&ts, 0);
while(true) {
fprintf(stderr, "> ");
fflush(stderr);
getline (&line, &n, stdin);
if(!strcmp(line, "END\n")) {
break;
}
fprintf(pp, line);
}
fflush(stdout);
pclose(pp);
free(line);
}
E.g., with someFile containing:
Hi
Bye
I get:
$./a.out
Hi
Bye
> END
END was typed in to end the program.
But when I remove the clumsy nanosleep hack, I get
$./a.out
> Hi
Bye
END
How could this be achieved in a clean way? I would need to somehow
wait until the process being created by popen() blocks as it waits
for additional input being typed in. How could this be done?
Thanks for any help,
Christof
.
- Follow-Ups:
- Re: how to wait until a child blocks?
- From: David Schwartz
- Re: how to wait until a child blocks?
- From: Barry Margolin
- Re: how to wait until a child blocks?
- Prev by Date: mq_timedreceive() ignoring timeout value
- Next by Date: how to prevent second SIGINT from killing program
- Previous by thread: mq_timedreceive() ignoring timeout value
- Next by thread: Re: how to wait until a child blocks?
- Index(es):
Relevant Pages
|