Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Operating System Support Upd

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

1

Chapter-6
Operating System Support
1.1 Introduction
 An operating system is collection of system programs that control our computer and other peripherals
connected to it.
 An operating system is a program that manages the computer hardware, provides a basis for an application
programs and act as an intermediary between a user of a computer and the computer hardware.
 Basically, there are two views of OS
i. Top down view: OS is a program that acts as an intermediary between a user of a computer and the
computer hardware, and makes the computer system convenient to use.
ii. Bottom up view: Operating system is a resource manager who manages the hardware and software
resources in the computer system
 Examples of OS are MS-DOS, Linux, XP, windows 7, windows 8, vista etc.
 The main services (Functions) provided by operating system are
a. Process Management: -
 A program in execution is called process.
 An operating system takes care of creation and deletion of processes, scheduling system resources to different
processes requesting them and providing mechanism for communicating between the processes.

b. Memory Management: -
 Memory management functions of an operating system take care of allocation and de-allocation of memory
space to the program in need of these resources.
 It is responsible for managing both primary and secondary storage devices attached to computer system.
 Operating system uses various methods such as swapping, paging, compaction for memory management.

c. File Management: -
 File management function of an operating system takes care of file related activities such as organization,
storage, retrieval, naming sharing and protection of files.
 Operating system helps to create, modify, delete files and directories.
 It also manages the attributes such as hidden, shared etc. associated with files and directories.

d. Input/output Management: -
 An operating system keeps track of input/output devices i.e. which device is in use and by which process that
device is being used.
 It decides which process should use which device and how long.
 It allocates the device to the process and de-allocates the device after completion of job.

e. Security Management: -
 An operating system protects the resources and information of a computer against destruction and unauthorized
access.
 The operating system authenticates and validates the user login, verifies the user access to the local and shared
resources according to the security schema.

f. Error Detection/Recovery: -
 Operating system can detect the error in various level and task such as power failure, bad sector in hard disk
drive (HDD), un-readable files, manufacturing devices etc.
 On detection, it generates present log of the problems.

Compiled By : Bhesh Thapa


2

g. Network Management: -
 A network based operating system can find, establish and disconnect the network connection between two or
more than two computers, transfer data over the network, manages the resources and provide proper
communication between several computers in the network.

Two main function of OS

i. Operating system as extended machine/virtual machine/user interface


 The architecture of most computers at machine language level is primitive and awkward to program.
 Operating system programs that act as interface between user and the computer hardware.
 So operating system sits between the user and the hardware of computer system providing an operational environment
to the user and application program.
 User can not directly interact with the hardware so OS does the necessary translation to get instruction executed by the
CPU.

 Users can interact with the computers if and only if he/she understand the language of resident operating system.
 The program that hides the truth about the hardware from the programmer and present a nice, simple view of named
files that can be read and writes is an operating system.
 The abstraction provided by OS is simpler and easier to use than the underlying hardware.
 It is because of the operating system that users of a computer system don’t have to deal with computer’s hardware to get
their work done.
 Users can use simple commands to perform various tasks and let the operating system do the difficult work of
interacting with computer hardware. Thus, you can use a command like copy file1 file2 to copy ‘file1’ to ‘file2’ and let
the operating system communicate with the controller(s) of the disk that contain(s) the two files.
 Example: (Floppy disk I/O operation)
o Disks contain a collection of named files
o each file must be open for READ/WRITE
o after READ/WRITE complete close that file
o no any detail to deal
o OS hides the programmer from the disk hardware and presents a simple file oriented interface.
 So, the main function of operating system presents with the equivalent of an extended machine or virtual machine that is
easier too program then the underlying hardware.

Compiled By : Bhesh Thapa


3

ii. Operating system as Resource Manager


 The computer system has many resources including hardware (CPU, Memory, I/O Devices etc) and software
(application program) that are required to solve the problem.

Fig: Resources managed by OS

 Different program and user may try to access common resources simultaneously.
 The uncontrolled allocation may create deadlock among programs.
 So the operating system provides orderly controlled allocation of these resources thus preventing deadlock and the
failures.
 Operating system is a control program so it keeps track of who is using which resources, to grant resources requests,
to account for usage and to mediate conflicting request from different programs and users.
 Resource management includes multiplexing resources in 2 ways, they are
i. In Time
ii. In Space
 When Resource is Time Multiplexed
 “Who goes next and for how long”- is the main task for time resource manager in OS.
 Different programs or user take turn/s using it.
 Multiple programs will run by allocating the CPU through OS.
 When another program gets to use the CPU, the OS will choose.
 Sharing the printer is the ideal example for time multiplexing
 When Resource is Space Multiplexed
 Main memory is normally divided up among several running programs
 OS assume enough memory to hold multiple programs
 OS hold several programs in memory at once instead of completing one by one.
 The issues of fairness, protection and so on will solve by OS.
 Hard disk is another resource manager for space multiplexed
 So operating system acts as resource manager.

