我正在尝试创建一个程序,它将重复执行./program很多次遍布3个fork()ed进程。每个进程都有自己的一组数字,用作./program的输入。 由于exec函数将结束进程,我让每个子进程分叉到孙子进程以执行execvp(./ program)。这是我到目前为止的代码,但它似乎不太正确,我试图用gdb调试它,但更加困惑。目前它以Abort Trap 6结束 - 任何建议或建议都将非常感谢。
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
pid_t children[3];
int x = 0;
int minRange = 0;
int maxRange = 0;
int totalMax = 2000;
int numProcesses = 0;
if(argc != 2)
{
fprintf(stderr, "Error: invalid arguments.\n\n");
return 1;
}
numProcesses = atoi(argv[1]);
for (x = 0; x < numProcesses; x++)
{
minRange = ((totalMax / numProcesses) * x);
maxRange = ((totalMax / numProcesses) * (x + 1));
children[x] = fork();
if(children[x] == 0)
{
pid_t g_pid;
int i;
for(i = minRange; i < maxRange; i++)
{
printf("\n\n here 2 \n\n");
g_pid = fork();
if(g_pid == 0)
{
printf("\n\n here s \n\n");
char *argv[4];
argv[0] = "./program";
argv[1] = "numtest.txt";
argv[2] = "435";
argv[3] = NULL;
execvp(argv[0], argv);
}
break;
}
break;
}
else
continue;
}
}