You are on page 1of 44

Chapter 3: Operating Systems

• 3.1 The History of Operating Systems


• 3.2 Operating System Architecture
• 3.3 Coordinating the Machine’s Activities
• 3.4 Handling Competition Among Processes

3-1
補充

How does a computer


start executing?
Simple Answer 補充

• The program counter is initiated with a


particular address in a special memory
when the computer is powered on
– That address is start of a (special) program
 to bring up other programs and the system
– But DRAM is volatile!
– So, we use read-only memory (ROM)

3-3
補充

Suppose the computer runs


only one program …
Full control of everything,
e.g. CPU Manages everything
補充

Suppose the computer runs


many programs …
• How do they get executed?
• How do they get the most
important resource – the CPU?
Grabbing the CPU 補充

• In a single-processor computer,
only one CPU to be shared by all programs

• Who can get the microphone?


3-6
We Need a Chairperson! 補充

• The chairperson decides


who gets the microphone
to speak next
• Two ways to schedule:
– Let each speaker talk until he/she finishes
– “Interrupts” the speaker to get back the
microphone and turn to another speaker
 time sharing

3-7
Chairperson Can Do More 補充

• Which portion of blackboard


a speaker can write?
 memory management

Chairperson is OS

• Who can use 幻燈機 (projector)?


– Why not operated by chairperson?
– Device driver and management

3-8
What Is a System? 補充

• A set of interacting, interdependent entities


forming an integrated whole.
– From Wikipedia

• Five components
– Hardware User
– Software Procedure
Software
– Data System
– Procedure
Data Hardware
– User

3-9
Operating Systems

• Interface between a user and the computer hardware

• Provide an environment
in which a user can execute programs

• Goals
– Make the computer system convenient to use
– Use the computer hardware (resources)
in an efficient manner

3-10
Operating Systems
• One kind of software that
controls the overall operation of a computer
– Unix
– Linux: Ubuntu, Redhat, ...
– Sun Solaris (Sun/Oracle machines)
– Apple Mac OS
– MS DOS, Microsoft Windows

3-11
Smartphone Operating Systems
• Apple iOS

• Windows Phone

• BlackBerry OS

• Nokia Symbian OS

• Google Android

3-12
Functions of Operating Systems

• Oversee operation of computer

• Store and retrieve files

• Provide the user interface to request


execution of programs

• Coordinate the execution of programs


3-13
Components of OS

• For user: shell, privilege control (security)


• For data: file manager
• For hardware: device manager,
memory manager, and boot manager
• For software:
– Where to store: file manger, registry
– How to execute: scheduler, process manager

3-14
3.1 THE HISTORY OF
OPERATING SYSTEMS

3-15
History of Operating Systems

• Each program is called a “job”

• Early computers required significant setup time


• Each “job” required its own setup

• Operating Systems began as


systems for
simplifying setup and transitions between jobs

3-16
Evolution of Shared Computing (1)
• Batch processing (批次處理) (job queue)
– 它是先把工作及資料收集成一批,然後執行它們。
– 存在大量儲存設備中的工作,在一個工作佇列(job queue)中等待被執行。

• Interactive processing (互動式處理; 交談式處理) (real time)


– 允許在程式執行時,經由遠方的終端機與使用者對話
– Requires real-time processing
• Processing whose response time is restricted

• Time-sharing (one machine, many users) / Multitasking (one user, many tasks)
– Implemented by Multiprogramming (多重程式)
• Time is divided into intervals and then the execution of each job is restricted to only
one interval at a time.
• Shuffle jobs by dividing time into intervals
– Efficient than the sequential way
especially for jobs which must wait for peripheral devices
– Multitasking (多工) for a one-user system vs Time-sharing for multiple users
• Multiprocessor machines (load balancing)
– Sharing information and resources among different machines
– Load balancing, scaling

3-17
Evolution of Shared Computing (2)

• Networking
– 透多網路上多台機器來處理多個⼯作

• Embedded system (specific devices)


– 專⾨為了某個功能的所設計的系統
• 例如:醫療裝置、⾞載電⼦、⼿機、⼿持裝置
– 省電、即時、持續運作

• …

3-18
Figure 3.1 Batch processing

FIFO: first in first serve


3-19
Figure 3.2 Interactive processing

Text editing, music/movie playing, …


3-20
3.2 OPERATING SYSTEM
ARCHITECTURE

3-21
Types of Software
• Application software
– Performs specific tasks for users
(productivity, games, software development)

• System software
– Provides infrastructure for application software
– Consists of operating system
and utility software (公⽤軟體)
– Basis is not absolute,
e.g., Networking function was not utility before, but is now .
e.g., IE (Internet explorer), media player  lawsuits.

3-22
Figure 3.3 Software classification

0-23
Figure 3.4 The user interface act as an
intermediary between users and the
operating system kernel

3-24
Operating System Components (1)
• User Interface: Portion of an OS to define the interface
between the OS and its users
– Communicates with users
– Types of user interface
• Text based (Shell;外殼)
– e.g., Borne shell, C shell, Korn shell in Unix; MS-DOS for Microsoft
Windows
• Graphical user interface (GUI)
– e.g., Windows 10, Mac
– Window manager
– Often called WIMP (Windows, Icons, Menus, and Pointers)
– In the sell with GUI, one crucial component is the window manager,
whose job is to manage the placement of a window on the screen,
and to record the applications that are executed for each opened
window

3-25
Operating System Components (2)

