获取共享内存以创建超过6个子进程时出错

时间:2016-09-17 14:34:08

标签: c fork

我试图理解fork in c的工作原理。我想解决的问题是;给定f(上),我试图找到f(1)+ f(2)+ .. f(上)。 我想进行多进程编程以分配每个子进程并让每个子进程计算f(x)。

所以f(1),f(2)... f(上)由每个子进程计算。

父进程应计算以下f(1)+ .. + f(上)。 这是我的代码

test:
    [junit] Running org.jtaddeus.playground.LogicTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.141 sec
    [junit] Test org.jtaddeus.playground.LogicTest FAILED
    [junit] Running org.jtaddeus.playground.ValidatorTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.14 sec
    [junit] Test org.jtaddeus.playground.ValidatorTest FAILED

然而,每当我尝试使用超过6的参数运行此程序时。 我得到无效的参数错误。

1 个答案:

答案 0 :(得分:2)

你在pids

的范围之外写作
pids = (pid_t *) malloc(sizeof(int) * upper);
...
for(int i = 1; i <= upper; i++){
  pid = fork();
  if(pid == 0) {
    n = i;
    break;
  }
  pids[i] = pid; /* Here */
}

更改为

for(int i = 1; i < upper; i++){