Linux System Programming 學習筆記(六) 進程調度

來源:互聯網
上載者:User

標籤:des   style   blog   class   c   code   

1. 進程調度

the process scheduler is the component of a kernel that selects which process to run next.進程調度器需要使 處理器使用率最大化,並且提供 使多個進程並發執行的虛擬 Deciding which processes run, when, and for how long is the process scheduler‘s fundamental responsibility. 時間片:the scheduler allocates the process a “slice” of the processor‘s time Linux進程調度演算法CFS( Completely Fair Scheduler):enforce fair access to a resource among contending consumers 2. 時間片如果時間片過大,那麼 掛起進程 開始執行前的等待時間過長,這將減小 並發執行的粒度,甚至使用者覺察到延遲如果時間片過小,那麼 系統在進程之間切換的時間花銷將很大,時間局部性的優勢將喪失 CPU密集型:processes are hungry for CPU time,比如科學計算、數學計算、影像處理I/O密集型:spend more time blocked waiting for some resource than executing,often issuing and waiting for file or network I/O, blocking on keyboard input 3. CFS調度傳統Unix進程調度中,有兩個最基礎的變數:優先順序和時間片CFS引進 fair scheduling 演算法,CFS賦予每個進程一定比例的處理器時間,而不是時間片開始時,CFS賦予N個進程等同的 1/N 處理器時間,然後權衡每個進程比例進行動態調整Processes with the default nice value of zero have a weight of one, so their proportion is unchangedProcesses with a smaller nice value (higher priority) receive a larger weight, increasing their fraction of the processorprocess with a larger nice value (lower priority) receive a smaller weight, decreasing their fraction of the processor 為了確定每個進程每次啟動並執行實際時間,CFS needs to divide the proportions into a fixed period例如:20毫秒,2個相同優先順序的進程,則每個進程賦予相同的權重和處理器時間比例,即每個進程10毫秒如果是5個進程,則每個進程分配4毫秒,如果是200個進程呢?按照常理應該是 每個進程分配100微秒,但是由於 環境切換開銷巨大,並且 局部性優勢喪失,這將嚴重影響系統的吞吐率 這時CFS引入另一個變數:minimum granularity (最小粒度)minimum granularity是任一進程已耗用時間長度的下限,這將保證 環境切換開銷占 系統總時間開銷的比例 不會過大 By assigning proportions of the processor and not fixed timeslices,CFS is able to enforce fairness: each process gets its fair share of the processor 4. nice valprocesses are assigned priorities that affect how long they run,Unix has historically called these priorities nice valuesLegal nice values range from ?20 to 19 inclusive, with a default value of 0, nice值越大,優先順序越低,nice值越小,優先順序越高 
/* increments a process‘s nice value by inc and returns the newly updated value */#include <unistd.h>int nice (int inc);
Passing 0 for inc is an easy way to obtain the current nice value nice函數錯誤時返回-1,但是 新的nice值也可能就是-1,為了區分 函數錯誤 和 返回nice新值為-1,則如下使用:
int ret;errno = 0;ret = nice (10);    /* increase our nice by 10 */if (ret == ?1 && errno != 0)        perror ("nice");else        printf ("nice value is now %d\n", ret);

 

擷取/設定優先權:

#include <sys/time.h>#include <sys/resource.h>int getpriority (int which, int who);int setpriority (int which, int who, int prio);

 

/* returns the current process’s priority */int ret;ret = getpriority (PRIO_PROCESS, 0);printf ("nice value is %d\n", ret);
/* sets the priority of all processes in the current process group to 10 */int ret;ret = setpriority (PRIO_PGRP, 0, 10);if (ret == ?1)        perror ("setpriority");
5. 處理器關聯the process scheduler must decide which processes run on each CPU如果一個進程在一個CPU核上被調度,the process scheduler should aim to schedule it on the same CPU in the future因為 進程從一個CPU核遷移到另一個CPU核的代價是巨大的(主要是緩衝影響) if a process moves to a new CPU and writes new data into memory, the data in the old  CPU‘s cache can become stale這樣,進程調度器必須在 進程遷移CPU花銷 和 多個CPU負載平衡 之間取得平衡 The Linux scheduler attempts to schedule the same processes on the same processors for as long as possible, migrating a process from one CPUto another only in situations of extreme load imbalance. This allows the scheduler to minimize the  cache effects of migration but still ensure thatall processors in a system are evenly  loaded 6. 即時系統硬即時和軟即時A hard real-time system requires absolute adherence to operational deadlinesA soft real-time system does not consider over‐running a deadline to be a critical failure 7. Linux即時調度策略FIFO調度:A FIFO-classed process will continue running so long as no higher-priority process becomes runnable時間片輪轉:When an RR-classed process exhausts its timeslice, the scheduler moves it to the end of the list of processes at  its priority注意: 這兩種即時調度策略中,如果存在更高優先順序的進程,那麼低優先順序進程將不會運行
int policy;/* get our scheduling policy */policy = sched_getscheduler (0);switch (policy) {case SCHED_OTHER:        printf ("Policy is normal\n");        break;case SCHED_RR:        printf ("Policy is round-robin\n");        break;case SCHED_FIFO:        printf ("Policy is first-in, first-out\n");        break;case -1:        perror ("sched_getscheduler");        break;default:        fprintf (stderr, "Unknown policy!\n");}

 

Linux implements a range of 1 to 99 inclusive for the two real-time scheduling policiesLinux provides two system calls for retrieving the range of valid priority values:
int min, max;min = sched_get_priority_min (SCHED_RR);if (min == ?1) {        perror ("sched_get_priority_min");        return 1;}max = sched_get_priority_max (SCHED_RR);if (max == ?1) {        perror ("sched_get_priority_max");        return 1;}printf ("SCHED_RR priority range is %d - %d\n", min, max);
即時調度的注意事項:設計即時程式必須格外小心,can easily bring down the entire system(1) 如果系統中沒有更高優先順序的即時進程,CPU密集型 迴圈程式將會 一直運行到結束(2) Take care not to starve the rest of the system of processor time(3) If a real-time process busy-waits for a resource held by a lower-priority process, the real-time process will busy-wait forever.(因為低優先順序進程不會運行從而釋放資源)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.