Does the Linux scheduler prefer to run the child process after fork()
to the father process?
Usually, the forked process will execute exec
of some kind so, it is better to let child process to run before father process(to prevent copy on write).
I assume that the child will execute exec
as first operation after it will be created.
Is my assumption (that the scheduler will prefer child process) correct. If not, why? If yes, is there more reasons to run child first?
To quote The Linux Programming Interface (pg. 525) for a general answer:
The book goes on about the differences in kernel versions and also mentions CFS / Linux 2.6.32:
This behaviour is still present with CFS although there are some concerns for the future of this feature. Looking at the CFS implementation, it seems to schedule the parent before the child.
The way to go for you would be to set
/proc/sys/kernel/sched_child_runs_first
to a non-zero value.Edit: This answer analyzes the default behaviour and compares it to
sched_child_runs_first
.For the case where the child calls
exec
at the first opportunity you can usevfork
instead of fork.vfork
suspends the parent until the child calls_exit
orexec
*. However, once it callsexec
the child will be suspended if code has to be loaded from disk. In this case, the parent has a good chance to continue before the child does.