Quantcast
Channel: Active questions tagged linux-kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 12312

what's the execution order of multiple child processes

$
0
0

use the follwing code to fork four chile processes

#define _GNU_SOURCE
#include  <stdio.h>
#include  <unistd.h>
#include  <sys/wait.h>
#include  <stdlib.h>

pid_t pid;
int child(int id);

int main(void)
{
    int cpus = 0; 
    cpus = sysconf(_SC_NPROCESSORS_CONF);
    printf("cpu number %ld\n",cpus);

    if ((pid = fork())==0)
        child(1);  
    if ((pid = fork())==0)
        child(2);
    if ((pid = fork())==0)
        child(3);
    if ((pid = fork())==0)
        child(4);

    printf("fork over\n");
    int i,status;  
    for (i=0;i<4;i++)
    { 
    pid = wait(&status);
    }
    exit(0);
}

int child(int id)
{
    printf("child %d is running!pid :%d\n",id,getpid());
    exit(id);
}

I run that code in vmware and I set the cpu num to 1.
The expected output is the first child is the first to exectue. But the real output is on the contrary.

output: fork over
child 4 is running!pid :4258
child 3 is running!pid :4257
child 2 is running!pid :4256
child 1 is running!pid :4255

They are normal processes so the scheduling algorithm is CFS.
The process which has min vruntime should run first.
Obviously the child 1's vruntime is smaller than others.
So it's so confusing to get such output. I hope someone can help me to figure out it.

The Linux release version is Ubuntu 16.04
The kernel version is 4.4


Viewing all articles
Browse latest Browse all 12312

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>