730 likes | 905 Views
第二章 行程與執行緒. 鄧姚文 http://www.ywdeng.idv.tw. Processes and Threads 大綱. 行程 processes 執行緒 threads 行程間通訊 interprocess communication 傳統 IPC 的問題 排程 scheduling. 行程 processes. Pseudoparallelism 只有一個 CPU ,輪流執行多個行程, 每一個行程都持續有進度 平行處理 Parallel processing :多個 CPU 同時執行許多個行程,或合作加速一個行程的執行速度
E N D
第二章行程與執行緒 鄧姚文 http://www.ywdeng.idv.tw Tanenbaum作業系統2Ed
Processes and Threads大綱 • 行程 processes • 執行緒 threads • 行程間通訊 interprocess communication • 傳統 IPC 的問題 • 排程 scheduling Tanenbaum作業系統2Ed
行程 processes • Pseudoparallelism • 只有一個 CPU,輪流執行多個行程, 每一個行程都持續有進度 • 平行處理 Parallel processing:多個 CPU 同時執行許多個行程,或合作加速一個行程的執行速度 • 多重處理器 Multiprocessor:專指擁有多個 CPU 這件事情 Tanenbaum作業系統2Ed
ProcessesThe Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant Tanenbaum作業系統2Ed
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
Process Termination • 行程中止的狀況 • Normal exit (自願) • Error exit (自願) • Fatal error (非自願) • Killed by another process (非自願) Tanenbaum作業系統2Ed
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
Process States 行程狀態(1) • Possible process states • Running 執行中 • Blocked 等待中 • Ready 備妥 • Transitions between states shown Tanenbaum作業系統2Ed
Process States (2) • Lowest layer of process-structured OS • handles interrupts, scheduling • Above that layer are sequential processes Tanenbaum作業系統2Ed
Implementation of Processes (1) • PCB: process control block Tanenbaum作業系統2Ed
Implementation of Processes (2) • Skeleton of what lowest level of OS does when an interrupt occurs Tanenbaum作業系統2Ed
Threads 執行緒The Thread Model (1) • (a) Three processes each with one thread • (b) One process with three threads Tanenbaum作業系統2Ed
執行緒和行程的區別 • 單一執行緒行程就像十八羅漢, 忙不過來的時候可以產生分身, 甚至可以變身 • 占用較多資源 • 具有多執行緒的行程就像千手觀音, 忙不過來的時候只要多生一隻手出來即可 • 占用資源較少 Tanenbaum作業系統2Ed
Multiple Processes以多個程序的分身提供服務 Tanenbaum作業系統2Ed
Multithread以多個執行緒提供服務 Tanenbaum作業系統2Ed
The Thread Model (2) • Items shared by all threads in a process • Items private to each thread Tanenbaum作業系統2Ed
The Thread Model (3) • Each thread has its own stack Tanenbaum作業系統2Ed
Thread Usage (1) • A word processor with three threads Tanenbaum作業系統2Ed
Thread Usage (2) • A multithreaded Web server Tanenbaum作業系統2Ed
Thread Usage (3) • Rough outline of code for previous slide • (a) Dispatcher thread • (b) Worker thread Tanenbaum作業系統2Ed
Thread Usage (4) • Three ways to construct a server Tanenbaum作業系統2Ed
Implementing Threads in User Space • A user-level threads package Tanenbaum作業系統2Ed
Implementing Threads in the Kernel • A threads package managed by the kernel Tanenbaum作業系統2Ed
Hybrid Implementations • Multiplexing user-level threads onto kernel- level threads Tanenbaum作業系統2Ed
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
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
Pop-Up Threads • Creation of a new thread when message arrives (a) before message arrives (b) after message arrives Tanenbaum作業系統2Ed
Making Single-Threaded Code Multithreaded (1) • Conflicts between threads over the use of a global variable Tanenbaum作業系統2Ed
Making Single-Threaded Code Multithreaded (2) • Threads can have private global variables Tanenbaum作業系統2Ed
Interprocess CommunicationRace Conditions 競爭情況 • Two processes want to access shared memory at same time 最後寫入者 蓋掉前人的 Tanenbaum作業系統2Ed
關鍵區域 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
Critical Regions (2) • Mutual exclusion using critical regions Tanenbaum作業系統2Ed
Mutual Exclusion with Busy Waiting忙碌等待(1) • Strict Alternation • (a) Process 0. (b) Process 1. Tanenbaum作業系統2Ed
Mutual Exclusion with Busy Waiting (2) Peterson's solution for achieving mutual exclusion 最晚到者贏! Tanenbaum作業系統2Ed
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
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
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
The Producer and Consumer Problem 生產者與消費者 • The bounded-buffer problem count Consumer 消費者 Producer 生產者 Tanenbaum作業系統2Ed
生產者 消費者 Tanenbaum作業系統2Ed
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
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
Semaphores 號誌 • Down 和 Up 都是 Atomic Action,而且執行期間其他 process 無法存取該 semaphore • Atomic Action • 在一個單位時間內做完,期間不可以發生中斷 Tanenbaum作業系統2Ed
The producer-consumer problem using semaphores Tanenbaum作業系統2Ed
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
Mutexes 互斥 Implementation of mutex_lock and mutex_unlock Tanenbaum作業系統2Ed
Monitors 監督器 • 使用 Semaphore 時,程式設計師要很小心,避免寫錯順序 • Monitor:a high-level synchronization primitive • 由 compiler 安排 semaphore 的順序,避免人為錯誤 • Condition variables • Wait • Signal Tanenbaum作業系統2Ed
Monitors (1) Example of a monitor Tanenbaum作業系統2Ed
Monitors (2) • Outline of producer-consumer problem with monitors • only one monitor procedure active at one time • buffer has N slots Tanenbaum作業系統2Ed
Solution to producer-consumer problem in Java (part 1) Tanenbaum作業系統2Ed
Solution to producer-consumer problem in Java (part 2) Tanenbaum作業系統2Ed