Compiled By : Bhesh Thapa


4

1.4 Evolution of OS / Types of OS

i. Batch System
 In earlier system, the common input devices were card readers and tape drivers.
 The output devices were printer, tape and card punches.
 The user didn’t interact directly with the computer system.
 The programmer responsibility was to prepare the program, data and punch it onto the card and then is submitted to the
computer operator.
 At some time later, when the computer finishes whatever job it was currently running, an operator finally prints the
output and handover to the programmer.
 So in this system much time was wasted.
 So a method was adopted to reduce the waste of time known as batch system.
 In this method, the jobs with similar need and nature are batched together and are executed.

ii. Multi-Programmed System


 After the batch system, OS designer developed the concept of multiprogramming in which several jobs are in the main
memory.
 Multiprogramming concept increases the CPU utilization by organizing continuous job to the CPU.
 Here the memory is partitioned into several pieces, with different job in each partition.
 Whenever any job under execution doesn’t require CPU, the OS switches the processor to another job and so on.
Job1
Job2 Memory_Partition
Job3
OS

iii. Time Sharing System


 Time sharing system is the advance of multi-programmed system, CPU executes multiple task by switching from one
task to another.
 Here user can interact with each program while it is running because switching occurs so frequently i.e. at any instant of
time, the CPU is running one program, in the course of 1 second, it may work on several programs, thus giving the users
the illusion of parallelism. It is termed as pseudo-parallelism.
 Time sharing OS allow user to share the computer. Since switching among user is very fast, user get impression that
entire computer system is dedicated to particular user.
 In this system the concept of virtual memory was introduced.
 The main advantage of virtual memory is to store the remaining portion of program that is not fitting in the main
memory so as to run particular program.

iv. Parallel System (Multiprocessor system)


 Many systems to date are single processor system so there is a technological limit on the speed on which a single
processor chip can operate.
 If the system workload can’t be handled satisfactory by the single processor, one solution is to apply multiple
processors to the problem and it is known as multiprocessor environment.
 So parallel system has more than one processors communication with each other, sharing the computer bus, memory
and peripheral devices.

CPU CPU CPU

Memory
Fig: Multiprocessing architecture

Compiled By : Bhesh Thapa


5

v. Real Time System


 A real time system has well defined, fixed time limit i.e. processing must be done within the defined time limit
otherwise the system will fail.
 Real time operating system (RTOS) has well defined fixed time limit i.e. processing must be done within the defined
time limit, otherwise the system will fail.
 It can be classified as hard RTOS and soft RTOS.
 Hard real-time means you must absolutely hit every deadline. Very few systems have this requirement. Some examples
are air traffic system, factory automation system etc.
 Soft real time systems can miss some deadlines, but eventually performance will degrade if too many are missed. A
good example is the sound system in your computer. If you miss a few bits, no big deal, but miss too many and you're
going to eventually degrade the system.
 Some of the application of real time operating system are robotics, air traffic system, control system etc.

vii. Distributed System


 A network is a communication path between two or more systems.
 Distributed system is a collection of processors interconnected by a communication network in which each processor
has its own local memory and other peripheral, and the communication between two processor of the system take place
by message passing over the communication network.
 So using distributed system, it allows programs to run on multiprocessor simultaneously.
 A distributed system is a collection of independent computers that appear to the users of the system as a single
computer.

Process, Process State and its Transition


 A process is a program in execution.
 Example: Compiling of an instruction is a process. A system task, such as sending output to a printer is also a process.
 In order to execute a program, it needs several resources like CPU, memory, I/O devices etc, so to support it, OS runs
several internal programmed activities
 As the program execute it generally changes its state.
 The state of a process is defined as the current activity of that process. Each process may have one of the
following states.
1. New: The process is being created.
2. Running: Instruction is being executed.
3. Waiting: The process is waiting for some events to occur
4. Ready: The processor is ready to execute
5. Terminated: The process has finished execution
The below figure is a 5 state process diagram.

 Running state implies that process is currently being serviced by CPU.


 Ready state means that it needs CPU attention and time to run i.e. the process is waiting to be assigned to a
processor.

Compiled By : Bhesh Thapa


6

 Waiting state implies that process is not running currently and is waiting for some event to occur such as I/O
completion.

Threads
 A program may consist of multiple independent flow of control called as thread.
 Threads are also called as light weight process because thread run within a program and share resources of that program
to which it belongs to.
 Every program has at least one thread which is called as main thread.
 The life cycle of thread is similar to that of process.

Process vs Thread
1. A process can contain more than one thread.
2. A process is considered as “heavyweight” while a thread is deemed as “lightweight”.
3. Processes are heavily dependent on system resources available while threads require minimal amounts of resource.
5. Threads within a process communicate directly while processes do not communicate so easily.
6. Threads are easy to create while processes are not that straightforward.
7. It takes less time to create a new thread in an existing process then to create a new process.

