Copy-on-Write

Copy-on-Write

am 06.02.2008 06:49:16 von Rick Brown

Hi,

I had read that the operating systems that use copy-on-write mechanism
for fork(), it is better if they deliberately allow the CHILD to run
first.

This would be better because in 99% of the cases child will call
exec() and the new address space will be allocated. Instead if the
parent is executes first, an unnecessary copy of the pages is made (if
parents writes) and later on when child executes, a fresh address
space is executed.

So in linux, is a child run first or the parent? Can we rely on this
information?

TIA

Rick
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: Copy-on-Write

am 06.02.2008 12:00:03 von Manish Katiyar

On Feb 6, 2008 11:19 AM, Rick Brown wrote:
> Hi,
>
> I had read that the operating systems that use copy-on-write mechanism
> for fork(), it is better if they deliberately allow the CHILD to run
> first.
>
> This would be better because in 99% of the cases child will call
> exec() and the new address space will be allocated. Instead if the
> parent is executes first, an unnecessary copy of the pages is made (if
> parents writes) and later on when child executes, a fresh address
> space is executed.
>
> So in linux, is a child run first or the parent? Can we rely on this
> information?

No with fork() it is not guaranteed.......however if you use vfork()
child is guaranteed to run first......

>
> TIA
>
> Rick
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecartis@nl.linux.org
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>



