我正在尝试在运行 uClinux 的 SmartFusion2 SOM 上运行多个进程,但我只能在其上使用 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 时的同一个目录。但是由于我没有在上面的代码中发布文件路径,它应该适用于任何人。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句