【问题标题】:Scheduling during child process creation子进程创建期间的调度
【发布时间】:2014-05-08 07:01:57
【问题描述】:

我有这个实现来研究 fork 系统调用 API 和子进程的创建。 在这里,我从单亲创建 5 个子进程。我的问题是关于进程的调度。现在父进程和子进程将具有相同的优先级,因此调度程序决定调度它们的唯一方法可能是通过 PPID。

在第一次分叉之后,子进程应该执行,但显然父进程正在执行。这是正确的行为吗。 (我是谁来质疑操作系统的行为)还是我之前关于调度正确的声明..或者操作系统在这里玩了一些技巧。

while(i++<5)
{ 

    ret = fork();
    if(ret<0)
    { 
       perror("error in fork"); 
       printf("the final value of i is %lu\n", i);      
       exit(1); 
    }

    if(ret>0)
    { 
       printf("I am in parent process context\n");
       break ; 
    }

    if(ret==0)
    { 
           printf("I am in child process context\n"); 
           printf("in child .. ppid is %lu ...and pid is %lu...and number is %d\n",getppid(),getpid(),i);               
           continue;  


      }

}
    printf("in parent .. ppid is %lu ...and pid is %lu\n",getppid(),getpid());   

输出:

我在父进程上下文中

在父级中.. ppid 是 2084 ...并且 pid 是 2149

我在子进程上下文中

在孩子中.. ppid 是 2149 ...并且 pid 是 2150...并且数字是 1

我在父进程上下文中

在父级中.. ppid 是 2149 ...并且 pid 是 2150

我在子进程上下文中

在孩子中.. ppid 是 2150 ...并且 pid 是 2151...并且数字是 2

我在父进程上下文中

在父级中.. ppid 是 2150 ...并且 pid 是 2151

我在子进程上下文中

在孩子中.. ppid 是 2151 ...并且 pid 是 2152...并且数字是 3

我在父进程上下文中

在父级中.. ppid 是 2151 ...并且 pid 是 2152

我在子进程上下文中

在孩子中.. ppid 是 2152 ...并且 pid 是 2153...并且数字是 4

我在父进程上下文中

在父级中.. ppid 是 2152 ...并且 pid 是 2153

我在子进程上下文中

在孩子中.. ppid 是 2153 ...并且 pid 是 2154...并且数字是 5

在父级中.. ppid 是 2153 ...并且 pid 是 2154

【问题讨论】:

  • 好的,我找到了解决方案。有一个名为 sched_child_runs_first 的属性。我们可以在以下路径中找到它:/proc/sys/kernel/sched_child_runs_first。该值可以是 0 或 1。这决定了调度顺序。

标签: linux process operating-system fork


【解决方案1】:

好的,我找到了解决方案。有一个名为“sched_child_runs_first”的属性。 我们可以在以下路径中找到它:

/proc/sys/kernel/sched_child_runs_first.

该值可以是 0 或 1。这决定了调度顺序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多