作業系統
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Chp 5 CPU Scheduling ###### tags: `作業系統` [參照Chp 3](https://hackmd.io/Stbjgv5LSyipNPz0V-9onA#Scheduling) [可參考的網站](https://mropengate.blogspot.com/2015/01/operating-system-ch5-cpu-scheduling.html) ## CPU Schedule <font color=#ff0000>考</font> * a.k.a **Short-term scheduler** selects from among the processes in **ready queue**, and allocates the CPU to one of them * CPU scheduling decisions: ![](https://i.imgur.com/fhq85kH.png) ## Preemptive v.s. Non-Preemptive (待補) <font color=#ff0000>考</font> :::info <font color=#0000000> $Q$: Please explain what is the non-preemptive and preemptive scheduling? And also describe both scenarios of preemptive and non-preemptive scheduling. * Ans. + non-preemptive: Process cannot be preempted until completes its CPU burst(執行中的process不能被其他process中斷) + preemptive: new process arrives with CPU burst length less than remaining time of current executing process, preempt(process可以執行到一半換成執行其他CPU burst 較短的 process) </font> ::: ## Dispatcher * Gives control to the process selected by the short term scheduler e.g. 1. context switch 2. switch to user mode 3. restart user program * **Dispatch latency**: **time** it takes for the dispatcher to stop the process and start another (the shorter, the better) ## Criteria <font color=#ff0000>考</font> 1. **CPU utilization** : keep CPU busy(max) 2. **Throughput** : $\frac{completed ~ processes} { time}$(max) 3. **Turnaround time** : $t_{finish}-t_{start}$, waiting time + executing time (min) 4. **Waiting time** : wait in the ready queue (min) 5. **Response time** : amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment)(min) ($t_{response}-t_{request}$) 6. **Time slice (time quantum)**: max. time a thread can run before being preempted >[color=#00a000] > >4相對於3是不定的? >[name=Omnom] >[color=#FF00FF]總覺得這Response time寫的文謅謅的。 >Response time是指Process 從Arrive之後到第一次取得CPU資源的這段待在ready queue的時間,這聽起來跟waiting time很像,但是response time是專指第一次,有可能process開始run了以後因為排程(RR)或是中斷之類的原因再次回到ready queue,這時候waiting time會繼續增加,但是response time就跟這裡沒關係了。 >[name=Neko] ## Schedule Algorithm <font color=#ff0000>絕對必考</font> ### First-Come, First-Served (FCFS) Assume Arrival at 0 (k) ![](https://i.imgur.com/LZ4n5z9.png =80%x) average waiting time (k):$\frac{0+2+3+8+12}{5}=5$ <font color=#ff0000>考</font> * **Convoy effect**: 很多process 均在等待一個需要很長CPU Time 來完成工作process,造成Average waiting time大幅增加的不良現象 >[color=#00a000] > >有點naive >所謂「FCFS比較公平」應該是指「先到先處理」 >[name=Omnom] ### Shortest-Job-First(SJF) 1. nonpreemptive Assume Arrival at 0 (k) ![](https://i.imgur.com/E0cEaJY.png =80%x) average waiting time (k):$\frac{0+1+3+6+10}{5}=4$ 2. preemptive **Shortest-remaining-time-first (SRTF)** Assume Arrival (k): <font color=#f98000>0</font> <font color=#cc0000>1</font> <font color=#00a000>3</font> <font color=#f04000>4</font> <font color=#0000aa>5</font> ![](https://i.imgur.com/llxjmb0.png =80%x) average waiting time (k):$\frac{0+(1-1)+(2-1)+(3-3)+(4-4)+(5-5)+(8-5)+(11-4)}{5}=2.2$ * Pro Gives minimum average waiting time * Con 1. need to predict next CPU burst 2. starvation issue * Determine length of next CPU burst: exponential average waiting - $t_n$: actual length of $n^{th}$ burst - $\tau_{n+1}$:predicted value - $\alpha,\ 0 \leq\alpha\leq 1$ - $\tau_{n+1} = \alpha t_n + (1-\alpha)\tau_n$ ### Priority Scheduling * General type: determine the prioritized criteria. * Issue: **Starvation** – low priority processes may never execute Sol: **Aging** – as time progresses increase the priority of the process >[color=#00a000] > >priority的High和Low好像和平常我們認知的顛倒? >[name=Omnom] >[color=#FF00FF]等待越久的Process其執行的優先度越高,避免資源都被其他high priority的process占用 >[name=Neko] ### Round Robin (RR) ![](https://i.imgur.com/BYljnIC.png =80%x) * Description: Small unit of CPU time (time quantum), if there are $n$ threads in ready state and time quantum is $q$, split the time slot into $n$ chunks and distribute them equally of at most $q$ time units at once. * Issue: * context switch 不能太頻繁 * 耗費的時間不能太多 * quantum time取捨很重要 * Pro: 1. wait time 不會太大 2. 有multitasking的可能 * time quantum q - Large: + 太大會退化成 FCFS + turnaround time 小 - Small: + q 必須比 context switch 所花的時間大 (如Issue所述) + Better response (multitasking) <font color=#ff0000>考</font> :::info <font color=#0000000> $Q$ : What scheduling algorithms could result in starvation? <font color=#ff0000> SJF、Priority (without aging)</font> $Q$ : Explain the differences in the degree to which the following scheduling algorithms discriminate in favor of short processes: a. FCFS: <font color=#ff0000>Convoy effect (against short process</font> b. RR: <font color=#ff0000>treats all jobs equally, thus short jobs will be able to leave the system faster</font> c. Multilevel feedback queues: <font color=#ff0000> similar to RR</font> </font> ::: ### Multilevel Queue Scheduling ![](https://i.imgur.com/1CzicQX.png =80%x) * Ready queue partition: separate queues: * foreground (interactive) * background (batch) * Concept: 將ready queue分層,每層使用特定的algorithm e.g. - foreground – RR - background – FCFS - Scheduling between queues - Fixed priority scheduling: server from foreground to background queue (may lead to **starvation**) - Time slice: each queue gets certain amount of CPU time >[color=#FF00FF]此外,如果各個Queue裡面都有Process怎麼辦?因此queue之間也要做scheduling,就像講義25頁的圖,Queue也有優先度的區別。 >[name=Neko] ### Multilevel Feedback Queue Scheduling * Concept: - A process can move between the various queues (改良上述方式) * higher priority time quantum 要越小 ![](https://i.imgur.com/LhCBLxb.png) >Example >Three jobs $Q_0,\ Q_1,\ Q_2$ >If $Q_0$ gets the process, it gets 8 ms, if not done, shift to $Q_1$ and so on. :::info <font color=#000000> Which of the following statements are true about multilevel feedback queue scheduling algorithm? **(A)** A process can move between the various queue depending on the CPU time it uses. **(B)** If a process waits too long in a lower-priority queue, it may be moved to higher-priority queue to prevent starvation. (C ) Generally, the time quantum increases from lower-priority queue to higher-priority queue. **(D)** If the time quantum is set as a very large value, the queue may result in convoy effect as FCFS scheduling. (E) The time quantum is the smaller the better. </font> ::: >[color=#00a000] > >所以process一直在不同Queue轉來轉去? >[name=Omnom] >[color=#FF00FF]會監控Process的執行狀況並根據某些依據(通常是執行時間)來調整(upgrade and demote)Process應該要放在哪一個優先度的Queue中。而跟MFQ相同的是每個Queue可以有不同的排程演算法。 >[name=Neko] > ## Thread Scheduling <font color=#ff0000>考</font> 1. **process-contention scope (PCS)** thread library schedules user-level threads to run on LWP >[color=#FF00FF]這個只有在Many-to-1跟many-to-many的模型才成立 >[name=Neko] 2. **system-contention scope (SCS)** Kernel thread scheduled onto available CPU ## Multiple-Processor Scheduling (全篇待修) ### Approaches to Multiple-Processor Scheduling ### Multicore Processors ## Real-Time CPU Scheduling * Start time: a task must start in response to some events * Deadline: time at which the task must completes * **Soft** real-time systems – no guarantee as to when critical real-time process will be scheduled * **Hard** real-time systems – task must be serviced by its deadline, elsewise the result may be meaningless * Real-time operating system usually use priority schedulers >[color=#00a000] > >'critical real-time process' ? >[name=Omnom] >[color=#FF00FF]就是即時性很重要的Process,因為soft real-time沒有對process的時間有很嚴格的限制,所以才會這麼說。 >[name=Neko] ### Latency - If T = period, D = deadline, C = compute time - $C \leq D \leq T$ * Event lataency ($t_{occur}-t_{response}$) 1. Interrupt latency - arrival of an interrupt at the CPU to the start of the routine 2. Dispatch latency - dispatcher to stop one process and start another - Conflict phase 1. Preemption of any process running in kernel mode 2. Release by low- priority process of resources needed by high- priority processes ### Priority-Based Scheduling ![](https://i.imgur.com/27jBzLA.png) ### Rate-Monotonic Scheduling ![](https://i.imgur.com/1msKK7j.png) * Assign static property to periodic processes according to their periods * Highest frequency process gets the highest priority * Every RT process must have known period * Same priority: round robin * Missing deadlines + 用period設定priority可能造成問題(有可能在deadline前做不完) ![](https://i.imgur.com/8DVNtpn.png) ### Earliest Deadline First Scheduling (EDF) ![](https://i.imgur.com/rHvqZSp.png) - always run the one that has closest deadline (may be preempted) ### Lseat Slack First (LSF) Scheduling ![](https://i.imgur.com/sIZdHO6.png) - Consider both remaining compute time and deadline - Look how much a process can be deferred - slack = (time to deadline) - (amount of remaining computation) ### EDF vs. LSF * earlist deadline in EDF alaways finish before others * LSF can get balanced result * Difference when there is not enough time * EDF: some early deadline processes may finish * LSF: all deadline may be missed but roughly by the same amount ### Proportional Share Scheduling e.g. $A:50 ~~B:15 ~~C:20 ~~Share:100$ $D:30$ is denied since 50+15+20+**30**=115>100 ## Static & Dynamic Linker ## CPU Memory Access - CPU executes process by reading instruction, which resides in memory and requires processor to read and write in new location - Addresses amy be absolute or relative - absolute address: actual memory location - relative address: offset to the content of a register - commin relative addressing mode: PC-relative ### Bind of Instruction and Data - Adrdress binding in three stages - Compile time: memory location priori: absolute code - Load time: Must generate relocatable code if not known at compile time - Execution time: Binding delayed until run time if process can be moved during execution from one segment to another - Hardware support for address maps ### Multistep ![](https://i.imgur.com/X51DY2Q.png =40%x) ### Memory access specification - Absolute code: know where the program loaded - Position independent code: all addresses are relative (fPIC) - Dynamic relocatable code: relocated at load time - Logical addresses: absolute code traslated at RT (Need special meory translation hardware MMU) ### Logical vs. Physical Address Space - Logical address: generated by CPU (virtual address) - Physical Address: seen by the memory unit ## Operating-System Examples(待修) ## Algorithm Evaluation ### Deterministic Modeling * analytic evaluation - given algorithm and the system workload to produce a formula or number to evaluate the performance of the algorithm for that workload. * Deterministic modeling 1. type of analytic evaluation ### Queueing Models ### Simulations

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully