问题描述
我在这里想要一个小单元测试。但正如我所期望的程序不能正常工作。
I'm trying a small unit-testing here. But the program doesn't work as I expected.
char *args[2];
args[0] = (char*)"/usr/bin/firefox";
args[1] = NULL;
pid = fork();
printf("forked and my pid is %d\n",pid);
//check for errors
if (pid<0){
printf("Error: invoking fork to start ss has failed, Exiting\n ");
exit(1);
}
//the child process runs firefox
if (pid==0){
if (execv(args[0],args)<0){
perror("Error: running s with execvp has failed, Exiting\n");
}
printf("IVE BEEN KILLED\n");
}
//dad is here
printf("DAD IS GOING TO KILL U\n");
if (pid>0){
sleep(3);
kill(get_pidof(string("firefox")),SIGUSR1);
}
现在,我希望该程序如下运行:
1.孩子运行火狐(它一定支持)
2.爸爸打印爸爸要杀U(到目前为止好)
3,火狐将开放3秒,然后关闭(这是否太)
4,子进程将完成运行execv和打印IVE被打死。这不会发生。
Now, I would expect the program to run as follow:1.the child runs firefox (which it does)2.the dad prints DAD IS GOING TO KILL U (so far so good)3.the firefox would open for 3 seconds and then close (does that too)4.the child process would finish running execv and print "IVE BEEN KILLED". This doesn't happen.
我的目标是要知道,程序,execv RAN已经通过execv之后提出几个标志完成继续运行(其中香港专业教育学院被杀害的printf是)。
我有几千行的code和不希望使用其他的方法,除非必要的。
My aim is to know that the program which execv ran has finished by raising a few flags right after execv continues running (where the ive been killed printf is).I have a code of a few thousand lines and don't want to use other methods, unless necessary.
感谢
推荐答案
execv()
替换一个新的进程当前进程。当的火狐的通过 execv()
的的printf(我一直都被杀死\\ n)$ C $开始C>基本上不再存在在这个过程中,将永远不会被执行(见) 。这将是可能的父进程使用的或通过信号(如
信号(SIGCHLD,child_exit_handler)
),以警惕子进程的死亡。
execv()
replaces the current process with a new process. When firefox is started via execv()
the printf("IVE BEEN KILLED\n")
essentially no longer exists in the process and will never be executed (see man execv). It would be possible for the parent process to wait for the child, firefox, to exit by using wait() or to be alerted to the death of the child process via a signal (e.g. signal(SIGCHLD, child_exit_handler)
).
这篇关于fork()的杀()和execv()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!