国产无码免费,人妻口爆,国产V在线,99中文精品7,国产成人无码AA精品一,制度丝袜诱惑av,久久99免费麻辣视频,蜜臀久久99精品久久久久久酒店
        訂閱
        糾錯(cuò)
        加入自媒體

        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++)
               {
                   // 沒(méi)什么意義,純粹是模擬 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};
           // 用來(lái)保存 4 個(gè)線程的 id 號(hào)
           pthread_t ppid[4];
           // 用來(lái)設(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è)試開(kāi)始

        首先說(shuō)一下預(yù)期結(jié)果,如果沒(méi)有預(yù)期結(jié)果,那其他任何問(wèn)題都?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é)束之后才開(kāi)始執(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)了問(wèn)題。

        聲明: 本文由入駐維科號(hào)的作者撰寫,觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問(wèn)題,請(qǐng)聯(lián)系舉報(bào)。

        發(fā)表評(píng)論

        0條評(píng)論,0人參與

        請(qǐng)輸入評(píng)論內(nèi)容...

        請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

        您提交的評(píng)論過(guò)于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

        • 看不清,點(diǎn)擊換一張  刷新

        暫無(wú)評(píng)論

        暫無(wú)評(píng)論

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

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

          主站蜘蛛池模板: 91福利导航大全| 亚洲熟女视讯2| www黄色com| 熟女在线国产| 超碰日韩| 伊春市| jizz免费| 美女91社| 国产肥白大熟妇BBBB视频| 大色欧美| 亚洲精品国产AV| 一本大道东京热无码va在线播放| 久久免费少妇高潮久久精品99| 好吊AV| 中文字幕一二三区| 亚洲国内自拍| 大悟县| 在线?国产?精品?播放?VA| 丝袜美腿亚洲综合| 司法| 狠狠色AV一区二区| 鄯善县| 亚洲丝袜熟女在线樱桃| 国产精品伦子伦免费视频| 久久老司机| 欧美精品网| 汉中市| av一区二区三区| 九九国产在线观看| 修武县| 东北露脸91| 91色| 国产又大又粗| 伊人99| 日日cao| 波多野在线影院| 潼南县| 亚洲V色| 日本www色| 亚洲成人网站在线| 中文字幕高清|