【发布时间】:2018-04-01 01:48:39
【问题描述】:
我希望我的 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
【问题讨论】:
-
尝试在 printf 的末尾添加一个 \n..