--
Thanks & Regards,
********************************************
Manish Katiyar ( http://mkatiyar.googlepages.com )
3rd Floor, Fair Winds Block
EGL Software Park
Off Intermediate Ring Road
Bangalore 560071, India
***********************************************
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

RE: Copy-on-Write

am 06.02.2008 13:33:33 von Rajat Jain

Hi,

>>
>> I had read that the operating systems that use copy-on-write
>> mechanism for fork(), it is better if they deliberately allow the
>> CHILD to run first.
>>
>> This would be better because in 99% of the cases child will call
>> exec() and the new address space will be allocated. Instead if the
>> parent is executes first, an unnecessary copy of the pages is made
>> (if parents writes) and later on when child executes, a fresh
>> address space is executed.
>>
>> So in linux, is a child run first or the parent? Can we rely on this
>> information?
>
> No with fork() it is not guaranteed.......however if you use vfork()
> child is guaranteed to run first......
>

So as the author says, isn't running child first a good idea that needs to be implemented?

Thanks,

Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: Copy-on-Write

am 06.02.2008 13:41:48 von Manish Katiyar

On Feb 6, 2008 6:03 PM, Rajat Jain wrote:
>
> Hi,
>
> >>
> >> I had read that the operating systems that use copy-on-write
> >> mechanism for fork(), it is better if they deliberately allow the
> >> CHILD to run first.
> >>
> >> This would be better because in 99% of the cases child will call
> >> exec() and the new address space will be allocated. Instead if the
> >> parent is executes first, an unnecessary copy of the pages is made
> >> (if parents writes) and later on when child executes, a fresh
> >> address space is executed.
> >>
> >> So in linux, is a child run first or the parent? Can we rely on this
> >> information?
> >
> > No with fork() it is not guaranteed.......however if you use vfork()
> > child is guaranteed to run first......
> >
>
> So as the author says, isn't running child first a good idea that needs to be implemented?
>

Not sure......I am getting a bit confused.....If implemented will
there be any issues in case of backward compatibility ?? ... For
example what will happen to daemon programs where typically parent
process exits after a fork() and rest of the code is taken care by the
child . In that case will there be an issue ???


> Thanks,
>
> Rajat
>



--
Thanks & Regards,
********************************************
Manish Katiyar ( http://mkatiyar.googlepages.com )
3rd Floor, Fair Winds Block
EGL Software Park
Off Intermediate Ring Road
Bangalore 560071, India
***********************************************
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: Copy-on-Write

am 06.02.2008 14:17:57 von Daniel Cheng

Rajat Jain wrote:
[...]
>
> So as the author says, isn't running child first a good idea that needs to be implemented?

This have been discussed for a few times in lkml. The actual behavior
have been change back and fore for a few times.

You should never depend on this implementation detail for not doing
process synchronization.

>
> Thanks,
>
> Rajat
> -

--

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: Copy-on-Write

am 07.02.2008 16:30:48 von Mulyadi Santosa

Hi....

On Feb 6, 2008 12:49 PM, Rick Brown wrote:
> So in linux, is a child run first or the parent? Can we rely on this
> information?

Hm, this might be the answer:
http://lxr.linux.no/linux/kernel/sched.c#L1663

it says:
unsigned int __read_mostly sysctl_sched_child_runs_first = 1;

and I guess it has relationship with certain /proc or /sys entry.

regards,

Mulyadi.
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: Copy-on-Write

am 08.02.2008 00:46:12 von Brandon Philips

On 22:30 Thu 07 Feb 2008, Mulyadi Santosa wrote:
> On Feb 6, 2008 12:49 PM, Rick Brown wrote:
> > So in linux, is a child run first or the parent? Can we rely on this
> > information?
>
> Hm, this might be the answer:
> http://lxr.linux.no/linux/kernel/sched.c#L1663
>
> it says:
> unsigned int __read_mostly sysctl_sched_child_runs_first = 1;
>
> and I guess it has relationship with certain /proc or /sys entry.

$ sysctl -w kernel.sched_child_runs_first=0

Also available here: /proc/sys/kernel/sched_child_runs_first

Cheers,

Brandon
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: Copy-on-Write

am 08.02.2008 03:11:15 von rene

What kernel version did you get this code?

I looked at 2.6.22.1 (my kernel version) and found this piece of code:

/*
* wake_up_new_task - wake up a newly created task for the first time.
*
* This function will do some initial scheduler statistics housekeeping
* that must be done for every newly created context, then puts the tas=
k
* on the runqueue and wakes it.
*/
void fastcall wake_up_new_task(struct task_struct *p, unsigned long clo=
ne_flags)
{
......
if (likely(cpu == this_cpu)) {
if (!(clone_flags & CLONE_VM)) {
/*
* The VM isn't cloned, so we're in a good posi=
tion to
* do child-runs-first in anticipation of an e=
xec. This
* usually avoids a lot of COW overhead.
*/
if (unlikely(!current->array))
__activate_task(p, rq);
else {
....
}
set_need_resched();
} else
/* Run child last */
__activate_task(p, rq);

The variable ...sysctl_sched_child_runs
_first it's not present, such as /sys relationship..... Perhaps was
removed from oldest version, anybody know about that?

Cheers;

Ren=EA
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie"=
in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: Copy-on-Write

am 13.02.2008 06:44:11 von Brandon Philips

On 00:09 Fri 08 Feb 2008, Ren=EA wrote:
> What kernel version did you get this code?

It exists in 2.6.23+ IIRC. Poke around at the git-log for
kernel/sched.c

Cheers,

Brandon

> On Feb 7, 2008 9:46 PM, Brandon Philips wrote:
> > On 22:30 Thu 07 Feb 2008, Mulyadi Santosa wrote:
> > > On Feb 6, 2008 12:49 PM, Rick Brown wrot=
e:
> > > > So in linux, is a child run first or the parent? Can we rely on=
this
> > > > information?
> > >
> > > Hm, this might be the answer:
> > > http://lxr.linux.no/linux/kernel/sched.c#L1663
> > >
> > > it says:
> > > unsigned int __read_mostly sysctl_sched_child_runs_first =3D 1;
> > >
> > > and I guess it has relationship with certain /proc or /sys entry.
> >
> > $ sysctl -w kernel.sched_child_runs_first=3D0
> >
> > Also available here: /proc/sys/kernel/sched_child_runs_first
> >
> > Cheers,
> >
> > Brandon
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie"=
in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs