Linux :多處理器遇到實(shí)時(shí)進(jìn)程和普通進(jìn)程的程序設(shè)計(jì)
get_thread_info(thread_index);
long num = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5000000; j++)
{
// 沒什么意義,純粹是模擬 CPU 密集計(jì)算。
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;
}
// 打印計(jì)數(shù)信息,為了能看到某個(gè)線程正在執(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)建四個(gè)線程:0和1-實(shí)時(shí)線程,2和3-普通線程(非實(shí)時(shí))
int thread_num = 4;
// 分配的線程索引號(hào),會(huì)傳遞給線程參數(shù)
int index[4] = {1, 2, 3, 4};
// 用來保存 4 個(gè)線程的 id 號(hào)
pthread_t ppid[4];
// 用來設(shè)置 2 個(gè)實(shí)時(shí)線程的屬性:調(diào)度策略和優(yōu)先級(jí)
pthread_attr_t attr[2];
struct sched_param param[2];
// 實(shí)時(shí)線程,必須由 root 用戶才能創(chuàng)建
if (0 。 getuid())
{
printf("Please run as root ");
exit(0);
}
// 創(chuàng)建 4 個(gè)線程
for (int i = 0; i < thread_num; i++)
{
if (i <= 1) // 前2個(gè)創(chuàng)建實(shí)時(shí)線程
{
// 初始化線程屬性
pthread_attr_init(&attr[i]);
// 設(shè)置調(diào)度策略為:SCHED_FIFO
pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO);
// 設(shè)置優(yōu)先級(jí)為 51,52。
param[i].__sched_priority = 51 + i;
pthread_attr_setschedparam(&attr[i], &param[i]);
// 設(shè)置線程屬性:不要繼承 main 線程的調(diào)度策略和優(yōu)先級(jí)。
pthread_attr_setinheritsched(&attr[i], PTHREAD_EXPLICIT_SCHED);
// 創(chuàng)建線程
pthread_create(&ppid[i], &attr[i],(void *)thread_routine, (void *)&index[i]);
}
else // 后兩個(gè)創(chuàng)建普通線程
{
pthread_create(&ppid[i], 0, (void *)thread_routine, (void *)&index[i]);
}
}
// 等待 4 個(gè)線程執(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
腦殘測(cè)試開始
首先說一下預(yù)期結(jié)果,如果沒有預(yù)期結(jié)果,那其他任何問題都?jí)焊挥谜劻恕?/p>
一共有 4 個(gè)線程:
線程索引號(hào) 1和2:是實(shí)時(shí)線程(調(diào)度策略是 SCHED_FIFO,優(yōu)先級(jí)是 51,52);
線程索引號(hào) 3和4:是普通線程(調(diào)度策略是 SCHED_OTHER, 優(yōu)先級(jí)是 0);
我的測(cè)試環(huán)境是:Ubuntu16.04,是一臺(tái)安裝在 Windows10 上面的虛擬機(jī)。
我期望的結(jié)果是:
首先打印 1 號(hào)和 2 號(hào)這兩個(gè)線程的信息,因?yàn)樗鼈z是實(shí)時(shí)任務(wù),需要優(yōu)先被調(diào)度;
1 號(hào)線程的優(yōu)先級(jí)是 51,小于 2 號(hào)線程的優(yōu)先級(jí) 52,因此應(yīng)該是 2 號(hào)線程結(jié)束之后,才輪到 1 號(hào)線程執(zhí)行;
3 號(hào)和 4 號(hào)線程是普通進(jìn)程,它倆需要等到 1 號(hào)和 2 號(hào)線程全部執(zhí)行結(jié)束之后才開始執(zhí)行,并且 3 號(hào)和 4 號(hào)線程應(yīng)該是交替執(zhí)行,因?yàn)樗鼈z的調(diào)度策略和優(yōu)先級(jí)都是一樣的。
我滿懷希望的在工作電腦中測(cè)試,打印結(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)容不用輸出了,因?yàn)榍懊嬉呀?jīng)出現(xiàn)了問題。

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
最新活動(dòng)更多
-
6月20日立即下載>> 【白皮書】精準(zhǔn)測(cè)量 安全高效——福祿克光伏行業(yè)解決方案
-
7月3日立即報(bào)名>> 【在線會(huì)議】英飛凌新一代智能照明方案賦能綠色建筑與工業(yè)互聯(lián)
-
7月22-29日立即報(bào)名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會(huì)
-
7.30-8.1火熱報(bào)名中>> 全數(shù)會(huì)2025(第六屆)機(jī)器人及智能工廠展
-
7月31日免費(fèi)預(yù)約>> OFweek 2025具身機(jī)器人動(dòng)力電池技術(shù)應(yīng)用大會(huì)
-
免費(fèi)參會(huì)立即報(bào)名>> 7月30日- 8月1日 2025全數(shù)會(huì)工業(yè)芯片與傳感儀表展
推薦專題
- 1 AI 眼鏡讓百萬 APP「集體失業(yè)」?
- 2 大廠紛紛入局,百度、阿里、字節(jié)搶奪Agent話語權(quán)
- 3 深度報(bào)告|中國AI產(chǎn)業(yè)正在崛起成全球力量,市場(chǎng)潛力和關(guān)鍵挑戰(zhàn)有哪些?
- 4 上海跑出80億超級(jí)獨(dú)角獸:獲上市公司戰(zhàn)投,干人形機(jī)器人
- 5 國家數(shù)據(jù)局局長(zhǎng)劉烈宏調(diào)研格創(chuàng)東智
- 6 一文看懂視覺語言動(dòng)作模型(VLA)及其應(yīng)用
- 7 下一代入口之戰(zhàn):大廠為何紛紛押注智能體?
- 8 百億AI芯片訂單,瘋狂傾銷中東?
- 9 Robotaxi新消息密集釋放,量產(chǎn)元年誰在領(lǐng)跑?
- 10 格斗大賽出圈!人形機(jī)器人致命短板曝光:頭腦過于簡(jiǎn)單