6.OS PPT Swapna
6.OS PPT Swapna
BY
SWAPNA.C
ASST.PROF
IT DEPARTMENT
CHAPTER 1: INTRODUCTION
The operating system controls the hardware and coordinates its use among the
various application programs for the various users.
We can also view a computer system as consisting of hardware, software, and
data.
The operating system provides the means for proper use of these resources in the
operation of the computer system.
An operating system is similar to a government. Like a government, it performs no
useful function by itself. It simply provides an environment within which other
programs can do useful work.
To understand more fully the operating system's role, we explore operating
systems from two viewpoints:
The user
The system.
USER VIEW
Users
Applications
Database System
Operating System
Hardware
COMPUTER-SYSTEM ORGANIZATION
Once the kernel is loaded and executing, it can start providing services to the
system and its users.
Some services are provided outside of the kernel, by system programs that are
loaded into memory at boot time to become system processes, or system
daemons that run the entire time the kernel is running.
On UNIX, the first system process is init and it starts many other daemons.
Once this phase is complete, the system is fully booted, and the system
waits for some event to occur.
The occurrence of an event is usually signaled by an interrupt.
INTERRUPTS
When an interrupt occurs, the operating system preserves the state of the
CPU by storing the registers and the program counter
Determines which type of interrupt has occurred and transfers control to
the interrupt-service routine.
An interrupt-service routine is a collection of routines (modules), each of
which is responsible for handling one particular interrupt (e.g., from a
printer, from a disk)
The transfer is generally through the interrupt vector, which contains the
addresses of all the service routines
Interrupt architecture must save the address of the interrupted instruction.
INTERRUPT TIMELINE
Interrupt-driven I/O cycle.
Intel Pentium processor event-vector table
STORAGE STRUCTURE
Main memory – the only large storage media that the CPU can access directly
Random access
Typically volatile
Secondary storage – extension of main memory that provides large nonvolatile
storage capacity
Hard disks – 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
Solid-state disks – faster than hard disks, nonvolatile
Various technologies
Becoming more popular
Tertiary storage
STORAGE DEFINITION
The basic unit of computer storage is the bit. A bit can contain one of two
values, 0 and 1. All other storage in a computer is based on
collections of bits.
A byte is 8 bits, and on most computers it is the smallest convenient chunk
of storage.
A less common term is word, which is a given computer architecture’s
native unit of data. A word is made up of one or more bytes.
STORAGE DEFINITION (CONT.)
To start an I/O operation, the device driver loads the appropriate registers
within the device controller.
The device controller, in turn, examines the contents of these registers to
determine what action to take (such as “read” a character from the
keyboard).
The controller starts the transfer of data from the device to its local buffer.
Once the transfer of data is complete, the device controller informs the
device driver via an interrupt that it has finished its operation.
The device driver then returns control to the operating system, possibly
returning the data or a pointer to the data if the operation was a read.
For other operations, the device driver returns status information.
DIRECT MEMORY ACCESS STRUCTURE
Interrupt-driven I/O is fine for moving small amounts of data but can produce
high overhead when used for bulk data movement such as disk I/O.
To solve this problem, direct memory access (DMA) is used.
After setting up buffers, pointers, and counters for the I/O device, the device
controller transfers an entire block of data directly to or from its own buffer
storage to memory, with no intervention by the CPU.
Only one interrupt is generated per block, to tell the device driver that the
operation has completed. While the device controller s performing these
operations, the CPU is available to accomplish other work.
Some high-end systems use switch rather than bus architecture. On these
systems, multiple components can talk to other components concurrently,
rather than competing for cycles on a shared bus. In this case, DMA is even
more effective. The figure in next slide shows the interplay of all
components of a computer system.
HOW A MODERN COMPUTER WORKS
Single user cannot keep CPU and I/O devices busy at all times
Multiprogramming organizes jobs (code and data) so CPU always has one
to execute
A subset of total jobs in system is kept in memory
Batch systems:
One job selected and run via job scheduling
When it has to wait (for I/O for example), OS switches to another job
Timesharing systems:
Logical extension of batch systems -- CPU switches jobs so frequently
that users can interact with each job while it is running, creating
interactive computing
TIMESHARING SYSTEMS
Usually disks used to store data that does not fit in main memory or data that
must be kept for a “long” period of time
Proper management is of central importance
Entire speed of computer operation hinges on disk subsystem and its
algorithms
OS activities
Free-space management
Storage allocation
Disk scheduling
Some storage need not be fast
Tertiary storage includes optical storage, magnetic tape
Still must be managed – by OS or applications
CACHING
A VMM allow the user to install multiple operating systems to run application
written for operating systems other than the native host.
Apple laptop running Mac OS X host Windows as a guest
Developing apps for multiple OSes without having multiple systems
Testing applications without having multiple systems
Executing and managing compute environments within data centers
VIRTUALIZATION ARCHITECTURE STRUCTURE
COMPUTING ENVIRONMENTS - TRADITIONAL
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Thread Scheduling
Multiple-Processor Scheduling
Real-Time CPU Scheduling
Operating Systems Examples
Algorithm Evaluation
OBJECTIVES
Whenever the CPU becomes idle, the operating system must select one of
the processes in the ready queue to be executed.
The selection process is carried out by the CPU scheduler.
The ready queue may be ordered in various ways.
CPU scheduling decisions may take place when a process:
1. Switches from running state to waiting state
2. Switches from running state to ready state
3. Switches from waiting state to ready state
4. When a process terminates
For situations 1 and 4, there is no choice in terms of scheduling. A new
process (if one exists in the ready queue) must be selected for execution.
There is a choice, however, for situations 2 and 3.
NONPREEMPTIVE SCHEDULING
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest time
SJF is optimal – gives minimum average waiting time for a given set of
processes
How do we know what is the length of the next CPU request
Could ask the user
what if the user lies?
EXAMPLE OF SJF
P4 P1 P3 P2
0 3 9 16 24
Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
DETERMINING LENGTH OF NEXT CPU BURST
Can only estimate (predict) the length – in most cases should be similar to
the previous CPU burst
Pick the process with shortest predicted next CPU burst
Can be done by using the length of previous CPU bursts, using exponential
averaging
P1 P2 P4 P1 P3
Average waiting
0
time
1
= [(10-1)+(1-1)+(17-2)+5-3)]/4
5 10
=1726/4 = 6.5 msec 26
ROUND ROBIN (RR)
Each process gets a small unit of CPU time (time quantum q). After this time
has elapsed, the process is preempted and added to the end of the ready
queue.
If there are N processes in the ready queue and the time quantum is q, then
each process gets 1/N of the CPU time in chunks of at most q time units at
once. No process waits more than (N-1)q time units.
Timer interrupts every quantum to schedule next process
Performance
q large FIFO
q small q must be large with respect to context switch, otherwise
overhead is too high
PRIORITY SCHEDULING
The CPU is allocated to the process with the highest priority (smallest integer
highest priority)
Preemptive
Nonpreemptive
SJF is priority scheduling where priority is the inverse of predicted next CPU burst
time
P1 P2 P1 P3 P4
0 1 6 16 18 19
Average waiting time = 8.2 msec
COMBINING PRIORITY SCHEDULING AND RR
A process can move between the various queues; aging can be implemented this
way
Multilevel-feedback-queue scheduler defined by the following parameters:
number of queues
scheduling algorithms for each queue
method used to determine when to upgrade a process
method used to determine when to demote a process
method used to determine which queue a process will enter when that process
needs service
EXAMPLE OF MULTILEVEL FEEDBACK QUEUE
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is served FCFS
When it gains CPU, job receives 8 milliseconds
If it does not finish in 8 milliseconds, job is
moved to queue Q1
At Q1 job is again served FCFS and receives 16
additional milliseconds
If it still does not complete, it is preempted
and moved to queue Q2
MAIN MEMORY
MEMORY MANAGEMENT
Background
Swapping
Contiguous Memory Allocation
Segmentation
Paging
Structure of the Page Table
Example: The Intel 32 and 64-bit Architectures
Example: ARM Architecture
OBJECTIVES
A program must be brought (from disk) into memory and placed within a
process for it to be run
A program can be written in machine language, assembly language, or high-
level language.
Main memory and registers are the only storage entities that a CPU can
access directly
The CPU fetches instructions from main memory according to the value of the
program counter.
Typical instruction execution cycle – fetch instruction from memory, decode the
instruction, operand fetch, possible storage of result in memory.
MEMORY PROTECTION
Hardware device that at run time maps virtual addresses to physical address
To start, consider simple scheme where the value in the base register is added
to every address generated by a user process at the time it is sent to
memory
Base register now called relocation register
MS-DOS on Intel 80x86 used 4 relocation registers
DYNAMIC LOADING
Dynamically linked libraries – system libraries that are linked to user programs
when the programs are run.
Similar to dynamic loading. But, linking rather than loading is postponed
until execution time
Small piece of code, stub, used to locate the appropriate memory-resident
library routine
Stub replaces itself with the address of the routine, and executes the routine
Operating system checks if routine is in processes’ memory address
If not in address space, add to address space
Dynamic linking is particularly useful for libraries
System also known as shared libraries
CONTIGUOUS ALLOCATION
Relocation registers used to protect user processes from each other, and from
changing operating-system code and data
Base register contains value of smallest physical address
Limit register contains range of logical addresses – each logical address
must be less than the limit register
MMU maps logical address dynamically
Can then allow actions such as kernel code being transient – comes and
goes as needed. Thus, kernel can change size dynamically.
HARDWARE SUPPORT FOR RELOCATION AND LIMIT
REGISTERS
MULTIPLE-PARTITION ALLOCATION
Variable-partition -- sized to a given process’ needs.
Hole – block of available memory; holes of various size are scattered
throughout memory
When a process arrives, it is allocated memory from a hole large enough
to accommodate it
Process exiting frees its partition, adjacent free partitions combined
Operating system maintains information about:
a) allocated partitions b) free partitions (holes)
DYNAMIC STORAGE-ALLOCATION PROBLEM
Shuffle memory contents to place all free memory together in one large
block
Compaction is possible only if relocation is dynamic, and is done at
execution time
I/O problem -- cannot perform compaction while I/O is in progress
involving memory that is being compacted.
Latch job in memory while it is involved in I/O
Do I/O only into OS buffers
NON-CONTIGUOUS ALLOCATION
Partition the a program into a number of small units, each of which can
reside in a different part of the memory.
Need hardware support.
Various methods to do the partitions:
Segmentation.
Paging
paged segmentation.
SEGMENTATION
If page table is kept in main memory every data/instruction access requires two
memory accesses
One for the page table and one for the data / instruction
The two memory access problem can be solved by the use of a special fast-lookup
hardware cache called associative memory or translation look-aside buffers (TLBs)
Associative memory – parallel search
Page # Frame #
Shared code
One copy of read-only (reentrant) code shared among processes (i.e., text
editors, compilers, window systems)
Similar to multiple threads sharing the same process space
Also useful for inte-rprocess communication if sharing of read-write pages is
allowed
Private code and data
Each process keeps a separate copy of the code and data
The pages for the private code and data can appear anywhere in the logical
address space
SHARED PAGES EXAMPLE
STRUCTURE OF THE PAGE TABLE
Memory structures for paging can get huge using straight-forward methods
Consider a 32-bit logical address space
Page size of 1 KB (210)
Page table would have 4 million entries (232 / 210)
If each entry is 4 bytes -> Page table is of size 16 MB
That amount of memory used to cost a lot.
Do not want to allocate that contiguously in main memory
What about a 64-bit logical address space?
PAGE TABLE FOR LARGE ADDRESS SPACE
Hierarchical Paging
Hashed Page Tables
Inverted Page Tables
HIERARCHICAL PAGE TABLES
A logical address (on 32-bit machine with 1K page size) is divided into:
a page number consisting of 22 bits
a page offset consisting of 10 bits
Since the page table is paged, the page number is further divided into:
a 12-bit page number
a 10-bit page offset
where p1 is an index into the outer page table, and p2 is the displacement within the
page of the inner page table
Known as forward-mapped page table
ADDRESS-TRANSLATION SCHEME
64-BIT LOGICAL ADDRESS SPACE
One solution is to divide the outer page table. Various ways of doing so.
Example – three-level page table
Even with 2nd outer page table, the outer-outer table is still 234 bytes
in size.
And possibly 4 memory access to get to one physical memory
location.
The next step would be four-level. But ….
64-BIT LOGICAL ADDRESS SPACE (CONT.)
Similar to hashed but each entry refers to several pages (such as 16) rather
than 1
Especially useful for sparse address spaces (where memory references are
non-contiguous and scattered)
INVERTED PAGE TABLE
Rather than each process having a page table and keeping track of all
possible logical pages, track all the physical pages
Use inverted page-table, which has one entry for each real page of
memory
An entry the inverted-page table consists of the virtual address of the
page stored in that real memory location, with information about the
process that owns that page.
What is maximum size of the inverted page-table?
INVERTED PAGE TABLE ARCHITECTURE
INVERTED PAGE TABLE (CONT.)
TLB holds translation table entries (TTEs) for fast hardware lookups
A cache of TTEs reside in a translation storage buffer (TSB)
Includes an entry per recently accessed page
Virtual address reference causes TLB search
If miss, hardware walks the in-memory TSB looking for the TTE corresponding to the
address
If match found, the CPU copies the TSB entry into the TLB and translation
completes
If no match found, kernel interrupted to search the hash table
The kernel then creates a TTE from the appropriate hash table and stores it in
the TSB, Interrupt handler returns control to the MMU, which completes the
address translation.
EXAMPLE: THE INTEL 32 AND 64-BIT
ARCHITECTURES
Major part of swap time is transfer time; total transfer time is directly
proportional to the amount of memory swapped
SCHEMATIC VIEW OF SWAPPING
SWAPPING (CONT.)
Does the swapped out process need to swap back in to same physical
addresses?
Depends on address binding method
Plus must consider pending I/O to / from process memory space
Modified versions of swapping are found on many systems (i.e., UNIX, Linux,
and Windows). A common variation:
Swapping is normally disabled
Swapping is started if the amount of free memory (unused memory
available for the operating system or processes to use) falls below a given
threshold.
Swapping is disabled again once memory demand reduced below the
threshold
Another variation. Swapping portions of processes--rather than entire
processes--to decrease swap time.
Typically, these modified forms of swapping work in conjunction with virtual
memory (covered soon).
CONTEXT SWITCH TIME INCLUDING SWAPPING
If next processes to be located a CPU (say A) is not in memory and there is not
enough physical memory to accommodate A, then we need to swap out
one of the processes in memory (say B) and swap in process A.
Context switch time can then be very high
100MB process swapping to hard disk with transfer rate of 50MB/sec
Swap out time of 2000 ms
Plus swap in of same sized process
Total context switch swapping component time of 4000ms (4 seconds)
Can reduce context switch time by knowing how much memory is really being
used. System calls to inform OS of memory use:
request_memory() and release_memory()
CONTEXT SWITCH TIME AND SWAPPING (CONT.)
Rather than each process having a page table and keeping track of all
possible logical pages, track all the physical pages – using an inverted
page-table.
The inverted page-table has one entry for each real page of memory.
An entry the inverted-page table consists of the virtual address of the
page stored in that real memory location, with information about the
process that owns that page.
What is maximum size of the inverted page-table?
COMPUTING ENVIRONMENTS – DISTRIBUTED
Operating systems made available in source-code format rather than just binary
closed-source
Counter to the copy protection and Digital Rights Management (DRM) movement
Started by Free Software Foundation (FSF), which has “copyleft” GNU Public
License (GPL)
Examples include GNU/Linux and BSD UNIX (including core of Mac OS X), and
many more
Can use VMM like VMware Player (Free on Windows), Virtualbox (open source and
free on many platforms - http://www.virtualbox.com)
Use to run guest operating systems for exploration