Transcript Cont.

Overview of Operating Systems.
Structure and Functionality
• We examine here fundamental concepts of operating
systems
• These concepts refer to how an operating system is
organized (structure)
• ...and to various capabilities (functionality) of
operating systems
1
Case
• You are using your laptop
• The following programs are open:
• ...Eclipse (an IDE)
• some email system
• PeopleSoft (mysjsu...)
• a text editor (e.g., MSWord)
• Turbo Tax (software to prepare your taxes)
• a web browser
2
(Cont.)
• Most likely your laptop is a single-processor
computer
• Question:
How can it handle multiple programs?
...
3
Multi-Programming
• The OS keeps several
programs (jobs) in main
memory simultaneously
4
(Cont.)
• Multiprogramming is especially important in the case
of a large computer system...
• When the computer system is expensive, and has
many devices attached to it...
...multiprogramming increases the utilization of the
CPU and of those devices
• if possible, the CPU should never be idle
5
The Basic Idea of Multiprogramming
• The CPU starts executing the first program (job)
• Suppose the program needs to perform an I/O
operation (e.g., to read or to write)...
• Question: What should happen at this point?
...
• ...the program will have to wait while the device
controller performs the operation
• Problem: during this wait, the CPU would be idle
• this would be a poor utilization of the CPU
6
(Cont.)
• Solution:
...tell the CPU to execute another program
• If this second program needs to wait, the CPU is
assigned to a third program
• or to the first, if it is no longer waiting
• In this manner, the CPU is well utilized
7
A Historical Note on Multiprogramming
• Historically, multiprogramming appeared at a time
when there was no user interaction with the computer
system
• The programs were loaded into files:
...one or more files would contain the code
...one or more files would store the data used by the
program
• Example: a program that performs numerical
calculations on the given data
8
(Cont.)
• The output of the program was printed on an output
file
• hence, there was no ‘real-time’ user/system
interaction
9
An Extension of Multiprogramming: Time-Sharing
(also called ‘Multitasking’)
• Later, real-time user/system interaction was provided
• Now we have multiple users interacting with the
same, large computer
• and all those users require real-time interaction...
• It is no longer possible to keep any user waiting for
too long
...either they would become irritated
...or would fall asleep
|
10
An Extension of Multiprogramming: Time-Sharing
(also called ‘Multitasking’)
• Later, real-time user/system interaction was provided
• Now we have multiple users interacting with the
same, large computer
• and all those users require real-time interaction...
• It is no longer possible to keep any user waiting for
too long
...either they would become irritated
...or would fall asleep
• The response time to any user action should be fast
• say, at most a second...
11
What Can Be Done?
• We could assign the CPU
to work on one of the jobs
for a short period of time
• Then, the CPU is
assigned to another job,
and so on
• If the CPU is fast, users
would believe the entire
CPU is theirs...
|
12
Another Perspective on Multiprogramming
• In multiprogramming, several programs are loaded in
main memory
• each program is assigned exclusive use of a
portion of memory
• This is called space-multiplexed sharing
• “a resource (e.g., memory) is divided into two or
more distinct units, and the individual parts are
allocated to different programs”
13
(Cont.)
• In multiprogramming the CPU is successively
assigned to execute different programs
• hence the programs share the CPU
• but, this is a different kind of sharing than before...
• Time-multiplexed sharing
• “a resource is not divided into units, but instead,
one program is assigned the entire resource for a
period of time”
14
Concept of an Abstract Machine
• “Multiple program executions ... each appear to have
their own private computer on which to execute.
This is accomplished, first, by designing the OS so
that it carefully manages the computer’s processor,
memory, devices, and all other [...] resources so that
they are shared among the executing programs.
And, second, by presenting an abstraction of the
machine itself - called an abstract machine - to the
programmer.
15
Concept of an Abstract Machine
• “Multiple program executions ... each appear to have
their own private computer on which to execute.
This is accomplished, first, by designing the OS so
hiding tedious
that it carefully manages the computer’s processor,
details
the
memory, devices, and all other [...] resources
soofthat
computer
they are shared among the executing programs.
And, second, by presenting an abstraction of the
machine itself - called an abstract machine - to the
programmer.
16
(Cont.)
“Each abstract machine is a ‘simulation’ of a real
computer: Each program is given its own abstract
machine on which to execute.
The OS will implement these abstractions by sharing
the underlying hardware in a manner that, ideally, is
invisible to the application programmer.”
17
(Cont.)
• Each program runs in its own, separate, abstract
machine
• And a program is not aware of the existence of the
other programs
• ...unless they are ‘communicating’ programs...
(will cover this later)
18
Other Issues in Time-Sharing Systems
• Suppose there are many jobs to be done, and they
don’t fit in main memory
• what could be done in this case?
• Jobs should be kept in a job pool, in disk
• Job scheduling (long-term scheduling)
• to decide which jobs to bring from disk into main
memory
• CPU scheduling (short-term scheduling)
• to decide which of the jobs in main memory, which
are ready to run, should run next
19
(Cont.)
• The jobs that remain on disk, need to be given a
chance to run
• to achieve an acceptable response time
• A simple approach is to swap jobs (swapping):
• we remove a job from main memory and put a
new job from disk
20
(Cont.)
A better approach is based on virtual memory:
...a distinction is established between physical
memory (main memory) and logical memory
...a job could be assigned a small piece of physical
memory but its logical memory could be large...
• we will examine this in detail later
21
Case
(Assume a Time-Sharing Environment)
• A "malicious programmer" decides to play games
with the OS...
• He writes a program with an infinite loop, so that no
other program can do anything...
• But the people that wrote the OS anticipated this
possibility and introduced a ‘timer’...
22
Timer (Implemented in Hardware)
• Used to prevent a process in an infinite loop from
'hogging' resources
...the OS sets a counter
...with every clock tick the counter is decremented
...when the counter reaches zero an interrupt
is generated
• The timer is set up before scheduling the process
to regain control or to terminate a process that
exceeds its allotted time
23
But...
• The malicious programmer resets the counter from
within the infinite loop
• so that it never reaches zero...
• Question:
How to deal with this problem?
...
24
Using Dual-Mode Operation...
• Dual-mode operation allows OS to protect itself and
other system components
• User mode and kernel mode
• Mode bit provided by hardware
25
kernel: ‘the part of
Using Dual-Mode Operation...
the OS that is
implemented to
execute as trusted
• Dual-mode operation allows OS to protect
itself and
software’
other system components
• User mode and kernel mode
• Mode bit provided by hardware
26
The Mode Bit...
• Provides ability to distinguish when system is
running user code or kernel code
• Some instructions designated as privileged are only
executable in kernel mode
• A system call changes mode to kernel mode
• ...returning from a system call resets it to user mode
27
example: any
instruction to
manipulate the
• Provides ability to distinguish when system
timer is
The Mode Bit...
running user code or kernel code
• Some instructions designated as privileged, are only
executable in kernel mode
• A system call changes mode to kernel
• ...returning from a system call resets it to user mode
28
The Mode Bit...
examples:
• Provides ability to distinguish when system is
to perform an
running user code or kernel code
I/O operation;
• Some instructions designated as privileged,
to create a are only
executable in kernel mode
process
• A system call changes mode to kernel
• ...returning from a system call resets it to user mode
29
System Call
• “A style of invoking system functions whereby the
calling process uses a trap instruction to begin
running an OS function...
...When the function is completed, the process
returns to execute the application program.”
• Trap instruction:
“An instruction that causes the control unit to behave
as if an interrupt had occurred...
It is normally used to preempt the current process
and to start the OS at an entry point determined by
the trap table.”
30
System Call
• “A style of invoking system functions whereby the
calling process uses a trap instruction to begin
running an OS function...
...When the function is completed, the process
stop running
returns to execute the application program.”
the current
• Trap instruction:
process...
“An instruction that causes the control unit to behave
as if an interrupt had occurred...
It is normally used to preempt the current process
and to start the OS at an entry point determined by
the trap table.”
31
System Call
• “A style of invoking system functions whereby the
calling process uses a trap instruction to begin
running an OS function...
...When the function is completed, the process
returns to execute the application program.”
• Trap instruction:
“An instruction that causes the control unit to behave
as if an interrupt had occurred...
serves a purpose
It is normally used to preempt the current process
similar to the
and to start the OS at an entry point determined by
interrupt vector
the trap table.”
32
33
because the system
call is executed in
kernel mode, it can
use privileged
instructions
34
Note on System Calls
• The program makes a library function call
• library function: has same name and parameters
as system call
• The library function will use the trap instruction
35
(Cont.)
• The idea of the previous solution is that because
setting the counter is a privileged instruction, it can
be executed only when mode bit is 0 (kernel mode)
• ...and since a user program executes with mode bit 1,
it cannot reset the counter
• But, again...the malicious programmer, from
within his program resets the mode bit to 0!
• Can the programmer do that?
36
Conclusions from the Case
• To support multiprogramming it is necessary to
introduce ‘protection measures’
...a mode bit (hardware)
...user mode and kernel mode (protected)
...privileged instructions (executable only in kernel
mode)
...system calls initiated with a trap instruction
(interrupt)
37
General Comment
• Different computers and different OSs do things
differently
• But if you look at them at a certain level of
abstraction they are rather similar
• Consider the ‘trap’ instruction
• To fully understand how it works would require
examining low level details
• which are rather uninteresting
38
(Cont.)
• You need to learn to live at a certain level of
abstraction
• in other words, you need to be able to think
abstractly...
• don’t get nervous if there is a little ambiguity!
39
Storage Structure
S
• Main memory: the only large storage media that
the CPU can access directly
• Secondary storage: extension of main memory
that provides large nonvolatile storage capacity
• Magnetic disks (secondary storage): rigid metal
or glass platters covered with magnetic recording
material
• disk surface is logically divided into tracks, which are
subdivided into sectors
• the disk controller determines the logical interaction
between the device and the computer
40
Storage Hierarchy
S
• The storage systems are organized in a hierarchy
• speed
• cost
• volatility
• Caching: copying information into faster storage
system; main memory can be viewed as a fast
cache for secondary storage.
41
S
StorageDevice
Hierarchy
42
Caching
S
• Important principle, performed at many levels in a
computer (in hardware, OS, software)
• Information in use is copied from slower to faster
storage temporarily
• Faster storage (cache) is checked first to
determine if information is there
• If it is, information is used directly from the
cache (fast)
• If not, data is copied to cache and then used
there
43
(Cont.)
S
• Cache smaller than storage being cached
• Cache management is an important design
problem
• Cache size and replacement policy
44
Performance of Various Kinds of Storage
45
(Cont.)
S
• Multitasking environments must be careful to use
most recent value, no matter where it is stored in
the storage hierarchy
46
Process Management
• A process is a program in execution. It is a unit
of work within the system. A program is a passive
entity, a process is an active entity.
• A process needs resources to accomplish its task
...which resources?
• ...the CPU, memory, I/O devices, files
• Process termination requires reclaim of any
reusable resources
47
(Cont.)
• A single-threaded process has one program
counter specifying location of next instruction to
execute
• process executes instructions sequentially, one
at a time, until completion
• A multi-threaded process has one program
counter per thread
48
(Cont.)
• Typically a system has many processes
• some are user processes, some are operating
system processes
• running concurrently on one or more CPUs
• Concurrency is achieved by multiplexing the CPU
among the processes / threads
49
Summary.
Important Concepts
•
•
•
•
•
•
•
•
multiprogramming
...CPU utilization
time sharing (multitasking)
...response time
resource sharing
...sharing the CPU (time-multiplexed sharing)
...sharing memory (space-multiplexed sharing)
abstract machine
50
(Cont.)
• job scheduling (long-term scheduling)
• CPU scheduling (short-term scheduling)
• timer (hardware) for measuring running
time
• dual-mode operation
• user mode and kernel mode
• mode bit
• privileged instructions
• system call
51
(Cont.)
• storage device hierarchy
• caching
• process management
52