Reputation: 1747
I have this implementation to study the fork system call API and child process creation. Here i am creating 5 child processes from single parent. My question is regarding the scheduling of the processes. Now Parent and child processes will have same priority and hence the only way a scheduler decides to schedule them is may be through there PPID.
Here after the first fork the child process should execute but apparently the parent is executing. Is this correct behavior. ( Who am i to question the OS behavior) or is my previous statement about scheduling correct..or is the OS playing some tricks here.
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());
Output:
I am in parent process context
in parent .. ppid is 2084 ...and pid is 2149
I am in child process context
in child .. ppid is 2149 ...and pid is 2150...and number is 1
I am in parent process context
in parent .. ppid is 2149 ...and pid is 2150
I am in child process context
in child .. ppid is 2150 ...and pid is 2151...and number is 2
I am in parent process context
in parent .. ppid is 2150 ...and pid is 2151
I am in child process context
in child .. ppid is 2151 ...and pid is 2152...and number is 3
I am in parent process context
in parent .. ppid is 2151 ...and pid is 2152
I am in child process context
in child .. ppid is 2152 ...and pid is 2153...and number is 4
I am in parent process context
in parent .. ppid is 2152 ...and pid is 2153
I am in child process context
in child .. ppid is 2153 ...and pid is 2154...and number is 5
in parent .. ppid is 2153 ...and pid is 2154
Upvotes: 1
Views: 436
Reputation: 1747
Ok I found the solution. There is this attribute called "sched_child_runs_first". We can find this in the following path:
/proc/sys/kernel/sched_child_runs_first.
This value can be 0 or 1. This decides the scheduling sequence.
Upvotes: 1