Download Lecture notes 03

Document related concepts

Process management (computing) wikipedia , lookup

Transcript
Chapter 3:
Operating Systems
Computer Science: An Overview
Tenth Edition
by
J. Glenn Brookshear
Presentation files modified by Farn Wang
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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.5 Security
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-2
Functions of Operating Systems
• Oversee operation of computer
– bridge between software and central hardware
• Manage system resources
•
•
•
•
•
Store and retrieve files
Schedule programs for execution
Coordinate the execution of programs
Protect system resources
Maintain security
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-3
So, what is an operating system ?
Believe it or not, once upon a time, a judge
tried to define what an operating system is
in the court.
“Well, the verdict is ….”
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-4
Evolution of Operating Systems (1/6)
The struggle of performance enhancement.
• The period of no OS
– operator = programmer
– loading programs from console panel switches,
cards, paper tapes
– monitoring through display lights
– error handling by operators
– output to tapes or cards
later  card readers, line printers, magnetic tapes
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-5
Evolution of Operating Systems (2/6)
• The later period of no OS
– card readers, line printers, magnetic tapes
– assemblers
– loaders, linkers
– libraries
– device drivers
– high-level languages: FORTRAN, COBOL, …
• compilers
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-6
2015/03/31 stopped here
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-7
Evolution of Operating Systems (3/6)
• Before the birth of OS
– high-level languages: FORTRAN, COBOL, …
• compilers
human intervention  low CPU utilization
– operator  programmer
– batch processing to share loading & setup time.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-8
Evolution of Operating Systems (4/6)
• The birth of OS
– Automatic job
sequencing
– resident monitor
– always staying in the
memory
loader
job sequencing
monitor
control card
interpretter
user
program
area
memory allocation
for a resident monitor
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-9
Evolution of Operating Systems (5/6)
• Parallel I/O
– magnetic tapes: sequential access
– offline preparation of program and data to
magnetic tapes
– disks
• random access
• evolving to replace magnetic tapes
– Spooling (Simultaneous Peripherial
Operation On-Line)
• memory  disks  I/O
• preparing data in disk for another process
printing
spool.
Copyright © 2008 •
Pearson
Education, Inc.
Publishing as Pearson Addison-Wesley
3-10
Evolution of Operating Systems (6/6)
• Shared Computing
– Batch processing
– Interactive processing
• Requires real-time processing
– Time-sharing/Multitasking
• Implemented by Multiprogramming
– Multiprocessor machines
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-11
Batch processing (
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
)
3-12
Interactive processing (
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
)
3-13
Memory Layout for
Multiprogrammed
System (吳宇)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-14
Types of Software (
)
• Application software
– Performs specific tasks for users
– The key to acceptance of OS and hardware
• System software
– Provides infrastructure for application software
– Consists of operating system and utility
software
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-15
Software classification (
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
)
3-16
Operating System Components (
)
• Shell: Communicates with users
– Text based
– Graphical user interface (GUI)
• Kernel: Performs basic required functions
– Memory manager
– Scheduler and dispatcher
– File manager
– Device drivers
– ……
How much should be in kernel ?
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-17
The shell as an interface between
users and the operating system (
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
)
3-18
Memory operations with protection
Hardware support for OS implementation.
• Dual-mode operation allows OS to protect
itself and other system components
– User mode and kernel mode
– Mode bit provided by hardware
• Provides ability to distinguish when system is
running user code or kernel code
• Some instructions designated as privileged, only
executable in kernel mode
• System call changes mode to kernel, return from
call resets it to user
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-19
Getting it Started (Bootstrapping)
To pull himself up
by his bootstrap.
Baron Müchhausen
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-20
Getting it Started (Bootstrapping)
• Bootstrap: Program in ROM (example of
firmware)
– Run by the CPU when power is turned on
– initializes and tests that a basic requirement of
hardware, peripherals and external memory
devices are connected.
– Transfers operating system from mass storage
to main memory
– Executes jump to operating system
example: BIOS
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-21
The booting process
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-22
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
• programs, data, buffers, flags, queues, ……
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-23
Process Administration
• Scheduler: Adds new processes to the
process table 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.
• normal termination, I/O calls, time-out, errors, …
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-24
Process Administration
• Scheduling policy
– scheduling strategies, scheduling algorithm
– decides which process gets the CPU time.
• Assumes a queue of process ready for
execution.
– FCFS (first come first serve)
– shortest job first
– round robin
– priority
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-25
Diagram of Process State
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-26
Process Control Block (PCB)
Data structure for information
associated with each process
• Process state
• Program counter
• CPU registers
• CPU scheduling information
• Memory-management information
• Accounting information
• I/O status information
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-27
Process Control Block (PCB)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-28
Context switch from process to process
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-29
Time-sharing between process A and
process B
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-30
Process Scheduling Queues
• Job queue – set of all processes in
the system
• Ready queue – set of all processes
residing in main memory, ready and
waiting to execute
• Device queues – set of processes
waiting for an I/O device
• Processes migrate among the
various queues
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-31
Ready Queue And Various I/O Device Queues
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-32
Representation of Process
Scheduling
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-33
Schedulers
• Long-term scheduler
– (or job scheduler)
– selects which processes should be
brought into the ready queue
• Short-term scheduler
– (or CPU scheduler)
– selects which process should be
executed next and allocates CPU
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-34
Addition of Medium Term
Scheduling
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-35
Schedulers (Cont)
• Short-term scheduler is invoked very
frequently (milliseconds)  (must be fast)
• Long-term scheduler is invoked very
infrequently (seconds, minutes)  (may
be slow)
• The long-term scheduler controls the
degree of multiprogramming
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-36
Schedulers (Cont)
• Processes can be described as either:
– I/O-bound process – spends more time
doing I/O than computations, many short
CPU bursts
– CPU-bound process – spends more time
doing computations; few very long CPU
bursts
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-37
Context Switch
• When CPU switches to another process,
the system must save the state of the old
process and load the saved state for the
new process via a context switch
• Context of a process represented in the
PCB
• Context-switch time is overhead; the
system does no useful work while
switching
• Time dependent on hardware support
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-38
Handling Competition for Resources
Race conditions
Me
Balance
read
3000
write 3000-500
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-39
Handling Competition for Resources
Normal scenario
Me
Balance
Mom
read
3000
write 3000-500
read
2500
write 2500+1000
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-40
Handling Competition for Resources
Shaky scenario
Me
Balance
read
3000
Mom
read
write 3000-500
2500
write 2500+1000
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-41
Handling Competition for Resources
Race condition (I)
Me
Balance
read
3000
Mom
read
3000
write 3000-500
write 3000+1000
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-42
Handling Competition for Resources
Race condition (II)
Me
Balance
read
3000
Mom
read
3000
write 3000-500
write 3000+1000
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-43
Handling Competition for Resources
Critical section problem
• formalization of the problem
while (true) {
remainder
section
– process synchronization and
– resource competition
code to guard the CS
entry section
critical
section
at any moment, at most
one process can be in its
critical section.
code to guard the CS
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
exit section
}
3-44
Handling Competition for Resources
Normal scenario
Me
Balance
Mom
read
3000
Critical
section
write 3000-500
read
2500
Critical
write
2500+1000
section
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-45
Handling Competition for Resources
Race condition (I)
Me
Balance
read
3000
Critical
section
Mom
read
3000
Critical
section
write 3000-500
write 3000+1000
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-46
Handling Competition for Resources
• Desirable solutions
– Mutual exclusion:
• Requirement for proper implementation of a critical
region
– Progressiveness:
• If the resources are available and someone is
waiting, then someone will use the resources.
– Bounded waiting:
• If someone is waiting, she/he will use the resources
in finite time.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-47
Handling Competition for Resources
• Single processor
– centralized control of resources
– sometime solution can be enforced with
hardware
• e.g., disable context switching by interrupt masks
• parallel or distributed computing
– centralized control
– distributed control
– distributed resources
– multi-copies of a resource
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-48
Handling Competition for Resources
Two approaches for solutions
• Pessimistic:
– use various techniques to guarantee no two
process in the CS at the same time in all
scenarios.
– Can be overkill.
• Optimistic:
– let processes do whatever they want.
– backtrack or kill processes when something is
wrong.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-49
Handling Competition for Resources
- Deadlock
• Processes block each other from
continuing
• Conditions required for deadlock
1. Competition for non-sharable resources
2. Resources requested on a partial basis
3. An allocated resource can not be forcibly
retrieved
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-50
Handling Competition for Resources
- Deadlocktransfer $500
from B to A.
Me
Account A
Account B
Mom
transfer $1000
from A to B.
A scenario:
• Me: Please give access to B.
• OS: Granted!
• Mom: Please give access to A.
• OS: Granted!
• Me: Please give access to A.
• OS: Someone is using A.
Please wait.
• Mom: Please give access to B.
• OS: Someone is using B.
Please wait.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-51
Figure 3.7 A deadlock resulting from
competition for nonshareable railroad
intersections
Resources for competition
•
•
•
•
•
•
•
CPU time
memory space
application software
hardware
message channels
files
database records
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-52
Deadlock management
• Prevention: design some off-line resource
allocation policies to prevent deadlocks.
• Avoidance: design some on-line analysis
techniques to avoid deadlocks.
• Detection: design some techniques to
detect the symptom of deadlocks.
• Breaking-up: Kill some processes or let
them roll back.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-53
Deadlock management
• Prevention: design some off-line resource
allocation policies to prevent deadlocks.
– efficient in decision making
– inaccurate
– inefficient in resource utilization.
– may overkill a lot.
• Avoidance
• Detection
• Breaking-up
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-54
Deadlock management
• Prevention
• Avoidance: design some on-line analysis
techniques to avoid deadlocks.
– efficient in decision making
– accurate to some degree.
– maybe inefficient in resource utilization.
– may overkill.
• Detection
• Breaking-up
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-55
Deadlock management
• Prevention
• Avoidance
• Detection: design some techniques to
detect the symptom of deadlocks.
– is costly for precise detection.
– is very expensive in distributed environment.
– resorts to approximate detection.
– how often should we do it ?
• Breaking-up
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-56
Deadlock management
•
•
•
•
Prevention
Avoidance
Detection
Breaking-up: Kill some processes or let
them roll back.
– log construction to restore the system states!
– wasted computation.
– How many to kill ?
– Who to kill ?
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-57
Memory Manager (1/2)
• Allocates space in main memory
– In von-Neumann’s model, data & instruction
must be in memory to be executed.
– Large space size for a process good for the
process performance.
– Large space sizes for processes bad for CPU
utilization
• Protects spaces of users and OS
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-58
Memory Manager (2/2)
• virtual memory
– May create the illusion that the machine has
more memory than it actually does.
– Phyical space  Logical space
• hardware support for address translation
– playing a “shell game” in which blocks of data
(pages) are shifted back and forth between
main memory and mass storage
– Page replacement policies ?
– works with von Neumann’s model in practice
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-59
File Manager
• Directory (or Folder): A user-created
bundle of files and other directories
(subdirectories)
• Directory Path: A sequence of directories
within directories
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-60
Security
• Attacks from outside
– Problems
• Insecure passwords
• Sniffing software
– Counter measures
• Auditing software
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-61
Security (continued)
• Attacks from within
– Problem: Unruly processes
– Counter measures: Control process activities
via privileged modes and privileged
instructions
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-62
Protection and Security
• Protection – any mechanism for
controlling access of processes or users to
resources
• Security – defense of the system against
internal and external attacks
– Huge range, including denial-of-service,
worms, viruses, identity theft, theft of service
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-63
Protection and Security
• Systems generally first distinguish among
users, to determine who can do what
– User identities (user IDs, security IDs) include
name and associated number, one per user
– User ID then associated with all files,
processes of that user to determine access
control
– Group identifier (group ID) allows set of users
to be defined and controls managed, then also
associated with each process, file
– Privilege escalation allows user to change to
effective ID with more rights
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-64