Linux :多處理器遇到實時進程和普通進程的程序設計
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í)行結束
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];
// 用來設置 2 個實時線程的屬性:調度策略和優(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]);
// 設置調度策略為:SCHED_FIFO
pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO);
// 設置優(yōu)先級為 51,52。
param[i].__sched_priority = 51 + i;
pthread_attr_setschedparam(&attr[i], &param[i]);
// 設置線程屬性:不要繼承 main 線程的調度策略和優(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í)行結束
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
腦殘測試開始
首先說一下預期結果,如果沒有預期結果,那其他任何問題都壓根不用談了。
一共有 4 個線程:
線程索引號 1和2:是實時線程(調度策略是 SCHED_FIFO,優(yōu)先級是 51,52);
線程索引號 3和4:是普通線程(調度策略是 SCHED_OTHER, 優(yōu)先級是 0);
我的測試環(huán)境是:Ubuntu16.04,是一臺安裝在 Windows10 上面的虛擬機。
我期望的結果是:
首先打印 1 號和 2 號這兩個線程的信息,因為它倆是實時任務,需要優(yōu)先被調度;
1 號線程的優(yōu)先級是 51,小于 2 號線程的優(yōu)先級 52,因此應該是 2 號線程結束之后,才輪到 1 號線程執(zhí)行;
3 號和 4 號線程是普通進程,它倆需要等到 1 號和 2 號線程全部執(zhí)行結束之后才開始執(zhí)行,并且 3 號和 4 號線程應該是交替執(zhí)行,因為它倆的調度策略和優(yōu)先級都是一樣的。
我滿懷希望的在工作電腦中測試,打印結果如下:
====> 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
后面打印內容不用輸出了,因為前面已經(jīng)出現(xiàn)了問題。

請輸入評論內容...
請輸入評論/評論長度6~500個字
最新活動更多
-
7月8日立即報名>> 【在線會議】英飛凌新一代智能照明方案賦能綠色建筑與工業(yè)互聯(lián)
-
7月22-29日立即報名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會
-
7月31日免費預約>> OFweek 2025具身機器人動力電池技術應用大會
-
7.30-8.1火熱報名中>> 全數(shù)會2025(第六屆)機器人及智能工廠展
-
免費參會立即報名>> 7月30日- 8月1日 2025全數(shù)會工業(yè)芯片與傳感儀表展
-
即日-2025.8.1立即下載>> 《2024智能制造產業(yè)高端化、智能化、綠色化發(fā)展藍皮書》
推薦專題