Hi, I know the vfork guarantees the children run first. I am just curious why sched_child_runs_first doesn't work. If there is a chance that child process run first, I would be more glade, since I guess that how it works, works with probability. However I tried more the 30+ times, the result was always father process ran first. Eventhough I have't tried that much, I guess my result still told the truth --- sched_child_runs_first doesn't work at all. On Fri, Dec 8, 2017 at 3:50 PM, Alexander Kapshuk <alexander.kapshuk@gmail.com> wrote:
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
-- Cheers, Rock