computer science — an overview j. glenn brookshear chapter three operating systems and networks

83
Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Post on 21-Dec-2015

238 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Computer Science — An Overview J. Glenn Brookshear

Chapter ThreeOperating Systems and Networks

Page 2: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Online

• Operating System Architecture

• Important Issues in OS

• Coordinating the Machine’s Activities

• Handling Competition Among Processes

• Networks

• Network Protocols

• Security

Page 3: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Operating System Architecture

Page 4: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

What is Operating System ?

• Operation System ( OS )– 個人電腦作業系統

• Windows 95/98 、 Windows Me 、 Windows XP

– 伺服器作業系統• Windows NT/2000 、 Unix 、 Linux 、 FreeBSD

– 掌上型電腦、智慧型手機作業系統• Palm OS 、 EPOC 、 Windows CE

– 其他的作業系統• DEC VMS 、 DOS 、 Mac OS

• Peter’s Lesson 12

Page 5: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

作業系統( Operating System )

• 為了方便對硬體的應用而發展出來的程式模組。

• 做為使用者( User )和 Hardware 之間的溝通的橋樑、介面( Interface )。

• 控制計算機的運作流程、管理系統的資源、仲裁資源使用上的衝突、維護計算機正確的使用。

• 期使計算機發揮最大的效能。

Page 6: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

從運用的觀點看電腦系統架構

Computer Hardware

OS

Application User

Page 7: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Why OS?

• 從運用的觀點看電腦架構、從設計 OS 的角度來觀察 OS ,探討 OS 內部如何運作

• 對於 Programmer 而言,應學習如何撰寫 Windows 應用程式。然而在學習程式設計的過程中,還應該學習 Windows 這個作業系統的架構與性能,才能將作業系統的觀念與應用程式的寫作相互呼應。

• 了解作業系統,可以幫助 Programmer 了解諸多程式設計上的「 Why 」。

Page 8: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Software Classification

• Applications

• System Software– Utilities– Operation System

• Interface (shell)

• Kernel

Page 9: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Figure 3.3Software classification

software

Page 10: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Application Program

• 應用程式( Application Program , AP )是以 computer 為工具來解決某些問題的程式。– Ex : Database system 、 Program

development software 、 Desktop publishing system 、 GAME 、 I-PHOME 、 IE

Page 11: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Utility

• 公用軟體( Utility Software )是一些軟體做為 OS 功能的延伸,依個人需求而安裝與否。– Ex : Disk format software 、 Compression

software 、 Network utility 、 Modem software

• 系統程式( System Program , SP )是用來支援 computer 本身的運作。– Ex :文字編輯器、組譯程式、編譯程式、連結程式、載入程式、直譯程式

Page 12: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

作業系統核心( Kernel )

• 一個電腦內部隨時都在執行的程式 • 常駐於記憶體中, OS 最基本的部份• Kernel 的功能

– File Management 、 I/O Operation (Device Driver) 、 Memory Management 、 CPU Dispatch and Scheduler

– Interrupt Handling 、 Process Creation and Destruction 、 Process State Switching 、 Process Synchronization

Page 13: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Interface

• 兩種不同裝置為了進行資料傳輸所制定的一種規範– Ex : IDE 介面、 API 、 Command Interpreter

• OS 的命令解譯系統 Command Interpreter– Also, we call command-interpreter as a shell.

– 圖形介面 (graphic user interface , GUI) v.s. 文字介面

– 設計考慮的因素:螢幕格式、系統回應時間、錯誤控制能力

Page 14: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Figure 3.4Shell

Kernel Operating system

Page 15: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

• With a shell interface, you can choose a favorite shell and define your personalize environment:

> cp directory1/file directory2/file

> mycopy directory1 directory2 file

• You can put a window environment upon the shell.

directory1 directory2

file file

Shell Interface

copy

Page 16: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Important Issues in OS

Page 17: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Some Important Issues

• Batch System

• Multiprogramming System

