test1:
#include
#include
int main(void)
{
printf("this is in the new process...\n");
sleep(5);
printf("I have sleep 5 seconds...\n");
}
test2:
#include
#include
#include
void f(void)
{
printf("After exec the new process...\n");
}
int main(void)
{
if(fork()==0)
{
execv("./test1",NULL);
}
f();
}
test3:
#include
#include
#include
void f(void)
{
printf("After exec the new process...\n");
}
int main(void)
{
if(fork()==0)
{
if(execv("./test1",NULL)<0)
f();
}
}
test4:
#include
#include
#include
void f(void)
{
printf("After exec the new process...\n");
}
int main(void)
{
system("./test1");
f();
}
test5:
#include
#include
#include
void f(void)
{
printf("After exec the new process...\n");
}
int main(void)
{
system("./test1 &");
f();
}
test2运行结果:
this is in the new process...
After exec the new process...
I have sleep 5 seconds...
test3运行结果:
this is in the new process...
I have sleep 5 seconds...
test4运行结果:
this is in the new process...
I have sleep 5 seconds...
After exec the new process...
test5运行结果:
this is in the new process...
After exec the new process...
I have sleep 5 seconds...
test2:fork出新进程后执行execv执行新的进程,不影响原进程,如果不fork,那么新的进程会取代原进程
test3:execv执行成功后不返回,所以f()执行不到
test4:通过系统调用的方式,test1执行完成后system才有返回值,然后继续执行
test5:后台调用,直接返回,执行结果也test2相同