Scheduling
 Scheduling is the process of determining which process will actually run when there are multiple runnable processes.
 The part of OS that make this decision is called scheduler.
 The algorithm it uses is called scheduling algorithm.
 Proper selection of scheduling algorithm is necessary for providing maximum CPU utilization.
 Some of the selection criteria are
1. Make sure each process get equal access to CPU.
2. Keep the CPU 100% busy.
3. Minimize the response time for interactive users.
4. Maximize the number of jobs processed per hour (Throughput).

Compiled By : Bhesh Thapa


7

Scheduling can be divided into three levels.


 Long Term Scheduling
 Middle Term Scheduling
 Short Term Scheduling

 If we consider batch system, there will often be more processes submitted than the number of processes that can be
executed simultaneously.
 So incoming processes are spooled to disk.
 The Long Term Scheduler also called as Job Scheduler, selects process from this process pool and loads the selected
process into the memory for execution.
 Once the job scheduler makes a job active, it stays until it terminates.
 The main objective of long term scheduler is to provide appropriate number of jobs to medium term scheduler i.e.
control the degree of multiprogramming.
 If the number of job is too few then the CPU may be idle and if the number of jobs is too many then memory
management system become overhead.

 The Medium Term Scheduler also called as swapper, swap the process in and out of memory.
 It can be sometime needed to reduce the degree of multiprogramming by removing the process from memory and
storing them onto the disk.
 These processes can then be reintroduced into memory by the Medium Term Scheduler.
 This operation is known as swapping.
 Any memory management system that supports multiprogramming use swapping for mixing the processes or freeing
the memory.

 The Short Term Scheduler selects the process to get the processor from among the processes which are already loaded
in the memory.
 Also known as dispatcher, it allocates the processes to the CPU for fixed time quantum and at the end of the quantum
the process releases the CPU and returns to the pool of processes from where it was selected.

Compiled By : Bhesh Thapa


8

Comparisons between Different types of Scheduler

Objective of Scheduling
There can be many objectives that must be considered in the designing of scheduling algorithm:
1. Fairness: Make sure that each process gets its fair share of the CPU.
2. Predictable: A given job should be run in same amount of time as predicted.
3. Throughput: Maximum number of jobs should be processed per unit time.
4. Efficiency: The CPU must be kept busy 100 percent of the time.
5. Resource: Scheduling mechanism should keep the resource of the system busy.
6. Priorities: Scheduling mechanism should favor the highest priority processes.

Scheduling Criteria
Different criteria have been suggested for the comparison of different scheduling algorithms. Some of them are
1. CPU Utilization: We have to keep the CPU as busy as possible. It may range from 0 to 100 percent.
2. Throughput: It is the measure of work in term of number of process completed per unit time. For long processes, this
rate may be 1 process per hour, for short transaction, throughput may be 10 process per second.
3. Turnaround time: It is defined as the time interval between the time of submission and completion of the job. So it is
the sum of the time periods spent in waiting to get into memory, waiting in ready queue, execution on CPU and doing
I/O.
Turnaround Time=time of completion of job - tome of submission of job
Or
Turnaround Time=waiting time + Burst time or (/) service time
4. Waiting Time: It is the sum of period waiting in the ready Queue.
5. Response Time: It is defined as the time interval between the job submission and the first response produced by the
job. In the interactive system, the turnaround time is not the best criteria. Response time is the amount of time it take to
start responding, not time taken to output the response.
Compiled By : Bhesh Thapa
9

Types of Scheduling
a. Preemptive Scheduling
b. Non-Preemptive Scheduling

In Preemptive Scheduling, Even if the CPU has been allocated to a certain process, it can be snatched by any other process at
any time either due to time constrain or due to priority reason. It applies that if any process with higher priority become ready
for its execution, the process which is currently using the CPU will be forced to give up the CPU that higher priority job can run
first.
In Non-Preemptive Scheduling, once the CPU is allocated to a process, the process keeps the CPU until it terminates or its
transition to blocked state. This means that once a CPU is allocated to a process, this process can use the CPU for its own
execution till it willingly surrenders or leave the CPU.

Scheduling Algorithms:
1. First Come First Service (FCFS):
FCFS is a simple non-preemptive scheduling algorithm. Processes are assigned to the CPU in the order in which the process
arrive. That is the process that request the CPU first is allocated the CPU first. The implementation of the FCFS policy is
managed with a FIFO queue. When the job enters the system from outside, it is started immediately and allowed to run as long
as it wants to.

2. Priority Scheduling:
In this algorithm, each process in the system is given a priority, then the scheduling must be done according to the priority of
each process i.e. CPU is allocated to the process with the highest priority. Equal priority processes are scheduled in FCFS order.

3. Round Robin Scheduling:


This algorithm is similar to the FCFS but now it is preempted FCFS algorithm. The preemption takes place at a fixed
interval of time called as quantum time or time slice. If a process doesn’t complete before its CPU time expires, the
CPU is preempted and given to the next process waiting in the queue. The preempted process is then placed back of
the ready list. If the process has blocked or finished before the quantum has elapsed the CPU switching is done.

