sched_child_runs_first doesn't work

Mulyadi Santosa mulyadi.santosa at gmail.com
Fri Dec 8 00:53:11 EST 2017


On Fri, Dec 8, 2017 at 10:07 AM, Rock Lee <rockdotlee at gmail.com> wrote:

> Hi,
>
> I ran my test code but always got the father process run first, even
> after setting sched_child_runs_first. Could anyone give me a hint?
> Here is my test code.
>
> #include <stdio.h>
> #include <unistd.h>
> #include <sys/time.h>
>
> int main(void)
> {
>     struct timeval tv;
>     struct timezone tz;
>     int err;
>     pid_t ret = fork();
>
>     if (ret == 0) {
>         err = gettimeofday(&tv, &tz);
>         if (!err)
>             printf("child time sec %d, usec %d\n", tv.tv_sec, tv.tv_usec);
>
>          printf("this child.\n");
>     } else if (ret > 1) {
>         err = gettimeofday(&tv, &tz);
>
>         if (!err)
>             printf("father time sec %d, usec %d\n", tv.tv_sec, tv.tv_usec);
>         printf("this father.\n");
>     } else
>         printf("err!!\n");
>     return 0;
> }
>
>
> --
> Cheers,
> Rock
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>


Hi...

IIRC, gettimeofday() will eventually call system call (with the same name,
I think). And I guess this is where the root cause come.

Your child might actually ran first, but since it called system call,
re-scheduling the kicked in, and parent got the chance to run.

In order to prove this theory, try to execute printf("this is parent") or
printf("this is child") first, then gettimeofday() later.

Good luck and cmiiw

-- 
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20171208/b6f8b0e9/attachment.html 


More information about the Kernelnewbies mailing list