• Interactive System

• Real-time System

• Multitasking

• Multiprocessor OS

• Bootstrap

Page 18: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Beginning of Computer

• In 1940-1950, single-processor systems began to serve the human.

• A computer is shared by many people.

• The execution of each program, called a job, was handled as an isolated activity.

• Computer operators control the process of computers: store a program, execution, get output.

Page 19: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Batch System

• 批次系統( Batch System ):將使用者的資料依程式的特性分門別類存放,同一類的程式一起處理。– Similar jobs ( all C programs ) work together.– One job finished, then next job can execute.

• Batch can reduce the work of operator.– Example : Not need to load the C compiler for

each programs.

Page 20: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Figure 3.1Batch processing

Page 21: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Queue

• A mass storage

• First-in, first-out (FIFO)

C B A C B AABC

Page 22: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Monitor

• 可以不要 Operators 一直監控電腦的執行嗎?

• 以一個常駐監督程式( Monitor )放置於 Memory ,代替人工監視程式的執行的進度,可以依預設的流程,自動轉移 CPU 控制權到指定的 Job.

• A set of instructions (with job control language, JCL) explains the steps required to prepare the machine for a particular job.

Page 23: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Multiprogramming System

• 所有等待要執行的 Job 均在工作集中區( Job Pool ) 等待,隨時有 Job 在執行– 提高 CPU 使用率( Utilization )

• OS 利用排班器( Scheduler )選擇 Job

Job Pool

OS 選擇某 Job 一來執行

Job 3

CPU

Page 24: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Job Priority

• Usually, the job is first-in first-out.

• However, we can set the priority such that the job with higher priority executes first.

Page 25: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

分時作業系統 ( Time Sharing System )( 1

)• Task :要達成某個目標而執行的一連串動作,通常是比較抽象像 Job 這樣的概念。

• 分配給每個 Task 的一小段 CPU 時間,時間到( Time out )則換其他 Task 工作。

• CPU 在數個 Tasks 之間轉換以執行工作。• 又稱為多工系統( Multi-tasking System )

Task 1 Task 2 Task 3 Task 1 Task 2 Task 3

CPU 的工作流程

Page 26: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Time Sharing System ( 2 )• Task 分配到的一小段 CPU 時間,稱為時間配額( Time Quantum )、 Time Slot、 Time Unit 、 Time Slice

• 由 OS 來控制 Time Quantum 的分配• What is the advantage of time sharing

system?

Page 27: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Interactive System

• For some applications, the user must interact with the program during its execution.– For example, reservation system, word processing,

game.

• Every user thinks that he/she owns the computer.

• Time sharing system can provide interactive concept without degrading the performance of computer.

Page 28: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Figure 3.2 Interactive processing

Page 29: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Real Time System

• User 對於時間的要求很嚴謹時,在限定時間內做出反應,專門應用的控制裝置– Hard Real-time :對時間要求最高– Soft Real-time :對 Real-time 有要求的

Job 有優先權• 需快速 CPU 及大量 Memory

Page 30: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Multiprocessor OS

• Communication and competition between processors

• Load Balancing– Making sure that the processors are used efficiently

• Scaling– Breaking tasks into a number of subtasks

compatible with the number of processors in the machine

Page 31: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Boot Strapping

• The processing to start up the computer: boot strapping or booting.

• A bootstrap loader – It saves in the ROM (Read Only Memory).– The first program is executed when the

computer powers up.– Usually, it load the OS (or another big loader)

to the computer.

Page 32: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

•When your PC switches on: – The processor starts executing the ROM,– then, loads and executes the boot sector,– then, loads the rest of the OS and execute it.

•This scheme allows an OS to be upgraded.Bootcircuitry Processor

BootROM violate Memory

(RAM)Bootsector Disk

Bootstrapping a Computer

Page 33: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Figure 3.5 (A)The booting process

Page 34: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Figure 3.5 (B)The booting process

Page 35: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Virtual Memory ( 1/2 )

