一、目的
测试fork 以后的子进程调用在别的路径下存放的可执行文件。
二、code
1、父进程:fork.c
#include <unistd.h>
#include <stdio.h>
int main()
{
int pid = -1;
printf("------ This is the main progress---------------\n");
printf("------Use execv function run external image-----------\n");
pid = fork();
if(pid < 0)
{
printf("fork error\n");
}
else if(pid == 0)
{
const char *path = "/test/fork-test/hello";
int ret = 0 ;
ret = execv(path,NULL);
if(ret < 0)
{
printf("execv run error\n");
}
}
return 0;
}
其中test/fork-test/hello是由hello.c 文件生成
#include <stdio.h>
int main()
{
printf("---------------start Run in hello process---------------------\n");
printf("-----------------Hello World-----------------------------------\n");
}
编译hello.c 生成image hello,然后编译 fork.c 生成可执行文件,执行该可执行文件显示:
------ This is the main progress---------------
------Use execv function run external image-----------
---------------start Run in hello process---------------------
-----------------Hello World-----------------------------------
三、summary
如果不同的进程有好多人开发维护,在系统启动时候可以使用该方式把若干进程按照自己想要的顺序启动起来,实现多线程的启动。