Is this expected?

This cycle is expected to be busy, I think the wait call will take care of this. If so, how can this be done in order not to fulfill expectations?

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();
             }
    }
}
+3
source share
4 answers

waitwill cause the kernel to select other tasks that are not marked as locked, so this does not apply to the expected wait. Nor does it switch a bit overly for fork (), why not use a simple if statement?

+3
source

You are right, waitwaiting for the undeserved, transferring the processor to the core until the child comes out.

+1
source

( , wait()).

, , do stuff with child. ( 100%), , .

+1

, ( ), . OTOH , . , , .

+1

Source: https://habr.com/ru/post/1773255/


All Articles