• Kernel (核⼼)
– Performs basic required functions
– Main components
• File manager (檔案管理者)
• Device drivers (裝置驅動程式)
• Memory manager (記憶體管理者)
• Scheduler (排程程式) and dispatcher (時段分配程式)

3-26
File Manager
• File manager: coordinate the use of the
machine’s mass storage facilities
– Directory (or Folder): A user-created bundle of files
and other directories (subdirectories)
– Directory Path: A sequence of directories within
directories
– File descriptor: Information needed to find and
manipulate the file, e.g., file name, extension, size,
updated date, permissions, attributes, …

3-27
Device driver
• Device drivers
– Communicate with the controllers (or at times,
directly with peripheral devices) to carry out
operations on the peripheral devices attached
to the machine

3-28
Memory Manager

• Memory Manager
– Allocates space in main memory
– What if the program is larger than the memory?
virtual memory (虛擬記憶體)
• create the illusion that
the machine has more memory than it actually does
by playing a “shell game”
in which blocks of data (pages) are shifted back and forth
between main memory and mass storage
• Paging: memory is grouped into pages
to facilitate the mapping and shuffling

3-29
Example of Paging

• There are 8 pages; each is of 4KB


– Main memory is of size 16KB (4 pages)
– Programs use virtual Page 0
address to access Page 1
Page 2
data and code
Page 3
– OS does the mapping Page 4
and paging Page 5 Main memory
Page 6
Page 7
Disk
Virtual address

3-30
Getting it Started (Bootstrapping; 啟動)

• Boot loader: Program in ROM


(which an example of firmware韌體)
– Run by the CPU when power is turned on
– Transfers operating system
from mass storage to main memory
– Executes jump to operating system

• Most ROM in today’s PCs is constructed with


flash memory technology

3-31
Figure 3.5 The booting process

3-32
BIOS and Firmware

• The boot loader program


and other basic input/output functions
are contained in a special ROM, called BIOS
(basic input/output system)

• A program stored in ROM is called firmware (韌體)


– Hardware or software?

3-33
3.3 COORDINATING THE
MACHINE’S ACTIVITIES

3-34
Coordinating the Machine’s Activities

• An operating system
coordinates the execution of
application software,
utility software, and
units within the operating system itself.

3-35
Processes

• Process (程序):
The activity of executing a program

• Process State (程序狀態):


Current status of the activity
– Program counter
– General purpose registers
– Related portion of main memory

3-36
Process Administration

• Scheduler (排程程式):
– Adds new processes to the process table
• Ready state: The progress of the process can continue
• Waiting state: Its progress is currently delayed until some external
event occurs
– and removes completed processes from the process table

• Dispatcher (時段分配程式):
Controls the allocation of time slices to the processes
in the process table
– The end of a time slice is signaled by an interrupt.

3-37
Figure 3.6 Time-sharing between
process A and process B

• Starvation: Processes waiting for but never given a time slice


3-38
Exercise

• List the components of a typical OS and summarize their


functions.

• Ans.
– User interface: Communications with the machine’s environment
– File manager: Coordinates the use of the machine’s mass
storage
– Device drivers: Handle communication with the machine’s
peripheral devices
– Memory manager: Coordinates the use of the machine’s main
memory
– Scheduler: Coordinates the processes in the system.
– Dispatcher: Controls the assignment of processes to CPU time

3-39
3.4 HANDLING COMPETITION
AMONG PROCESSES

3-40
Handling Competition for Resources
• Flag (旗標) = set (1) or clear (0)
– 為控制⼀機器或其周邊(e.g., 印表機)的使⽤分配,
OS須⽤⼀個旗標(flag)隨時記錄此機器是否已被分配使⽤
– 先⽤test指令查flag狀態,若為clear,則⽤ set指令設定flag
 不同程序可能會競爭兩個指令
– Test-and-set instruction: 結合test和set為單⼀指令
– Semaphore (信號): A properly implemented flag
• The railroad signals used to control access to sections of track.

• Critical Region (臨界區域): A group of instructions


that should be executed by only one process at a time
• 如同同時只能供⼀節⽕⾞⾏駛的鐵路。
– Mutual exclusion: Requirement for proper
implementation of a critical region 3-41
Deadlock (死結)
• Deadlock: Processes block each other from continuing

• Conditions required for deadlock


1. Competition for non-sharable resources
2. Resources are requested on a partial basis
3. An allocated resource can not be forcibly retrieved

• Removing deadlock
– Techniques that attack the 3rd condition fall into the category known
as deadlock detection and correction schemes
– Techniques that attack the first two conditions are known as deadlock
avoidance schemes
• Converting non-shareable resources into shareable ones
• Requiring each process to request all the resources at one time

3-42
Figure 3.7 A deadlock resulting from
competition for nonshareable railroad
intersections

3-43
Exercise

• Suppose the following solutions have been proposed for


removing the deadlock that occurs on a single-lane bridge
when two cars meet. Identify which condition for deadlock
is removed by each solution
– (a) Do not let a car onto the bridge until the bridge is empty
– (b) If cars met, make one of them back up
– (c) Add a second lane to the bridge

• Ans.
– (a) This guarantees that the non-shareable resource is not
required and allocated on a partial basis (condition 2)
– (b) This means that the non-shareable resource can be forcibly
retrieved (condition 3)
– (c) This makes the non-shareable resource shareable, which
removes the competition (condition 1)

3-44

You might also like