使用 vfork() 的多个进程
Multiple Processes with vfork()
我正尝试在 SmartFusion2 SOM 运行ning uClinux 上 运行 多个进程,但我只能在其上使用 vfork() 而不能使用 fork() 。我一直在尝试 运行 以下代码来测试 运行 多个进程,但我没有得到我想要的结果。该代码应该同时 运行 两个不同的程序,但我遇到了 SEGV 错误。
代码如下:
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(){
pid_t pid;
pid = vfork();
if(pid > 0){
printf("I am the parent of pid = %d\n", pid);
execve("/home/path/to/executable2", NULL, NULL);
}
else if (!pid){
printf("I am the baby\n");
execve("/home/path/to/executable1", NULL, NULL);
}
else if (pid == -1){
perror("fork");
}
return 0;
}
它编译得很好,但我的输出是这样的:
I am the baby
I am the parent of pid = 140
SEGV
谁能帮我看看我做错了什么?
原来我的代码是正确的,但我使用的文件路径是我计算机上的根目录,而不是 运行 关闭 SmartFusion2 时的同一目录。但是由于我没有 post 上面代码中的文件路径,所以它应该对任何人都可以正常工作。
我正尝试在 SmartFusion2 SOM 运行ning uClinux 上 运行 多个进程,但我只能在其上使用 vfork() 而不能使用 fork() 。我一直在尝试 运行 以下代码来测试 运行 多个进程,但我没有得到我想要的结果。该代码应该同时 运行 两个不同的程序,但我遇到了 SEGV 错误。
代码如下:
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(){
pid_t pid;
pid = vfork();
if(pid > 0){
printf("I am the parent of pid = %d\n", pid);
execve("/home/path/to/executable2", NULL, NULL);
}
else if (!pid){
printf("I am the baby\n");
execve("/home/path/to/executable1", NULL, NULL);
}
else if (pid == -1){
perror("fork");
}
return 0;
}
它编译得很好,但我的输出是这样的:
I am the baby
I am the parent of pid = 140
SEGV
谁能帮我看看我做错了什么?
原来我的代码是正确的,但我使用的文件路径是我计算机上的根目录,而不是 运行 关闭 SmartFusion2 时的同一目录。但是由于我没有 post 上面代码中的文件路径,所以它应该对任何人都可以正常工作。