Linux内核参数sysctl_sched_child_runs_first
概述 Linux2.6.23版本引入了CFS调度器,通过sched_child_runs_first设置是否子进程优先运行, 下面是 SUSE Documentation 关于此参数的说明: A freshly forked child runs before the parent continues execution. Setting this parameter to 1 is beneficial for an application in which the child performs an execution after fork. For example make -j performs better when sched_child_runs_first is turned off. The default value is 0. make -j<cpu个数>支持并行编译 设置这个参数后子进程会优先在父进程前执行. 验证 使用The Linux Programming Interface中的例子: fork_whos_on_first.c, fork_whos_on_first.count.awk 来验证一下: ... int main (int argc, char *argv[]) { ... for (j = 0; j < numChildren; j++) { switch (childPid = fork ()) { case -1: exit(-1); case 0: getpid(); printf ("%d child\n", j); _exit (EXIT_SUCCESS); default: getpid(); printf ("%d parent\n", j); wait (NULL); /* Wait for child to terminate */ break; } } exit (EXIT_SUCCESS); } sysctl -w kernel.sched_child_runs_first=1 ./fork_whos_on_first 10000 > fork.txt awk -f ./fork_whos_on_first.count.awk fork.txt All done parent 99983 99.98% child 17 0.02% 虽然设置了 sched_child_runs_first, 并不是子进程每次都能先于父进程被调度。 ...