Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization...

69
Operating System Concept 授授授授 授授授 授授 授授 授授授
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    281
  • download

    1

Transcript of Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization...

Page 1: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Operating System Concept授課教授 周立德 教授 助教 林昱宏

Page 2: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

2

Page 3: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Shared Memory and Race Condition

Def : Process 彼此間透過對 shared memory之存取,達到溝通目的。 OS不提供額外支援,只提供 shared memory。 Programmer的責任需提供 Mutual Exclusive之存取控制之同步

(Synchronization)機制 Race Condition

在 shared memory溝通方式下,若未對共享變數提供互斥存取之同步機制,則可能造成共享變數之最終值會受process之執行相對順序的影響,及執行順序不同 ,其最終結果也就不可預期。

3

Page 4: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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 4

Page 5: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

5

Page 6: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Design Critical Section

SW Solution Algo. 1

Mutual is OK Progress is not OK

不想進 C.S 的 process會阻礙想進者 Bounded Waiting is OK

Algo. 2 Mutual is OK Progress is not OK

Possibility of Deadlock exists(過度禮讓) Bounded Waiting is OK

Algo. 3 All conditions are satisfied 6

Page 7: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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 Mutual Exclusion is OK Progress is OK Bounded Waiting is not OK

7

Page 8: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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)

8

Page 9: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

9

Page 10: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

10

Page 11: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

11

Page 12: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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 wait(mutex) readcount=readcount-1 If(readcount==0)then

signal(wrt)

signal(mutex) Until False

12

Page 13: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Synchronization Problem

Dining PhilosophersChopstick[5]:semaphoreProcess

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 13

Page 14: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

14

Page 15: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

State[i]=thinking

Usage

dp:dining-ph

Philosopher (i) Repeat

dp.pickup(i) Eating dp.putdown(i) Thinking

Until False15

Page 16: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Ch 8 Deadlock

Deadlock Resource Allocation Graph Basic facts Handling Deadlocks Combined Approach to Deadlock Handling

16

Page 17: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

17

Page 18: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Resource Allocation Graph with deadlock

18

Page 19: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

19

Page 20: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Handling Deadlocks

Deadlock prevention Deadlock avoidance

Pros no deadlock

Cons utilization lower and throughput lower

Deadlock detection and recovery Pros

utilization 和 throughput相對較高 Cons

系統可能進入死結狀態 Cost高 20

Page 21: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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需依資源編號順序提出申請

21

Page 22: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

22

Page 23: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Data Structures for the Banker’s Algorithm

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].

Let n = number of processes, and m = number of resources types.

23

Page 24: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Safety Algorithm

1. Let Work and Finish be vectors of length m and n, respectively. Initialize:

Work := AvailableFinish [i] = false for i - 1,3, …, n.

2. Find and i such that both: (a) Finish [i] = false

(b) Needi WorkIf no such i exists, go to step 4.

3. Work := Work + Allocationi

Finish[i] := truego to step 2.

4. If Finish [i] = true for all i, then the system is in a safe state. 24

Page 25: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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 restored25

Page 26: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

26

Page 27: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

27

Page 28: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Detection Algorithm (Cont.)

3. Work := Work + AllocationiFinish[i] := truego 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.

28

Page 29: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Recovery Algo.

Kill processes 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.

29

Page 30: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

30

Page 31: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Ch9 Memory Management

Address binding of instructions and data to memory addresses can happen at three different stages Compile time Load time Execution time

31

Page 32: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

32

Page 33: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

33

Page 34: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

34

Page 35: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

35

Page 36: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

36

Page 37: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

37

Page 38: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

38

Page 39: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Paging(Cont.)

39

Page 40: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

40

Page 41: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

41

Page 42: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Inverted page table

以 physical memory 之 frame 為記錄對象 ,若有 n 個frames 則 inverted page table 就有 n 個 entry,entry紀錄 <process id, page no.>

42

Page 43: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Segmentation

Memory-management scheme that supports user view of memory.

Physical memory視為一個夠大的連續可用區塊

Logical memory視為一組 segment之集合,而各段大小不一定相等

43

Page 44: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Paged segment memory management

44

Page 45: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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 之大小即起始位置

Page 46: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

46

Page 47: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Demand paging

以 paging memory management為基礎 , 採 lazy swapper,程序執行不需全部載入 pages,而是載入所需 .若試著存取不在 memory 的 pages 則 page fault.需載入 lost pages 使 process繼續執行

47

Page 48: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

11110

00

Frame # valid-invalid bit

page table48

Page 49: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

49

Page 50: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.

50

Page 51: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Page Replacement Algo.

FIFO 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 51

Page 52: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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的時間更多 .

52

Page 53: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Thrashing

Solutions Decrease multiprogramming degree Page fault frequency bound Working set

More practices are required

53

Page 54: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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.54

Page 55: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

File Organization Method

Sequential Access File Direct Access Index Access File Index Sequential Access Method

55

Page 56: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

File Protection

Physical Protection Backup

Logical Protection Name protection Password protection Access group protection

56

Page 57: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Directory Structure

Single-Level Directory A single directory for all users.

Two-Level Directory Separate directory for each user

Tree-Structured Directories Efficient searching

Acyclic-Graph Directories 刪檔案問題 檔案指標變懸置 沒被參考才可刪除

Page 58: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

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

Page 59: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Free-Space Management

Bit vector 優點:易實做、容易找到可用區塊 缺點:不適用於大型 disk system

Linked list 優點:不浪費空間 缺點:不容易找到可用區塊

Grouping Counting

Page 60: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Ch12 I/O

Polling 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

60

Page 61: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Interrupt

61

Page 62: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Kernel I/O Subsystem

Buffering 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 來分配資源 需注意死結問題

Page 63: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Ch13 Disk management

Disk access time 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.

63

Page 64: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Disk scheduling Algo.

FCFS越早到達的 track request優先服務

SSTF距離目前讀寫頭位置最近的 track request優先服務

Page 65: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Disk scheduling Algo.(cont.) SCAN

讀寫頭來回不斷掃描,遇有請求即刻服務。而當磁頭遇 track盡頭或開頭才折返

C-SCAN與 SCAN類似,差別在於只提供單方向的服務,回程不做任何服務

Look類似 SCAN,差別在於服務完後該方向之最後一個 track請求後,即刻折返提供服務,無須到盡頭 /開端才折返

C-Look與 Look類似,差別在於只提供單方向的服務,回程不做任何服務

Page 66: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Ch14 Network Structures

Background Motivation Topology Network Types Communication Design Strategies

Page 67: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Background & Motivation

Nodes Types Mainframes Workstations Personal Computers

Motivation Resource sharing Computation speedup – load sharing Reliability Communication – message passing

Page 68: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Topology

Fully connected network Partially connected network Tree-structured network Star network Ring networks

Single links Double links

Bus network Linear bus Ring bus

Page 69: Operating System Concept 授課教授 周立德 教授 助教 林昱宏. Ch7 Process Synchronization Shared Memory and Race Condition Critical Section Design Critical Section SW.

Communication Domain name service (DNS)

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