Is this loop busy waiting, I would think the wait call takes care of that. If it is, how can it be fixed to not busy wait?
id = fork();
for (i = 0; i < 20; i++)
{
switch (id)
{
case 0:
/* do stuff with child */
exit(0);
default:
{
if (children>=3) {
int s;
wait(&s);
children--;
}
children++;
id = fork();
}
}
}
waitwill cause the kernel to select other jobs that are not marked as blocked, so this isn’t a case of busy-waiting. Also isn’t switch a bit excessive for a fork(), why not use a simple if statement?