Operating System Support Upd
Operating System Support Upd
Operating System Support Upd
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.
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.
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.
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.
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.
Memory
Fig: Multiprocessing architecture
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).
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.
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.
Highest Priority
System Processes
Interactive 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:
Deadlock
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………
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.
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.
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.
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.
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………
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.
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.
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
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.
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
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
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.
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
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
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.
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 e
Segment 5 (4K)
Segment 6 (4K)
Segment 2 (5K)
Segment 7 (5K)
Segment 0 (4K)
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.