sched_child_runs_first doesn't work

Alexander Kapshuk alexander.kapshuk at gmail.com
Fri Dec 8 02:50:53 EST 2017


If  my understanding of what Linus says in the post referenced below
is correct, there's never a guarantee which process would run first,
parent or child.
http://yarchive.net/comp/linux/child-runs-first.html

vfork(), on the other hand, is said in the post to always run the
child process first. See below for an example.
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>

int main(void)
{
        struct timeval tv;

        switch(vfork()) {
        case -1:
                perror("vfork failed\n");
                exit(-1);
        case 0:
                if (gettimeofday(&tv, NULL) == 0)
                        printf("child time sec %ld, usec %ld\n",
tv.tv_sec, tv.tv_usec);
                exit(0);
        default:
                if (gettimeofday(&tv, NULL) == 0)
                        printf("parent time sec %ld, usec %ld\n",
tv.tv_sec, tv.tv_usec);
        }
        return 0;
}

Sample run:
./gettimeofday
child time sec 1512719270, usec 716672
parent time sec 1512719270, usec 716748



More information about the Kernelnewbies mailing list