4. Shortest Job First Scheduling:


Key concept of this algorithm is “CPU is allocated to the process with Least CPU Burst Time”.
Among the process in the ready queue, CPU is always assigned to the process with least CPU burst requirement. If
the two processes having the same length, next CPU burst time, FCFS is used i.e. one which arrives first, will be
taken up first by the CPU.
The SJF is either preemptive or non-preemptive.

5. Multilevel Queue Scheduling:


Multilevel queue was created for a situation in which processes are easily classified into different groups.
Multilevel Queue scheduling has the following characteristics:
a. Processes are divided into different queue based on their type. Processes are permanently assigned to one
queue, generally based on some priority of process i.e. system processes, interactive processes, batch system,
end user processes, process priority and process type.
b. Each Queue has its own scheduling algorithm. For example, interactive processes may user Round Robin
Scheduling Method, while Bath processes may use the FCFS method.
In addition, there must be scheduling among the queue and is generally implemented as fixed priority preemptive
scheduling. Foreground (Interactive) processes may have higher priority over the background (batch) queue
processes.

Compiled By : Bhesh Thapa


10

Highest Priority
System Processes

Interactive Processes

Batch System Processes

End User Processes

Lowest Priority

No process in the batch queue could run unless the queue for the system processes and interactive processes
were all empty. If an interactive process entered the ready queue while a batch process was running, the batch
process would be preempted.

Example:

See class notes for solution………….

Deadlock

Compiled By : Bhesh Thapa


11

Compiled By : Bhesh Thapa


12

Compiled By : Bhesh Thapa


13

Compiled By : Bhesh Thapa


14

Example:
A system has 2 process and 3 resources. Each process need maximum of two resources, is deadlock possible?
Explain.
See class notes for solution………

Compiled By : Bhesh Thapa


15

Deadlock Detection and Recovery


There is an algorithm for deadlock detection which periodically determine whether a deadlock has occurred in the system or not.
i. Maintain a wait for graph. Nodes in this graph represent processes. If process i is waiting for a resource held by
process j, then there exist an edge from i to j.
ii. Periodically invoke an algorithm that searches for cycle in the graph. If there is a cycle in the wait for graph, a
deadlock is said to be exist in the system.
figure below shows a wait for graph, p1 is waiting for a resource held by p2, p2 is waiting for resource held by p3 and p2 is
waiting for resource held by p1. There is a cycle in the graph and thus processes are deadlocked.

P1 P2

P3

When a deadlock algorithm determine that a deadlock exists, the system must recover from the deadlock. There are two
approaches for breaking deadlock.
1. Simply kill one or more processes in order to break circular wait.
2. Preempt some resources from one or more of the deadlock processes.

Memory Management
 Memory is one of the important resources that must be carefully managed.
 Most computer have a memory hierarchy, with a small amount of very fast, expensive, volatile Cache Memory, some
numbers of megabytes of medium speed, medium price, volatile memory (RAM), hundreds or thousands of megabytes
of slow, cheap, nonvolatile Disk storage.
 The part of the Operating system that manages the memory hierarchy is called Memory Manager.
 its job is to keep track of which part of memory are in use and which part are not in use, to allocate memory to process
when they need it and de-allocate it when they are done, and to manage swapping between main memory and disk when
main memory is not big enough to hold all the processes.
 Memory management plays vital in multiprogramming environment.
 Memory is a long array of bytes, bytes act as a storage location or unit.

Memory Management Techniques


Some of the memory management techniques are explained below.

1. Swapping
 A process must be in memory to be executed.
 A process, however can be swapped temporarily out of memory to backing store and then brought back into memory for
continued execution.
 For example, assume a multiprogramming environment with round robin CPU scheduling algorithms.
 When a quantum expires, the memory manager will start to swap out the processes that just finished and to swap
another process into the memory space that has been freed
 With the batch system, organizing the memory into fixed partitions is simple in which a job is kept in the memory until
the completion of job.
 With time sharing system, the situation is different.
 There are number of users accessing the memory to hold their processes, so it is necessary to keep excess processes on
the disk.
 To run these processes, they must be brought back into main memory.
 Moving processes from main memory to disk and back is called swapping.
 Since all the parts of a process are not loaded into main memory, so a technique called virtual memory is adopted to
run this process.
 Below figure show how swapping is used.

Compiled By : Bhesh Thapa


16

 Here process A, B and C are swapped in during stage 1 to 3. At stage 4, A is swapped out to disk. Process D come in
and goes out. Finally A comes again. This task of moving a process from main memory to disk and back is done by
memory management unit.

3. Partitioning
 We know that in multiprogramming environment, main memory is partitioned into several pieces, with different job in
each partition.
 Main memory can be partitioned in the following two ways.

a. Fixed Sized Partition


 It is desirable to allow multiple processes to run at once.
 On time sharing system, having multiple processes in memory at once allows another process to use the CPU if one