• Main memory is not enough for multi-processes.• In fact, it is enough to load a part of the program that

is really executed now.• The MMU (memory management unit) controls

the swapping of programs between main memory and disk.• Virtual addresses ( logic view ) are converted to

physical addresses ( memory ) and visa.• The concept is called as virtual memory.

Page 36: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Virtual Memory ( 2/2 )

• Simple systems use swapping:• a sleeping program is stored on disk• it may be reloaded to a different address

• More sophisticated systems use paging:• a page is (say) a 16K block of memory• unused pages are stored on disk• relies on programs using few pages at once• can lead to thrashing

Page 37: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Coordinating the Machine’s Activities

Page 38: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Process

• Process 是執行中的程式– 可能是 User Programs ( or Task )或是

OS 中的 I/O 執行的程式。• Process 不只是 Program Code ,還包含許多資訊。

• Process is an activity.

Page 39: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Process Table

• Process Control Block (PCB): 在 OS 中以 PCB 來記載各個 Process 的 Information 。– Process state 、 Process

identifier 、 Priority 、 Memory Management information 、 Resource allocation information 、 Contents of PC, register, stack, variables 、 Accounting

• Process table is the collection of PCB to kept track of all the process information.

Page 40: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Process Switch

• 對於 Multiprogramming 系統,當 CPU 要換到另一 Process 工作,要將舊的 Process 的 PCB 存起來並載入新的 Process 之 PCB 。

• The procedure of changing form one process to another is called a process switch or context switch.

• Context Switch 是系統很大的負擔。

Page 41: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Figure 3.6Time-sharing between process A and process B

Page 42: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Context Switch

OS 儲存PA 的 PCB

OS 載入PB 的 PCB

OS 載入PA 的 PCB

OS 儲存PB 的 PCB

Process PA Process PB

PA runs

(1)

(2)(3)

PB waits

PA waits

PB waits PA runs

PB runs

Page 43: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Process State Transition

• Process 的狀態( State )代表 Process 此時的動態。

• Process 在其生命週期中,可能因系統發生某一事件( Event )時,會造成它 State 的轉換。

• Process 的 State 會不斷的改變。• 不同 OS 有不同的 States and events 。• 這些狀態的轉換可以用 State Transition

Diagram 表現。

Page 44: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

State Transition Diagram

New

Suspend Ready

CompleteRunningReady

Suspend Blocked

Blocked

(a)

(h)

(e)(f)(h)(i)

(c) (g)(b)

(i)

(d)

Page 45: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Process 的 State ( 1/2 )• Running (執行): Process 正在使用

CPU

• Ready (預備): Process 正在等待 CPU 的使用權

• Blocked 或 Wait (等待): Process 正在等待某事的發生

• New (新建立): Process 存放在 Disk 的 Job Pool 中,等待被選入記憶體中

Page 46: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Process 的 State ( 2/2 )

• Complete (完成): Process 執行完畢或因錯誤而結束工作

• Suspend (暫停) Ready : Process 原本在 Ready 狀態,暫時取出以減少 CPU 運作負荷

• Suspend Blocked : Process 原本在 Blocked 狀態,暫時取出以減少 I/O 運作負荷

Page 47: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Events 的解釋( 1/2 )• Dispatch (分配):分配到 CPU 的使用權• Time out (時間截止):分配到的 CPU 的

Time Quantum 已經用盡• Blocked (中止):正在使用 CPU 的

Process 因等待 I/O 或其他訊號而被中止 CPU 的使用權

• Wakeup (叫醒):得到 I/O 或其他服務完畢,可以進入等待 CPU 的狀態

Page 48: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Events 的解釋( 2/2 )

• Suspend (暫停):因系統運作負荷太高,暫時被選取,放置一旁不與機會啟動

• Resume (重新啟動):因系統運作負荷降低,可增加 Process 運作的數量,因此被選中取回,有機會被啟動

Page 49: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Cooperate Processing

