fork()的行为不符合预期

时间:2018-04-01 01:48:39

标签: fork xv6

我希望我的fork()允许我的子进程打印它们在循环中的位置以测试我的循环调度算法,但计数永远不会被打印。更具体地说,父进程执行所有操作,我从未看到来自我的孩子的打印。有什么想法吗?我可能会误解fork()或wait()的使用。

for (i = 0; i < 64; i++)
  {
    pid = fork();
    if (pid < 0) {
      cprintf("fork failed\n");
      return -1;
    } else if (pid > 0) {
      cprintf("Parent: child process has pid: %d\n", pid);
      wait();
      cprintf("Parent: child process %d has completed\n", pid);
    } else if (pid == 0) {
      for (int n = 0; n < 1000; n++)
      {
        cprintf("Child: this child is at %d in the loop", n);
      }
      exit();
    }
}

感谢。

编辑: 输出如下

$ testscheduler
Test Scheduler

Round Robin Test
Parent: child process has pid: 4
Parent: child process 4 has completed
Parent: child process has pid: 5
Parent: child process 5 has completed
Parent: child process has pid: 6
Parent: child process 6 has completed
Parent: child process has pid: 7
Parent: child process 7 has completed
Parent: child process has pid: 8
Parent: child process 8 has completed
Parent: child process has pid: 9
Parent: child process 9 has completed
Parent: child process has pid: 10
Parent: child process 10 has completed
Parent: child process has pid: 11
Parent: child process 11 has completed
...
...
...
Parent: child process has pid: 66
Parent: child process 66 has completed
Parent: child process has pid: 67
Parent: child process 67 has completed

1 个答案:

答案 0 :(得分:0)

您的用户模式程序似乎有效。

我怀疑您修改了调度算法,请编辑您的帖子以获取更多信息。