process is blocked waiting for I/O to finish, thus increases the CPU utilization.
 The easiest way to achieve multiprogramming is simply to divide the main memory into number of static partitions at
system generation time.
 A process may be loaded into a partition of equal or greater size.
 Memory manager will allocate a partition to a process that best fits it.
 Unused memory within the allocated partition is called internal fragmentation.
 Partition can be equal or unequal.

Compiled By : Bhesh Thapa


17

Advantage:
 Simple to implement
 Little OS overhead

Disadvantage:
 Inefficient use of memory due to internal fragmentation. Main memory utilization is extremely inefficient because any
program, no matter how small, occupies an entire portion. This phenomenon, in which there is wasted space internal to
a partition due to the fact that the block of data loaded is smaller than the partition, is referred to as internal
fragmentation.

Allocation (Placement) Algorithms:


Different allocation algorithms are used for variable storage allocation to determine which partition to allocate to a process.
Some of them are
1. First Fit
The memory manager scan the list of segment until it finds first hole that is big enough to hold the requirement. In this
method, search for the hole starts at first block and ends as soon as the block large enough to hold the process
requirement is found.

2. Next Fit
It is and advancement of First Fit. It works similar to Fist Fit except that it starts searching the list from the place where
it left off last time, instead of always at the beginning as First Fit does.

3. Best Fit
In this method, the entire available memory blocks are searched; the block that is best suited to the requirement is
selected.

4. Worst Fit
In this method, an incoming process is placed in the hole which leaves the maximum amount of unused space. I.e. it
always takes the block with largest available hole.

Example:
For the following partition of 100k, 500k, 200k, 300k and 600k in order, place the process 212k, 417k, 122k and 426k in order
according to first, next best and worst fit.
See class notes for solution………

b. Variable (Dynamic) size Partition


 In order to overcome the difficulties with fixed partitions, another approach called as Dynamic or Variable Partitions
was developed.
 With this technique, the partitions are of variable length and numbers. When a process is brought into memory, it is
allocated exactly as much memory as it requires and no more. An example, using 64 Mbytes of main memory, is shown
in figure.

Compiled By : Bhesh Thapa


18

 The operation of swapping system is illustrated in above figure.


 Initially no process is in main memory.
 Then process1 of 20K size, process2 of 14K size and process3 of 18K size are swapped in into memory from disk.
 In figure e, process2 is swapped out to disk.
 Then process4 with 8k size is swapped in. again in figure g process1 is swapped out to disk.
 Then process2 which was previously swapped out is swapped in as shown in figure h.
 We have total holes of size (6M + 6M + 4M) = 16M.
 Now if any process of 16M (assume) is ready for execution, but it has to wait because there is no contiguous partition to
satisfy the requirement.
 This unused memory holes are called External Fragmentation.
 So the dynamic partition will eventually lead to a situation where there are a lot of small unused holes in memory which
declines them memory utilization.

 One technique for overcoming the External Fragmentation is compaction.


 From time to time, the operating system shifts the processes so they are contiguous and so that the entire unused
memory hole is together in one block.
 For example, in figure h, compaction will result a block of free memory of size 16M, hence is large enough to hold any
process having size lesser or equal to 16M.
 The only difficulty in compaction is that it is time consuming procedure and waste of processor time.

Relocation and Memory Protection


 Multiprogramming introduces the overhead of relocation and protection.
 As we know that the main memory is usually divided into two partitions: one for the resident OS and one for the user
process.
 We also want several user processes to reside in the memory at the same time.
 Different process run at different address space.
 So protection deals with following issues.
1. Protection the operating system from user processes.
2. Protecting the user processes from one another.
 We can provide this protection by using a relocation register(base) and limit register.

Compiled By : Bhesh Thapa


19

Fig: Hardware support for relocation and limit register

 The relocation register contains the value of small physical address while the limit register contains the length of
partition.
 With relocation and limit registers, each logical address must be less than the limit register otherwise it will generate
addressing error.
 The primary function of a limit register is to detect attempts to access memory location bound the boundary assigned by
operating system.
 When any process is scheduled, the base register is loaded with the address of the start of its partition, and the limit
register is loaded with the length of the partition where that process is stored. Every memory address generated
automatically has the base register contents added to it before being sent to main memory. Thus if the bae register is
100k, a CALL 100, instruction is effectively turned into a CALL 100+100 instruction, without the instruction itself
being modified. Addresses are also checked against the limit register to make sure that they do not attempt to address
memory outside the current partition.
 The MMU maps the logical address dynamically by adding the value in relocation register.
 Finally, the mapped address is sent to memory

Question: Given three process p1 with limit register value= 400, and relocation register value = 1100.
What is the physical address for logical address 350 and 500?
Solution:
For 350, physical address = 1100+ 350 = 1450
For 500, it will generate addressing error because 500> 400. In this case, it attempts to access the instruction other than that of
p1.

