简体   繁体   English

“ execv”子进程的存在和终止

[英]“execv” child process existance and termination

popen() alternative - I am using the accepted answer (pipe/fork/exec method) to solve my issue. popen()替代 -我正在使用accepted答案(pipe / fork / exec方法)来解决我的问题。 Only difference is, instead of execl , I am using execv . 唯一的区别是,我使用的是execv而不是execl

Now, my question is, does parent process have any control over child process created by execv ? 现在,我的问题是,父进程对execv创建的子进程有任何控制权吗? Lets say this whole sequence suggested in accepted answer is for tailing 1 file and I have many such files; 可以说,在accepted答案中建议的整个顺序是拖尾1个文件,而我有很多这样的文件。 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? 我将整个序列放在一个函数中,如果我在某个时间点多次调用此函数,是否可能会有很多子tail进程?

What I want to know is, 1) Can I have multiple child processes running at any point in time? 我想知道的是,1)我可以在任何时间运行多个子进程吗? 2) how does child processes (created by execv) terminate? 2)子进程(由execv创建)如何终止? after execv call does parent know when child process (created by execv) is done/terminated? execv调用之后,父母是否知道子进程(由execv创建)何时完成/终止? - answered. -回答。

Execl and execv behave the same way. Execl和execv的行为方式相同。 The only difference is how you specify the argument vector . 唯一的区别是您如何指定参数向量 The "l" functions take the argument vector as a comma-delimited list. “ l”函数将参数向量作为逗号分隔的列表。 Eg, 例如,

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

The "v" funtcions take the argument vector as an actual vector. “ v”函数将参数向量作为实际向量。 Eg, 例如,

char *args[] = {"/bin/ls", "-r", "-t", "-l", NULL };
execv("/bin/ls", args);

See here or here for info about process control in C. 有关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. 父母可以使用waitpid()函数来等待,直到孩子完成执行或检查孩子是否完成执行。

父母接收到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. 如果您的管道来自孩子的性病,通常可以通过观察管道是否达到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). 您仍然可以通过http://en.wikipedia.org/wiki/Zombie_process收割孩子以避免僵尸(如果您不关心孩子的退出状态,通常使用双叉来完成,因此您可以真正子进程作为孙子并获得中间子)。 The GLib code is the most complex possible example probably: http://git.gnome.org/browse/glib/tree/glib/gspawn.c#n1191 GLib代码可能是最复杂的示例: http : //git.gnome.org/browse/glib/tree/glib/gspawn.c#n1191

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM