sched_child_runs_first doesn't work
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
On Fri, Dec 8, 2017 at 10:07 AM, Rock Lee <rockdotlee@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@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
Hi, I tried a test in that code without any gettimeofday, the result is the same. BTW, I've tried on my raspberrpi 3 with kernel 4.13. Moreover, I also tried several ubuntu 16.04. The result were always father process ran first. On Fri, Dec 8, 2017 at 1:53 PM, Mulyadi Santosa <mulyadi.santosa@gmail.com> wrote:
On Fri, Dec 8, 2017 at 10:07 AM, Rock Lee <rockdotlee@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@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
-- Cheers, Rock
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
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
Hi, On Fri, Dec 08, 2017 at 11:07:15AM +0800, Rock Lee 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; }
Maybe here: https://stackoverflow.com/questions/17391201/does-proc-sys-kernel-sched-chil... Bo
-- Cheers, Rock
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
I've read the post before, and I've seen a lot of saying "sched_child_runs_first just not a guarantee of anything". But still, there should be a moment that child process ran first. From my experiment, there was no single time that child process ran before father process. Isn't it interesting ? On Fri, Dec 8, 2017 at 7:18 PM, YU Bo <tsu.yubo@gmail.com> wrote:
Hi,
On Fri, Dec 08, 2017 at 11:07:15AM +0800, Rock Lee 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; }
Maybe here:
https://stackoverflow.com/questions/17391201/does-proc-sys-kernel-sched-chil...
Bo
-- Cheers, Rock
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- Cheers, Rock
participants (4)
-
Alexander Kapshuk -
Mulyadi Santosa -
Rock Lee -
YU Bo