訂閱
糾錯
加入自媒體

Linux :多處理器遇到實時進程和普通進程的程序設(shè)計

   get_thread_info(thread_index);
   long num = 0;
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 5000000; j++)
       {
           // 沒什么意義,純粹是模擬 CPU 密集計算。
           float f1 = ((i+1) * 345.45) * 12.3 * 45.6 / 78.9 / ((j+1) * 4567.89);
           float f2 = (i+1) * 12.3 * 45.6 / 78.9 * (j+1);
           float f3 = f1 / f2;
       }
       // 打印計數(shù)信息,為了能看到某個線程正在執(zhí)行
       printf("thread_index %d: num = %ld ", thread_index, num++);
   }
   // 線程執(zhí)行結(jié)束
   printf("thread_index %d: exit ", thread_index);
   return 0;

void main(void)

   // 一共創(chuàng)建四個線程:0和1-實時線程,2和3-普通線程(非實時)
   int thread_num = 4;
   // 分配的線程索引號,會傳遞給線程參數(shù)
   int index[4] = {1, 2, 3, 4};
   // 用來保存 4 個線程的 id 號
   pthread_t ppid[4];
   // 用來設(shè)置 2 個實時線程的屬性:調(diào)度策略和優(yōu)先級
   pthread_attr_t attr[2];
   struct sched_param param[2];
   // 實時線程,必須由 root 用戶才能創(chuàng)建
   if (0 。 getuid())
   {
       printf("Please run as root ");
       exit(0);
   }
   // 創(chuàng)建 4 個線程
   for (int i = 0; i < thread_num; i++)
   {
       if (i <= 1)    // 前2個創(chuàng)建實時線程
       {
           // 初始化線程屬性
           pthread_attr_init(&attr[i]);
           // 設(shè)置調(diào)度策略為:SCHED_FIFO
           pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO);
           // 設(shè)置優(yōu)先級為 51,52。
           param[i].__sched_priority = 51 + i;
           pthread_attr_setschedparam(&attr[i], &param[i]);
           // 設(shè)置線程屬性:不要繼承 main 線程的調(diào)度策略和優(yōu)先級。
           pthread_attr_setinheritsched(&attr[i], PTHREAD_EXPLICIT_SCHED);
           // 創(chuàng)建線程
           pthread_create(&ppid[i], &attr[i],(void *)thread_routine, (void *)&index[i]);
       }
       else        // 后兩個創(chuàng)建普通線程
       {
           pthread_create(&ppid[i], 0, (void *)thread_routine, (void *)&index[i]);
       }
   }
   // 等待 4 個線程執(zhí)行結(jié)束
   for (int i = 0; i < 4; i++)
       pthread_join(ppid[i], 0);
   for (int i = 0; i < 2; i++)
       pthread_attr_destroy(&attr[i]);

編譯成可執(zhí)行程序的指令:

gcc -o test test.c -lpthread

腦殘測試開始

首先說一下預(yù)期結(jié)果,如果沒有預(yù)期結(jié)果,那其他任何問題都壓根不用談了。

一共有 4 個線程:

線程索引號 1和2:是實時線程(調(diào)度策略是 SCHED_FIFO,優(yōu)先級是 51,52);

線程索引號 3和4:是普通線程(調(diào)度策略是 SCHED_OTHER, 優(yōu)先級是 0);

我的測試環(huán)境是:Ubuntu16.04,是一臺安裝在 Windows10 上面的虛擬機。

我期望的結(jié)果是:

首先打印 1 號和 2 號這兩個線程的信息,因為它倆是實時任務(wù),需要優(yōu)先被調(diào)度;

1 號線程的優(yōu)先級是 51,小于 2 號線程的優(yōu)先級 52,因此應(yīng)該是 2 號線程結(jié)束之后,才輪到 1 號線程執(zhí)行;

3 號和 4 號線程是普通進程,它倆需要等到 1 號和 2 號線程全部執(zhí)行結(jié)束之后才開始執(zhí)行,并且 3 號和 4 號線程應(yīng)該是交替執(zhí)行,因為它倆的調(diào)度策略和優(yōu)先級都是一樣的。

我滿懷希望的在工作電腦中測試,打印結(jié)果如下:

====> thread_index = 4
thread_index 4: SCHED_OTHER
thread_index 4: priority = 0
====> thread_index = 1
thread_index 1: SCHED_FIFO
thread_index 1: priority = 51
====> thread_index = 2
thread_index 2: SCHED_FIFO
thread_index 2: priority = 52
thread_index 2: num = 0
thread_index 4: num = 0
====> thread_index = 3
thread_index 3: SCHED_OTHER
thread_index 3: priority = 0
thread_index 1: num = 0
thread_index 2: num = 1
thread_index 4: num = 1
thread_index 3: num = 0
thread_index 1: num = 1
thread_index 2: num = 2
thread_index 4: num = 2
thread_index 3: num = 1

后面打印內(nèi)容不用輸出了,因為前面已經(jīng)出現(xiàn)了問題。

<上一頁  1  2  3  下一頁>  
聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權(quán)或其他問題,請聯(lián)系舉報。

發(fā)表評論

0條評論,0人參與

請輸入評論內(nèi)容...

請輸入評論/評論長度6~500個字

您提交的評論過于頻繁,請輸入驗證碼繼續(xù)

暫無評論

暫無評論

    掃碼關(guān)注公眾號
    OFweek人工智能網(wǎng)
    獲取更多精彩內(nèi)容
    文章糾錯
    x
    *文字標題:
    *糾錯內(nèi)容:
    聯(lián)系郵箱:
    *驗 證 碼:

    粵公網(wǎng)安備 44030502002758號