Virtual Memory
 Virtual memory is a technique of executing program instruction that may not fit entirely in system memory.
 The basic idea behind virtual memory is that the combined size of program, data, and stack may exceed the amount of
physical memory available for it.
 The operating system keeps those parts of the program currently in main memory, and rest on the disk.
 For example: 64 MB program can run on 32 MB machine by carefully choosing which 32 MB to keep in memory at
each instant, with pieces of program being swapped between disk and memory as needed.
 One major advantage of this scheme is that program size can be larger than size of physical memory.
 The main requirement for executing any program is that the entire program must be present in the main memory.
 This requirement will reduce the degree of multiprogramming.
 To overcome this restriction, the method of virtual memory management is used.
 Using this method, it is possible to execute the program even if it is not entirely in the primary memory.
 There are basically two techniques for virtual memory implementation: Paging and Segmentation
Advantage of virtual memory
1. No size restriction: The user program can be longer than the actual physical memory available.
2. This technique increases the CPU utilization.

Compiled By : Bhesh Thapa


20

3. As the part of the program is already present in the primary memory less I/O will be needed to load or swap the
program to main memory.

Memory Address: Logical(Virtual) Address and Physical Address


 Address act as a location to access the memory.
 An address generated by CPU is commonly referred to as logical address, whereas address seen by memory unit- that is
the one loaded into the memory-address register of the memory- is commonly referred to as physical address.
 So simply we can define logical address as the address of an instruction or data used by program whereas address of the
memory area where the user program actually resides is called physical address.
 Address binding is the process of mapping the program's logical or virtual addresses to corresponding physical or main
memory addresses.
 It is done by hardware device called memory management unit(MMU).

Paging
 Most virtual system uses a technique called paging that permits the physical address space of process to be
noncontiguous.
 These program generated addresses are called Virtual address and form the virtual address space.
 On computer without virtual memory, the virtual address is directly put onto the memory bus and causes the physical
memory word with the same address to be read or written.
 When virtual memory is used, the virtual addresses do not go directly to the memory bus, instead they go to memory
management unit (MMU) that maps the virtual addresses to the physical addresses.
 In this approach, the physical memory is divided into fixed size block called Frames and the logical memory is divided
into fixed sized block called Pages.
 The size of page is same as that of the frame.
 The key idea of this method is to place the pages of a process into the available frames of memory, whenever this
process is executed i.e. when a process is to be executed, its pages are loaded into available memory frames from the
backing store.
 When the program is executed, if all of its pages are loaded into the physical memory then it is called pure paging.
 But, if only those pages which are actually needed during process execution are brought in main memory by
implementing virtual memory technique, then it is called demand paging.
 To translate the logical address into the actual physical address, page table is used
Frame Number

Page 0
Page number Frame number
Page 1 0
0 1
Page 2 Page 0 1
1 5
Page 3 Page 3 2
2 6
. Page 4 3
3 2
Logical Memory 4
4 8
Page 1 5

Page 2 6
Page table
7

Page 4 8

9
Physical Memory

Fig: Paging model of logical and physical memory

Compiled By : Bhesh Thapa


21

 As shown in the above figure, the logical memory is divided into five pages and the physical memory is divided into ten
frames. The pages of logical memory are placed into noncontiguous frames. The MMU uses the page table to translate
the address from logical memory to physical memory.
 The below figure shows, how logical address is mapped into physical address using paging hardware.

Physical address

OS
CPU P D F D Page 0
Logical address

Page 0 Page 1
Page 1 Page 4
Page 2
Page Page 3
Table Page 4 Page 3

MMU
Figure: A paging hardware Physical memory

 If the size of logical address space is 2 m and page size is 2n addressing units then the higher order m-n bits of logical
address designate the page number, and the n lower order bits designate the page offset.
 Thus, the CPU generate a single address that is divided into two parts: page number (p) and page offset (d) as shown
below
P D
m-n n bit
 The page number is used as index to page table. The page table contains the base address of each page in the physical
memory.
 The offset d is added with the base address to get the actual physical address.

Example
The below example demonstrates the operation of MMU for mapping logical address to physical address.
 Let us consider we have 64K virtual address space with page size of 4k.
 Similarly, we have 32K physical address space.
 Then
Total number of pages = 64 / 4 = 16 pages.
Total number of page frame = 32 / 4 = 8 frames
 Now let the program try to access address 0 which is already mapped by page table using the instruction
MOV REG 0.

Compiled By : Bhesh Thapa


22

Fig: Page table


 Then, The MMU gets virtual address 0 and sees that virtual address 0 falls in page 0 (ok to 4k i.e. 0 to 4095) and sees it
is already mapped to page frame 2 (8k to 12k i.e. 8192 to 12287). So it transforms the address 0 to 8192 onto the bus.
 Similarly, the virtual address 8192 falls in page 2 (8k to 12k) and is transformed to 24576 because this page is mapped
