* sched_child_runs_first doesn't work
@ 2017-12-08 3:07 Rock Lee
2017-12-08 5:53 ` Mulyadi Santosa
` (2 more replies)
0 siblings, 3 replies; 13+ messages in thread
From: Rock Lee @ 2017-12-08 3:07 UTC (permalink / raw)
To: kernelnewbies
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
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
2017-12-08 3:07 sched_child_runs_first doesn't work Rock Lee
@ 2017-12-08 5:53 ` Mulyadi Santosa
2017-12-08 9:27 ` Rock Lee
2017-12-08 7:50 ` Alexander Kapshuk
2017-12-08 11:18 ` YU Bo
2 siblings, 1 reply; 13+ messages in thread
From: Mulyadi Santosa @ 2017-12-08 5:53 UTC (permalink / raw)
To: kernelnewbies
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 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
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
2017-12-08 3:07 sched_child_runs_first doesn't work Rock Lee
2017-12-08 5:53 ` Mulyadi Santosa
@ 2017-12-08 7:50 ` Alexander Kapshuk
2017-12-08 9:35 ` Rock Lee
2017-12-08 11:18 ` YU Bo
2 siblings, 1 reply; 13+ messages in thread
From: Alexander Kapshuk @ 2017-12-08 7:50 UTC (permalink / raw)
To: kernelnewbies
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
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
2017-12-08 5:53 ` Mulyadi Santosa
@ 2017-12-08 9:27 ` Rock Lee
0 siblings, 0 replies; 13+ messages in thread
From: Rock Lee @ 2017-12-08 9:27 UTC (permalink / raw)
To: kernelnewbies
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 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
--
Cheers,
Rock
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
2017-12-08 7:50 ` Alexander Kapshuk
@ 2017-12-08 9:35 ` Rock Lee
0 siblings, 0 replies; 13+ messages in thread
From: Rock Lee @ 2017-12-08 9:35 UTC (permalink / raw)
To: kernelnewbies
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
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
2017-12-08 3:07 sched_child_runs_first doesn't work Rock Lee
2017-12-08 5:53 ` Mulyadi Santosa
2017-12-08 7:50 ` Alexander Kapshuk
@ 2017-12-08 11:18 ` YU Bo
2017-12-08 13:07 ` Rock Lee
2 siblings, 1 reply; 13+ messages in thread
From: YU Bo @ 2017-12-08 11:18 UTC (permalink / raw)
To: kernelnewbies
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-child-runs-first-work
Bo
>
>--
>Cheers,
>Rock
>
>_______________________________________________
>Kernelnewbies mailing list
>Kernelnewbies at kernelnewbies.org
>https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
2017-12-08 11:18 ` YU Bo
@ 2017-12-08 13:07 ` Rock Lee
0 siblings, 0 replies; 13+ messages in thread
From: Rock Lee @ 2017-12-08 13:07 UTC (permalink / raw)
To: 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-child-runs-first-work
>
> Bo
>
>>
>> --
>> Cheers,
>> Rock
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> Kernelnewbies at kernelnewbies.org
>> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
--
Cheers,
Rock
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
@ 2017-12-08 17:34 Perr Zhang
0 siblings, 0 replies; 13+ messages in thread
From: Perr Zhang @ 2017-12-08 17:34 UTC (permalink / raw)
To: kernelnewbies
maybe you should run the program like this:
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
@ 2017-12-08 17:36 Perr Zhang
2017-12-09 7:34 ` alireza sanaee
0 siblings, 1 reply; 13+ messages in thread
From: Perr Zhang @ 2017-12-08 17:36 UTC (permalink / raw)
To: kernelnewbies
maybe you should run the program like this:
taskset 1 ./your_test_app
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
2017-12-08 17:36 Perr Zhang
@ 2017-12-09 7:34 ` alireza sanaee
2017-12-09 8:19 ` valdis.kletnieks at vt.edu
0 siblings, 1 reply; 13+ messages in thread
From: alireza sanaee @ 2017-12-09 7:34 UTC (permalink / raw)
To: kernelnewbies
I think if it works in that way, it doesn't make sense at all!!!! Parent
and child ordering rules should preserve even on different cores!
On Fri, Dec 8, 2017 at 9:06 PM, Perr Zhang <strongbox8@zoho.com> wrote:
>
> maybe you should run the program like this:
>
> taskset 1 ./your_test_app
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20171209/9433e352/attachment.html
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
2017-12-09 7:34 ` alireza sanaee
@ 2017-12-09 8:19 ` valdis.kletnieks at vt.edu
0 siblings, 0 replies; 13+ messages in thread
From: valdis.kletnieks at vt.edu @ 2017-12-09 8:19 UTC (permalink / raw)
To: kernelnewbies
On Sat, 09 Dec 2017 11:04:09 +0330, alireza sanaee said:
> I think if it works in that way, it doesn't make sense at all!!!! Parent
> and child ordering rules should preserve even on different cores!
Find where in kernel/sched.c there's specific code to guarantee that
if the child/parent is started on one core, the other isn't scheduled again
until after the first one runs, rather than run immediately because it's
able to run on an otherwise idle core, so you don't get a 'first' or
'last', but 'at the same time'.
And if they run concurrently, the printf output will have a race
condition. At that point, the order of output is probably determined
by something other than which one scheduled first (the I/O stack
or exit() processing being the primary suspects).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 486 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20171209/8d57b26b/attachment-0001.bin
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
[not found] <lpg685rdn2epu90c9jcf5r3m.1512810666185@email.kingsoft.com>
@ 2017-12-09 15:42 ` strongbox8
2017-12-11 2:09 ` Rock Lee
0 siblings, 1 reply; 13+ messages in thread
From: strongbox8 @ 2017-12-09 15:42 UTC (permalink / raw)
To: kernelnewbies
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20171209/56ddccb5/attachment.html
^ permalink raw reply [flat|nested] 13+ messages in thread
* sched_child_runs_first doesn't work
2017-12-09 15:42 ` strongbox8
@ 2017-12-11 2:09 ` Rock Lee
0 siblings, 0 replies; 13+ messages in thread
From: Rock Lee @ 2017-12-11 2:09 UTC (permalink / raw)
To: kernelnewbies
On Sat, Dec 9, 2017 at 11:42 PM, strongbox8 <strongbox8@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
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2017-12-11 2:09 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-08 3:07 sched_child_runs_first doesn't work Rock Lee
2017-12-08 5:53 ` Mulyadi Santosa
2017-12-08 9:27 ` Rock Lee
2017-12-08 7:50 ` Alexander Kapshuk
2017-12-08 9:35 ` Rock Lee
2017-12-08 11:18 ` YU Bo
2017-12-08 13:07 ` Rock Lee
-- strict thread matches above, loose matches on Subject: below --
2017-12-08 17:34 Perr Zhang
2017-12-08 17:36 Perr Zhang
2017-12-09 7:34 ` alireza sanaee
2017-12-09 8:19 ` valdis.kletnieks at vt.edu
[not found] <lpg685rdn2epu90c9jcf5r3m.1512810666185@email.kingsoft.com>
2017-12-09 15:42 ` strongbox8
2017-12-11 2:09 ` Rock Lee
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).