问题描述
这是我的代码的细分.
我有一个程序可以派生一个孩子(并将孩子的 pid 注册到一个文件中),然后做自己的事情.孩子成为程序员用 argv 有尊严的任何程序.当子进程完成执行时,它会向父进程发送一个信号(使用 SIGUSR1),以便父进程知道从文件中删除子进程.父级应该停止一秒钟,通过更新其表来确认已删除的条目,然后从中断的地方继续.
pid = fork();开关(PID){情况1:{退出(1);}案例0:{(*table[numP-1]).pid = getpid();//全局存储pid添加();//将表格保存到文本文件中自由(表);//释放表execv(argv[3], &argv[4]);//使用argv执行新程序printf("执行完毕
");德尔(getpid());//从文件中删除pid刷新请求();//发送SIGUSR1给父母返回0;}默认:{...//做自己的事}}
问题是 after execv 成功启动并完成(返回 0 之前的 printf 语句让我知道),我没有看到 switch 语句中的其余命令正在执行.我想知道 execv 中是否有一个 ^C 命令,它在完成时会杀死孩子,因此永远不会完成其余的命令.我查看了手册页,但没有发现任何关于该主题的有用信息.
谢谢!
execv 用新进程替换当前进程.为了产生一个新进程,您可以使用例如system()
、popen()
或 fork()
和 exec()
Heres a breakdown of my code.
I have a program that forks a child (and registers the child's pid in a file) and then does its own thing. The child becomes any program the programmer has dignified with argv. When the child is finished executing, it sends a signal (using SIGUSR1) back to the parent processes so the parent knows to remove the child from the file. The parent should stop a second, acknowledge the deleted entry by updating its table, and continue where it left off.
pid = fork();
switch(pid){
case -1:{
exit(1);
}
case 0 :{
(*table[numP-1]).pid = getpid(); //Global that stores pids
add(); //saves table into a text file
freeT(table); //Frees table
execv(argv[3], &argv[4]); //Executes new program with argv
printf("finished execution
");
del(getpid()); //Erases pid from file
refreshReq(); //Sends SIGUSR1 to parent
return 0;
}
default:{
... //Does its own thing
}
}
The problem is that the after execv successfully starts and finishes (A printf statement before the return 0 lets me know), I do not see the rest of the commands in the switch statement being executed. I am wondering if the execv has like a ^C command in it which kills the child when it finishes and thus never finishes the rest of the commands. I looked into the man pages but did not find anything useful on the subject.
Thanks!
execv replaces the current process with a new one. In order to spawn a new process, you can use e.g. system()
, popen()
, or a combination of fork()
and exec()
这篇关于C execv() 函数是否终止子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!