我想一个小单元测试在这里。但该计划不能按我的预期工作。现在fork()的杀()和execv()
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. 孩子跑的Firefox(它一定支持) 2.爸爸打印爸会杀了U(到目前为止好) 3. Firefox将打开3秒,然后关闭(也是如此) 4.子进程将完成运行execv并打印“IVE BE KILLED”。这不会发生。
我的目标是要知道哪些execv运行程序已经execv之后提出几个标志完成继续运行(其中香港专业教育学院被杀害的printf是)。 我有一个几千行代码,不想使用其他方法,除非必要。
感谢
我已经重新标记这是C.有没有C++代码中的例子可见一斑! – Rook