Page 50: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Co-processing

• Several processes can work together for one job.

• These processes can be the same computer or different platforms.

• How can processes cooperate ?– Client Server Model– CORBA ( Common Object Request Broker

Architecture )

Page 51: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

The Client/Server Model

• The simplest model of two object communication: client-server model

Figure 3.7 The client/server model

Page 52: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Figure 3.8Communication structure

Page 53: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

CORBA

• It was developed by the OMG (Object Management Group)– provides the set of standards and specifications.– Why: the desire to establish a uniform message-

passing system that can support such a distribution system in computer networks

• CORBA provides a standard for network-wide communication between clients and servers with heterogeneous platform.

Page 54: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Handling Competition Among Processes

Page 55: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Resource Management

• One of the most important tasks on a computer is the resource management.

• Resources includes memory, hardware device, files, CPU times, PCB...

• OS is the administrator of resources when two processes compete the same resource.

Page 56: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Resource Allocation

• 當系統正常運作時, Process 使用每一項資源,都必須依循下面規則:1. 要求( Request )2. 配置( Allocated )與使用( Use )3. 釋放( Release )

Page 57: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖 - Request

Process A

資源 R1

要求 Request

Process A 要求一個 R1 型態的資源

R1 型態的資源共有三個

Page 58: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖 - Allocation

Process A

資源 R1

R1 型態的資源共有三個

配置 Allocated

OS 配置一個 R1 型態的資源給 Process A

Page 59: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖 - Release

Process A

資源 R1

Process A 釋放一個 R1 型態的資源

R1 型態的資源共有三個

Page 60: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 1-1 )

Process A Process B

資源 Disk

資源 Printer

(1) 配置 Allocated

Page 61: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 1-2 )

Process A Process B

資源 Disk

資源 Printer

(1) 配置 Allocated

(2) 配置 Allocated

Page 62: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 1-3 )

Process A Process B

資源 Disk

資源 Printer

(1) 配置 Allocated

(3) 要求 Request

(2) 配置 Allocated

Page 63: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 1-4 )

Process A Process B

資源 Disk

資源 Printer

(1) 配置 Allocated

(4) 配置 Allocated

(3) 要求 Request

(2) 配置 Allocated

Page 64: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 1-5 )

Process A Process B

資源 Disk

資源 Printer

(5) Process A 執行完畢釋放資源

(3) 要求 Request

(2) 配置 Allocated

Page 65: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 1-6 )

Process A Process B

資源 Disk

資源 Printer

(2) 配置 Allocated

(6) 配置 Allocated

Page 66: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 1-7 )

Process A Process B

資源 Disk

資源 Printer

(7) Process B 執行完畢釋放資源

存在一個 Processes 執行的順序 : A → B 使 Processes 執行完畢離開

Page 67: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 2-1 )

Process A Process B

(1) 配置 Allocated

資源 Printer

資源 Disk

Page 68: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 2-2 )

Process A Process B

(1) 配置 Allocated

(2) 配置 Allocated

資源 Printer

資源 Disk

Page 69: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 2-3 )

Process A Process B

(1) 配置 Allocated

(2) 配置 Allocated

(3) 要求 Request

資源 Printer

資源 Disk

Page 70: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

資源分配圖( 2-4 )

Process A Process B

(1) 配置 Allocated

(2) 配置 Allocated

(3) 要求 Request

( 4) 要求 Request

資源 Printer

資源 Disk

Page 71: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Figure 3.9A deadlock resulting from competition for

nonshareable railroad intersections

Page 72: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Deadlock

• Deadlock: the condition in which two or more processes are blocked because each is waiting for access to resources allocated to another.

• All of four conditions occur Deadlock – Mutual Exclusion– Hold and Wait– Non-preemptive– Circular Wait

Page 73: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

互斥條件

• Mutual exclusion 、 Mutual exclusive

• 至少有一資源是不可共用的,一次只有一個 Process 能使用此資源。– EX : Printer 、 Write to a file

