我必须分叉一些子进程,并在名为PCB的结构体数组中跟踪一些值。我使用了一个名为forker的函数来分叉子进程,并试图在其中修改结构体数组,但是当我在fork函数后打印整个结构体时,它是空白的且全是零。forker函数跟踪已启动的所有子进程的总数,我使用它作为数组的索引。有任何想法为什么它不保存我的结构体中的值吗?
分叉函数
int forker(int totaltoLaunch, int simulLimit, int timeLimit,
int totalLaunched, PCB *processTable)
{
pid_t pid;
if (totalLaunched == simulLimit) {
return (totaltoLaunch);
}
else if (totaltoLaunch > 0)
{
if ((pid = fork()) < 0)
{
perror("fork");
}
else if (pid == 0)
{
processTable[totalLaunched].occupied = 1;
processTable[totalLaunched].pid = pid;
processTable[totalLaunched].startSeconds = 0;
processTable[totalLaunched].startNano = 1;
printf("I am a child and supposed to run %d seconds\n", timeLimit);
exit(0);
}
else if (pid > 0)
{
forker(totaltoLaunch - 1, simulLimit, timeLimit,
totalLaunched + 1, processTable);
}
}
else
return (0);
}
这是我创建进程表的方式 PCB processTable[20];
processTable
,但我猜你打印的是父进程的processTable
。 - ikegami