1

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.

Community
  • 1
  • 1
hari
  • 9,439
  • 27
  • 76
  • 110
  • If you are able to use it, GLib has a bombproof implementation of this stuff: http://developer.gnome.org/glib/2.30/glib-Spawning-Processes.html It is pretty difficult to get it all right (I know because I wrote the GLib one, of course it may not be all right still, but it handles a ton of details) – Havoc P Dec 11 '11 at 23:19
  • The phrase "child process created by execv" is incorrect. execv does not create a child. – William Pursell Dec 13 '11 at 03:07

3 Answers3

3

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. E.g.,

execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", NULL);

The "v" funtcions take the argument vector as an actual vector. E.g.,

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.

Christopher Neylan
  • 8,018
  • 3
  • 38
  • 51
2

The parent receives a SIGCHLD signal.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Thanks Oli. In the whole sequence, can I do it in such a way that even if I need to do tail (or any other command for that matter) for 10 (for example) files, I do it one after another. I do not want to have more than 1 child process alive at any point in time. Is that possible? – hari Dec 11 '11 at 22:49
  • thanks Oli. I have another probably a little different question: when I do `kill` on the child, does it release all the resources right away? does process table of OS knows about its death immediately? – hari Dec 11 '11 at 23:13
  • if you kill a process, yes, all of its resources will be released by the operatin system except for an entry in the process table containing the child's return value. the child is considered then to be a zombie. your parent should "reap" the child process by using wait(), which will collect its return value and release the process table entry. if your parent process dies before it reaps its children, init will do the reaping for it. see the accepted answer here: http://stackoverflow.com/questions/2353079/reaping-zombie-process-child – Christopher Neylan Dec 11 '11 at 23:26
1

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

Havoc P
  • 8,365
  • 1
  • 31
  • 46
  • If the child judiciously closes all fds, you're outta luck. – jørgensen Dec 11 '11 at 23:35
  • Often the whole point is to capture the child's stdout, so it's safe to assume it will either not close stdout, or that if it does, you're done with it anyhow. If your goal is something other than slurping the child's output, then you aren't in the "often" case, of course. – Havoc P Dec 11 '11 at 23:50
  • @Havoc P: Thanks for responding. I am on `FreeBSD`. My problem is, I am using this pipe/fork/exec method and I am seeing a lot of zombies at a point in time - that fills up my processes table (this is a low-end box) and thats why I am killing my main parent process and getting out of the program. Now, some thing is creating a lot of child processes inside my main program (parent process). But I am not sure how that is possible. But when the fork() fails, (because of max proc) and at that time I see a lot of with parent as the main process. How can I trace this back to who culprit? – hari Dec 12 '11 at 00:02
  • @jørgensen Can you please elaborate more? – hari Dec 12 '11 at 00:11
  • zombies are because someone didn't waitpid their child ... you basically have to stare at the code and find the missing cleanups. or use the intermediate child approach to avoid the need to reap later. – Havoc P Dec 12 '11 at 00:13