A B

Allocated

Request

Disk

Allocated

Request

Printer

Printer & Disk are not sharable

Page 74: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

等待條件

• Wait for condition 、 Hold and wait

• 至少必須存在一個 Process ,此 Process 至少擁有( Hold )一項資源,但卻同時等待( Wait )其他已被別的 Process占用之資源。

A B

Allocated

Request

Disk

Allocated

Request

Printer

A 擁有 Disk 卻在等待 Printer

B 擁有 Printer 卻在等待 Disk

Page 75: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

不可搶用條件

• Non-preemptive condition 、 No preemption

• 一項資源一旦被占用,就必須等待Process 使用完被釋放後,才能被分配給其他的 Process 。

A B

Allocated

Request

Disk

Allocated

Request

Printer

Disk & Printer are non-preemptive

Page 76: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

循環等待條件( Circular Wait)

• 必須有一組 Processes 形成集合{ P1 、 P2……Pn-1 、 Pn}

– P1等待的資源被 P2 占用, P2 等待的資源被 P3 占用,……, Pn-1 等待的資源被 Pn

占用, Pn 等待的資源被 P1占用。

A B

Allocated

Request

Disk

Allocated

Request

Printer

A 等待的 Printer 被 B 占用

B 等待的 Disk 被 A 占用

Page 77: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

處理死結的方法

1. OS 使用某一協議,使 System 絕不會進入 Deadlock 狀態。

2. 允許 System 進入 Deadlock 狀態,再想辦法復原。

3. 忽視 Deadlock 的問題,假裝從沒發生過。

Page 78: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Critical Region

• Critical Region or Critical Section

• 臨界段落是 Process 中的一段程式碼,該段程式碼會讀取或修改共享的變數。

• 某一 Process 在進入其臨界段落時,不允許其他 Processes 亦進入他們的臨界段落。

• Question :如果其他 Process 亦進入其臨界段落,會發生什麼問題?

Page 79: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

臨界段落範例( 1 )

Process P1 Process P2

begin begin

w:=5; a:=1;

z:=1; b:=3;

x:=1; y:=2;

y:=w+x; x:=y+3;

end end

P1 的臨界段落 P2 的臨界段落

Page 80: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

臨界段落範例( 2 )P1 P2 P1 P2

w:=5;

z:=1;

x:=1;

a:=1;

b:=3;

y:=2;

y:=w+x; (y=6)

x:=y+3; (x=9)

P1 : x=1 y=6

P2 : x=1 y=9!

P2 P1

a:=1;

b:=3;

y:=2;

x:=y+3; (x=5)

w:=5;

z:=1;

x:=1;

y:=w+x; (y=1)

P1 : x=1 y=6

P2 : x=5 y=2

P1 P2

w:=5;

z:=1;

x:=1;

y:=w+x; (y=1)

a:=1;

b:=3;

y:=2;

x:=y+3; (x=5)

P1 : x=1 y=6

P2 : x=5 y=2

Page 81: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Semaphore

• OS 提供 Semaphore 做為管理 process critical region 的機制。

• OS 與 process 將 critical section 視為系統的資源。

• Semaphore S 為代表資源的數目。

Page 82: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Semaphore Detection

• 利用監測 S ,來判斷是否可以進入 critical region 或使用資源。– S1 表示仍有資源可分配給 process 。對

critical region 的問題,表示尚無 Process 進入 critical region 。

– S=0 表示已無資源可分配給 process 。對 critical region 的問題,表示已有 Process 進入 critical region 。

Page 83: Computer Science — An Overview J. Glenn Brookshear Chapter Three Operating Systems and Networks

Summary: OS’s Works

• An operation system shell :• starts up the computer (bootstrapping)• controls devices: mouse, screen, disks, ...

• deals with manufacturer differences by swapping device drivers

• manages resources: processor, memory, files...• provides a procedure library for programs• protects programs from each other• starts up user interface programs