My question is extension of this one: popen creates an extra sh process
Motives:
1) My program need to create a child which does tail
on a file. I need to process the output line by line. That is why I am using popen
because it returns FILE *. I can easily fetch single line, do what I need to do and print it.
One problem with popen is that you do not get pid of child (tail command in my case).
2) My program should not exit before its child is done. So I need to do wait
; but without pid, I cannot do it.
How can I achieve both the goals?
A possible (kludge) solution: do execvp("tail -f file > tmpfile") and the keep reading that tmpfile. I am not sure how good this solution is, though.
pipe
, a function of theexec*
family andfdopen
. This is non-standard, but so ispopen
.wait
. Just read the pipe up toEOF
.execvp("tail -f file > tmpfile")
won't work, redirection is a feature of the shell and you're not running the shell here. Even if it worked it would be an awful solution. Suppose you have read to the end of the file, but the child process has not ended yet. What do you do?Why aren't you using pipe/fork/exec method?
I'm not sure why you need the process ID of the child. When the child exits, your pipe read will return an EOF. If you need to terminate the child, just close the pipe.
You can use
wait
as it doesn't want a PID to wait for but simply waits for the any child process to exit. If you have created other child processes you can keep track of them, and ifwait
returns an unknown PID you can assume it's from yourpopen
process.