sched_child_runs_first doesn't work

Rock Lee rockdotlee at gmail.com
Sun Dec 10 21:09:31 EST 2017


On Sat, Dec 9, 2017 at 11:42 PM, strongbox8 <strongbox8 at zoho.com> wrote:
> in my test, it has a chance to get the output from the child first using the
> taskset command. otherwise, the child and the parent are always on different
> cores in which case i think the parent is running and the child is on the
> rbtree of runqueue of other core just after fork at the most moment
>

Your explanation is right. Child process doesn't have chance to run
util parent's time slice is over(Even though there's no time slice
concept in CFS). From the print, it looked like parent process run
first. So we need force parent and child process re-schedule before
print, so that child prcess have the chance to compare "vruntime" with
parent process. In this situation, we could see child process run
first. I modified the test code like this, "sleep (1);" added :

#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();

    sleep(1);

    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;
}

Executed this app like this:

# taskset 1 ./fork

In the end, I could get child prcess run first sometimes.

-- 
Cheers,
Rock



More information about the Kernelnewbies mailing list