onto the physical page frame 6.
 Since there are 16 pages but only 8 frames so not all pages can be loaded into available page frame at a time, the cross
in figure shows the pages that are not mapped currently.
 Now if the program tries to access an unmapped page then we get a trap called page fault. For example, if MMU gets
virtual address 32780, then it sees that this address falls in page 8(32k to 36k i.e. 32768 to 36864) but not mapped yet
(cross symbol present).
 Since there is not any available page frame to map this current page, so one of the existing page must be replaced by
this current frame. The page to be replaced is decided by Page replacement algorithm.
 Now, suppose the OS decide to remove page 1, then the firstly, OS would load page 8 at physical address 4k, secondly,
replace the 1 in virtual page 1 entry with cross, and finally, replace the cross in virtual page 8 entry with 1.

 Internal Working principle of MMU


1. Let us assume that a program to access address 8196
2. The MMU sees that virtual address 8196 falls in page 3(8k to 12k) and sees that it is already mapped to page
frame 6.
3. Since we have used 16-bit virtual address space (216 = 64K) so virtual address logical address 8196 in binary is
0010000000000100
4. The incoming 16-bit address is split into two parts: page number and offset
P D
m-n n bit
with 2n page size and 2m logical address space
5. Here, 2n = 4k
2n = 4 * 1024
2n = 4096
2n = 2 12
n =12 bits

Compiled By : Bhesh Thapa


23

6. Similarly, 2m = 64k i.e. m=16 bits

7. Therefore, higher order (m-n) i.e. 4 bits are used for indexing into page table (Or we can directly say, for
addressing 16 pages we need 4 bits) and lower n bits are used as page offset as shown in below figure.

0010 000000000100

P D

Fig: internal operation of MMU with 16, 4k pages

o So with 4 bit we can address 16 pages, and with 12 bit we can address all 4K i.e. 4*1024 = 4096 offsets within
each page.
o Similarly, 2n = 8 frames, i.e. 3 bits can be used to address all 8-page frame in main memory.
o The page number is used as index into page table. If the present / absent bit is o, a trap to operating system is
caused i.e. page fault occur but if the present/absent bit is 1, the 3-bit page frame number found in page table is
copied to output register along with original 12-bit offset, thus forming a 15-bit physical address i.e.
110000000000100 = 24580
o This address is put onto the memory bus as the physical memory address.

Question:
1. Consider a paged memory system with 8 pages of 8 kb size and 16 pages’ frames in memory.
i. How many bits are there in logical address?
j. How many bits are there in physical address?
k. Using the given page table, compute the physical address for the logical address 18325.

7 10
6 4
5 0
4 7
3 13
2 11
1 14
0 5
Solution:
Given page size = 8kb
So we need 2n = 8 * 1024 i.e. 2n = 8192 i.e. n= 13 bits to address each page offset

Compiled By : Bhesh Thapa


24

Given total pages = 8, so we need 2n = 8, i.e. n=3 bits to address each page. ---------i)

i. So total bits in logical address = bits required to address 8 pages + bits required to address all
8k offset in each page
= 3 + 13
=16 bits.

Again given page frame number = 16, hence we need 2n = 16, i.e. n=4 bits to address each frame.

ii. Similarly, total bits in physical address = bits required to address 16 frame + bits required to address
all 8kb offset in each frame
= 4+ 13
= 17 bits.

iii. Now given virtual address = 18325


This can be written in 16-bit binary as
0100 0111 1001 0101
Here page index (p) = first 3 higher order bits = 010 ………..from i)
Offset (d) = remaining 13 bits = 0 0111 1001 0101

So using the above page table, the page with index 010 i.e. 2 is already mapped into 4-bit physical address=11
i.e.1011
Therefore, the final effective physical address is obtained by combining base address and offset as below
1 0110 0111 1001 0101 i.e. 92053

Translation lookaside Buffers (TLBs)

 Most of the paging scheme keeps the page table in the memory. But due to their large size, this design has enormous
impact on performance.
 Using paging, in order to fetch an instruction, an additional memory reference is needed to access the page table. So this
reduces the rate the CPU can get instruction and data out of memory, thus reducing the performance of CPU.
 One of the solution to this problem is to have a small hardware device called as TLB inside MMU.
 The TLB is an associative memory which maps the virtual addresses to physical addresses without going through the
page table. The associative memory provides storing information that can be retrieved based on its content (Content
addressable), not its storage location

CPU P D OS
Logical address

Virtual Page Page Frame


Page Frame

TLB T TLB hit TLB hit


Miss F D

Page Table TLB Physical Memory

 When a CPU generates a logical address then this is presented to MMU for translating the logical address into physical
address.
 Then the hardware checks to see if its virtual page number is present in the TLB by comparing it to all the entries
simultaneously i.e. Parallel.
 If a valid match is found, then the page frame is directly taken from the page frame without going to page table.
Compiled By : Bhesh Thapa
25

 So in TLB, if page will get its associative frame then it is called TLB hit.
 But if the virtual page number is not on the TLB then the MMU detects TLB miss and does an ordinary page table
