popen() alternative - I am using the accepted
answer (pipe/fork/exec method) to solve my issue. Only difference is, instead of execl
, I am using execv
.
Now, my question is, does parent process have any control over child process created by execv
? Lets say this whole sequence suggested in accepted
answer is for tailing 1 file and I have many such files; I put this whole sequence in a function and if I call this function many times, at a point in time, is it possible to have many child tail
processes around?
What I want to know is, 1) Can I have multiple child processes running at any point in time? 2) how does child processes (created by execv) terminate? after execv
call does parent know when child process (created by execv) is done/terminated? - answered.
Execl and execv behave the same way. The only difference is how you specify the argument vector . The "l" functions take the argument vector as a comma-delimited list. Eg,
execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", NULL);
The "v" funtcions take the argument vector as an actual vector. Eg,
char *args[] = {"/bin/ls", "-r", "-t", "-l", NULL };
execv("/bin/ls", args);
See here or here for info about process control in C.
In general, a child process terminates when it's finished executing. The parent, or other processes, can kill the child at any time. A parent can use the waitpid() function to wait until a child has finished executing or to check if a child has finished executing.
父母接收到SIGCHLD
信号。
If you have a pipe from the child's stdout, you can often avoid signal/wait aggravation by just watching for the pipe to get EOF. You still have to reap the child to avoid zombies though http://en.wikipedia.org/wiki/Zombie_process (if you don't care about the child's exit status, this is normally done with a double fork so you run your real subprocess as a grandchild and reap the intermediate child). The GLib code is the most complex possible example probably: http://git.gnome.org/browse/glib/tree/glib/gspawn.c#n1191
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.