Skip to contentSkip to conversation

Copy-on-Write

DISCUSSION Rick Brown 7 participants 9 messages Feb 6, 2008 – Feb 13, 2008 1 message not in the archive

lore
Share & tools
lore

Rick Brown

author
#0 lore raw

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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#1 lore raw
Quoted 6 lines · 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.

Quoted 8 lines

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......

Quoted 11 lines

TIA

Rick

--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ

Signature · 13 lines
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 [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#2 lore raw

Hi,

Quoted 11 lines
Quoted 11 lines

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.

Quoted 6 lines
Quoted 2 lines

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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#3 lore raw
Quoted 14 lines · Rajat Jain

Hi,

Quoted 11 lines
Quoted 11 lines

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.

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

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 ???

Quoted 4 lines

Thanks,

Rajat

Signature · 13 lines
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 [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#4 lore raw
In reply to Rajat Jain

Rajat Jain wrote:
[...]

Quoted 2 lines

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.

Quoted 5 lines

Thanks,

Rajat
-

Signature · 5 lines
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#5 lore raw
In reply to Rick Brown

Hi....

Quoted 2 lines · Rick Brown

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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#6 lore raw
Quoted 11 lines · Mulyadi Santosa
Quoted 2 lines · Rick Brown

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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#7 lore raw

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 task
* on the runqueue and wakes it.
*/
void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags)

 {
       ......
       if (likely(cpu == this_cpu)) {
                if (!(clone_flags & CLONE_VM)) {
                        /*
                         * The VM isn't cloned, so we're in a good position to
                          * do child-runs-first in anticipation of an exec. 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ê
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 1 line · Renê

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

Quoted 14 lines · Brandon Philips
Quoted 13 lines · Mulyadi Santosa
Quoted 11 lines · Rick Brown
Quoted 2 lines

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.

Quoted 7 lines
Quoted 7 lines
 $ 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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs