C execv()函数是否终止子进程? [英] Does the C execv() function terminate the child proccess?

查看:296
本文介绍了C execv()函数是否终止子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码细分.

我有一个程序可以派生一个孩子(并将孩子的pid注册到文件中),然后执行自己的操作.子级变为程序员用argv修饰的任何程序.当子级执行完毕后,它将使用SIGUSR1发送信号给父级进程,因此父级知道要从文件中删除该子级.父级应该停一秒钟,通过更新其表来确认已删除的条目,然后从上次中断的地方继续.

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\n");  
    del(getpid());                     //Erases pid from file 
    refreshReq();                      //Sends SIGUSR1 to parent
    return 0;
}
default:{
    ... //Does its own thing
}
}

问题是execv成功启动和完成之后(返回0之前的printf语句让我知道),我看不到switch语句中正在执行的其余命令.我想知道execv是否像其中的^ C命令那样在执行完毕时杀死孩子,因此永远不会完成其余的命令.我查看了手册页,但没有找到关于此主题的任何有用信息.

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.

谢谢!

推荐答案

execv 用新进程替换当前进程.为了产生新的流程,您可以使用例如system()popen()fork()exec()

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()函数是否终止子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