2014-03-18 30 views
0

主線程內我已經創建多個線程(4個線程)。雖然每個線程都執行相同的功能,但線程調度與預期不同。按我的OS的理解,Linux的CFS調度會 分配「T」虛擬運行時間段,並在該時間段到期,CPU從當前線程被搶佔,並 分配給下一個線程。以這種方式,每個線程將獲得公平的CPU份額。我得到的不是預期。意外結果在linux下CFS schedualar在C/C++多線程場景

我期待所有的線程(線程1-4,主線程)之前相同的線程(任意)獲得CPU下一次會得到CPU。

預期輸出是

foo3 - > 1 --->現在時間:00:17:45.346225000

foo3 - 現在> 1 --->時間:00:17: 45.348818000

foo4 - > 1 --->現在時間:00:17:45.350216000

foo4 - > 1 --->現在時間:00:17:45.352800000

主要運行---> 1- - >現在時間:00:現在45.355803000

主要運行---> 1 --->時間:17 00:17:45.360606000

foo2的 - 現在> 1 --->時間: 00:17:45.345305000

foo2的 - > 1 --->現在時間:00:17:45.361666000

foo1 - 現在> 1 --->時間:00:17:45.354203000

foo1 - > 1 --->現在時間:00:17:45.362696000

foo1 - > 2 --->現在時間:00:17:45.362716000 // foo1線程獲得預期

foo1 CPU第二次 - > 2 --->現在時間:00:17:45.365306000

但我正在逐漸

foo3 - > 1 --->現在時間:00:17:45.346225000

foo3 - > 1 --->現在時間:00:17:45.348818000

foo4-- > 1 --->現在時間:00:17:45.350216000

foo4 - > 1 --->現在時間:00:現在45.352800000

主要運行---> 1 --->時間:17 00:17:45.355803000

主正在運行---> 1 --->時間現在:00:17:45.360606000

foo3 - > 2 --->時間現在:00:17:45.345305000 // // foo3線程獲得CPU第二次在按照CFS安排其他線程之前無法預期

foo3 - > 2 --->現在時間:00:17:45。361666000

foo1 - > 1 --->現在時間:00:17:45.354203000

foo1 - > 1 --->現在時間:00:17:45.362696000

foo1 - > 2 --->現在時間:00:17:45.362716000

foo1 - 現在> 2 --->時間:00:17:45.365306000

這裏是我的節目( thread_multi.c PP)

#include <pthread.h> 
#include <stdio.h> 
#include "boost/date_time/posix_time/posix_time.hpp" 
#include <iostream> 
#include <cstdlib> 
#include <fstream> 
#define NUM_THREADS 4 

using namespace std; 

std::string now_str() 
{ 
    // Get current time from the clock, using microseconds resolution 
    const boost::posix_time::ptime now = 
     boost::posix_time::microsec_clock::local_time(); 

    // Get the time offset in current day 
    const boost::posix_time::time_duration td = now.time_of_day(); 
    const long hours  = td.hours(); 
    const long minutes  = td.minutes(); 
    const long seconds  = td.seconds(); 


    const long nanoseconds = td.total_nanoseconds() - ((hours * 3600 + minutes * 60 + seconds) * 1000000000); 
    char buf[40]; 

    sprintf(buf, "Time Now : %02ld:%02ld:%02ld.%03ld", hours, minutes, seconds, nanoseconds); 
    return buf; 
} 


/* This is our thread function. It is like main(), but for a thread*/ 
void *threadFunc(void *arg) 
{ 
    char *str; 
    int i = 0; 

    str=(char*)arg; 

    while(i < 100) 
    { 

     ++i; 
     ofstream myfile ("example.txt", ios::out | ios::app | ios::binary); 

     if (myfile.is_open()) 
      { 
      myfile << str <<"-->"<<i<<"--->" <<now_str() <<" \n"; 

      } 
      else cout << "Unable to open file"; 
     // generate delay 
     for(volatile int k=0;k<1000000;k++); 



     if (myfile.is_open()) 
      { 
      myfile << str <<"-->"<<i<<"--->" <<now_str() <<"\n\n"; 
      myfile.close(); 
      } 
      else cout << "Unable to open file"; 
    } 


} 

int main(void) 
{ 
    pthread_t pth[NUM_THREADS]; // this is our thread identifier 
    int i = 0; 

    pthread_create(&pth[0],NULL, threadFunc, (void *) "foo1"); 
    pthread_create(&pth[1],NULL, threadFunc, (void *) "foo2"); 
    pthread_create(&pth[2],NULL, threadFunc, (void *) "foo3"); 
    pthread_create(&pth[3],NULL, threadFunc, (void *) "foo4"); 



std::cout <<".............\n" <<now_str() << '\n';  

    while(i < 100) 
    { 

     for(int k=0;k<1000000;k++); 

     ofstream myfile ("example.txt", ios::out | ios::app | ios::binary); 
      if (myfile.is_open()) 
      { 
      myfile << "main is running ---> "<< i <<"--->"<<now_str() <<'\n'; 
      myfile.close(); 
      } 
      else cout << "Unable to open file"; 


     ++i; 
    } 

// printf("main waiting for thread to terminate...\n"); 
    for(int k=0;k<4;k++) 
    pthread_join(pth[k],NULL); 

std::cout <<".............\n" <<now_str() << '\n';  
    return 0; 
} 

這裏是完全公平調度程序的信息

kernel.sched_min_granularity_ns = 100000 kernel.sched_wakeup_granularity_ns = 25000 kernel.sched_latency_ns = 百萬

作爲每sched_min_granularity_ns值時,任何任務將在最短的時間內執行,如果任務需要超過最小時間,則計算時間片,並且每個任務都將執行該時間片。

這裏時間片使用公式進行計算,

時間片=( 下的所有任務的每個任務/總重量的重量即CFS運行隊列)X sched_latency_ns

燦任何人都解釋爲什麼我會得到調度的結果? 任何幫助理解輸出將高度讚賞。 預先感謝您。

我在linux下使用gcc。

EDIT 1:

如果我改變了循環

對(INT K = 0; k < 100000; k ++);

對(INT K = 0; k < 10000; k ++);

然後有時線程1連續得到CPU 10次,線程2連續5次得到CPU,線程3連續獲得5次CPU,主線程連續2次,線程4連續獲得7次CPU。看起來不同的線程在隨機時間被搶佔。

任何線索爲這些隨機的次數連續CPU分配給不同的線程。

回答

0

CPU分配一些時間來執行每個線程。爲什麼每個線程的打印數量都不一樣?

我的例子中解釋:

承認你的電腦可以通過NS 承認,使1個打印使100個指令等同用25個指示 承認每個線程有1ns的工作

現在你必須明白,在計算機上的所有程序是完成100條可用指令

如果當你的線程想要打印的東西有100條指令可用,它可以打印4個句子。 如果當你的線程想要打印某些東西時,有40條指令可用,它可以打印1個句子。由於其他程序正在使用指令,因此只有40條指令。

你明白了嗎?

如果您有任何問題,歡迎您。 :)