Download presentation
Presentation is loading. Please wait.
1
Operating System Concept
授課教授 周立德 教授 助教 林昱宏
2
Ch7 Process Synchronization
Shared Memory and Race Condition Critical Section Design Critical Section SW solution HW solution Semaphore Monitor Synchronization Problem Bounded Buffer Reader and Writer Dining Philosophers
3
Shared Memory and Race Condition
Def : Process 彼此間透過對shared memory之存取,達 到溝通目的。OS不提供額外支援,只提供shared memory。 Programmer的責任 需提供 Mutual Exclusive之存取控制之同步(Synchronization)機制 Race Condition 在shared memory溝通方式下,若未對共享變數提供互斥存取之同步機制,則可能造成共享變數之最終值會受process之執行相對順序的影響,及執行順序不同,其最終結果也就不可預期。
4
Critical Section Def : Process中對共享變數進行存取的敘述程式碼之 集合,其他程式碼皆稱Remainder Section Arch. Repeat Entry Section C.S Exit Section R.S Until False C.S design 是指設計Entry Section 及 Exit Section Mutual Exclusion Progress Bounded Waiting
5
Critical Section(Cont.)
Mutual Exclusion If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. Progress If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. Bounded Waiting A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Assume that each process executes at a nonzero speed No assumption concerning relative speed of the n processes. Mutual Exclusion 在任何時間點最多指允許一個process在他自己的c.s內活動,不得有多個process同時在各自的c.s內活動 Progress 不想進入c.s之process(或在r.s中活動的process),絕不可阻礙其他process進入c.s(或參與進入c.s之決策) 且在有限的時間必須自那些想進入c.s之process中,選出一個process讓他進入c.s(即no deadlock) Bounded Waiting 自process提出進入c.s之申請到他獲准進入c.s的等待時間是有限的,即若n個process接想進入c.s, 則任一process至多等待n-1次以後,即可進入c.s(即no starvation)
6
Design Critical Section
SW Solution Algo. 1 Mutual is OK Progress is not OK 不想進C.S的process會阻礙想進者 Bounded Waiting is OK Algo. 2 Possibility of Deadlock exists(過度禮讓) Algo. 3 All conditions are satisfied
7
Design Critical Section (Cont.)
HW instr. Test-and-Set Mutual Exclusion is OK Progress is OK Bounded Waiting is not OK Other processes may starvation(同一個process可能多次進入C.S) SWAP
8
Design Critical Section (Cont.)
Semaphore (Binary) Solution to C.S design and synchronization Data type Two atomic operations P (S): while S 0 do no-op; //called wait(s) S--; V(S): S++;//called signal(s)
9
Design Critical Section (Cont.)
Critical Section Design mutex : semaphore=1 (init. value) Processi Repeat p(mutex); C.S v(mutex); R.S Until False All conditions are satisfied
10
Design Critical Section (Cont.)
Monitor Def : a advanced data type used to resolve synchronization problem Shared Data Operations (Procedures) Initiation Code Mutual Exclusion is ready Must Concentrate on synchronization
11
Synchronization Problem
Bounded Buffer- Producer and Consumer Shared variables mutex : semaphore=1 empty : semaphore=n full : semaphore =0 Producer Repeat Produce an item wait(empty) wait(mutex) Add item to buffer Signal(mutex) Signal(full) Until False Consumer Repeat Wait(full) Wait(mutex) Retrieve an item from buffer Signal(mutex) Signal(empty) Consume item Until False
12
Synchronization Problem
wrt : semaphore=1 readcount : int =0 mutex :mutex =1 Writer Repeat wait(wrt) Perform writing signal(wrt) Until False Reader Repeat wait(mutex) readcount=readcount+1 If readcount==1 then wait(wrt) signal(mutex) Perform reading readcount=readcount-1 If(readcount==0)then signal(wrt) Until False
13
Synchronization Problem
Dining Philosophers Chopstick[5]:semaphore Process Repeat Wait(chopstick[i]) Wait(chopstick[(i+1)Mod 5]) Eating Signal(chopstick) Signal(chopstick[(i+1)Mod 5]) thinking Until false Dead Lock may occur
14
Synchronization Problem(Cont.)
Type dining-ph =Monitor Var state:array[5] of (thinking,hungry,eating) self:array[5] of condition Procedure entry pickup(i) Begin state[i]=hungry Test(i) If(state[i]!=eating) then Self[i].wait Procedure test(i) If (state[(k+4)mod5]!=eating AND state[k]==hungry AND state[(k+1)mod 5]!=eating ) then State[k]=eating Self[k].singal
15
Synchronization Problem(Cont.)
Procedure putdown(i) State[i]=thinking Test((i+4) mod 5) Test((i+1)mod 5) Init. Code For i=0 to 4 Usage dp:dining-ph Philosopher (i) Repeat dp.pickup(i) Eating dp.putdown(i) Thinking Until False
16
Ch 8 Deadlock Deadlock Resource Allocation Graph Basic facts
Handling Deadlocks Combined Approach to Deadlock Handling
17
Ch 8 Deadlock Def : 系統中存在一組processes彼此形成circular waiting,造 成processes皆無法執行下去造成cpu utilization and throughput 低落 Deadlock四必要條件 Mutual exclusion Only one process at a time can use a resource. Hold and wait A process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption A resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait There exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0.
18
Resource Allocation Graph with deadlock
19
Basic Facts No cycle→No deadlock
有Cycle,不一定有deadlock(for multi-resource instance) If all resources are single instance, then a cycle exists means that a deadlock exists
20
Handling Deadlocks Deadlock prevention Deadlock avoidance
Pros no deadlock Cons utilization lower and throughput lower Deadlock detection and recovery utilization和throughput相對較高 系統可能進入死結狀態 Cost高
21
Handling Deadlocks Deadlock prevention Break one of four requiremnets
Mutual Exclusion Impossible(某些資源本身就具互斥性質) Hold and wait 規定除非process可以一次取得完成工作所須知全部資源,才准許持有resource,否則不得持有任何資源 允許process先持有部份資源,但若要提出其他資源申請之前,需先釋放持有之所有資源才可申請. No preemption 改成preemption即可,即高優先權process可搶奪其他process之資源來完成工作 Circular waiting 賦予各資源編號,process需依資源編號順序提出申請
22
Handling Deadlocks Deadlock Avoidance
When a process requests an available resource How much resource is hold by each process currently How much resource does each process need to complete jobs Available resource in system Execute Banker’s algo.(included safety algo.) If system is in safety state ,then permit requests Or reject this request and process must wait for next time to request
23
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types. Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available. Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj. Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj. Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task. Need [i,j] = Max[i,j] – Allocation [i,j].
24
Safety Algorithm 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work := Available Finish [i] = false for i - 1,3, …, n. 2. Find and i such that both: (a) Finish [i] = false (b) Needi Work If no such i exists, go to step 4. 3. Work := Work + Allocationi Finish[i] := true go to step 2. 4. If Finish [i] = true for all i, then the system is in a safe state.
25
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of resource type Rj. 1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim. 2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available. 3. Pretend to allocate requested resources to Pi by modifying the state as follows: Available := Available = Requesti; Allocationi := Allocationi + Requesti; Needi := Needi – Requesti;; If safe the resources are allocated to Pi. If unsafe Pi must wait, and the old resource-allocation state is restored
26
Algo. Summary Check requesti <= Needi
Check requesti <= Available Make a sheet Run safety algo. Goal of safety algo. Find more than or equal to one safe sequence, OS follows this sequence allocate resources and make all processes complete their jobs
27
Detection Algorithm 1. Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work :- Available (b) For i = 1,2, …, n, if Allocationi 0, then Finish[i] := false;otherwise, Finish[i] := true. 2. Find an index i such that both: (a) Finish[i] = false (b) Requesti Work If no such i exists, go to step 4.
28
Detection Algorithm (Cont.)
3. Work := Work + Allocationi Finish[i] := true go to step 2. 4. If Finish[i] = false, for some I, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] = false,then Pi is deadlocked. Algorithm requires an order of m x n2 operations to detect whether the system is in deadlocked state.
29
Recovery Algo. Kill processes Resource Preemption Kill all processes
Kill the process one by one Resource Preemption Selecting a victim – minimize cost. Rollback – return to some safe state, restart process fro that state. Starvation – same process may always be picked as victim, include number of rollback in cost factor.
30
Combined Approach to Deadlock Handling
Combine the three basic approaches prevention avoidance detection allowing the use of the optimal approach for each of resources in the system. Use most appropriate technique for handling deadlocks within each class.
31
Ch9 Memory Management Address binding of instructions and data to memory addresses can happen at three different stages Compile time Load time Execution time
32
Dynamic Loading Routine is not loaded until it is called
Better memory-space utilization; unused routine is never loaded. Useful when large amounts of code are needed to handle infrequently occurring cases. No special support from the operating system is required implemented through program design.
33
Dynamic Linking Linking postponed until execution time.
Small piece of code, stub, used to locate the appropriate memory-resident library routine. Stub replaces itself with the address of the routine, and executes the routine. Operating system needed to check if routine is in processes’ memory address. E.g Dynamic Linking library
34
Logical vs. Physical Address Space
The concept of a logical address space that is bound to a separate physical address space is central to proper memory management. Logical address – generated by the CPU; also referred to as virtual address. Physical address – address seen by the memory unit. Logical and physical addresses are the same in compile- time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme.
35
Swapping A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution. Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images. Roll out, roll in – swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be loaded and executed. Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped. Modified versions of swapping are found on many systems, i.e., UNIX and Microsoft Windows.
36
Memory allocation First-fit: Allocate the first hole that is big enough. Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size. Produces the smallest leftover hole. Worst-fit: Allocate the largest hole; must also search entier list. Produces the largest leftover hole. First-fit and best-fit better than worst-fit in terms of speed and storage utilization.
37
Fragmentation External Fragmentation : 在連續性配置下,所有free blocks之size皆無法滿足process大小需求,但這些free blocks size加總大於等於process size,但由於不連續,依 然不能配置,形成記憶體浪費 Internal Fragmentation : 配給process之空間超過process 所需,其造成的差值空間,此process用不到且其他 process亦無法使用 First-fit and Best-fit 無internal fragmentation ,but external fragmentation
38
Paging Divide physical memory into fixed-sized blocks called frames
Divide logical memory into blocks of same size called pages. Keep track of all free frames. To run a program of size n pages, need to find n free frames and load program. Set up a page table to translate logical to physical addresses. Internal fragmentation.
39
Paging(Cont.)
40
Page Table Register Memory and PTBR(Page Table Base Register)
TLB(Translation Lookaside Buffer) register Effective Memory Access Time P*(TLB access time + memory access time)+(1-p)*(TLB access time +2*memory access time) P:TLB hit ratio
41
Multi-level paging Def : paging the page table
By multi-paging, a large page table can be divided into more small piece one separately in memory
42
Inverted page table 以physical memory 之frame 為記錄對象,若有n個 frames則inverted page table 就有n個entry,entry紀錄 <process id, page no.>
43
Segmentation Memory-management scheme that supports user view of memory. Physical memory 視為一個夠大的連續可用區塊 Logical memory 視為一組segment之集合,而各段大小不一定相等
44
Paged segment memory management
45
Page v.s. segment Page Segment 各page size相同 各segment大小不同 無 有外部斷裂 有內部斷裂
Memory protection share較困難 較易實現 與user對memory看法不一致 一致 Logical address為單一量 兩個量(s,d) 無須check page offset<page size 須check segment offset<segment limit Page table只記錄frame No. Segment table記segment之大小 即起始位置
46
Ch10 Virtual Memory Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical address space can therefore be much larger than physical address space. Need to allow pages to be swapped in and out. Virtual memory can be implemented via: Demand paging Demand segmentation
47
Demand paging 以paging memory management為基礎,採lazy swapper, 程序執行不需全部載入pages,而是載入所需.若試著存 取不在memory的pages則page fault.需載入lost pages 使process繼續執行
48
Valid-Invalid Bit With each page table entry a valid–invalid bit is associated (1 in-memory, 0 not-in-memory) Initially valid–invalid but is set to 0 on all entries. Example of a page table snapshot. During address translation, if valid–invalid bit in page table entry is 0 page fault. Frame # valid-invalid bit 1 1 1 1 page table
49
Page Fault If there is ever a reference to a page, first reference will trap to OS page fault OS looks at another table to decide: Invalid reference abort. Just not in memory. Get empty frame. Swap page into frame. Reset tables, validation bit = 1. Restart instruction: Least Recently Used block move auto increment/decrement location
50
What happens if there is no free frame?
Page replacement – find some page in memory, but not really in use, swap it out. algorithm performance – want an algorithm which will result in minimum number of page faults. Same page may be brought into memory several times.
51
Page Replacement Algo. FIFO OPT LRU-最少被用的swap out LRU Approximation
Belady anomaly Page越多反而錯誤率越高? 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 OPT 難以實做 LRU-最少被用的swap out LRU Approximation Reference bit Second chance LFU/MFU 被用到最少/多次的swap out
52
Thrashing 在demanding paging, multiprogramming, global replacement policy,若process分配到的frames不足,則 process會經常page fault且需page replacement,則此 process可能會搶奪其他process的frames,造成其他 process也page fault,同時再繼續搶其他process的frames, 如此之下,所有process 都page fault及等待swap in/out. 此時cpu utilization下降,multiprogramming機制試圖引 入更多process但原本frames就不足,造成更多page fault. 如此造成cpu utilization急速下降,paging I/O異常忙碌, 花在io上比花在執行code的時間更多.
53
Thrashing Solutions Decrease multiprogramming degree
Page fault frequency bound Working set More practices are required
54
Ch11File System 各種file operation create write read
reposition within file – file seek delete truncate open(Fi) – search the directory structure on disk for entry Fi, and move the content of entry to memory. close (Fi) – move the content of entry Fi in memory to directory structure on disk.
55
File Organization Method
Sequential Access File Direct Access Index Access File Index Sequential Access Method
56
File Protection Physical Protection Logical Protection Backup
Name protection Password protection Access group protection
57
Directory Structure Single-Level Directory Two-Level Directory
A single directory for all users. Two-Level Directory Separate directory for each user Tree-Structured Directories Efficient searching Acyclic-Graph Directories刪檔案問題 檔案指標變懸置 沒被參考才可刪除
58
File-System Structure
Contiguous Allocation 優點:avg seek time較短、支援random access以及sequential access 缺點:浪費空間、檔案大小無法擴充 Linked Allocation 優點:省空間、檔案大小可擴充 缺點: avg seek time較長,不支援random access Indexed Allocation 優點:不浪費空間、檔案大小可擴充,支援random access 缺點:需額外 index table,檔案太大時單一index能無法容納所有block
59
Free-Space Management
Bit vector 優點:易實做、容易找到可用區塊 缺點:不適用於大型disk system Linked list 優點:不浪費空間 缺點:不容易找到可用區塊 Grouping Counting
60
Ch12 I/O Polling Interrupts Direct Memory Access
Busy-wait cycle to wait for I/O from device Interrupts CPU Interrupt request line triggered by I/O device Interrupt handler receives interrupts Direct Memory Access
61
Interrupt
62
Kernel I/O Subsystem Buffering Spooling Device reservation
store data in memory while transferring between devices Spooling 利用disk作為急大的緩衝區在使用,CPU將output以file形式送往disk spooling area後,即認為工作結束。而output device此時或稍後可自spooling area取出file進行I/O operation,如printer Device reservation Provides exclusive access to a device 利用system call來分配資源 需注意死結問題
63
Ch13 Disk management Disk access time Swap-space
Seek time (bottle neck) 將磁頭移到指定的track上方所花的時間 Latency 將sector轉到磁頭下方所花的時間 Transfer time Data在disk與memory之間的傳輸時間 Swap-space Virtual memory uses disk space as an extension of main memory.
64
FCFS SSTF Disk scheduling Algo. 越早到達的track request優先服務
65
Disk scheduling Algo.(cont.)
SCAN 讀寫頭來回不斷掃描,遇有請求即刻服務。而當磁頭遇track盡頭或開頭才折返 C-SCAN 與SCAN類似,差別在於只提供單方向的服務,回程不做任何服務 Look 類似SCAN,差別在於服務完後該方向之最後一個track請求後,即刻折返提供服務,無須到盡頭/開端才折返 C-Look 與Look類似,差別在於只提供單方向的服務,回程不做任何服務
66
Ch14 Network Structures Background Motivation Topology Network Types
Communication Design Strategies
67
Background & Motivation
Nodes Types Mainframes Workstations Personal Computers Motivation Resource sharing Computation speedup – load sharing Reliability Communication – message passing
68
Topology Fully connected network Partially connected network
Tree-structured network Star network Ring networks Single links Double links Bus network Linear bus Ring bus
69
Communication Domain name service (DNS) Routing Strategies
specifies the naming structure of the hosts, as well as name to address resolution (Internet). Routing Strategies Fixed routing Virtual circuit Dynamic routing Connection Strategies Circuit switching Message switching Packet switching Contention Token passing Message slots
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.