Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How CPU allocation is done in Linux ? Thread level or Process level? [closed]

I am trying to understand how CPU is distributed among different processes with different no of threads.I have two programs Program1 and Program2.

Program1 has 5 threads whereas Program2 has ONLY main thread.

SCENARIO -1 :

terminal-1 :  ./Program1 
terminal-2 :  ./Program2 

When I run Program1 in one terminal and Program2 in another terminal , the CPU allocation is done 50% for Program1 and 50% for Program2. Each thread of Program1 is getting 10% (cumulatively 50% for Program1)

This shows, no matter the no of threads a process have, every process will get equal share of CPU. This shows CPU allocation is done at Process level.

pstree shows

├─bash───P1───5*[{P1}]
├─bash───P2───{P2}

SCENARIO -2 :

terminal-1 : ./Program1 &  ./Program2

When I run both Program1 and Program2 in SAME terminal , the CPU allocation is done equal for Program1 and all threads of Program2. It means each thread of Program1 is getting almost 17% (cumulatively Program1 is getting 83%) and Program2 is also getting 17%. This shows CPU allocation is done at Thread level.

pstree shows

 ├─bash─┬─P1───5*[{P1}]
 │      └─P2

I am using Ubuntu 12.04.4 LTS, kernel - config-3.11.0-15-generic. I have also used Ubuntu 14.04.4 , kernel-3.16.x and got similar results.

Can anyone explain how CPU scheduler of LINUX KERNEL distinguishing SCENARIO-1 and SCENARIO-2?

I think the CPU scheduler is distinguishing both SCENARIO in somewhere before allocating CPU. To understand how CPU scheduler is distinguishing SCENARIO-1 and SCENARIO-2 ,I have downloaded Linux kernel source code.
However, I haven't found in source code where it is distinguishing SCENARIO-1 and SCENARIO-2. It will be great if anyone point me the source code or function where the CPU scheduler is distinguishing SCENARIO-1 and SCENARIO-2.

Thanks in advance.

NOTE : Although Ubuntu is based on Debian, surprisingly, in Debian 8 (kernel-3.16.0-4-686-pae) in both SCENARIO's CPU allocation is done at Thread level means each thread of Program1 is getting almost 17% (cumulatively Program1 is getting 83%) and Program2 is also getting 17%.

Here is the code : Program1(with 5 threads)

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

// Let us create a global variable to change it in threads
int g = 0;

// The function to be executed by all threads
void *myThreadFun(void *vargp)
{

    // Store the value argument passed to this thread
    int myid = (int)vargp;

    // Let us create a static variable to observe its changes
    static int s = 0;

    // Change static and global variables
    ++s; ++g;

    // Print the argument, static and global variables
    printf("Thread ID: %d, Static: %d, Global: %d\n", myid, ++s, ++g);

while(1); // Representing CPU Bound Work

}

int main()
{
    int i;
    pthread_t tid[5];

    // Let us create three threads
    for (i = 0; i < 5; i++)
        pthread_create(&tid[i], NULL, myThreadFun, (void *)i);

    for (i = 0; i < 5; i++)
     pthread_join(tid[i],NULL);

    return 0;
}

Program2 ( with only main thread)

#include <stdio.h>
#include <stdlib.h>

int main()
{
 while(1);// Representing CPU Bound Work
}

To disable all optimization by gcc, I have used O0 option while compiling the both programs.

gcc -O0 program1.c -o p1 -lpthread
gcc -O0 program2.c -o p2 

UPDATE: As per explanation of ninjalj , in Scenario-1, CPU allocation is done at control groups level and as I am using two different terminal(means two different session), thus 2 different control groups and each control groups is getting 50% CPU allocation. This is due to reason, autogroup is enabled by default.

As Program2 has ONLY one thread and Program1 has more threads, I want to run both the program in separate terminal(different session) and get more CPU allocation for Program1 ( as in Scenario-2, Program1 is getting 83% CPU allocation compared to 17% of Program2). Is it possible in any way,that CPU allocation of Scenario1 will be same as Scenario-2 in Ubuntu?

Also it is surprising to me although Ubuntu is based on Debian, still Debian and Ubuntu is behaving differently. In case of Debian, Program1 is getting more CPU in both Scenario.

like image 768
bholanath Avatar asked Feb 24 '16 23:02

bholanath


1 Answers

The linux kernel does not distinguish processes vs. threads in scheduling.

Threads are processes that just happen to share most of their memory. Beyond that, they are treated equally by the scheduler.

You can have 50 processes and 30 threads. That's 80 "things" and the kernel will schedule them without regard to whether they are processes or threads

like image 105
Craig Estey Avatar answered Oct 11 '22 03:10

Craig Estey