lookup.
 Then it removes one of the entries from TLB and replace it with the page table entry just looked up.
 Thus if that page is used again soon, the second time it results hit rather than miss. If the page is not able to get its
associative frame from TLB then it is called TLB miss.

Page Replacement Algorithm

 If the program is of five pages 1, 2, 3, 4, 5 and out of these only pages 1, 4, 2 are loaded into main memory at the time
of execution.
 If the program tries to access the address of page number 3, then there will be an error because page 3 is not swapped in
into main memory at the time of loading the program. This error is called page fault.
 When a page fault occurs, the operating system has to choose a page to remove from memory to make room for the
page that has to be brought in.
 If it throws out a page just before it is about to be used, then it will just have to go get that page again almost
immediately. This condition is called as thrashing. So thrashing results too many paging operation.
 The page replacement is done by swapping the required pages from backup storage to main memory and vice-versa.
 There are several page replacement algorithms.
 The operating system chooses the particular algorithm; whose page fault rate is lowest.
 Some of the page replacement algorithm are
1. Optimal page replacement algorithm
Replace the page which will not be used (referenced) for longest period of time.
2. Least Frequently used page replacement algorithm
Throw page with smallest count of page references.
3. First in first out page replacement algorithm
Remove the page which was loaded first in memory
4. Least recently used page replacement algorithm
Throw out the page that has been unused for the longest time.

Segmentation:
 Paging is not visible to programmer but segmentation is visible to the programmer.
 Segmentation is a memory management technique of dividing memory into many completely independent address
space called segment.
 Each segment consists of linear sequence of addresses starting from 0 to maximum value depending on the size of
segment.
 The size of segment can increase or decrease depending upon the data stored or nature of operation performed on that
segment.
 This change doesn’t affect other segment in the memory as segment are usually of larger size.
 The logical and used address space is a collection of segment. Each segment is loaded on the physical memory. Each
segment has a name and a length.
 The main advantages are
1. There is no internal fragmentation.
2. By moving the segment around, fragmented memory space can be combined into a single free area.
3. It simplifies the handling of growing data structure because the OS will grow or shrink the segment as needed.

Disadvantages:
1. There is difficulty in managing variable size segment on secondary storage.
2. The maximum size of segment is limited by the size of memory.

Implementation of Segmentation
 The implementation of segment differs from paging.
 Pages are fixed size and segments are not.
 Fig a shows an example of physical memory initially containing five segments. Now segment 1 is removed and segment
7 which is smaller is put in its place. As shown in fig b, between the segment 7 and segment 2, there is an unused area
called hole. Then segment 4 is replaced by segment 5 as shown is fig c and segment 3 is replaced by segment 6 as
shown in figure d. After the system has been running for a while, memory will be divided up into a number of chunks,
Compiled By : Bhesh Thapa
26

some containing hole. This phenomenon is called external fragmentation i.e. waste of memory hole. It can be recovered
by compaction i.e. shifting all the segments downward in order to combine all the holes in order to maximize the unused
memory so as to utilize it, as shown in figure e.

Figure a figure b figure c figure d

Segment 4 (7K) Segment 4 (7K) Segment 5 (4K) Segment 5 (4K)


Segment 5 (4K) Segment 5 (4K)

Segment 3 (8K) Segment 3 (8K) Segment 3 (8K)


Segment 6 (4K)
Segment 2 (5K) Segment 2 (5K) Segment 2 (5K) Segment 2 (5K)
Segment 1 (8K)
Segment 7 (5K) Segment 7 (5K) Segment 7 (5K)
Segment 0 (4K) Segment 0 (4K) Segment 0 (4K) Segment 0 (4K)

Figure e

Segment 5 (4K)

Segment 6 (4K)
Segment 2 (5K)
Segment 7 (5K)
Segment 0 (4K)

Differences between Segmentation and Paging

Paging Segmentation
1. In paging, the memory is divided into 1. In segmentation, the memory is divided
fixed size frames. into variable size segments.
2. The logical address is divided into 2. The logical address is divided into
pages by MMU. segments by programmer.
3. Paging is invisible to the user’s view. 3. Segmentation is a memory
management technique which supports
the programmer view of memory.
4. This scheme suffers from internal 4. This scheme suffers from external
fragmentation. fragmentation.
5. The OS maintain page map table for 5. The OS maintain segment map table
mapping between pages and frames. for mapping purpose.
6. Memory allocation in paging takes less 6. Memory allocation in segmentation
processing because pages are easier to takes more processing because
place in computer memory since they segments are of variable size.
are always same size
7. In paging, size of pages are fixed. 7. In segmentation, the size of segment is
limited to the size of memory.
8. The OS uses page number and
displacement to calculate physical 8. The OS uses segment number and
address. displacement to calculate the physical
Compiled By : Bhesh Thapa
27

address.

Compiled By : Bhesh Thapa

You might also like