1 / 73

第二章 行程與執行緒

第二章 行程與執行緒. 鄧姚文 http://www.ywdeng.idv.tw. Processes and Threads 大綱. 行程 processes 執行緒 threads 行程間通訊 interprocess communication 傳統 IPC 的問題 排程 scheduling. 行程 processes. Pseudoparallelism 只有一個 CPU ,輪流執行多個行程, 每一個行程都持續有進度 平行處理 Parallel processing :多個 CPU 同時執行許多個行程,或合作加速一個行程的執行速度

Download Presentation

第二章 行程與執行緒

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第二章行程與執行緒 鄧姚文 http://www.ywdeng.idv.tw Tanenbaum作業系統2Ed

  2. Processes and Threads大綱 • 行程 processes • 執行緒 threads • 行程間通訊 interprocess communication • 傳統 IPC 的問題 • 排程 scheduling Tanenbaum作業系統2Ed

  3. 行程 processes • Pseudoparallelism • 只有一個 CPU,輪流執行多個行程, 每一個行程都持續有進度 • 平行處理 Parallel processing:多個 CPU 同時執行許多個行程,或合作加速一個行程的執行速度 • 多重處理器 Multiprocessor:專指擁有多個 CPU 這件事情 Tanenbaum作業系統2Ed

  4. ProcessesThe Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant Tanenbaum作業系統2Ed

  5. Process Creation • 產生新行程的主要事件 • System initialization • Execution of a process creation system • User request to create a new process • Initiation of a batch job • Daemon: 在背景執行的行程,以提供服務為主要的目的 Tanenbaum作業系統2Ed

  6. Process Termination • 行程中止的狀況 • Normal exit (自願) • Error exit (自願) • Fatal error (非自願) • Killed by another process (非自願) Tanenbaum作業系統2Ed

  7. Process Hierarchies 行程階層 • Parent creates a child process, child processes can create its own process • Forms a hierarchy • UNIX calls this a "process group" • Windows has no concept of process hierarchy • all processes are created equal Tanenbaum作業系統2Ed

  8. Process States 行程狀態(1) • Possible process states • Running 執行中 • Blocked 等待中 • Ready 備妥 • Transitions between states shown Tanenbaum作業系統2Ed

  9. Process States (2) • Lowest layer of process-structured OS • handles interrupts, scheduling • Above that layer are sequential processes Tanenbaum作業系統2Ed

  10. Implementation of Processes (1) • PCB: process control block Tanenbaum作業系統2Ed

  11. Implementation of Processes (2) • Skeleton of what lowest level of OS does when an interrupt occurs Tanenbaum作業系統2Ed

  12. Threads 執行緒The Thread Model (1) • (a) Three processes each with one thread • (b) One process with three threads Tanenbaum作業系統2Ed

  13. 執行緒和行程的區別 • 單一執行緒行程就像十八羅漢, 忙不過來的時候可以產生分身, 甚至可以變身 • 占用較多資源 • 具有多執行緒的行程就像千手觀音, 忙不過來的時候只要多生一隻手出來即可 • 占用資源較少 Tanenbaum作業系統2Ed

  14. Multiple Processes以多個程序的分身提供服務 Tanenbaum作業系統2Ed

  15. Multithread以多個執行緒提供服務 Tanenbaum作業系統2Ed

  16. The Thread Model (2) • Items shared by all threads in a process • Items private to each thread Tanenbaum作業系統2Ed

  17. The Thread Model (3) • Each thread has its own stack Tanenbaum作業系統2Ed

  18. Thread Usage (1) • A word processor with three threads Tanenbaum作業系統2Ed

  19. Thread Usage (2) • A multithreaded Web server Tanenbaum作業系統2Ed

  20. Thread Usage (3) • Rough outline of code for previous slide • (a) Dispatcher thread • (b) Worker thread Tanenbaum作業系統2Ed

  21. Thread Usage (4) • Three ways to construct a server Tanenbaum作業系統2Ed

  22. Implementing Threads in User Space • A user-level threads package Tanenbaum作業系統2Ed

  23. Implementing Threads in the Kernel • A threads package managed by the kernel Tanenbaum作業系統2Ed

  24. Hybrid Implementations • Multiplexing user-level threads onto kernel- level threads Tanenbaum作業系統2Ed

  25. Scheduler Activations • Goal – mimic functionality of kernel threads • gain performance of user space threads • Avoids unnecessary user/kernel transitions • Kernel assigns virtual processors to each process • lets runtime system allocate threads to processors Tanenbaum作業系統2Ed

  26. Scheduler Activations(cont’d) • Basic idea • When kernel knows a thread is blocked • It notifies the process’s run-time system • Upcall: a known starting address in the process’s run-time system • The run-time reschedule its threads • Problem: • Fundamental reliance on kernel (lower layer) • Calling procedures in user space (higher layer) Tanenbaum作業系統2Ed

  27. Pop-Up Threads • Creation of a new thread when message arrives (a) before message arrives (b) after message arrives Tanenbaum作業系統2Ed

  28. Making Single-Threaded Code Multithreaded (1) • Conflicts between threads over the use of a global variable Tanenbaum作業系統2Ed

  29. Making Single-Threaded Code Multithreaded (2) • Threads can have private global variables Tanenbaum作業系統2Ed

  30. Interprocess CommunicationRace Conditions 競爭情況 • Two processes want to access shared memory at same time 最後寫入者 蓋掉前人的 Tanenbaum作業系統2Ed

  31. 關鍵區域 Critical Regions (1) • Four conditions to provide mutual exclusion • No two processes simultaneously in critical region • No assumptions made about speeds or numbers of CPUs • No process running outside its critical region may block another process • No process must wait forever to enter its critical region Tanenbaum作業系統2Ed

  32. Critical Regions (2) • Mutual exclusion using critical regions Tanenbaum作業系統2Ed

  33. Mutual Exclusion with Busy Waiting忙碌等待(1) • Strict Alternation • (a) Process 0. (b) Process 1. Tanenbaum作業系統2Ed

  34. Mutual Exclusion with Busy Waiting (2) Peterson's solution for achieving mutual exclusion 最晚到者贏! Tanenbaum作業系統2Ed

  35. Mutual Exclusion with Busy Waiting (3) TSL: Test and Set Lock • Entering and leaving a critical region using the TSL instruction 需要硬體支援 將 1 存入 LOCK 並取得 LOCK 的舊值 若舊值=0 表示鎖成功,否則:已經被別人鎖去! Tanenbaum作業系統2Ed

  36. Busy Waiting 的問題 • 浪費 CPU time • Priority inversion problem • Multi-level priority queue • High priority process in busy waiting • Low priority process in critical region • No process progresses • Spin Lock: a lock that uses busy waiting Tanenbaum作業系統2Ed

  37. Sleep and Wakeup • Sleep • A system call • Process is suspended (in waiting state) until another process wakes it up • Wakeup • Has one parameter: the process to be awakened (back to ready state) Tanenbaum作業系統2Ed

  38. The Producer and Consumer Problem 生產者與消費者 • The bounded-buffer problem count Consumer 消費者 Producer 生產者 Tanenbaum作業系統2Ed

  39. 生產者 消費者 Tanenbaum作業系統2Ed

  40. Producer and Consumer Problem • 漏掉的 Wakeup • 只有在 count 值從 0 變成 1 的時候才做 wakeup • Wakeup waiting bit • A piggy bank for wakeup signals • 在變更 count 值的時候發生 race condition • 用 critical region 把 count = count + 1 和 count = count – 1 包起來 Tanenbaum作業系統2Ed

  41. Semaphores 號誌 • Semaphore 是一個大於等於 0 的整數 • = 0 no wakeups were saved • > 0 one or more wakeups were pending • Down (P) • If semaphore == 0 then sleep;semaphore = semaphore - 1; • Up (V) • semaphore = semaphore + 1;If there is a process sleep on the semaphore then wake it up Tanenbaum作業系統2Ed

  42. Semaphores 號誌 • Down 和 Up 都是 Atomic Action,而且執行期間其他 process 無法存取該 semaphore • Atomic Action • 在一個單位時間內做完,期間不可以發生中斷 Tanenbaum作業系統2Ed

  43. The producer-consumer problem using semaphores Tanenbaum作業系統2Ed

  44. Semaphores • Binary semaphore 二元號誌 • Initialized to 1 • Used by 2 ore more processes to ensure that only one of them can enter its critical region at the same time • Mutex 互斥 • When semaphore’s ability to count is not needed • A simplified version of semaphore • Two states: unlocked & locked Tanenbaum作業系統2Ed

  45. Mutexes 互斥 Implementation of mutex_lock and mutex_unlock Tanenbaum作業系統2Ed

  46. Monitors 監督器 • 使用 Semaphore 時,程式設計師要很小心,避免寫錯順序 • Monitor:a high-level synchronization primitive • 由 compiler 安排 semaphore 的順序,避免人為錯誤 • Condition variables • Wait • Signal Tanenbaum作業系統2Ed

  47. Monitors (1) Example of a monitor Tanenbaum作業系統2Ed

  48. Monitors (2) • Outline of producer-consumer problem with monitors • only one monitor procedure active at one time • buffer has N slots Tanenbaum作業系統2Ed

  49. Solution to producer-consumer problem in Java (part 1) Tanenbaum作業系統2Ed

  50. Solution to producer-consumer problem in Java (part 2) Tanenbaum作業系統2Ed

More Related