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

Post on 21-Dec-2015

233 views 1 download

Tags:

Transcript of 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

Online

• Operating System Architecture

• Important Issues in OS

• Coordinating the Machine’s Activities

• Handling Competition Among Processes

• Networks

• Network Protocols

• Security

Operating System Architecture

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

作業系統( Operating System )

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

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

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

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

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

Computer Hardware

OS

Application User

Why OS?

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

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

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

Software Classification

• Applications

• System Software– Utilities– Operation System

• Interface (shell)

• Kernel

Figure 3.3Software classification

software

Application Program

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

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

Utility

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

software 、 Network utility 、 Modem software

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

作業系統核心( 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

Interface

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

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

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

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

Figure 3.4Shell

Kernel Operating system

• 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

Important Issues in OS

Some Important Issues

• Batch System

• Multiprogramming System

• Interactive System

• Real-time System

• Multitasking

• Multiprocessor OS

• Bootstrap

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.

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.

Figure 3.1Batch processing

Queue

• A mass storage

• First-in, first-out (FIFO)

C B A C B AABC

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.

Multiprogramming System

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

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

Job Pool

OS 選擇某 Job 一來執行

Job 3

CPU

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.

分時作業系統 ( 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 的工作流程

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?

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.

Figure 3.2 Interactive processing

Real Time System

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

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

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

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.

•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

Figure 3.5 (A)The booting process

Figure 3.5 (B)The booting process

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.

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

Coordinating the Machine’s Activities

Process

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

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

• Process is an activity.

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.

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 是系統很大的負擔。

Figure 3.6Time-sharing between process A and process B

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

Process State Transition

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

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

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

Diagram 表現。

State Transition Diagram

New

Suspend Ready

CompleteRunningReady

Suspend Blocked

Blocked

(a)

(h)

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

(c) (g)(b)

(i)

(d)

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

CPU

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

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

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

Process 的 State ( 2/2 )

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

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

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

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

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

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

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

Events 的解釋( 2/2 )

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

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

Cooperate Processing

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 )

The Client/Server Model

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

Figure 3.7 The client/server model

Figure 3.8Communication structure

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.

Handling Competition Among Processes

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.

Resource Allocation

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

資源分配圖 - Request

Process A

資源 R1

要求 Request

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

R1 型態的資源共有三個

資源分配圖 - Allocation

Process A

資源 R1

R1 型態的資源共有三個

配置 Allocated

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

資源分配圖 - Release

Process A

資源 R1

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

R1 型態的資源共有三個

資源分配圖( 1-1 )

Process A Process B

資源 Disk

資源 Printer

(1) 配置 Allocated

資源分配圖( 1-2 )

Process A Process B

資源 Disk

資源 Printer

(1) 配置 Allocated

(2) 配置 Allocated

資源分配圖( 1-3 )

Process A Process B

資源 Disk

資源 Printer

(1) 配置 Allocated

(3) 要求 Request

(2) 配置 Allocated

資源分配圖( 1-4 )

Process A Process B

資源 Disk

資源 Printer

(1) 配置 Allocated

(4) 配置 Allocated

(3) 要求 Request

(2) 配置 Allocated

資源分配圖( 1-5 )

Process A Process B

資源 Disk

資源 Printer

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

(3) 要求 Request

(2) 配置 Allocated

資源分配圖( 1-6 )

Process A Process B

資源 Disk

資源 Printer

(2) 配置 Allocated

(6) 配置 Allocated

資源分配圖( 1-7 )

Process A Process B

資源 Disk

資源 Printer

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

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

資源分配圖( 2-1 )

Process A Process B

(1) 配置 Allocated

資源 Printer

資源 Disk

資源分配圖( 2-2 )

Process A Process B

(1) 配置 Allocated

(2) 配置 Allocated

資源 Printer

資源 Disk

資源分配圖( 2-3 )

Process A Process B

(1) 配置 Allocated

(2) 配置 Allocated

(3) 要求 Request

資源 Printer

資源 Disk

資源分配圖( 2-4 )

Process A Process B

(1) 配置 Allocated

(2) 配置 Allocated

(3) 要求 Request

( 4) 要求 Request

資源 Printer

資源 Disk

Figure 3.9A deadlock resulting from competition for

nonshareable railroad intersections

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

互斥條件

• Mutual exclusion 、 Mutual exclusive

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

A B

Allocated

Request

Disk

Allocated

Request

Printer

Printer & Disk are not sharable

等待條件

• 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

不可搶用條件

• Non-preemptive condition 、 No preemption

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

A B

Allocated

Request

Disk

Allocated

Request

Printer

Disk & Printer are non-preemptive

循環等待條件( 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 占用

處理死結的方法

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

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

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

Critical Region

• Critical Region or Critical Section

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

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

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

臨界段落範例( 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 的臨界段落

臨界段落範例( 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

Semaphore

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

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

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

Semaphore Detection

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

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

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

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