kernel學習之進程搶佔和切換

來源:互聯網
上載者:User

搶佔時伴隨著schedule()的執行。核心提供了一個TIF_NEED_RESCHED標誌來表明是否要用schedule()調度一次。

根據搶佔發生的時機分為使用者搶佔和核心搶佔。使用者搶佔發生在核心即將返回到使用者空間的時候。核心搶佔發生在返回核心空間的時候。

1、使用者搶佔:核心在即將返回使用者空間時檢查進程是否設定了TIF_NEED_RESCHED標誌,如果設定了,就會發生使用者搶佔。使用者搶佔發生的時機:從系統調用或中斷處理常式返回使用者空間的時候。

2、核心搶佔:在不支援核心搶佔的核心中,核心進程如果自己不主動停止,就會一直的運行下去。無法響應即時進程。搶佔核心雖然犧牲了環境切換的開銷,但獲得了更大的輸送量和回應時間。2.6的核心添加了核心搶佔,同時為了某些地方不被搶佔,又添加了自旋鎖。在進程的thread_info結構中添加了preempt_count該數值為0,當進程使用一個自旋鎖時就加1,釋放一個自旋鎖時就減1.為0時表示核心可以搶佔。

核心發生搶佔的時機:1、從中斷處理常式返回核心空間時,核心會檢查preempt_count和TIF_NEED_RESCHED標誌,如果進程設定了TIF_NEED_RESCHED標誌,並且preempt_count為0,發生核心搶佔。2、當核心再次用於可搶佔性的時候,當進程所有的自旋鎖都釋放了,釋放程式會檢查TIF_NEED_RESCHED標誌,如果設定了就會調用schedule。3、顯示調用schedule時4、核心中的進程被堵塞的時候。

進程的切換:

從一個啟動並執行進程轉換到另一個可啟動並執行進程時,稱為進程切換(process switch)或環境切換(context switch).

1、硬體上下文(hardware context)

2、任務狀態段tss

3、硬體內容相關的切換:linux核心中的硬體上下文不像intel原始設計的那樣存放在tss中而是用一個特定了資料結構來儲存。

4、context_switch

/*       *context_switch - switch to the new MM and the new  *thread's register state.  */      static inline voidcontext_switch(struct rq *rq, structtask_struct *prev,              struct task_struct *next)  {       structmm_struct *mm, *oldmm;       prepare_task_switch(rq,prev, next);       trace_sched_switch(prev,next);       /*擷取即將調度的進程記憶體指標*/     mm =next->mm;       /*儲存當前要替換的進程使用的記憶體資料結構*/     oldmm= prev->active_mm;       /*       * For paravirt, this is coupled with an exitin switch_to to *查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/      * combine the page table reload and the switchbackend into       * one hypercall.       */     arch_start_context_switch(prev);       /*如果指向的即將調用的進程記憶體指標為null,則為核心線程*/     if(!mm) {           next->active_mm= oldmm;           /*記憶體描述結構使用計數加1*/         atomic_inc(&oldmm->mm_count);           enter_lazy_tlb(oldmm,next);       }else               /*不是核心線程,切換頁表*/         switch_mm(oldmm,mm, next);             if(!prev->mm) {           prev->active_mm= NULL;           rq->prev_mm= oldmm;       }             /*            * Since the runqueue lock will be released bythe next       * task (which is an invalid locking op but inthe case       * of the scheduler it's an obviousspecial-case), so we       * do an early lockdep release here:       */      #ifndef __ARCH_WANT_UNLOCKED_CTXSW       spin_release(&rq->lock.dep_map,1, _THIS_IP_);  #endif             /*Here we just switch the register state and the stack. */     /*處理器狀態切換*/     switch_to(prev,next, prev);       barrier();             /*       * this_rq must be evaluated again because prevmay have moved       * CPUs since it called schedule(), thus the'rq' on its stack       * frame will be invalid.       */     finish_task_switch(this_rq(),prev);        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.