Op
Op
Op
O P E R AT I N G
S YS T E M S - I
For T.Y.B.Sc. Computer Science : Semester – V
[Course Code CS 351 : Credits - 2]
CBCS Pattern
As Per New Syllabus, Effective from June 2021
Price ` 325.00
N5861
OPERATING SYSTEMS - I ISBN 978-93-5451-169-1
Second Edition : August 2022
© : Authors
The text of this publication, or any part thereof, should not be reproduced or transmitted in any form or stored in any
computer storage system or device for distribution including photocopy, recording, taping or information retrieval system or
reproduced on any disc, tape, perforated media or other information storage device etc., without the written permission of
Authors with whom the rights are reserved. Breach of this condition is liable for legal action.
Every effort has been made to avoid errors or omissions in this publication. In spite of this, errors may have crept in. Any
mistake, error or discrepancy so noted and shall be brought to our notice shall be taken care of in the next edition. It is notified
that neither the publisher nor the authors or seller shall be responsible for any damage or loss of action to any one, of any kind, in
any manner, therefrom. The reader must cross check all the facts and contents with original Government notification or publications.
Published By : Polyplate Printed By :
NIRALI PRAKASHAN YOGIRAJ PRINTERS AND BINDERS
Abhyudaya Pragati, 1312, Shivaji Nagar, Survey No. 10/1A, Ghule Industrial Estate
Off J.M. Road, Pune – 411005 Nanded Gaon Road
Tel - (020) 25512336/37/39 Nanded, Pune - 411041
Email : [email protected]
DISTRIBUTION CENTRES
PUNE
Nirali Prakashan Nirali Prakashan
(For orders outside Pune) (For orders within Pune)
S. No. 28/27, Dhayari Narhe Road, Near Asian College 119, Budhwar Peth, Jogeshwari Mandir Lane
Pune 411041, Maharashtra Pune 411002, Maharashtra
Tel : (020) 24690204; Mobile : 9657703143 Tel : (020) 2445 2044; Mobile : 9657703145
Email : [email protected] Email : [email protected]
MUMBAI
Nirali Prakashan
Rasdhara Co-op. Hsg. Society Ltd., 'D' Wing Ground Floor, 385 S.V.P. Road
Girgaum, Mumbai 400004, Maharashtra
Mobile : 7045821020, Tel : (022) 2385 6339 / 2386 9976
Email : [email protected]
DISTRIBUTION BRANCHES
DELHI BENGALURU NAGPUR
Nirali Prakashan Nirali Prakashan Nirali Prakashan
Room No. 2 Ground Floor Maitri Ground Floor, Jaya Apartments, Above Maratha Mandir, Shop No. 3,
4575/15 Omkar Tower, Agarwal Road No. 99, 6th Cross, 6th Main, First Floor, Rani Jhanshi Square,
Darya Ganj, New Delhi 110002 Malleswaram, Bengaluru 560003 Sitabuldi Nagpur 440012 (MAH)
Mobile : 9555778814/9818561840 Karnataka; Mob : 9686821074 Tel : (0712) 254 7129
Email : [email protected] Email : [email protected] Email : [email protected]
[email protected] | www.pragationline.com
Also find us on www.facebook.com/niralibooks
Preface …
The book has its own unique features. It brings out the subject in a very simple and lucid
manner for easy and comprehensive understanding of the basic concepts. The book covers
theory of Introduction to Operating Systems, Processes and Threads, Process Scheduling,
Synchronization and Memory Management.
A special word of thank to Shri. Dineshbhai Furia, and Mr. Jignesh Furia for
showing full faith in us to write this text book. We also thank to Mr. Amar Salunkhe and
Mr. Akbar Shaikh of M/s Nirali Prakashan for their excellent co-operation.
We also thank Ms. Chaitali Takle, Mr. Ravindra Walodare, Mr. Sachin Shinde, Mr. Ashok
Bodke, Mr. Moshin Sayyed and Mr. Nitin Thorat.
Although every care has been taken to check mistakes and misprints, any errors,
omission and suggestions from teachers and students for the improvement of this text book
shall be most welcome.
Authors
Syllabus …
1. Introduction to Operating Systems (6 Lectures)
• Operating Systems Overview - System Overview and Functions of Operating Systems
• What does an OS do?
• Operating System Operations
• Operating System Structure
• Protection and Security
• Computing Environments - Traditional, Mobile, Distributed, Client/Server, Peer to Peer
Computing
• Open Source Operating System
• Booting
• Operating System Services
• System Calls, Types of System Calls and their Working
2. Processes and Threads (6 Lectures)
• Process Concept - The Processes, Process States, Process Control Block
• Process Scheduling - Scheduling Queues, Schedulers, Context Switch
• Operations on Process - Process Creation with Program Using fork(), Process Termination
• Thread Scheduling - Threads, Benefits, Multithreading Models, Thread Libraries
3. Process Scheduling (7 Lectures)
• Basic Concept - CPU-I/O Burst Cycle, Scheduling Criteria, CPU Scheduler, Preemptive
Scheduling, Dispatcher
• Scheduling Algorithms - FCFS, SJF, Priority Scheduling, Round-Robin Scheduling, Multiple
Queue Scheduling, Multilevel Feedback Queue Scheduling
4. Synchronization (5 Lectures)
• Background
• Critical Section Problem
• Semaphores: Usage, Implementation
• Classic Problems of Synchronization: The Bounded Buffer Problem, The Reader Writer
Problem, The Dining Philosopher Problem
5. Memory Management (12 Lectures)
• Background - Basic Hardware, Address Binding, Logical Versus Physical Address Space,
Dynamic Loading, Dynamic Linking and Shared Libraries
• Swapping
• Contiguous Memory Allocation - Memory Mapping and Protection, Memory Allocation,
Fragmentation
• Paging - Basic Method, Hardware Support, Protection, Shared Pages
• Segmentation - Basic Concept, Hardware
• Virtual Memory Management - Background, Demand Paging, Performance of Demand
Paging, Page Replacement - FIFO, Optimal, LRU, MFU
Contents …
Introduction to
Operating Systems
Objectives …
To understand Basic Concepts of Operating System
To learn Services and Functions of an Operating System
To study various types of Operating Systems
To learn Operating System Structure
To study Operating System Operations and Operating System Components
To understand Concept of Protection and Security
To study different Computing Environments
To learn Concept of System Calls and its Working
To study different Types of System Calls
To understand the Concept System Booting
1.0 INTRODUCTION
• An operating system acts as an interface between the user of a computer system and
the computer hardware.
• The purpose of an operating system is to provide an operating environment in which a
user can execute programs in a convenient and efficient manner.
• An operating system is the most essential system software that manages the
operations of a computer. Without an operating system, it is not possible to use the
computer.
• An Operating System (OS) is a system program that acts as an intermediary between
the user and the computer hardware and controls the execution of all kinds of
programs.
• The operating system provides certain services to programs and to the users of those
programs in order to make their tasks easier.
• The basic task of an operating system is to control and co-ordinate the use of hardware
among the various application programs for various users.
• In short, an operating system is a program which takes control of the operation of the
computer system.
1.1
Operating Systems - I Introduction to Operating Systems
• The purpose of this control is to make the computer system operate in the way
intended by the user and in a systematic, reliable and efficient manner.
• Various types of operating systems are UNIX, MS-DOS, MS-Windows
95/98/ME//XP/Vista/7/8/10, Linux, Windows-NT/2000, OS/2, Android, Windows Mobile,
iOS, Symbian OS, Mac OS, ZyNOS and so on.
Operating System
Computer
Hardware
• System programs are the set of utility programs supplied with an operating system to
provide basic services for the computer users (compilers, text editors, assembler etc.).
• Users are the people (like system analyst, programmers, and operational users etc.)
who interact with the computer system.
• A batch is a sequence of user jobs formed for the purpose of processing by a batch
processing operating system.
• Batch operating system is one where programs and data are collected together in a
batch before processing starts.
• A job is predefined sequence of commands, programs, instructions and data that are
combined in to a single unit called job.
• Memory management in batch system is very simple. Memory is usually divided into
two areas namely, operating system and user program area as shown in Fig. 1.3.
Operating system
User program
area
System Batch
area Monitor •••
I/O
CPU
Card reader Line printer
Fig. 1.5: Concept of Spooling
• The most common spooling application is print spooling. In print spooling, documents
are loaded into a buffer and then the printer pulls them off the buffer at its own rate.
1.8
Operating Systems - I Introduction to Operating Systems
• Because the documents are in a buffer where they can be accessed by the printer, we
can perform other operations on the computer while the printing takes place in the
background.
• Spooling also lets we place a number of print jobs on a queue instead of waiting for
each one to finish before specifying the next one.
• Spooling is also used for processing data at remote sites. The CPU sends the data via
communication paths to remote printer.
Advantage of Spooling:
1. The spooling operation uses a disk as a very large buffer.
2. Spooling is however capable of overlapping I/O operation for one job with
processor operations for another job.
Disadvantages:
1. There is an extra overhead of maintaining table of card images.
2. An extreme of spooling is called as staging of tapes. To avoid the wear of the
magnetic tape, it is copied onto the disk and CPU will access the disk and not the
tape.
Multiprogramming Operating System:
• In multiprogramming operating system more than one program/job can be execute at
the same time.
• The term multiprogramming means a situation in which a single CPU divides its time
between more than on job. In short multiprogramming operating systems allow
concurrent execution of multiple programs or jobs.
• In multiprogramming, more than one programs or jobs are present in the memory at a
time. Multiprogramming means to maintain two or more jobs concurrently in
execution.
• The basic idea of multiprogramming is as follows:
o The job pool consists of number of jobs. The operating system picks jobs from job
pool. Many jobs are kept in memory simultaneously.
o The number of jobs present in memory at a time is called degree of
Multiprogramming. The operating system start execution of a job. (Only one job is
executed at a time).
o If a job requires I/O, then operating system switches to another job and executes it.
CPU would not sit idle. When that job requires to wait for an I/O, the operating
system switches to another job and so on.
• The Fig. 1.6 shows the multiprogramming system with job execution. Fig. 1.6 shows
that job 1 started its execution and if it requires I/O then, operating system switch to
job 2 which is executed next and CPU would not remain idle.
• Fig. 1.7 shows that several jobs are ready to run, the system must keep them in
memory at a time.
• As there will always be several jobs in memory all the time, operating system requires
some form of memory management.
• If many jobs are ready to run, then operating system must select among them.
Operating system has to perform some CPU scheduling to schedule only one job at a
time.
1.9
Operating Systems - I Introduction to Operating Systems
Operating
system
Program 1
(reading input)
Program 2
(running)
Job 1 CPU I/O
Program 3
(writing input)
Job 2 CPU I/O
1.10
Operating Systems - I Introduction to Operating Systems
3. The jobs are executed in the order in Multiprogramming systems need more
which they were submitted, that is, sophisticated scheduling algorithm, as
FCFS (First-Come-First-Serve basis). multiple processes reside in the main
memory at the same time.
4. These systems do not tend to achieve These systems ensure that the CPU has
efficient CPU utilization. always something to execute, thus
increasing the CPU utilization.
Multitasking or Time Sharing Operating System:
• A running state of a program is called as a process or a task. A CPU handling multiple
tasks at a time is known as multitasking.
• A multitasking operating system can handle multiple tasks together by applying
multiprogramming technique.
• The time-sharing systems are also called multitasking systems. A multitasking
operating system supports two or more active processes simultaneously.
• An instance of a program in execution is called a process or a task. Multitasking
operating system supports two or more active processes simultaneously.
• In a multi tasking environment, operating system ensures that multiple tasks run on a
single system by sharing the CPU time.
• Though they access the CPU one by one, it appears to the user that all the tasks are
running simultaneously.
• For example, while one has opened multiple tabs in a single browser and whenever
the user switches from one tab to another, operating system schedules the current
process to access the CPU. However, user is never concerned and this is how multi
tasking is achieved.
• The two most common methods for sharing the CPU time is either cooperative
multitasking or preemptive multitasking.
1. Cooperative Multitasking: The cooperative multitasking lets the programs decide
when they wish to let other tasks run. Early versions of the MacOS (uptil MacOS 8)
and versions of Windows earlier than Win95/WinNT used cooperative
multitasking (Win95 when running old apps).
2. Preemptive Multitasking: Preemptive multitasking moves the control of the CPU
to the OS, letting each process run for a given amount of time (a time slice) and
then switching to another task. This method prevents one process from taking
complete control of the system and thereby making it seem as if it is crashed. This
method is most common today, implemented by among others OS/2, Win95/98,
WinNT, Unix, Linux, BeOS, QNX, OS9 and most mainframe OS.
• Fig. 1.8 shows multitasking concept. The user gives instructions to the operating
system or to a program directly, and receives an immediate response.
• Operating system handles multitasking in the way that it can handle multiple
operations/ executes multiple programs at a time.
1.11
Operating Systems - I Introduction to Operating Systems
Word
Operating CPU
• system
•
•
Web
browser
CPU
User 2
User 5
Active link
User 1 User 6
Fig. 1.9
1.12
Operating Systems - I Introduction to Operating Systems
Fig. 1.10
Advantages of Multiprocessor Operating Systems:
1. It increased throughput by increasing the number of processors, more work done
in a shorter period of time.
1.13
Operating Systems - I Introduction to Operating Systems
2. Multiprocessors can also save money (cost) compared to multiple single systems.
Because the processors can share peripherals cabinets and power supplies.
3. It increases reliability, if functions can be distributed properly among several
processors, then the failure of one processor will not halt the system, but rather
will only slow it down.
4. These systems provide higher performance due to parallel processing.
Disadvantages of Multiprocessor Operating Systems:
1. If one processor fails then it will affect in the speed.
2. Multiprocessor systems are expensive.
3. Complex OS and large main memory is required.
Real Time Operating System:
• Real-time operating systems are the operating systems that are used in real-time
applications where the data processing must be done in a fixed interval of time.
• Real time operating system is used in an environment where a large number of events,
mostly external to the computer system, must be accepted and processed in a short
time or within stipulated deadline.
• A real time computer system may be defined as, "one which controls an environment
by receiving data, processing them and taking action or returning results very quickly
to affect the functioning of the environment at that time".
• A real-time system is a data processing system in which the time interval required to
process and respond to inputs is so small that it controls the environment.
• Examples of real time system include Nuclear Power Plant Control, Medical Imaging
Systems, Industrial Control Systems, Weapon Systems, Robots, Air Traffic Control
Systems, Space Navigation, and Flight Control System etc.
• Real-time systems are used when there are rigid time requirements on the operation
of a processor or the flow of data and real-time systems can be used as a control device
in a dedicated application.
• A real-time operating system must have well-defined, fixed time constraints, otherwise
the system will fail.
On Line Disk File
CPU
Traffic
Sensors and
Light Controller
Fig. 1.11
1.14
Operating Systems - I Introduction to Operating Systems
• Real time operating systems are of two types namely, hard real time operating systems
soft real time operating systems.
• Hard real-time operating systems guarantee that critical tasks complete on time. In
hard real time operating systems, the actions must be taken within the specified
timeline.
• In soft real time operating systems, it is not mandatory to meet the deadline. A critical
real-time task gets priority over other tasks and retains the priority until it completes.
Advantages:
1. Useful for real time practical industrial applications.
2. As user gates a higher degree of control the flexibility is setting up the priority of
processes is greater.
3. Fast response, the system is immediately updated.
4. It offers better utilization of systems and produces more output from all the
resources.
Disadvantages:
1. RTOSs are very complex and difficult to maintain.
2. Real Time Operating System (RTOS) is expensive because it is using heavy system
resources.
3. It is more difficult to backup these systems when they are in use.
Difference between Time Sharing System and a Real-Time Operating System:
Sr.
Time-Sharing Operating System Real-Time Operating System
No.
1. It is the logical extension of This OS is designed for environments
multiprogramming system that supports where a huge number of events must
interactive users and is designed to be processed within fixed time
provide a quick response time. constraints.
2. Round-robin scheduling is used to Most real-time systems use priority-
execute the programs. based pre-emptive scheduling.
3. This OS tends to reduce the CPU idle Effective resource utilization and user
time by sharing it among multiple users. convenience are of secondary concern
in a real-time system.
4. More sophisticated memory In these OSs, the programs remain in
management is needed in time-sharing the main memory most of the time and
OSs to provide separation and protection there is a little swapping of programs
of multiple user programs. between main and secondary memory.
Thus, memory management is less
demanding in real-time systems.
5. Process deals with more than one Process deals with single application at
application simultaneously. a time.
1.15
Operating Systems - I Introduction to Operating Systems
Distributed Applications
Middleware Service
Network
Fig. 1.12: Distributed Systems
1.16
Operating Systems - I Introduction to Operating Systems
Advantages:
1. Reliability: A distributed system is more reliable than a single system. If one
machine from system crashes, the rest of the computers remain unaffected.
2. Speed: A distributed system can have more computing power than a mainframe.
Its speed makes it different than other systems.
3. Performance: The collection of processors in the system can provide higher
performance than a centralized computer.
4. Sharing of Resources: As data or resources are shared in distributed system, it is
essential for various applications like banking, ticket reservation system.
Disadvantages:
1. Troubleshooting: Troubleshooting and diagnosing problems are the most
important disadvantages of distributed system.
2. Networking: The underlying network in distributed system can cause several
problems such as transmission problem, overloading, loss of messages.
3. Complexity: Typically, distributed systems are more complex than centralized
systems.
4. Security: The easy distributed access in distributed system which increases the
risk of security.
Network Operating System:
• A Network Operating System (NOS) runs on a server and provides the server the
capability to manage data, users, groups, security, applications, and other networking
functions.
• The primary purpose of the network operating system is to allow shared file and
printer access among multiple computers in a network, typically a local area network
(LAN), and a private network or to other networks.
• Examples of network operating systems include Microsoft Windows Server 2003,
Microsoft Windows Server 2008, UNIX, Linux, Mac OS X, Novell NetWare, and BSD.
• Network operating systems are operated on a server and offer the facility of security,
users, applications, data management, and other networking related functions.
• Its objective is to permit file sharing and printer access among various computers in a
network, i.e., local area network (LAN), Private network, etc.
• Network Operating System is a computer operating system that facilitates to connect
and communicate various autonomous computers over a network.
• An autonomous computer is an independent computer that has its own local memory,
hardware, and operating system. NOS can be specialized to serve as client-server
operating system and peer-to-peer operating system.
• Peer-to-peer NOS is an operating system in which all the nodes are functionally and
operationally equal to each other. They all are capable to perform similar kinds of
tasks. The nodes are directly connected with each other in the network.
• In peer-to-peer NOS, all computer systems contain equal privileges for consuming the
all resources which are presented on the network.
1.17
Operating Systems - I Introduction to Operating Systems
• The advantages of the peer-to-peer NOS includes Easy to install and setup, setup cost is
low, sharing of information and resources is fast and easy and so on. Disadvantages of
the peer-to-peer NOS includes less secure, no centralized storage system, low
performance and so on.
• The client-server NOS operate with a single server and multiple client computers in
the network (Refer Fig. 1.13).
• The client operating system runs on the client machine, while the NOS is installed on
the server machine. The server machine is a centralized hub for all the client
machines.
• The client machines generate a request for information or some resource and forward
it to the server machine. The server machine, in turn, replies to the client machine by
providing appropriate services.
Print Server
Printer File
Server
9. OS provides file management which refers to the way that the operating system
manipulates stores and retrieves data.
10. OS provide an overall control to the computer system.
11. OS provide memory management functions like allocation and deallocation of
primary memory.
12. It managing the processes i.e. assigning the processor to a process at a time. OS
processor management for multitasking.
13. OS provide job accountability, which helps to track of all time and their resources,
which are used by several tasks and multiple users.
14. OS manages all I/O devices communication with their drivers respectively.
15. OS monitors the entire system to identify the errors and bugs and fix immediately
without losing any type of data.
1.20
Operating Systems - I Introduction to Operating Systems
User process
User mode
(mode bit = 1)
User process executing Calls system call Return from system call
• An example of the multimode system can be described by the systems that support
virtualization. These CPU’s have a separate mode that specifies when the Virtual
Machine Manager (VMM) and the virtualization management software is in control of
the system.
• For these systems, the virtual mode has more privileges than user mode but less than
kernel mode.
1.3.2 Timer
• If the user program goes in an infinite loop or fail to call system services and never
return control to the operating system then we use timer.
• A timer can be set to interrupt the computer after some specified period of time. A
timer can be fixed or variable.
• For variable timer, operating system sets a counter to some initial value. It uses fixed
rate clock.
• For each clock ticks, the counter is decremented. When counter reaches to zero, an
interrupt occurs and control is transferred automatically to the operating system.
1.4 OPERATING SYSTEM STRUCTURE [Oct. 18]
• Fig. 1.15 shows the basic structure of an operating system which shows various parts
of an operating system and each part consists of a number of programs.
• Structure of an operating systems have a layered structure, with the bottom most layer
forming the hardware part of the computer and the outer most (top most) layer
forming the User Interface (UI). In between these two layers are kernel layer and shell
layer.
• The kernel is the innermost layer and is the central controlling part of the operating
system. The kernel is the core of the operating system and provides the basic services
for all other parts of the operating system.
• The services of a kernel are requested by other parts of the operating system or by
application programs through a specified set of program interfaces, sometimes known
as system calls.
• A typical kernel contains programs that are used for the basic functions of an
operating system like process management, input/output devices management.
• Kernel manages all operations of computer and hardware and acts as a bridge
between the user and the resources of the system by accessing various computer
resources like the CPU, I/O devices and other resources.
• The shell is the next layer to the kernel. A shell is software that provides an interface
for users of an operating system access the services of a kernel.
• The shell is the layer of programming that understands and executes the commands a
user enters. In some systems, the shell is called a command interpreter.
• Programs in the user interface part either provide a Command Line Interface (CLI) or
a Graphical User Interface (GUI) to the user. These programs use facilities by shell.
• A user interacts with programs in the User Interface (UI) typically with the command
interpreter to request use of resources and services provided by the system.
• The hardware consists of Central Processing Unit (CPU), the main memory, I/O
devices, etc. provides the basic computing resources.
1.22
Operating Systems - I Introduction to Operating Systems
User
Shell/Command
interpreter
Kernel
Hardware
1.23
Operating Systems - I Introduction to Operating Systems
• Initially, MS-DOS was designed as a small-size and simple system, and with limited
scope, but grew beyond its scope with time.
• It was designed with the idea of providing more functionality within loss space;
therefore, it was not carefully divided into modules.
• Though MS-DOS has a limited structuring, there is no clear separation between the
different interfaces and level of functionality.
• For example, application programs can directly call the basic I/O routines to
read/write data on disk instead of going through a series of interfaces.
• This exemption makes the MS-DOS system susceptible to malicious programs that may
lead to system crash. Moreover, due to the lack of hardware protection and dual-mode
operation in the Intel 8088 system (for which MS-DOS system was developed), the base
hardware was directly accessible to the application programs.
• The original UNIX operating system also has simple structure like MS-DOS. The UNIX
OS consists of following two separable parts:
(i) Systems programs, and (ii) The kernel.
• Kernel consists of everything below the system-call interface and above the physical
hardware.
• Kernel provides the file system, CPU scheduling, memory management, and other
operating-system functions; a large number of functions for one level.
• System calls define the API to UNIX; the set of system programs commonly available
defines the user interface. The programmer and user interfaces define the context that
the kernel must support.
• The operating system has a much greater control over the computer and over the
applications that make use of that computer.
• Fig. 1.17 shows original UNIX system.
2. Layered Structure:
• In the layered structure, the operating system is organized as a hierarchy of layers
or levels with each layer built on the top of the layer below it.
• The top-most layer (layer N) is the User Interface (UI), while the bottom-most layer
(layer 0) is the hardware. Each layer has a well defined function and comprises data
structures and a set of routines.
• The layers are constructed in such a manner that a typical layer (say, layer N) is able
to invoke operations on its lower layers and the operations of layer N can be invoked
by its higher layers.
• Fig. 1.18 shows layered structure of an operating system.
Application Application
•••
Program Program
User Mode
Kernel Mode
Layer N
Layer 1
Layer 0
Hardware
Fig. 1.18: Layered Structure of Operating System
• A layered design was first used in the THE (Technische Hogeschool Eindhoven)
operating system designed by a team led by Edsger W. Dijkstra.
• Fig. 1.19 shows the structure of THE operating system.
Layer 5
The operator
Layer 4
User program
Layer 3
Input / Output management
Layer 2
Operator process communication
Layer 1
Memory and Drum management
Layer 0
Processor allocation and Multiprogramming
Fig. 1.19: Layered Structure of THE Operating System
1.25
Operating Systems - I Introduction to Operating Systems
1.26
Operating Systems - I Introduction to Operating Systems
• As UNIX expanded, the kernel becomes large and it is difficult to manage. The
microkernel approach is used by operating system.
• In the mid-1980s, researchers at Carnegie Mellon University developed an operating
system called Mach that modularized the kernel using the microkernel approach.
• It structures the operating system by moving all non-essential components from the
kernel and implementing them as system and user-level programs into user space.
• So, small core operating system is running at kernel level and OS Services built from
many independent user-level processes.
• Main function of micro-kernels is to provide communication facility between the
client program and various services running in user mode, communication is achieved
by message passing
• It provides minimal process and memory management, in addition to a
communication facility.
• Examples of microkernel include Tru64 UNIX, Mac OS X kernel (Darwin), QNX (real-
time operating system).
• Fig. 1.20 shows structure of a typical microkernel. Communication is provided by
message passing.
• In the Fig. 1.20 the microkernel contains basic requirements such as memory
management, CPU scheduling and interprocess communication. The only software
executing at the privileged level i.e. kernel mode is the microkernel.
• The other functions of the operating system are removed from the kernel mode and
run in the user mode. These functions may be device drivers, file servers, application
program communication etc.
Messages Messages
Hardware
1.27
Operating Systems - I Introduction to Operating Systems
Disadvantages:
(i) Performance of micro-kernels suffers due to increased system function overhead.
(ii) Micro kernel does not handle I/O communication directly.
(iii) The execution of the microkernel is slower as compared to the monolithic kernel.
4. Monolithic Structure:
• Monolithic structure is the oldest structure of the operating system. The monolithic
operating system is also known as the monolithic kernel.
• A monolithic kernel is an operating system structure where the entire operating
system is working in kernel space. The kernel can access all the resources present in
the system.
• In the monolithic systems, each component of the operating system is contained
within the kernel.
• All the basic services of OS like process management, file management, memory
management, exception handling, process communication etc. are all present inside
the kernel only.
• Monolithic operating system example includes Linux, BSD, OS/360, Open VMS, XTS-
400, z/TPF and so on. Fig. 1.21 shows the monolithic structure.
1.28
Operating Systems - I Introduction to Operating Systems
(ii) To add any new service, the entire operating system needs to be modified by the
user.
(iii) Security issues are always there because there is no isolation among various
servers’ present in the kernel.
• Handheld systems are used to connect to wireless networks to use the company's web
portal. Wireless technology is also used. Due to new technology, faster access with low
cost is possible.
• Mobile computers can synchronize with computers to allow very portable use of
company data or information.
• Mobile devices can also connect to wireless and cellular networks to use the
company’s web portals as well as other web resources.
• At home, most users had a single computer with a slow modem connection to the
office, the Internet or both.
• Today, network-connection speeds once available only at great cost are relatively
inexpensive, giving home users more access to more data.
• These fast data connections are allowing home computers to serve up web pages and
to run computer networks that include client computers, servers and so on. Some
home users have firewalls to protect their networks from security breaches.
Advantages:
1. All the nodes in the distributed system are connected to each other. So nodes can
easily share data with other nodes.
2. More nodes can easily be added to the distributed system i.e. it can be scaled as
required.
3. Failure of one node does not lead to the failure of the entire distributed system.
Other nodes can still communicate with each other.
Disadvantages:
1. It is difficult to provide adequate security in distributed systems because the nodes
as well as the connections need to be secured.
2. The database connected to the distributed systems is quite complicated and
difficult to handle as compared to a single user system.
3. Overloading may occur in the network if all the nodes of the distributed system try
to send data at once.
1.32
Operating Systems - I Introduction to Operating Systems
3. The server need not be located physically close to the clients. Yet the data can be
accessed efficiently.
Client
Desktop
Client
Smartphone
1.33
Operating Systems - I Introduction to Operating Systems
142.58.110.2
209.52.108.37
• Open source operating systems are released under a license where the copyright
holder allows others to study, change as well as distribute the software to other people
without any cost or charge.
• GNU/Linux is the most popular open source operating system released under the GNU
General Public License (GPL).
• FreeBSD is a free open source OS, a Unix-like operating system that’s based
on Berkeley Software Distribution (BSD).
• Closed source software is highly guarded. Only the owners of the source code have the
legal right to access that code. Microsoft Windows is an example of closed source.
• Closed source code cannot be legally altered or copied, and the user pays only to use
the software as it is intended they cannot modify it for new uses nor share it with their
communities.
Advantages:
1. Open source operating systems are available freely on internet without any
charge.
2. In open source operating system user can modify and edit changes as per his/her
choice and requirements.
Disadvantages:
1. Constant updates are headaches sometimes as users have to keep update his/her
system and code.
2. Frequent modifications are challenge for users to decide which open source
system is better.
3. Sometimes hardware does not compatible with open source operating systems.
1.35
Operating Systems - I Introduction to Operating Systems
• When a CPU receives a reset event - for instance, when it is powered up or rebooted -
the instruction register is loaded with a predefined memory location, and execution
starts there.
• At that location is the initial bootstrap program. This program is in the form of Read
Only Memory (ROM), because the RAM is in an unknown state at system startup.
• ROM is convenient because it needs no initialization and cannot be infected by a
computer virus.
• Some systems like cellular phones, PDAs, etc. store the entire operating system in
ROM. Storing the operating system in ROM is suitable for small operating systems,
simple supporting hardware, and rugged operation.
• A problem with this approach is that changing the bootstrap code requires changing
the ROM hardware chips.
• Some systems resolve this problem by using Erasable Programmable Read Only
Memory (EPROM), which is read only except when explicitly given a command to
become writable.
• All forms of ROM are also known as firmware, since their characteristics fall
somewhere between those of hardware and those of software.
• A problem with firmware in general is that executing code there is slower than
executing code in RAM.
• Some systems store the operating system in firmware and copy it to RAM for fast
execution. A final issue with firmware is that it is relatively expensive, so usually only
small amounts are available.
• Fig. 1.24 shows booting process of computer system.
Main memory
Disk storage
ROM Rea
Bootstrap program dO
(permanent) S
Load OS Operating
Operating system
system
St
art
de
vice
RAM dri
ve
(volatile) rs
Devices
1.36
Operating Systems - I Introduction to Operating Systems
• In this case, the bootstrap runs diagnostics and has a bit of code that can read a single
block at a fixed location (say block zero) from disk into memory and execute the code
from that boot block.
• The program stored in the boot block may be sophisticated enough to load the entire
operating system into memory and begin its execution.
• More typically, it is simple code (as it fits in a single disk block) and only knows the
address on disk and length of the remainder of the bootstrap program.
• All of the disk-bound bootstrap, and the operating system itself, can be easily changed
by writing new versions to disk. A disk that has a boot partition is called a boot disk or
system disk.
• Now that the full bootstrap program has been loaded, it can traverse the file system to
find the operating system kernel, load it into memory, and start its execution. It is only
at this point that the system is said to be running.
User Interfaces
System Calls
Protection
Erros and
detection Security
Services
Operating System
Hardware
because these things are taken care of by the operating system. The operating
system must be able to load the program into memory and to execute it. The
program must be able to terminate its execution, either normally or abnormally.
3. I/O Operations: Each program requires an input and after processing the input
submitted by user it produces output. This involves the use of I/O devices. The
input may be either from a file on the disk or from some other input device. The
output may be written to some file on the disk or sent to some output devices such
as printer, plotter. Since user programs cannot execute I/O operations directly, the
operating system must provide some means to perform I/O.
4. File System Manipulation: While working on the computer, generally a user is
required to manipulate various types of files like as opening a file, saving a file and
deleting a file from the storage disk. Program needs to read a file or write a file.
The operating system gives the permission to the program for operation on file.
Maintain details of files or directories with their respective details. This is an
important task that is also performed by the operating system.
5. Communications: Operating system performs the communication among various
types of processes in the form of shared memory. In multitasking environment, the
processes need to communicate with each other and to exchange their
information. These processes are created under a hierarchical structure where the
main process is known as parent process and the sub processes are known as child
processes.
6. Error Detection: Error can occur anytime and anywhere. Error may occur in CPU,
in I/O devices or in the memory hardware. Operating system deals with hardware
problems. To avoid hardware problems the operating system constantly monitors
the system for detecting the errors and fixing these errors (if found). The main
function of operating system is to detect the errors like bad sectors on hard disk,
memory overflow and errors related to I/O devices. After detecting the errors,
operating system takes an appropriate action for consistent computing.
7. Resource Allocation: In the multitasking environment, when multiple jobs are
running at a time, it is the responsibility of an operating system to allocate the
required resources (like as CPU, main memory, tape drive or secondary storage
etc.) to each process for its better utilization. For this purpose various types of
algorithms are implemented such as process scheduling, CPU scheduling, disk
scheduling etc.
8. Accounting: Operating system keeps an account of all the resources accessed by
each process or user. In multitasking, accounting enhances the system
performance with the allocation of resources to each process ensuring the
satisfaction to each process.
9. Protection: If a computer system has multiple users and allows the concurrent
execution of multiple processes, then the various processes must be protected
from one another's activities. Protection refers to mechanism or a way to control
the access of programs, processes, or users to the resources defined by a computer
system. Providing protection to program, data, and files and to ensure data
security.
1.38
Operating Systems - I Introduction to Operating Systems
• Another option, in an interactive system, is to ask the user (via a sequence of system
calls to output the prompting message and to read the response from the terminal)
whether to replace the existing file or to abort the program.
• Now that both files are set up, we enter a loop that reads from the input file (a system
call) and writes to the output file (another system call). Each read and write must
return status information regarding various possible error conditions.
• On input, the program may find that the end of the file has been reached or that there
was a hardware failure in the read (such as a parity error).
• The write operation may encounter various errors, depending on the output device
(no more disk space, printer out of paper, and so on).
• Finally, after the entire file is copied, the program may close both files (another system
call), write a message to the console or window (more system calls), and finally
terminate normally (the final system call).
• Fig. 1.26 shows implementation of a system call. System call offers the services of the
operating system to the user programs via API (Application Programming Interface).
• Typically, a number is associated with each system call, and the system-call interface
maintains a table indexed according to these numbers.
• The system call interface then invokes the intended system call in the operating
system kernel and returns the status of the system call and any return values.
• The caller needs to know nothing about how the system call is implemented or what it
does during execution.
• Rather, it just needs to obey the API (Application Programming Interface) and
understand what the operating system will do as a result of the execution of that
system call.
User application
open ()
User mode
System call interface
Kernel mode
open ()
Implementation of
i open ()
sys call
return
Fig. 1.26: System Call Implementation or Invoking System Call [open()]
1.40
Operating Systems - I Introduction to Operating Systems
• Most of the details of the operating-system interface are hidden from the programmer
by the API and are managed by the run-time support library. Often, more information
is required than simply the identity of the desired system call.
• The standard C language provides a portion of the system call interface for many
versions of UNIX and Linux operating systems.
• For example, the C language program invokes the printf() statement. The C language
library intercepts the necessary system call(s) in the operating system write() system
call.
• The C language library takes the value returned by write() system call and passes it
back to the user program as shown in Fig. 1.27.
#include <stdio.h>
int main ()
{
printf ("Greetings");
return 0;
}
User
mode
Standard C library
Kernel
mode
write()
write()
system call
User mode 1 2 3
User process executing Gets system call Return from system call
1.41
Operating Systems - I Introduction to Operating Systems
X: parameters
for call
Code for
Use parameters
system
Load address X from table X
call 13
system call 13
User program
Operating system
4. Information Maintenance: These system calls handle information and its transfer
between the operating system and the user program. Some examples of these
system calls are get time or date, set time or date, get system data, set system data
etc.
5. Communication: These system calls are useful for interprocess communication
(IPC). Examples of these system calls are creating and deleting connection, send
and receive messages, read and write messages etc. There are two common models
of IPC namely, the message passing model and the shared-memory model. In the
message-passing model, the communicating processes exchange messages with
one another to transfer information. Message-passing model uses a common
mailbox to pass messages between processes. In shared memory model,
processes use shared memory creates and shared memory attaches system calls to
create and gain access to regions of memory owned by other processes.
PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. What is operating system?
(a) collection of programs that manages hardware resources
(b) system service provider to the application programs
(c) link to interface the hardware and application programs
(d) All of the above mentioned
2. Which one of the following is not true?
(a) kernel is the program that constitutes the central core of the operating system
(b) kernel is the first part of operating system to load into memory during booting
(c) kernel is made of various modules which cannot be loaded in running
operating system
(d) kernel remains in the memory during the entire computer session
3. What is the main function of the command interpreter?
(a) to get and execute the next user-specified command
(b) to provide the interface between the API and application program
(c) to handle the files in operating system
(d) none of the mentioned
4. Which of the following is typically a part of the operating system but not the
kernel?
(a) Graphical User Interface (b) Network Management
(c) Device Driver Management (d) Compiler
5. Which of the following is abstracted by operating system?
(a) Processor (b) Memory
(c) Network Cards (d) All of the above mentioned
1.43
Operating Systems - I Introduction to Operating Systems
13. Which is the first program run on a computer when the computer boots up?
(a) System software (b) Operating system
(c) System operations (d) None of the above
14. Which system contains multiple nodes that are physically separate but linked
together using the network?
(a) Monolithic (b) Layered
(c) Distributed (d) Client-server
15. The initial program that is run when the computer is powered up is called as,
(a) boot program (b) bootloader
(c) initializer (d) bootstrap program
16. In the layered approach of operating systems,
(a) Bottom Layer(0) is the User interface
(b) Highest Layer(N) is the User interface
(c) Bottom Layer(N) is the hardware
(d) Highest Layer(N) is the hardware
17. Operating system provides the different types of services to the user and for
accessing these services, the interface is provided by the an OS is,
(a) system calls (b) Application Programming Interface
(c) Native library (d) None of the above
18. The goal of an operating system is to,
(a) Execute user programs and make solving user problems easier
(b) Make the computer system convenient to use
(c) Use the computer hardware in an efficient manner
(d) All of the mentioned
19. Information maintenance is a type of,
(a) input (b) output
(c) system call (d) processes
20. Which is a program that acts as an intermediary between a user of a computer and
the computer hardware?
(a) System Manager (b) Application program
(c) Operating system (d) Source code
21. System calls of the operating system provide interface to,
(a) programs (b) processes
(c) services (d) both (a) and (b)
22. Which controls and coordinates use of hardware among various applications and
users?
(a) System Manager (b) Operating system
(c) Application program (d) Source code
1.45
Operating Systems - I Introduction to Operating Systems
23. Which system call is used to access data from a file that is stored in the file system?
(a) close() (b) write()
(c) open() (d) read()
24. An operating system may be interrupted by,
(a) Hardware interrupts
(b) Software interrupts
(c) Both hardware interrupts as well as software interrupts
(d) Neither hardware interrupts nor software interrupts
25. Which is changes the system from user mode to kernel mode and vice versa?
(a) Operating System (b) Interrupt
(c) System Call (d) Hardware Manager
26. Which of the following is an example of spooled device?
(a) A graphic display device
(b) A line printer used to print the output of a number of jobs
(c) A terminal used to enter input data to a running program
(d) A secondary storage device in a virtual memory system
27. Which of the following requires a device driver?
(a) Register (b) Cache
(c) Main memory (d) Disk
28. Which of the following operating system reads and reacts in actual time?
(a) Quick Response System (b) Real Time System
(c) Time Sharing System (d) Batch Processing System
29. Logical extension of multiprogramming of operating system is,
(a) time sharing (b) multi tasking
(c) single programming (d) both (a) and (b)
30. Unix OS is an,
(a) Time Sharing Operating System (b) Multi-User Operating System
(c) Multi-tasking Operating System (d) All the Above
31. Which of the following is not advantage of multiprogramming?
(a) Increased throughput
(b) Shorter response time
(c) Decreased operating system overhead
(d) Ability to assign priorities to jobs
32. Which is the most essential system software that manages the operations of a
computer?
(a) Operating System (b) Booting System
(c) Hardware System (d) None of the Above
1.46
Operating Systems - I Introduction to Operating Systems
42. A system consisting of more than one processor i.e. tightly coupled is a,
(a) multimedia system (b) distributed system
(c) parallel system (d) All of the Above
43. Which is the conceptual design and fundamental operational structure of a
computer system?
(a) processing (b) architecture
(c) programming (d) None of the Above
44. Which is a system is an operating system with multiple CPUs?
(a) multiprogramming (b) multitasking
(c) clustered (d) All of the Above
45. Operating systems for handheld devices that contain computer are called as,
(a) mobile operating system (b) embedded operating system
(c) handheld operating system (d) All of the Above
46. Operating system ________ refers to the use of software to allow system hardware
to runmultiple instances of different operating systems concurrently, allowing to
run different applications requiring different operating systems on one computer
system.
(a) virtualization (b) architecture
(c) clustering (d) All of the Above
47. Which envolves ensuring controlled access to the system resources while which
involves protecting system form unauthorized users?
(a) Protection, Security (b) Memory, processor
(c) Buffering, Spooling (d) All of the Above
48. The client-server computing enviroment provides an interface to which a ________
can send a request to perform an action in response, the ________ executes the
action and sends back results to the client.
(a) node, computer (b) client, server
(c) network, topology (d) None of the Above
49. Which is a type of computing that delivers computing, storage and even
apllications as a service across a computer network?
(a) grid computing (b) network computing
(c) cloud computing (d) All of the Above
50. Internet is an example of which system.
(a) client-server (b) multiprogramming
(c) batch (d) handheld
51. Which is an operating system that helps to run other application software on
mobile devices?
(a) Network operating system (b) Distributed operating system
(c) Mobile operating system (d) All of the Above
1.48
Operating Systems - I Introduction to Operating Systems
10. A computer program makes a system call when it makes a request to the
operating system’s shell.
11. A distributed operating system is an extension of the network operating system
that supports higher levels of communication and integration of the machines on
the network.
12. Operating System controls the hardware components and coordinates for an
efficient use of these hardware components for the various users.
13. The kernel directly interacts with the computer hardware as well as it controls the
use of hardware among different types of application programs.
14. open source refers to a program in which the source code is available to the
general public for use and/or modification from its original design free of charge
15. Linux is the best-known and most-used open source operating system.
16. OS is the first software to be loaded into computer memory when the computer is
switched on and called booting.
17. A system resource manger is a way for programs to interact with the operating
system.
18. Bootstrap loader locates the kernel, loads it into main memory and starts its
execution.
19. An kernel supplies different kinds of services to both the users and to the
programs as well.
20. System Call is used to show all services offered by the OS. It serves as an interface
b/w user program and Operating System. The system calls interface layer includes
the entrance point in the kernel code.
21. Security refers to a mechanism or a way to control the access of programs,
processes, or users to the resources defined by a computer system.
22. An operating system provides users the services to execute the programs in a
convenient manner.
23. System call provides the services of the operating system to the user programs via
Application Program Interface (API).
24. File management system calls are responsible for file manipulation such as
creating a file, reading a file, writing into a file etc.
25. Peer-to-peer network operating systems allow users to share resources and files
located on their computers and to access shared resources found on other
computers.
Answers
1. (T) 2. (T) 3. (F) 4. (T) 5. (F)
6. (F) 7. (T) 8. (T) 9. (T) 10. (F)
11. (T) 12. (T) 13. (F) 14. (T) 15. (T)
16. (T) 17. (F) 18. (T) 19. (F) 20. (T)
21. (F) 22. (T) 23. (T) 24. (T) 25. (T)
1.52
Operating Systems - I Introduction to Operating Systems
1.53
Operating Systems - I Introduction to Operating Systems
1.54
CHAPTER
2
2.0 INTRODUCTION
• A process is a smallest unit of work that is scheduled by operating system. A process is
basically a program in execution.
• A process needs resources, such as CPU time, memory, files and I/O devices, to
accomplish its task. These resources are allocated either when the program is created,
or when it is executing.
• Operating system enable processes to share and exchange information protect the
resources of each process from other processes and enable synchronization among
processes.
• To meet these requirements, process management is required which is integral part of
an operating system.
• Thread is the segment of a process means a process can have multiple threads and
these multiple threads are contained within a process.
• Multithreading allows the execution of multiple parts of a program at the same time.
These parts are known as threads and are lightweight processes available within the
process.
2.1
Operating Systems - I Processes and Threads
• A process is defined as, an entity which represents the basic unit of work to be
implemented in the system.
• When a program is loaded into the memory and it becomes a process. The process
contains program counter which specify the next instruction to be executed.
• Fig. 2.1 shows a simplified layout of a process inside main memory. The process can be
divided into four sections namely, stack, heap, text and data.
max
stack
heap
data
text
0
Fig. 2.1: Process in Memory
• Each process has following sections:
1. A text section contains the program code.
2. A data section contains global and static variables.
3. The heap section is used for dynamically allocated memory to a process during its
run time.
4. The stack section is used for local variables. A process stack which contains the
temporary data (such as subroutine parameters, return addresses, and temporary
variables).
• Use of the process concept enables an OS to execute both sequential and concurrent
programs equally easily.
2.2
Operating Systems - I Processes and Threads
• A process is just an executing program, including the current values of the program
counter, register and variables.
• Conceptually each process had its own virtual CPU. In reality, of course, the real CPU
switches back and forth from process to process; thus it is much a collection of
processes running in parallel. This rapid switching back and forth is called multi-
programming.
D A
Time
(a) (b) Process (c)
Fig. 2.2: Process Models
• In Fig. 2.2 (a), computer multiprogramming four programs in the memory.
• In Fig. 2.2 (b), we can see how this abstracted into four processes, each with its own
flow of control (i.e. its own program counter), and each one running independent of
the other ones.
• In Fig. 2.2 (c), we can see that viewed over a long enough time interval, all the
processes have made progress, but at any given instant only one process is actually
running.
• With the CPU switching back and forth among the processes, the rate at which a
process performs its computation will not be uniform, and probably not even
reproducible if the same processes are run again. Thus, processes are an activity of
some kind.
• It has a program, input, output and a state. A single processor may be shared among
several processes, with some scheduling algorithm being used to determine when to
stop work on one process and service a different one.
• A process may be in one of the following states depending on the current activity of
the process:
1. New: A process is said to be new state if it is being created.
2. Ready: A process is said to be ready state if it is ready for the execution and
waiting for the CPU to be allocated to it.
3. Running State: A process is said to be in running state if the CPU has been
allocated to it and it is currently being executed.
4. Waiting or Blocked: A process is said to be in waiting state if it has been blocked
by some event. Unless that event occurs the process cannot continue it execution.
5. Terminated: A process is said to be in waiting state if it has completed its
execution normally or it has been terminated abnormally by the OS because of
some error or killed by some other processes.
New
admitted Interrupt Terminated
exit
Ready Running
Scheduler dispatch
I/O or Event completion I/O or Event wait
Waiting
• A PCB stores descriptive information pertaining to a process, such as its state, program
counter, memory management information, information about its scheduling,
allocated resources, accounting information, etc. that is required to control and
manage a particular process.
• The basic purpose of PCB is to indicate the so far progress of a process. Fig. 2.4 shows a
typical PCB.
• The PCB is a data structure with fields for recording the various aspects of process
execution and the usage of resources.
• In simple words, the operating system maintains the information about each process
in a record or a data structure called Process Control Block (PCB).
Process
Pointer
State
Process Number
Program Counter
CPU Registers
Memory Allocations
Event Information
List of Open Files
•
•
•
Fig. 2.4: Process Control Block (PCB)
• Process Control Block (PCB) is a data structure that stores information about a
particular process. A PCB keeps all the information needed to keep track of a process
as listed below:
1. Pointer: The pointer has an address of the next PCB, whose process state is ready.
2. Process State: Process state specifies the current state of the process. The current
state of the process may be new, ready, running, waiting, terminated and so on.
3. Process Number: Each process is identified by its process number called Process
Identification Number (PID). The process-id is provided by the operating system.
4. Priority: The process is assigned the priority at the time of its creation. Process
with the highest priority is allocated the CPU first among all the processes.
5. Program Counter: It is a pointer indicates the address of the next instruction to be
executed for the process.
6. CPU Registers: Various CPU registers like accumulators, index registers, stack
pointers, and general-purpose register etc. where process need to be stored for
execution for running state.
7. CPU Scheduling Information: This includes process priority and other scheduling
information which is required to schedule the process.
2.5
Operating Systems - I Processes and Threads
• Process scheduling is used in determining which processes run when there are
multiple runnable processes.
• The operating system maintains the following important process scheduling queues:
1. Job queue keeps all the processes in the system. As the process enter the system, it
is put into a job queue.
2. Ready queue keeps a set of all processes residing in main memory, ready and
waiting to execute. A new process is always put in this queue. [April 19]
3. A process may have an input/output request, and the device requested may be
busy. In such case, the input/output request in maintained is the device queue. The
list of processes waiting for a particular I/O device is called a Device queue.
Queue Header PCB7 PCB2
Ready Head
Queue Registers Registers
Tail
• •
• •
• •
Mag Head
Tape
Unit 0 Tail
Mag Head
Tape
Unit 1 Tail PCB3 PCB14 PCB6
Disk Head
Unit 0 Tail
PCB5
Terminal Head
Unit 0 Tail
•
•
•
2. The process could issue an I/O request and then be placed in an I/O queue.
3. The process could be removed forcibly from the CPU, as a result of an interrupt,
and again put in the ready queue.
• In the first two cases, the process transition from the waiting state to ready state
occurs. A process continues this cycle until it terminates.
• When the process terminates, it is removed from all queues and its PCB and resources
are de-allocated.
Time slice
expired
• Schedulers are special system software which handles process scheduling in various
ways.
• The main task of scheduler is to select the jobs to be submitted into the system and to
decide which process to run.
• In other words, the job of process scheduling is done by a software routine (module)
called as scheduler.
• There are three different types of scheduler as shown in Fig. 2.7 namely, long-term
scheduler, medium-term scheduler and short-term scheduler.
• In Fig. 2.7 initially process is maintained in the Batch queue. Then, job of the long-term
scheduler is to fetch the process from batch queue to ready queue (main memory).
• Then, short-term scheduler selects process from the ready queue and assigns them to
CPU. When process completes their task, then, process will end or terminate.
• During that period, some processes may ask for I/O device so kept in I/O waiting
queue. After that I/O completion, they are moved back to ready queue.
• Now, consider process is executing and due to time slice process is suspended so it will
be swap out from CPU and it will be kept in suspended and swapped out queue.
• Once swap out is done, it will be swap in ready queue. Thus scheduling is done by mid-
term scheduler and this completes job done in time-sharing system.
2.8
Operating Systems - I Processes and Threads
Medium term
scheduler Suspended and
swapped out queue
Long term
scheduler
Batch Short term
jobs scheduler
Ready CPU Exit
Batch queue
queue
Interactive Suspended
programs Medium term queue
scheduler
Fig. 2.7: Types of Schedulers
• The types of schedulers are explained below:
1. Long-term Scheduler (Job Scheduler or Admission Scheduler):
• The long-term scheduler selects the job or process to be executed from job pool on a
secondary storage device and loads them into memory for execution. The long-term
scheduler executes less frequently.
• The long-term scheduler is invoked when the process leaves the system. Because of the
longer duration between executions.
• The long-term scheduler can afford to take more time to decide which process should
be selected for execution.
• A long-term scheduler determines which programs are admitted to the system for
processing.
• A long-term scheduler selects processes from the queue and loads them into memory
for execution. Process loads into the memory for CPU scheduling.
• The primary objective of the job scheduler is to provide a balanced mix of jobs, such as
I/O bound and processor bound.
• A long-term scheduler also controls the degree of multiprogramming. If the degree of
multiprogramming is stable, then the average rate of process creation must be equal
to the average departure rate of processes leaving the system.
2. Short-term Scheduler (CPU Scheduler or Process Scheduler):
• Short-term scheduler selects a job from ready queue and submits it to CPU. As the
short-term scheduler selects only one job at a time, it is invoked very frequently.
• The main objective of short-term scheduler is to increase system performance in
accordance with the chosen set of criteria.
• It is the change of ready state to running state of the process. CPU scheduler selects a
process among the processes that are ready to execute and allocates CPU to one of
them.
2.9
Operating Systems - I Processes and Threads
• Short-term schedulers, also known as dispatchers, make the decision of which process
to execute next. Short-term schedulers are faster than long-term schedulers.
• In case of input/output bound jobs as the ready queue is almost empty, short-term
scheduler has very less work to do.
• The systems like time sharing do not have long-term scheduler. The jobs are placed
directly in the ready queue for the short-term scheduler. But such some systems have
an additional type of scheduler called as medium-term scheduler.
3. Medium-term Scheduler: [Oct. 16]
• Some OSs, such as time-sharing systems, may introduce an additional, intermediate
level of scheduling (medium term scheduler). Medium term scheduling is a part of
swapping so it is also known swapper.
• Swapping is term associated with the medium-term scheduler by which processes are
temporarily removed and then brought back to the ready queue.
• The medium term scheduler temporarily removes processes from main memory and
places them on secondary memory (such as a disk drive) or vice versa. This is
commonly referred to as "swapping out" or "swapping in" (Refer Fig. 2.8).
• Medium-term scheduling removes the processes from the memory. It reduces the
degree of multiprogramming.
I/O waiting
I/O
queues
Interrupt or
system call
Save State into PCB1
Reload state from PCB2
Interrupt or
system call
Save State into PCB2
Reload state from PCB1
Fig. 2.9
2.11
Operating Systems - I Processes and Threads
2.12
Operating Systems - I Processes and Threads
Idle
Idle
Interrupt or System call Executing
Executing
2.13
Operating Systems - I Processes and Threads
• In UNIX operating system process creation is achieved through the fork() system call.
The fork() system call creates a child process and sets up its execution environment,
then it allocates an entry in the process table i.e. a PCB for the newly created process
and marks its state as ready.
• The newly created process is called the child process and the process that initiated it
(or the process when execution is started) is called the parent process.
• A child process can have only one parent, but a parent process can have multiple child
processes.
• Also, both the parent and child processes have the same memory, open files, and
environment strings, but they have distinct address spaces.
• When a process creates a new process, two possibilities exist in terms of execution:
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have terminated.
• When a process creates a new process, there are two possibilities in terms of address
space of the new process.
1. The child process has the same program and data as parent.
2. The child process has new program loaded into it.
• Fig. 2.11 shows process creation using fork() system call.
Parent Process
(pid>0)
wait() Parent Resumes
Execution
exec() exit()
Child Process
(pid = 0)
Fig. 2.11: Creating a Process using the UNIX fork() System Call
• After creation of the child process, let us see the fork() system call details.
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
• Creates the child process. After above call, there are two processes, the existing one is
called the parent process and the newly created one is called the child process.
• The fork() system call returns either of the following three values:
1. Negative value to indicate an error, i.e., unsuccessful in creating the child process.
2. Returns a zero for child process.
3. Returns a positive value for the parent process. This value is the process ID of the
newly created child process.
2.14
Operating Systems - I Processes and Threads
if (pid == 0) {
printf("This is child process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
} else { // Parent process
sleep(2);
printf("This is parent process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
printf("Newly created process id or child pid is %d\n", pid);
}
return 0;
}
Output:
Before fork: Process id is 166629
This is child process
Process id is 166630 and PPID is 166629
Before fork: Process id is 166629
This is parent process
Process id is 166629 and PPID is 166628
Newly created process id or child pid is 166630
EXECLP System Call:
• The EXECLP system call is used after a FORK call by one of the two processes to replace
the processes memory space with a new program.
• This call loads a binary file into memory and starts its execution. So two processes can
be easily communicates with each other.
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
int main()
{
int pid;
/* fork a child process */
pid = fork();
2.16
Operating Systems - I Processes and Threads
if (pid< 0)
{ /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0){ /* child process */
execlp("/bin/wc","wc",NULL);
}
else{ /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
}
return 0;
}
NICE System Call:
• Using NICE system call, we can change the priority of the process in multi-tasking
system. The new priority number is added to the already existing value.
Example:
#include<stdio.h>
main()
{
int pid, retnice
print("press DEL to stop process \n");
pid-fork();
for(::)
{
if(pid == 0)
{
retnice = nice (− 5);
print("child gets higher CPU priority %d \n", retnice);
sleep(1);
}
2.17
Operating Systems - I Processes and Threads
else
{
retnice=nice(4);
print("Parent gets lower CPU priority %d \n", retnice);
sleep(1);
}
}
}
SIGCLD System Call:
• When the process is terminated it should be removed from the process table and shell
should know that the process has died.
• Whenever process is died, it intimates the parent by signal sigcld.
Example:
#include<stdio.h>
#include<signal.h>
void abc()
int pid
main()
{
pid = fork();
if(pid == 0)
else
{
signal (SIGCLD, abc); //signal function to handle SIGCLD.signal
for(i=0; i<1000; i++)
print(%d, i)
print("parent existing \n");
}
}
void abc()
{
print("child died \n");
getchar();
}
EXEC System Call:
• The EXEC system call is used to execute a file which is residing in an active process.
When EXEC is called the previous executable file is replaced and new file is executed.
2.18
Operating Systems - I Processes and Threads
• By using exec system call, we will replace the old file or program from the process
with a new file or program.
• The entire content of the process is replaced with a new program. The new program is
loaded into the same process space.
• The current process is just turned into a new process and hence the process id PID is
not changed, this is because we are not creating a new process we are just replacing a
process with another process in exec.
• The exec system call is a collection of functions and in C programming language, the
standard names for these functions are as follows:
1. execl: l is for the command line arguments passed a list to the function
2. execle: It is an array of pointers that points to environment variables and is
passed explicitly to the newly loaded process.
3. execlp: p is the path environment variable which helps to find the file passed as
an argument to be loaded into process.
4. execv: v is for the command line arguments. These are passed as an array of
pointers to the function.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("PID of example.c = %d\n", getpid());
char *args[] = {"Hello", "C", "Programming", NULL};
execv("./hello", args);
printf("Back to example.c");
return 0;
}
2. Error Exit: Another reason for termination is that process finds fatal error,
suppose user types the command sample.c and no such files present, the compiler
simply exits.
3. Fatal Error: The third reason for termination is an error caused by the process,
often due to a program bug. Examples include executing an illegal instruction,
referencing nonexistent memory, or dividing by zero. In some systems
(e.g., UNIX), a process can tell the operating system that it wishes to handle certain
errors itself, in which case the process is signaled (interrupted) instead of
terminated when one of the errors occurs.
4. Killed by another Process: The fourth reason a process might terminate is that
the process executes a system call telling the operating system to kill some other
process. In UNIX this call is kill. The corresponding Win32 function is Terminate
Process.
• Consider the following example program with exit() system call:
#include <stdio.h>
#include <stdlib.h>
void exitfunc() {
printf("Called cleanup function - exitfunc()\n");
return;
}
int main() {
atexit(exitfunc);
printf("Hello, World!\n");
exit (0);
}
Output:
Hello, World!
Called cleanup function - exitfunc()
• The process may return a status value (typically an integer) to its parent process (via
the wait() system call). It also can terminate a child process by parent process
issuing kill() system call.
• The kill() system call can occur when the child has exceeded allocated resources, task
assigned to the child is no longer required, or the parent is exiting and the operating
system does not allow a child to continue if its parent terminates.
2.20
Operating Systems - I Processes and Threads
2.21
Operating Systems - I Processes and Threads
• Competition for the CPU with SCS scheduling takes place among all threads in the
system. Systems using the one-to-one model (such as Windows XP, Solaris 9, and
Linux) schedule threads using only SCS.
• Typically, PCS is done according to priority—the scheduler selects the runnable thread
with the highest priority to run.
• User-level thread priorities are set by the programmer and are not adjusted by the
thread library, although some thread libraries may allow the programmer to change
the priority of a thread.
Pthread Scheduling:
• Now, we highlight the POSIX Pthread API that allows specifying either PCS or SCS
during thread creation.
• POSIX (Portable Operating System Interface) is a set of standard operating
system interfaces based on the Unix operating system.
• Pthread identifies the following contention scope values:
o PTHREAD_SCOPE_PROCESS schedules threads using PCS scheduling.
o PTHREAD_SCOPE_SYSTEM schedules threads using SCS scheduling.
• On systems implementing the many-to-many model, the PTHREAD_SCOPE_PROCESS
policy schedules user-level threads onto available LWPs.
• The number of LWPs is maintained by the thread library, perhaps using scheduler
activations.
• The PTHREAD_SCOPE_SYSTEM scheduling policy will create and bind an LWP for each
user-level thread on many-to-many systems, effectively mapping threads using the
one-to-one policy.
2.22
Operating Systems - I Processes and Threads
• The Fig. 2.12 shows the single threaded and multithreaded processes.
Thread
Thread
Kernel space
CPU
User space
Kernel space
CPU
2.25
Operating Systems - I Processes and Threads
2.26
Operating Systems - I Processes and Threads
Kernel thread
• Only one thread can access the kernel at a time. Multiple threads are unable to run in
parallel on multiprocessors.
• Green threads a thread library available for Solaris 2- uses many-to-one thread model.
• Fig. 2.16 shows many-to-one model concept.
User thread
K Kernel thread
Kernel threads
Advantages:
1. Many threads can be created as per user’s requirement.
2. Provides the best accuracy on concurrency.
3. When a thread performs a blocking system call, the kernel can schedule another
thread for execution.
4. Multiple kernel or equal to user threads can be created.
Disadvantages:
1. Multiple threads of kernel are an overhead for OS.
2. Low performance.
3. True concurrency cannot be achieved.
2.29
Operating Systems - I Processes and Threads
PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. Which is a program in execution?
(a) Thread (b) Process
(c) Stack (d) Heap
2. Which is the segment of a process?
(a) Thread (b) Process
(c) Stack (d) Heap
3. The process of saving the context of one process and loading the context of another
process is known as,
(a) CPU scheduling
(b) Context switching
(c) Link to interface the hardware and application programs
(d) All of the above mentioned
4. Which of the following information is stored in process control block to support
process context switching?
(a) The date and time of switching
(b) UID
(c) PID
(d) Terminal from where the process was initiated
5. The address of the next instruction to be executed by the current process is
provided by the,
(a) CPU registers (b) Program counter
(c) Process stack (d) Pipe
6. A Process Control Block (PCB) does not contain which of the following?
(a) Code (b) Stack
(c) Bootstrap program (d) Data
7. In Unix Which system call creates the new process?
(a) fork (b) create
(c) new (d) none of the mentioned
8. A process can be terminated due to,
(a) Normal exit (b) fatal error
(c) Killed by another process (d) all of the mentioned
9. What is the ready state of a process?
(a) When process is scheduled to run after some execution
(b) When process is unable to run until some task has been completed
(c) When process is using the CPU
(d) None of the above mentioned
10. A set of processes is deadlock if,
(a) Each process is blocked and will remain so forever
(b) Each process is terminated
(c) All processes are trying to kill each other
(d) None of the above mentioned
2.30
Operating Systems - I Processes and Threads
2.31
Operating Systems - I Processes and Threads
2.32
Operating Systems - I Processes and Threads
33. Which of the following need not necessarily be saved on a context switch between
processes?
(a) General purpose registers (b) Translation look aside buffer
(c) Program counter (d) All of the above mentioned
34. Which of the following does not interrupt a running process?
(a) A device (b) Timer
(c) Scheduler process (d) Power failure
35. A thread is also called as ________.
(a) Heavy Weight Process (HWP) (b) Light Weight Process (LWP)
(c) Both (a) & (b) (d) None of the above mentioned
36. In which model one to one relationship of user level thread to the kernel level
thread i.e. each user threads maps to a kernel thread.
(a) One to Many (b) Many to One
(c) Many to Many (d) One to One
37. Which of the following operating system not uses the kernel level thread?
(a) Windows XP (b) MS-DOS
(c) Linux (d) Solaris
38. Process Control Block (PCB) contains which of the following,
(a) list of open and close files (b) process state
(c) process number (d) All of the mentioned
(e) All of the above mentioned
39. The address of the next instruction to be executed for the current process is stored
in ________.
(a) CPU registers (b) program counter
(c) process state (d) process number
40. A process's current activity is represented by ________.
(i) program counter (ii) contents of the processor's registers
(iii) stack (iv) data section
(a) 1 (b) 1, 2
(c) 1, 2, 3 (d) 1, 2, 3, 4
41. A process generally also includes the process ________, which contains temporary
data (such as function parameters, return addresses and local variables).
(a) text section (b) program counter
(c) stack (d) data section
42. A process generally also includes the process ________, which contains global
variables.
(a) heap (b) program counter
(c) stack (d) data section
2.33
Operating Systems - I Processes and Threads
2.34
Operating Systems - I Processes and Threads
60. Which of the following components of program state are shared across threads in
a multithreading process?
(a) Register values (b) Heap memory
(c) Global variables (d) Stack memory
(e) Both (b) and (c)
Answers
1. (b) 2. (a) 3. (b) 4. (c) 5. (b) 6. (c) 7. (a) 8. (d) 9. (a) 10. (a)
11. (d) 12. (a) 13. (b) 14. (d) 15. (b) 16. (b) 17. (b) 18. (d) 19. (a) 20. (b)
21. (a) 22. (a) 23. (a) 24. (c) 25. (c) 26. (b) 27. (c) 28. (a) 29. (b) 30. (c)
31. (b) 32. (d) 33. (b) 34. (c) 35. (b) 36. (d) 37. (b) 38. (e) 39. (b) 40. (b)
41. (c) 42. (d) 43. (d) 44. (c) 45. (a) 46. (c) 47. (d) 48. (c) 49. (c) 50. (a)
51. (a) 52. (a) 53. (a) 54. (a) 55. (d) 56. (d) 57. (d) 58. (d) 59. (c) 60. (e)
Q.II Fill in the Blanks
1. A _______ is a program in execution.
2. The execution of a process must progress in a _______ fashion.
3. The current activity of a process is known as its _______.
4. The operating system maintains all PCBs in process _______ queues.
5. A PCB is a _______ in an operating system kernel that contains the information
needed to keep track of a process.
6. A _______ is also referred as job of batch system or task of time-sharing system.
7. The PCB is identified by an integer _______.
8. A process is a program that performs a _______ thread of execution.
9. _______ queue helps to store all the processes in the system.
10. The _______ scheduler, or admission scheduler, decides which jobs or processes are
to be admitted to the ready queue (in main memory).
11. In general, most processes can be described as either I/O bound or _______ .
12. An _______ process is one that spends more of its time doing I/O than it spends
doing computations.
13. A _______ process, in contrast, generates I/O requests infrequently, using more of its
time doing computations.
14. The threads implemented at kernel level are known as _______ threads.
15. The _______ scheduler temporarily removes processes from main memory and
places them in secondary memory (such as a hard disk drive)
16. The _______ scheduler (also known as the CPU scheduler) decides which of the
ready, in-memory processes is to be executed (allocated a CPU) after a
clock interrupt, an I/O interrupt, an operating system call or another form
of signal.
17. A _______ is an execution unit that has its own program counter, a stack and a set of
registers that reside in a process.
18. A _______ switch is the process of storing the state of a process.
19. When more than one process is runnable, the operating system must decide which
one first. The part of the operating system concerned with this decision is called
the _______.
2.36
Operating Systems - I Processes and Threads
20. The _______ scheduler is the component of the operating system that decide which
process should run next, which process should continue in running state.
21. _______, the threads extension of the POSIX standard, may be provided as either a
user- or kernel-level library.
22. Switching the CPU to another process requires saving the state of the old process
and loading the saved state for the new process, this task is known as a _______
switch.
23. The context of a process is represented in the Process Control Block (PCB) of a
_______.
24. A process may be created by another process using the _______ method.
25. The _______ system call is used to terminate a process.
26. Scheduling of threads involves two boundary scheduling using _______ level
threads to _______ level threads.
27. The threads implemented at the user level are known as _______ threads.
28. A thread _______ provides the programmer an API for creating and managing
threads.
29. The model in which one kernel thread is mapped to many user-level threads is
called as ________ model.
30. The model in which one user-level thread is mapped to many kernel level threads
is called as ________ model.
Answers
1. process 2. sequential 3. state 4. scheduling
5. data structure 6. process 7. process ID (PID) 8. single
9. Job 10. long term 11. CPU bound 12. I/O-bound
13. CPU-bound 14. kernel 15. medium term 16. short term
17. thread 18. context 19. scheduler 20. process
21. Pthreads 22. context 23. process 24. fork()
25. exit() 26. user, kernel 27. user 28. library
29. Many to one 30. One to many
Q.III State True or False:
1. A process is an instance of a program execution.
2. The thread contains text section, data section, heap and stack.
3. Ready queue is a type of queue helps you to set every process residing in the main
memory, which is ready and waiting to execute.
4. The process scheduler is a part of the operating system that decides which process
runs at a certain point in time.
5. The medium term scheduler also called as CPU Scheduler.
6. An efficient scheduling system will select a good process mix of CPU-bound
processes and I/O bound processes.
7. Processes waiting for a device to become available or to deliver data are placed
in device queues.
2.37
Operating Systems - I Processes and Threads
8. Process scheduling is determining which process moves from ready state to the
running state.
9. A thread library provides the programmer an API for creating and managing
threads.
10. The Win32 thread library is a kernel-level library available on Windows systems.
11. A long term scheduler is typical of a batch system.
12. A context switch enables multiple processes to share a single CPU.
13. A process is a program in execution.
14. Each process is represented in the operating system by a process context switch.
15. The processes that are residing in main memory and are ready and waiting to
execute are kept on a list called the ready queue.
16. A process may be created by another process using the exit() method.
17. Switching the CPU to another process requires performing a state save of the
current process and a state restore of a different process. This task is know as
a context switch.
18. Switching the CPU from one process to another process requires saving the state of
old process and loading the saved state of new process. This task is known as
Context Switch.
19. The exit() system call is used to terminate a process.
20. Context of a process represented in the scheduler.
21. Thread library schedules which thread of a process to run on which Light-Weight
Process (LWP) and how long.
22. The long term scheduler selects which process to remove from memory by
swapping.
Answers
1. (T) 2. (F) 3. (T) 4. (T) 5. (T)
6. (F) 7. (T) 8. (T) 9. (T) 10. (T)
11. (T) 12. (T) 13. (T) 14. (F) 15. (T)
16. (F) 17. (T) 18. (T) 19. (T) 20. (F)
21. (T) 22. (F)
Q.IV Answer the following Questions:
(A) Short Answer Questions:
1. What is process?
2. What is thread?
3. What is PCB?
4. What is context switch?
5. What is scheduler
6. What is meant by scheduling queue?
7. List types of scheduling queue.
8. Define scheduler.
9. List operations on processes.
2.38
Operating Systems - I Processes and Threads
October 2017
1. List any two operating system examples that uses one to one model. [1 M]
Ans. Refer to Section 2.4.3.1.
2. Give examples of operating system with user Pthread. [1 M]
Ans. Refer to Section 2.4.4.
3. What is context switching? State conditions when context switching occurs. [5 M]
Ans. Refer to Section 2.2.3.
4. State two benefits of multithreaded programming. [2 M]
Ans. Refer to Section 2.4.2.
April 2018
1. Differentiate between user level thread and kernel level thread. [2 M]
Ans. Refer to Page 2.26.
October 2018
1. List any two advantages of multithreaded programming. [1 M]
Ans. Refer to Section 2.4.2.
2. Write a short note on process states. [5 M]
Ans. Refer to Section 2.1.2.
3. What do you understand by a thread? [1 M]
Ans. Refer to Section 2.4.1.
April 2019
1. What is ready queue? [1 M]
Ans. Refer to Section 2.2.1 Point (2).
2. Which scheduler controls the degree of multiprogramming? [1 M]
Ans. Refer to Section 2.2.2.
3. What is process? Explain the different types of process states. [5 M]
Ans. Refer to Sections 2.1.1 and 2.1.2.
4. Explain many-to-many multithreading model. [2 M]
Ans. Refer to Section 2.4.2.
■■■
2.40
CHAPTER
3
Process Scheduling
Objectives …
To understand Concept of CPU Scheduling
To learn Concept of Scheduler and Criteria for Scheduling Algorithm
To study various CPU Scheduling Algorithms like FCFS, SJF, RR, etc.
To learn Multiple Queue Scheduling and Multilevel Feedback Queue Scheduling
3.0 INTRODUCTION
• The assignment of physical processors to processes allows processors to accomplish
work. In multi-programming environment, many processes are in memory.
• The problem of determining when processors should be assigned and to which
processes is called processor scheduling or CPU scheduling.
• When more than one process is run able, the operating system must decide which one
first. The part of the operating system concerned with this decision is called the
scheduler, and algorithm it uses is called the scheduling algorithm.
• Process scheduling is an essential part of multiprogramming operating systems which
allow more than one process to be loaded into the executable memory at a time and
the loaded process shares the CPU using time multiplexing.
3.1
Operating Systems - I Process Scheduling
3.2
Operating Systems - I Process Scheduling
• The algorithm used by the scheduler to carry out the selection of a process for
execution is known as scheduling algorithm.
• A number of scheduling algorithms are available for CPU scheduling such as FCFS, RR
etc. Each and every scheduling algorithm influences the resource utilization, overall
system performance, and quality of service provided to the user.
3.1.1 CPU and I/O Burst Cycle [April 16, 18, Oct. 17]
• CPU scheduling is greatly affected by how a process behaves during its execution.
Almost all the processes continue to switch between CPU (for processing) and I/O
devices (for performing I/O) during their execution.
• The success of CPU scheduling depends upon the observed property of processes such
as process execution is a cycle of CPU execution and I/O waits.
• Processes alternate back and forth between these two states. Process execution begins
with a CPU burst. It is followed by an I/O burst, which is followed by another CPU
burst, then another I/O burst and so on.
• Eventually the last CPU burst will end with a system request to terminate execution,
rather than another I/O burst.
•
•
•
load store
add store CPU burst
read from file
store increment
index CPU burst
write to file
load store
add store CPU burst
read from file
•
•
•
Fig. 3.3: Execution is an Alternating Sequence of CPU and I/O Bursts
• In short, we can say that the process execution comprises alternate cycles of CPU burst
(the time period elapsed in processing before performing the next I/O operation) and
I/O burst (the time period elapsed in performing I/O before the next CPU burst).
3.3
Operating Systems - I Process Scheduling
• For scheduling purposes, the scheduler may consider some performance measures
and optimization criteria. Different CPU scheduling algorithms have different
properties and may favor one class of processes over another.
• In choosing which algorithm to use in a particular situation/condition, the properties
of various algorithms must be considered. Scheduling algorithm is one which selects
some job from ready queue based on some criteria and submits it to the CPU.
• Many criteria have been suggested for comparing CPU scheduling algorithms criteria,
which are used include CPU utilization, balanced utilization, throughput, waiting time,
turnaround time and response time.
1. CPU Utilization: CPU utilization is defined as, the percentage of time the CPU is
busy in executing processes. For higher utilization, CPU must be kept as busy as
possible, that is, there must be some process running at all times. CPU Utilization
may range from 0 to 100 percent. In a real system it should range from 40 percent
to 90 percent.
2. Throughput: If the CPU is busy executing processes, then work is being done. One
measure of work is the number of processes that are completed per time unit
called throughput. For long processes, this rate may be one process per hour, for
short transactions, throughput might be 10 processes per second. Throughput is
defined as, the total number of processes that a system can execute per unit of
time.
3. Turnaround Time (TAT): It is the difference between the time a process enters
the system and the time it exits the system. From the point of view of a particular
process, the important criterion is how long it takes to execute that process. The
interval from the time of submission of a process to the time of completion is the
turnaround time. Turnaround Time is the sum; of the periods spent waiting to get
into memory writing in the ready queue, executing on the CPU and doing I/O.
Turnaround time is defined as, the amount of time that has rolled by from the
time of creation to the termination of a process. [April 16]
4. Waiting Time: The CPU scheduling algorithm does not affect the amount of time
during which a process executes of does I/O, if affects only the amount of time that
a process spends waiting in the ready queue. Waiting time is the sum of the
periods spent waiting in the ready queue. Waiting time is defined as, the time
spent by a process while waiting in the ready queue.
3.4
Operating Systems - I Process Scheduling
5. Balanced Utilization: Balanced utilization is defined as, the percentage of time all
the system resources are busy. It considers not only the CPU utilization but the
utilization of I/O devices, memory, and all other resources. To get more work done
by the system, the CPU and I/O devices must he kept running simultaneously. For
this, it is desirable to load a mixture of CPU-bound and I/O-bound processes in the
memory.
6. Response Time: Response time is defined as, the time elapsed between the
moment when a user initiates a request and the instant when the system starts
responding to this request. In an interactive system TAT (Turnaround Time) may
not be best criterion. Often a process can produce some output early and can
continue computing new results while previous results are being output to the
user. Thus another measure is the time from submission of a request until the first
response is produced. This measure called response time is the amount of time it
takes to start responding, but not the time that it takes to output that response. The
Turnaround Time is generally limited by the speed of the output device.
3.6
Operating Systems - I Process Scheduling
• Dispatcher is a component which involves in the CPU scheduling. When the scheduler
completes its job of selecting a process, it is the dispatcher which takes that process to
the desired state/queue.
• The dispatcher is the module that actually gives control of the CPU to the process
selected by the short term scheduler.
• The module of the operating system that performs the function of setting up the
execution of the selected process on the CPU is known as dispatcher.
• The CPU scheduler only selects a process to be executed next on the CPU but it cannot
assign CPU to the selected process.
3.7
Operating Systems - I Process Scheduling
• The function of setting up the execution of the selected process on the CPU is
performed by another module of the operating system, known as dispatcher.
• The following are functions performed by dispatcher:
1. Loading the register of the process.
2. Switching operating system to the user mode.
3. Restart the program by jumping to the proper location in the user program.
CPU
Scheduler Dispatcher
Queued
processes
Fig. 3.4: Concept of Dispatcher
• The time taken by dispatcher to stop one process and start another process to run is
called dispatch latency time. [April 16, 18, 19 Oct. 16]
P0
executing
Save state
into PCB0
Dispatch
latency
Restore state
from PCB1
P1
executing
3.8
Operating Systems - I Process Scheduling
3.9
Operating Systems - I Process Scheduling
P1 P2 P3 P4
0 8 12 21 26
Waiting Time Turnaround Time
Waiting time of Process P1 = 0 TAT of process P1 = 8
Waiting time of Process P2 = 8 TAT of process P2 = 12
Waiting time of Process P3 = 12 TAT of process P3 = 21
Waiting time of Process P4 = 21 TAT of process P4 = 26
Average Waiting Time = (0 + 8 + 12 + 21)/4 = Average TAT = (8 + 12 + 21 + 26)/4 = 67/4
41/4 = 10.25 msec. = 16.75 msec.
3.10
Operating Systems - I Process Scheduling
Example 2: Consider the four jobs are scheduled for execution. Find the average
waiting time and turnaround time.
Process Arrival Time CPU Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Solution: Gantt Chart:
P1 P2 P3 P4
0 8 12 21 26
Waiting Time Turnaround Time
Waiting time = Starting time − Arrival time TAT = Ending Time − Arrival Time
Waiting time of Process P1 = 0 − 0 = 0 TAT of process P1 = 8 − 0 = 8
Waiting time of Process P2 = 8 − 1 =7 TAT of process P2 = 12 − 1 = 11
Waiting time of Process P3 = 12 − 2 = 10 TAT of process P3 = 21 − 2 = 19
Waiting time of Process P4 = 21 − 3 = 18 TAT of process P4 = 26 − 3 = 23
Average Waiting Time = (0 + 7 + 10 + 18)/4 = Average TAT = (8 + 11 + 19 + 23)/4 = 61/4 =
35/4 = 8.75 msec. 15.25 msec.
Advantages:
1. FCFS is easier to understand and implement as processes are simply to be added at
the end and removed from the front of queue. No process from in between the
queue is required to be accessed.
2. FCFS is well suited for batch systems where the longer time periods for each
process arc often acceptable.
Disadvantages:
1. Average waiting time is very large.
2. FCFS is not an attractive alternative on its own for a single processor system.
3. Another difficulty with FCFS tends to favor processor bound. Processes over I/O
bound processes and may result in inefficient use of both the processor and the I/O
devices.
3.2.2 SJF (Shortest Job First) Scheduling Algorithm [Oct. 17, April 18]
• The SJF scheduling algorithm is also knows as Shortest Process Next (SPN) scheduling
algorithm or Shortest Request Next (SRN) scheduling algorithm that schedules the
processes according to the length of the CPU burst they require.
• In SJF algorithm, the process with the shortest expected processing time is assigned to
CPU. Hence, the name is shortest job first.
• Jobs or processes are processed in the ascending order of their CPU burst times. Every
time the job with smallest CPU burst-time is selected from the ready queue.
3.11
Operating Systems - I Process Scheduling
• If the two processes having same CPU burst Time then they will be scheduled
according to FCFS algorithm. Performance of this algorithm is very good in
comparison to FCFS.
• The SJF scheduling algorithm is probably optimal; it gives minimal average waiting
time for a given set of processes.
• By moving short process before a long one, the waiting time of short process
decreases. Consequently average waiting time reduces.
• In SJF scheduling algorithm the process in the ready queue with the shortest expected
processing time is assigned to CPU next. SJF can be evaluated in two different
manners:
1. Non pre-emptive SJF: In this method, if CPU is executing one job, it is not stopped
in between before completion.
2. Pre-emptive SJF: In this method, while CPU is executing a job, if a new job arrives
with smaller burst time, then the current job is pre-empted (sent back to ready
queue) and the new job is executed. It is also called Shortest Remaining Time First
(SRTF).
Example: Consider following table, find turnaround time and wait time.
Jobs Burst Time Arrival Time
J1 4 0
J2 1 1
J3 2 2
J4 1 3
1. Non-preemptive SJF:
o At time unit only one job (J1) is in the ready queue. So we must start with J1.
o J2 and J4 has same burst times, therefore by applying FCFS we will consider J2
before J4.
o As it is non-pre-emptive, J1 will not be pre-empted before completion.
Gantt Chart:
J1 J2 J4 J3
0 4 5 6 8
3.12
Operating Systems - I Process Scheduling
2. Pre-emptive SJF:
At zero time units there is only one job in the ready queue. Therefore we must start
with J1. At one time unit a new job J2 arrives with smaller burst time therefore J1 will
be pre-empted and J2 will complete its execution.
At two time units J3 arrives and CPU starts executing it. J3 has a burst time of two units.
After one unit of it is over (i.e. at 3 time units) J4 arrives with 1 as burst time.
Now if we compare balance burst time of J3 (i.e. 2 – 1) and burst time of J4 (i.e. 1) both
is exactly same. Applying FCFS, J3 will continue. At the end J4 will be executed and then
balance of J1.
Gantt Chart:
Brought back for balance execution
↓
J1 J2 J3 J3 J4 J1
0 1 2 3 4 5 8
↓
Pre-empted
Jobs Turnaround Time Wait Time
J1 (8 – 0) = 8 (0 – 0) + (5 – 1) = 4
J2 (2 – 1) = 1 (1 – 1) = 0
J3 (4 – 2) = 2 (2 – 2) = 0
J4 (5 – 3) = 2 (4 – 3) = 1
13 5
13
Average Turnaround Time (TAT) = 4 = 3.25 msec.
5
Average wait time = 4 = 1.25 msec.
• In the above mentioned example J1 started its execution at zero time units but as it was
pre-empted, it's execution was over at 8 time units. Therefore, turnaround time for J1
is 8 – 0 = 8.
• J1 is arrived at zero time units and immediately started executing. Therefore, wait
time for J1 is (0 – 0) = 0.
• But when J1 was pre-empted, it was again waiting in the ready queue for CPU. This
time (5 – 1) if added to previous wait time.
∴ Total wait time of J1 = (0 – 0) + (5 – 1) = 4.
Example: Consider the following processes, with the CPU burst time given in
milliseconds. Calculate average waiting time and turnaround time. (Consider all jobs
arrived at same time).
Process Burst Time
P1 6
P2 8
P3 7
P4 3
3.13
Operating Systems - I Process Scheduling
3.14
Operating Systems - I Process Scheduling
Gantt Chart:
P1 P2 P3 P2 P1
0 1 2 4 8 17
Waiting time for P1 = 8 – 1 = 7 msec.
Waiting time for P2 = 4 – 2 = 2 msec.
Waiting time for P3 = 0 msec.
Average waiting time = (7 + 2 + 0) / 3 = 4.5 msec.
Advantages of SRT Scheduling:
1. SRT scheduling gives superior turnaround time performance to shortest process
next because a short job is given immediate preference to a running longer job.
2. Throughput in SRT scheduling is high.
Disadvantages of SRT Scheduling:
1. In SRT scheduling elapsed time (i.e., execution-completed-time) must be recorded,
it results an additional overhead on the processor.
2. In SRT scheduling starvation may be possible for the longer processes.
Shortest Remaining Time First (SRTF):
• Shortest Remaining Time (SRT) is a pre-emptive version of SJF. As the name suggests, it
selects the process for execution which has the smallest amount of time remaining
until completion.
• In SJF, once a process begins execution, it runs till completion. In SRTF a running
process may be preempted by a user process with a smallest estimated run time.
• SRT also known as Shortest Remaining Time First (SRTF) in which a scheduler chooses
the process that has the shortest remaining processing time.
Example: Consider four processes with the length of CPU burst time given in
milliseconds.
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Solution: Using Pre-emptive Shortest Job First Algorithm:
Gantt Chart:
P1 P2 P4 P1 P3
0 1 5 10 17 26
3.15
Operating Systems - I Process Scheduling
P1 P2 P4 P3
0 8 12 17 26
Waiting Time Turnaround Time
Waiting time of Process P1 = 0 TAT of process P1 = 8
Waiting time of Process P2 = 8 – 1 = 7 TAT of process P2 = 12 – 1 = 11
Waiting time of Process P3 = 17 – 2 = 15 TAT of process P3 = 26 – 2 = 24
Waiting time of Process P4 = 12 – 3 = 9 TAT of process P4 = 17 – 3 = 14
Average Waiting Time = (0 + 7+ 15 + 9)/4 = Average TAT = (8 + 11 + 24 + 14)/4 = 57/4
31/4 = 7.75 msec. = 14.25 msec.
Advantages of SRTF Scheduling:
1. Many processes execute in less amount of time. So, throughput is increased.
2. Processes which have short burst time run fast.
Disadvantages of SRTF Scheduling:
1. Practically it is not possible to predict the burst time.
2. Processes which have long burst time will have to wait for long time for execution.
Shortest Remaining Time Next (SRTN) Scheduling Algorithm:
• SRTN scheduling algorithm is a pre-emptive form of Shortest Job First (SJF) scheduling
algorithm. The shortest remaining time next also known as Shortest Time to Go (STG)
scheduling algorithm.
• SRTN is a scheduling discipline in which the next scheduling entity, a job or a process,
is selected on the basis of the shortest remaining execution time.
• SRTN scheduling may be implemented in either the non-preemptive or the pre-
emptive variety.
• In this algorithm a job is chosen whose remaining run time is the shortest. For a new
job, its run time is compared with remaining time of current job.
• If new job needs less time to complete than the current job, then, the current job is
blocked and the new job is run. It is used for batch systems and provides advantages
to new jobs.
3.16
Operating Systems - I Process Scheduling
• Consider following table, which contains four processes P1, P2, P3 and P4, with their
arrival times and required CPU burst (in milliseconds):
Processes Arrival Time CPU Burst
P1 0 7
P2 1 5
P3 3 2
P4 4 3
Gantt Chart:
P1 P2 P3 P4 P2 P1
0 1 2 4 7 11 17
3.17
Operating Systems - I Process Scheduling
• The SJF algorithm is a special case of the priority scheduling algorithm, where the
priority is the inverse of the next CPU burst, i.e. lower priority is assigned to the larger
CPU burst.
• Priorities are roughly categorized into internal and external priorities as explained
below:
1. Internal priorities are based on burst time, memory requirements, number of
open files etc. measurable quantities.
2. External priorities are human created e.g. seniority, influence of any person, etc.
In our syllabus only internal priorities are considered.
• The priority scheduling can be either pre-emptive or non-pre-emptive. When process
enters into the ready queue, its priority is compared with the priority of the current
running process.
• In a pre-emptive priority scheduling algorithm, at any time, the CPU is allocated one
process and if the priority of the newly arrived process is higher than the priority of
the currently running process, the process which was allocated is interrupted and
return to the queue. The higher priority job is started for execution.
• A non-preemptive priority - scheduling algorithm will simply put the new process at
the head of the ready queue.
• A problem with priority scheduling algorithm is indefinite blocking or starvation.
Here, the low priority process may be indefinitely locked out by the higher priority
processes.
• In general, completion of a process within finite time of its creation cannot be
guaranteed with this scheduling policy. A solution to the problem of starvation of low
priority processes is aging.
• Aging is process in which the priority of each process is gradually increased after the
process spends a certain amount of time in the system.
• Even a process with an initial priority 0, get the higher priority in the system and it
would be executed.
Working of Priority Scheduling Algorithm:
• Fig. 3.7 shows a system with four priority classes.
Runnable processes
Queue headers
Priority 3
Priority 2
3.18
Operating Systems - I Process Scheduling
Note:
• If there are more than one jobs with same priority apply FCFS.
• If along with priorities highest and lowest is not specified, please do not forget to
make your own assumption and specify it before you solve the problem.
3.19
Operating Systems - I Process Scheduling
3.2.4 Round Robin (RR) Scheduling Algorithm [April 16, 19, Oct. 18]
• The Round Robin (RR) scheduling algorithm is designed especially for time sharing
systems. RR is the pre-emptive process scheduling algorithm.
• Round robin scheduling is a preemptive version of First Come First Serve scheduling.
Processes are (FCFS) dispatched in a First In First Out (FIFO) sequence but each
process is allowed to run for only a limited amount of time.
• It is similar to FCFS scheduling but pre-emption is added to switch between processes.
A small unit of time called a Time Quantum or Time Slice is defined.
• A time quantum is generally from 10 to 100 milliseconds. The ready queue is treated
as a circular queue. The CPU scheduler goes around the ready queue, allocating CPU to
each process for a time interval of up to 1 time quantum.
• In RR scheduling processes are dispatched FIFO but are given a limited amount of
processor time called a time slice or a quantum.
3.20
Operating Systems - I Process Scheduling
• If a process does not complete before its quantum expires, the system preempts it and
gives the processor to the next waiting process. The system then places the preempted
process at the back of the ready queue.
• In Fig. 3.8, process P1 is dispatched to a processor, where it executes either until
completion, in which case it exists the system, or until its time slice expires, at which
point is preempted and placed at the tail of the ready queue. The scheduler then
dispatches process P2.
Dispatch
A B C D CPU Completed job
Preemption
Timeout
3.21
Operating Systems - I Process Scheduling
Example 2: Consider the following process arrived at given time. Calculate TAT and
Waiting time of each process, (time quantum=3).
Process Arrival Time Service Time
P1 0 5
P2 1 3
P3 2 8
P4 3 6
Solution: Gantt Chart:
P1 P2 P3 P4 P1 P3 P4 P3
0 3 69 12 14 17 20 22
3.22
Operating Systems - I Process Scheduling
• A multilevel queue scheduling algorithm partitions the ready queue into separate
queues.
• In a multilevel queue scheduling processes are permanently assigned to one queue,
depending upon their properties such as the size of the memory or the type of the
process or priority of the process. So each queue follows a separate scheduling
algorithm.
• In multilevel queue scheduling algorithm scheduling the processes are classified into
different groups such as System processes, Interactive processes, Interactive editing
processes, Batch processes, User processes etc., as shown in Fig. 3.9.
• The interactive processes are known as foreground processes and the batch processes
are known as background processes. These two types of processes have different
response-time requirements and so may have different scheduling needs.
• In addition, foreground processes may have priority (externally defined) over
background processes.
• A multilevel queue scheduling algorithm partitions the ready queue in several
separate queues.
• In Fig. 3.9 each queue has absolute priority over lower-priority queues. No process in
the batch queue, for example, could run unless the queues for system processes,
interactive processes, and interactive editing processes were all empty.
Highest priority
System process
Interactive process
Batch processes
Student processes
Lowest priority
Fig. 3.9
• If an interactive editing process entered the ready queue while a batch process was
running, the batch process would be preempted. Another possibility is to time-slice
among the queues.
• Here, each queue gets a certain portion of the CPU time, which it can then schedule
among its various processes.
• For instance, in the foreground-background queue example, the foreground queue can
be given 80 percent of the CPU time for RR scheduling among its processes, whereas
the background queue receives 20 percent of the CPU to give to its processes on an
FCFS basis.
3.24
Operating Systems - I Process Scheduling
FCFS
Highest System Processes
Priority
SJF
Medium priority Interactive Processes
queue
CPU
RR
Batch Processes
Gantt Chart:
P1 P1 P2 P1 P3 P2 P4 P1 P2 P4
Q1 Q2 Q1 Q2 Q1 Q2 Q1 Q2 Q2 Q2
0 5 12 17 25 29 32 37 42 52 57
P1 8
P2 2
P3 1
P4 10
P5 3
Find out turnaround time and wait time for each of the process using FCFS (First Come
First Served), SJF (Shortest Job First) and Round Robin (Quantum = 1).
3.27
Operating Systems - I Process Scheduling
P1 P4 P1 P4 P1 P4 P4 P4
16 17 18 19 20 21 22 23 24
Processes Turnaround Time
P1 (21 – 0) = 21
P2 (7 – 0) = 7
P3 (3 – 0) = 3
P4 (24 – 0) = 24
P5 (12 – 0) = 12
3.28
Operating Systems - I Process Scheduling
Non-pre-emptive SJF:
Job 1 Job 2 Job 4 Job 1
0 8 12 17 26
(8 – 0) + (12 – 1) + (17 – 2) + (26 – 3)
Average turn around time = 4
8 + 11 + 15 + 23
= 4
57
= 4
= 14.25
3.29
Operating Systems - I Process Scheduling
Problem 3: The following series of processes with the given estimated run times
arrive in the ready queue in the order shown. For the FCFS and SJF scheduling
policies, calculate the waiting time and wait time/run time ratio of each process.
Comment on the results.
Job Estimated runtime
1 10
2 50
3 2
4 100
5 5
Solution:
(i) FCFS: If the jobs arrive shortly one after the other in the order job1, job2, job3,
job4, job5 the result shown in the following Gantt chart:
The waiting time is 0 time units for Job1, 10 time units for Job2, 60 time units for
Job3, 62 time units for Job4, and 162 time units for Job5.
Thus, the wait time/runtime ratio for job1 = 0,
10
The wait time/runtime ratio for Job2 = 50 = 0.2,
60
The wait time/runtime ratio for Job3 = 2 = 30,
62
The wait time/runtime ratio for Job4 = 100 = 0.62,
162
The wait time/runtime ratio for Job5 = 5 = 32.4
(ii) SJF: The result of the above example, using SJF scheduling is shown in the
following Gantt chart:
Job 3 Job 5 Job 1 Job 2 Job 4
0 2 7 17 67 167
The waiting time is 7 for Job1, 17 time unit for Job2, 0 time unit for Job3, 67 time
unit for Job4, and 2 time unit for Job 5.
Thus, wait time/runtime ratio,
7
for Job1 = 10 = 0.7
17
for Job2 = 50 = 0.34
0
for Job3 = 2 = 0
3.30
Operating Systems - I Process Scheduling
67
for Job4 = 100 = 0.67
2
for Job5 = 5 = 0.4
From the above result, it is found that, SJF is a optional scheduling algorithm in
terms of minimizing the average waiting time of a given work load.
Problem 4: Five jobs arrive at time 0, in the order given.
Jobs Burst Time
1 10
2 29
3 3
4 7
5 12
Considering FCFS, SJF and RR (quantum = 10) scheduling algorithms for this set of
jobs, which algorithms would give the minimum average waiting time ?
Solution:
(a) For FCFS, we would execute the jobs as:
1 2 3 4 5
0 10 39 42 49 61
Jobs Waiting time
1 0
2 10
3 39
4 42
5 49
140
The average waiting time is then,
140
5 = 28
(b) For SJF (non-pre-emptive) we execute the jobs as:
3 4 1 5 2
0 3 10 20 32 61
Jobs Waiting time
1 10
2 32
3 0
4 3
5 20
65
3.31
Operating Systems - I Process Scheduling
3.32
Operating Systems - I Process Scheduling
Problem 6: Consider the following set of processes with CPU burst time given in
milliseconds.
Process Burst time Arrival time Priority
P1 8 0 4
P2 6 1 6
P3 7 3 3
P4 9 3 1 (highest)
Illustrate the evaluation of these process using non-preemptive SJF and priority
preemptive CPU scheduling algorithm. Also calculate average waiting time.
Solution: 1. Non-preemptive SJF:
Gantt Chart:
P1 P2 P3 P4
0 8 14 21 30
(8 − 0) + (14 − 1) + (21 − 3) + (30 − 3)
Average turn around time = 4
8 + 13 + 18 + 27
= 4
66
= 4 = 16.5
(0 − 0) + (8 − 1) + (14 − 3) + (21 − 3)
Average waiting time = 4
0 + 7 + 11 + 18 36
= 4 = 4 =9
3.33
Operating Systems - I Process Scheduling
Problem 7: Consider the following set of process with CPU burst time given in
milliseconds.
Process Burst time Arrival time
P1 5 1
P2 3 0
P3 2 2
P4 4 3
P5 8 2
Illustrate the execution of these process using FCFS and preemptive SJF. Calculate
average turn around time and average waiting time.
Solution: 1. FCFS:
P2 P1 P3 P5 P4
0 3 8 10 18 22
(8 − 1) + (3 − 0) + (10 − 2) + (22 + 3) + (18 − 2)
Average turn around time = 5
7 + 3 + 8 + 19 + 16
= 5
53
= 5 = 10.6
(3 − 1) + (0 − 0) + (8 − 2) + (18 − 3) + (10 − 2)
Average waiting time = 5
2 + 0 + 6 + 15 + 8
= 5
31
= 5 = 6.2
2. Preemptive SJF:
Gantt chart:
P2 P3 P4 P1 P5
0 3 5 9 14 22
(14 − 1) + (3 − 0) + (5 − 2) + (9 − 3) + (22 − 2)
Average turn around time = 5
45
= 5 =9
(9 − 1) + (0 − 0) + (3 − 2) + (5 − 3) + (14 − 2)
Average waiting time = 5
23
= 5 = 4.6
3.34
Operating Systems - I Process Scheduling
Problem 8: Consider the following set of process with CPU burst time given in
milliseconds.
Process Burst time Arrival time
P1 1 5
P2 0 7
P3 3 3
P4 2 10
Illustrate the execution of these process using RR (with time quantum = 3 ms) and
shortest remaining time first. Calculate average turn around time and average waiting
time.
Solution: 1. RR CPU Scheduling:
Gantt chart:
P2 P1 P4 P3 P2 P1 P4 P2 P4 P4
0 3 6 9 12 15 17 20 21 24 25
(17 − 1) + (21 − 0) + (12 − 3) + (25 − 2)
Average turn around time = 4
16 + 21 + 9 + 23
= 4
69
= 4 = 17.25
(15 − 3 − 1) + (20 − 6 − 0) + (9 − 3) + (21 − 6 − 2)
Average waiting time = 4
11 + 14 + 6 + 13
= 4
44
= 4 = 11
2. SJF or SRTF: (Preemptive)
Gantt chart: Preemptive SJF is also called shortest remaining time first.
P2 P1 P3 P2 P4
0 1 6 9 15 25
(6 − 1) + (15 − 0) + (9 − 3) + (25 − 2)
Average turn around time = 4
5 + 15 + 6 + 23 49
= 4 = 4 = 12.25
(1 − 1) + (9 − 1 − 0) + (6 − 3) + (15 − 2)
Average waiting time = 4
0 + 8 + 3 + 13
= 4
24
= 4 =6
3.35
Operating Systems - I Process Scheduling
Problem 9: Consider the following set of process with CPU burst time given in
milliseconds.
Process Burst time Arrival time Priority
P1 2 0 3
P2 3 1 1
P3 4 2 4 (Highest)
P4 3 3 2
Illustrate the execution of these process using non-preemptive SJF and
non-preemptive priority. Calculate average turn around time and average waiting
time.
Solution: 1. Non-preemptive SJF:
Gantt Chart:
P1 P2 P4 P3
0 2 5 8 12
(2 − 0) + (5 − 1) + (12 − 2) + (8 − 3)
Average turn around time = 4
2 + 4 + 10 + 5 21
= 4 = 4 = 5.25
(0 − 0) + (2 − 1) + (8 − 2) + (5 − 3)
Average waiting time = 4
9
= 4 = 2.25
2. Non-preemptive priority:
Gantt chart:
P1 P3 P4 P2
0 2 6 9 12
(2 − 0) + (12 − 1) + (6 − 2) + (9 − 3)
Average turn around time = 4
2 + 11 + 4 + 6 23
= 4 = 4 = 5.75
(0 − 0) + (9 − 1) + (2 − 2) + (6 − 3)
Average waiting time = 4
8+0+3
= 4
11
= 4
= 2.75
3.36
Operating Systems - I Process Scheduling
Problem 10: Consider the following set of process with CPU burst time given in
milliseconds.
Process Burst time Arrival time
J1 1 5
J2 0 7
J3 3 3
J4 2 10
Compute average time around time using RR (time quantum = 2) and shortest
remaining time first. [Oct. 16]
Solution: 1. RR:
Gantt chart:
J2 J1 J4 J2 J3 J1 J2 J3 J4 J1 J2 J4 J4 J4
0 2 4 6 8 10 12 14 15 17 18 19 21 23 25
(18 − 1) + (19 − 0) + (15 − 3) + (25 − 2)
Average turn around time = 4
17 + 19 + 12 + 23 71
= 4 = 4 = 17.75
Solution:
(i) Preemptive SJF: Gantt chart
P2 P1 P3 P4 P1
0 2 3 4 6 9 13
Idle time
Waiting time:
Process Waiting time
P1 (3 – 3) + (9 – 4) = 5 +
P2 (0 – 0) = 0+
P3 (4 – 4) = 0+
P4 (6 – 5) = 1
Total waiting time = 6
Average waiting time = 6/4 = 1.5
(ii) RR (Time quantum = 2): Gantt chart
P2 P1 P3 P4 P1 P4 P1
0 2 3 5 7 9 11 12 13
3.38
Operating Systems - I Process Scheduling
Compute the average turn around time and average waiting time using:
(i) SJF (Non-preemptive).
(ii) Round Robin (Time Quantum = 2). [5 M]
Solution:
(i) SJF (Non-preemptive):
P2 P3 P4 P1 P5
0 3 5 9 14 16
Turn around time = (14 – 3) + (3 – 0) + (5 – 2) + (9 – 3) + (16 – 13)
(11) + (3) + (3) + (6) + (3) 26
Avg. TT = 5 = 5 = 5.2
(9 – 1) + (0 – 0) + (3 – 2) + (5 – 3) + (14 – 13)
Waiting time = 5
8+1+2+1
= 5
Avg. waiting time = 2.4
(ii) Round Robin (Time quantum = 2):
P2 P1 P3 P4 P2 P1 P4 P5 P1
0 2 4 6 8 9 11 13 15 16
10 + 6 + 2 + 8 + 0
Average Waiting Time = 5 = 5.2
Problem 13: Consider the following set of processes, with the length of CPU burst time
and arrival time in milliseconds.
Process Burst time Arrival time
P1 5 1.5
P2 1 0
P3 2 2
P4 4 3
3.39
Operating Systems - I Process Scheduling
Illustrate the execution of these processes using pre-emptive SJF CPU scheduling
algorithm. Calculate average waiting time and average turn around time. Give the
contents of Gantt chart.
Solution: Preemptive SJF:
P2 P1 P3 P4 P1
0 1 1.5 2 4 8 12.5
Idle time
P1 Completed
(12.5 – 1.5) + (1 – 0) + (4 – 2) + (8 – 3)
Average Turn around time = 4
19
= 4 = 4.75
[(8 – 2) + (1.5 – 1.5)] + (0 – 0) + (2 – 2) + (4 – 3)
Average waiting time = 4
7
= 4 = 1.75
Problem 14: Consider the following set of processes, with the length of CPU burst time
and arrival time in milliseconds.
Process Burst time Arrival time Priority
P1 4 0 3
P2 3 2 1 (lowest)
P3 7 1 4 (highest)
P4 15 3 2
Illustrate the execution of these processes using pre-emptive priority algorithm. Draw
Gantt chart and calculate average turn around time and waiting time.
Solution: Preemptive priority algorithm:
P1 P3 P1 P4 P2
0 1 8 11 26 29
P1 Completed
(11 – 0) + (29 – 2) + (8 – 1) + (26 – 3)
Average Turnaround time = 4
68
= 4 = 17
[(8 – 1) + (0 – 0)] + (26 – 0) + (1 – 1) + (11 – 3)
Average waiting time = 4
41
= 4 = 10.25
3.40
Operating Systems - I Process Scheduling
Problem 15: Consider the following set of processes, with the length of CPU burst time
and arrival time in milliseconds.
Process Burst time Arrival time
P1 4 2
P2 6 0
P3 2 1
Illustrate the execution of these processes using Round Robin (RR) CPU scheduling
algorithm (quantum = 3 milliseconds). Calculate average waiting time and average
turn around time. Give the contents of Gantt chart. [5 M]
Solution: Round Robin Algorithm:
P2 P3 P1 P2 P1 P2
0 2 4 6 8 10 12
P3 Completed P1 Completed
P2 Completed
(10 – 2) + (12 – 0) + (4 – 1)
Average turnaround time = 3
23
= 3 = 7.6
[(8 – 6) + (4 – 2)] + [(10 – 8) + (6 – 2) + 0] + (2 – 1)
Average waiting time = 3
4 + 6 + 1 11
= 3 = 3 = 3.66
P1 10 3 0
P2 5 0 (high) 4
P3 2 1 3
P4 16 2 5
P5 8 4 (low) 2
P1 Preempted
(33 – 0) + (9 – 4) + (11 – 3) + (27 – 5) + (41 – 2)
Average turnaround time = 5
107
= 5 = 21.4
[(27 – 4) + (0 – 0) + (4 – 4) + (9 – 3) + (11 –5) + (33 – 2)]
Average waiting time = 5
66
= 5 = 13.2
Problem 17: Consider the following set of processes with the length of CPU burst time
and arrival time in milliseconds:
Process Burst time Arrival time Priority
P1 5 1 1 (h)
P2 6 0 2
P3 2 1 1
P4 4 0 3 (l)
Illustrate the execution of these processes using pre-emptive priority and FCFS
scheduling algorithm.
Calculate waiting time and turnaround time for each process and calculate average
waiting time and average turnaround time.
Give the contents of Gantt chart.
3.42
Operating Systems - I Process Scheduling
Solution:
(i) Pre-emptive priority algorithm:
P2 P1 P3 P2 P4
0 1 6 8 13 17
P2 Preempted
(6 – 1) + (13 – 0) + (8 – 1) + (17 – 0)
Average turnaround time = 4
42
= 4 = 10.5
(1 – 1) + [(8 – 1) + 0] + (6 – 1) + (13 – 0)
Average waiting time = 4
25 25
= 4 = 4 = 6.25
PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. Which technique allows OS to allocate a time interval of CPU execution for each
process.
(a) Process scheduling (b) Thread scheduling
(c) Both (a) and (b) (d) None of the above
2. CPU scheduling is the basis of _______.
(a) multiprocessor systems
(b) multiprogramming operating systems
(c) larger memory sized systems
(d) none of the mentioned
3. With multiprogramming which is used productively.
(a) time (b) space
(c) money (d) all of the mentioned
3.43
Operating Systems - I Process Scheduling
3.44
Operating Systems - I Process Scheduling
3.46
Operating Systems - I Process Scheduling
30. The strategy of making processes that are logically runnable to be temporarily
suspended is called as,
(a) Non preemptive scheduling (b) Preemptive scheduling
(c) Shortest job first (d) First come First served
31. What is scheduling?
(a) allowing a job to use the processor (b) making proper use of processor
(c) all of the mentioned (d) none of the mentioned
32. There are 10 different processes running on a workstation. Idle processes are
waiting for an input event in the input queue. Busy processes are scheduled with
the Round-Robin time sharing method. Which out of the following quantum times
is the best value for small response times, if the processes have a short runtime,
e.g. less than 10ms?
(a) tQ = 15ms (b) tQ = 40ms
(c) tQ = 45ms (d) tQ = 50ms
33. Orders are processed in the sequence they arrive if which rule sequences the jobs.
(a) earliest due date (b) slack time remaining
(c) first come, first served (d) critical ratio
34. Which of the following algorithms tends to minimize the process flow time?
(a) First come First served (b) Shortest Job First
(c) Earliest Deadline First (d) Longest Job First
35. Under multiprogramming, turnaround time for short jobs is usually _________ and
that for long jobs is slightly _________.
(a) Lengthened; Shortened (b) Shortened; Lengthened
(c) Shortened; Shortened (d) Shortened; Unchanged
36. Which of the following statements are true? (GATE 2010)
(i) Shortest remaining time first scheduling may cause starvation
(ii) Preemptive scheduling may cause starvation
(iii) Round robin is better than FCFS in terms of response time
(a) (i) only (b) (i) and (iii) only
(c) (ii) and (iii) only (d) (i), (ii) and (iii)
37. Which is the most optimal scheduling algorithm?
(a) FCFS – First come First served (b) SJF – Shortest Job First
(c) RR – Round Robin (d) None of the mentioned
38. The real difficulty with SJF in short term scheduling is ________.
(a) it is too good an algorithm
(b) knowing the length of the next CPU request
(c) it is too complex to understand
(d) none of the mentioned
3.47
Operating Systems - I Process Scheduling
3.48
Operating Systems - I Process Scheduling
A D
Start Ready Running Terminated
E F
Blocked
3.49
Operating Systems - I Process Scheduling
59. Which of the following statements is not true for Multi Level Feedback Queue
processor scheduling algorithm?
(a) Queues have different priorities
(b) Each queue may have different scheduling algorithm
(c) Processes are permanently assigned to a queue
(d) This algorithm can be configured to match a specific system under design
60. In which of the following scheduling criteria, context switching will never take
place?
(a) Round Robin (b) Preemptive SJF
(c) Non-preemptive SJF (d) Preemptive priority
61. In a multi-user operating system, 30 requests are made to use a particular resource
per hour, on an average. The probability that no requests are made in 40 minutes,
when arrival pattern is a Poisson distribution, is _________.
-15 -15
(a) e (b) 1 – e
-20 -20
(c) 1–e (d) e
62. The algorithm uses by scheduler is called as,
(a) dispatcher algorithm (b) scheduler algorithm
(c) Both (a) & (b) (d) None of the Above
63. Which scheduling is the process of selecting a process and allocating the processor
to the selected process for execution?
(a) CPU (b) Process
(c) Both (a) & (b) (d) None of the Above
Answers
1. (a) 2. (b) 3. (a) 4. (b) 5. (c) 6. (c) 7. (b) 8. (d) 9. (c) 10. (a)
11. (a) 12. (d) 13. (b) 14. (b) 15. (c) 16. (b) 17. (b) 18. (b) 19. (a) 20. (a)
21. (b) 22. (b) 23. (d) 24. (a) 25. (b) 26. (b) 27. (a) 28. (a) 29. (b) 30. (b)
31. (a) 32. (a) 33. (c) 34. (b) 35. (b) 36. (d) 37. (b) 38. (b) 39. (b) 40. (a)
41. (d) 42. (a) 43. (c) 44. (d) 45. (d) 46. (d) 47. (b) 48. (c) 49. (a) 50. (b)
51. (d) 52. (c) 53. (b) 54. (d) 55. (a) 56. (a) 57. (a) 58. (b) 59. (c) 60. (c)
61. (d) 62. (b) 63. (a)
Q.II Fill in the Blanks:
1. _______ is a set of policies and mechanisms which controls the order in which the
work to be done is completed.
2. The scheduling program which is a system software concerned with scheduling is
called the _______ and the algorithm it uses is called the _______ algorithm.
3. A scheduling algorithm should be designed so that CPU remains _______ as possible.
It should make efficient use of CPU.
4. _______ is the amount of work completed in a unit of time.
3.51
Operating Systems - I Process Scheduling
3.52
Operating Systems - I Process Scheduling
3.53
Operating Systems - I Process Scheduling
18. Dispatcher is a module that gives control of CPU to the process selected by short
term scheduler.
19. The main function of the dispatcher is switching, it means switching the CPU from
one process to another process.
20. In a multilevel queue-scheduling algorithm, processes are permanently assigned to
a queue on entry to the system.
21. Multilevel feedback queue scheduling, however, allows a process to move between
queues and the idea is to separate processes with different CPU-burst
characteristics. First Come First Serve (FCFS) implementation is based on FIFO
queue.
22. Priority scheduling is a non-preemptive algorithm and one of the most common
scheduling algorithms in batch systems.
23. SRT is the preemptive version of the SJN algorithm.
24. The success of CPU scheduling depends on the process execution is cycle of CPU
execution and I/O wait.
25. CPU scheduler or short-term scheduler selects the process from ready queue
allocates to CPU for execution.
26. In FCFS scheduling, the process that requests the CPU first, is allocated the CPU
first. Thus, the name First-Come-First-Served.
27. In SRT scheduling, processes are dispatched in FIFO but are given a small amount
of CPU time known as time quantum or time slice.
28. In SJF, the process with the least estimated execution time is selected from the
ready queue for execution. It associates with each process, the length of its next
CPU burst. SJF algorithm can be preemptive or non-preemptive.
29. In priority scheduling, a priority is associated with all processes. The processes are
executed in sequence according to their priority.
30. In RR scheduling, CPU is allocated to the process with highest priority. Priority
scheduling can be preemptive or non-preemptive.
Answers
1. (T) 2. (T) 3. (T) 4. (F) 5. (T)
6. (F) 7. (T) 8. (T) 9. (T) 10. (T)
11. (T) 12. (F) 13. (T) 14. (T) 15. (T)
16. (T) 17. (F) 18. (T) 19. (T) 20. (T)
21. (T) 22. (T) 23. (T) 24. (T) 25. (T)
26. (T) 27. (F) 28. (T) 29. (F) 30. (T)
Q.IV Answer the following Questions:
(A) Short Answer Questions:
1. What is process scheduling?
2. What is process scheduler?
3. What is CPU-I/O burst cycle?
3.54
Operating Systems - I Process Scheduling
4. Define dispatcher.
5. Enlist scheduling criteria.
6. Define response time and turnaround time.
7. What is preemptive scheduling and non-preemptive scheduling?
8. What is the purpose of scheduling algorithm?
9. Enlist various scheduling algorithms.
10. What is FCFS?
11. What is SJF?
12. Define multiple queue scheduling.
(A) Long Answer Questions:
1. Define process scheduling. Which criteria followed by process scheduling?
2. With the help of diagram describe CPU-I/O burst cycle.
3. Explain the CPU scheduling algorithms with example:
(i) RR, (ii) SJF, (iii) FCFS.
4. Describe the term dispatcher in detail.
5. What is meant by Multilevel Queue (MLQ) scheduling? Explain with
diagrammatically.
6. Define the following terms:
(i) CPU utilization,
(ii) Response time,
(iii) Turnaround time.
7. Compare FCFS and SJF.
8. Define CPU and I/O bound processes in detail.
9. Describe FCFS scheduling with example. Also state its advantages and
disadvantages.
10. What is priority scheduling? Explain with example. State its advantages and
disadvantages.
11. Write short note on: Multilevel feedback queue scheduling.
12. What is pre-emptive and non-preemptive scheduling? Compare them.
13. Consider the following snapshot of a system.
Job Arrival Time CPU Burst Time
1 0 7
2 1 2
3 2 5
4 3 4
Compute turnaround time using RR with quantum 3 and SJF (non-pre-emptive).
3.55
Operating Systems - I Process Scheduling
(i) Draw four Gantt charts illustrating the execution of these processes using
FCFS, SJF, a non-pre-emptive (a smaller priority number implies a higher
priority), and RR (quantum = 1) scheduling.
(ii) What is the turnaround time of each process for each of the scheduling
algorithms in part a?
(iii) What is the waiting time of each process for each of the scheduling
algorithms in part a?
(iv) Which of the schedules in part a results in the minimal average waiting time
(over all processes).
18. Consider the following set of process with CPU burst time given in milliseconds.
Process Burst time Arrival time
P1 1 5
P2 0 7
P3 3 3
P4 2 10
Illustrate average time around time using
(i) RR (time quantum = 3)
(ii) Shortest remaining time first.
Calculate Average Time Around Time.
UNIVERSITY QUESTIONS AND ANSWERS
April 2016
1. What will happen if all process is CPU bound in system? [1 M]
Ans. Refer to Section 3.1.1.
2. Define dispatch latency. [1 M]
Ans. Refer to Section 3.1.5.
3. Define turnaround time. [1 M]
Ans. Refer to Section 3.1.2 Point (3).
4. Consider the following set of processes, with the length of CPU burst time and
arrival time in milliseconds:
Process Burst Time Arrival Time
P1 4 2
P2 6 0
P3 2 1
Illustrate the execution of these processes using Round Robin (RR) CPU scheduling
algorithm (quantum = 3 milliseconds). Calculate average waiting time and average
turn around time. Give the contents of Gantt chart. [5 M]
Ans. Refer to Section 3.2.4.
5. State and explain criteria for computing various scheduling algorithms. [4 M]
Ans. Refer to Section 3.1.2.
3.57
Operating Systems - I Process Scheduling
October 2016
1. State functions of dispatcher. [1 M]
Ans. Refer to Section 3.1.5.
2. Consider the following set of processing with CPU burst time given in milliseconds.
Process Burst Time Arrival Time
J1 1 5
J2 0 7
J3 3 3
J4 2 10
Compute average time around time using RR (time quantum = 2) and shortest
remaining time first. [5 M]
Ans. Refer to Section 3.2.4.
3. Define pre-emptive and non-preemptive scheduling. State disadvantages of pre-
emptive scheduling. [4 M]
Ans. Refer to Sections 3.1.4.1 and 3.1.4.2.
April 2017
3.58
Operating Systems - I Process Scheduling
2. Consider the following set of processes with the length of CPU burst time and
arrival time in milliseconds:
Process Burst Time Arrival Time
P1 5 1
P2 6 0
P3 2 1
P4 4 0
Illustrate the execution of these processes using pre-emptive round robin CPU
scheduling algorithm. Also calculate wait time, turn around time of each process
and calculate average waiting time and average turn around time for above
situation. Also draw Gantt Chart. Consider the time quantum 3. [5 M]
Ans. Refer to Section 3.2.4.
April 2019
1. “Priority scheduling suffers from starvation”, True / False Justify. [1 M]
Ans. Refer to Section 3.2.3.
2. Consider the following set of processes with the length of CPU burst time and
arrival time in milliseconds:
Process Burst Time Arrival Time
P1 4 1
P2 2 0
P3 1 2
P4 3 3
P5 3 10
Compute average turn around time and average wait time using Round Robin (RR)
CPU scheduling algorithm with Time Quantum = 2, also draw the Gantt chart. [5 M]
Ans. Refer to Section 3.2.4.
3. What is dispatch latency time? Write the functions performed by dispatcher. [4 M]
Ans. Refer to Section 3.1.5.
■■■
3.60
CHAPTER
4
Synchronization
Objectives …
To learn Concept of Process Synchronization
To understand Critical-Section Problem
To study Classical Synchronization Problems
4.0 INTRODUCTION
• In the Operating System (OS), there are a number of processes present in a particular
state. At the same time, we have a limited amount of resources present, so those
resources need to be shared among various processes.
• Process synchronization is needed when multiple processes execute concurrently
sharing same system resources which results inconsistency of data and to avoid the
inconsistent results.
• Consider an example in Fig. 4.1, process A changing the data in a memory location
while another process B is trying to read the data from the same memory location.
• In this situation the both processes A and B access to the same shared resource or data
at the same time can be lead to the inconsistency of shared data.
• To avoid this type of inconsistency of data, the processes need to be synchronized with
each other.
Write Read
Process A Data Process B
Memory
Fig. 4.1
• In this chapter we will discuss synchronization in operating system in detail.
4.1 BACKGROUND
• Concurrent execution of the process means simultaneous execution of multiple
processes. Concurrent access often need access to shared data and shared resources.
4.1
Operating Systems - I Synchronization
Operating
system Print
requests
Process 1 Print
requests Printer
Process 2
4.2
Operating Systems - I Synchronization
• To guard against the race condition, we need to ensure that only one process at a time
can be manipulating the common resource. To obtain this, we need process
synchronization.
• The process which completes its task should release the resource and the process
which requires the resources and resources are not available (currently in use) would
automatically switch to wait state.
4.2 CRITICAL SECTION PROBLEM [April 16, 17, 18, Oct. 17, 18]
• To avoid race conditions and results, one must identify codes in critical sections in
each process.
• Critical section is a code segment that can be accessed by only one process at a time.
Critical section contains shared variables which need to be synchronized to maintain
consistency of data variables.
• Consider a system consisting of n processes {P0, P1, …, Pn−1}. Each process has a
segment of code, called a Critical Section, in which the process may be changing
common variables, updating a table, writing a file, and so on.
• The important feature of the system is that, when one process is executing in its
critical section, no other process is to be allowed to execute in its critical section. That
is, no two processes are executing in their critical sections at the same time.
• The critical-section problem is to design a protocol that the processes can use to
cooperate. Each process must request permission to enter its critical section. The
section of code implementing this request is the entry section.
• The critical section may be followed by an exit section. The remaining code is the
remainder section.
• The general structure of a typical process Pi is shown in Fig. 4.3. The entry section and
exit section are enclosed in boxes to highlight these important segments of code.
do
{
Entry Section
critical section
Exit Section
remainder section
} while (True);
Fig. 4.3: General Structure of a Typical Process Pi
• The critical section can be defined by following three sections:
1. Entry Section: Each process must request permission to enter its critical section;
processes may go to critical section through entry section.
4.3
Operating Systems - I Synchronization
Algorithm 1:
• In the first algorithm all processes share the common integer variable ‘turn’ initialize
as 0 or 1. If ‘turn=i’, then process Pi is allowed to execute in its critical section.
• This algorithm ensures that only one process at a time can be in critical section, but
does not satisfy the progress condition.
• The structure of process Pi is shown as in Fig. 4.4 (a).
do
{
while(turn!=i)
critical section
turn=j;
remainder section
} while (1);
Fig. 4.4 (a)
Algorithm 2:
• The problem of algorithm-1 is that it does not retain sufficient information about the
state of each process, it remember only which process is allowed to enter its critical
section.
• To remove this problem, we can replace the variable turn with the array as “Boolean
flag”. The elements of the array are initialized to false. If flag[i] is true, this value
indicates that Pi is ready to enter the critical section.
• This algorithm ensures mutual exclusion condition, but does not satisfy the progress
condition. In this case process Pi is shown as in Fig. 4.4 (b).
• Process Pi set flag[i] to be true, signaling that it is ready to enter its critical section,
then Pi checks to verify that Pj is not also ready to enter its critical section.
• If Pj is ready, then Pi would wait until Pj had indicated that it no longer needed to be in
the critical section i.e. until flag[j] was false.
• At this point, Pi would enter the critical section. On exiting critical section Pi would set
flag[i] to be false, allow the other process to enter its critical section.
do
{
flag[i]=true;
while(flag[j])
critical section
flag[i]=false;
remainder section
} while(1);
Fig. 4.4 (b)
4.5
Operating Systems - I Synchronization
Algorithm 3:
• In this algorithm we combine the idea of both above algorithm where all critical
section requirements are met. Each process shares two variables as:
Boolean flag[2]; int turn;.
• Initially flag[0]=flag[1]=false and the value of turn is 0 or 1. This algorithm ensures all
critical section requirements. In this case process Pi is shown as in Fig. 4.4 (c). Process
Pi can enter to critical section if flag[i] is true and turn is equal to i.
do
{
flag[i]=true;
turn=j;
while(flag[j]&&turn=j)
critical section
flag[i]=false;
remainder section
} while(1);
Fig. 4.4 (c)
Bakery Algorithm Solution (For Multiple Processes):
• The Bakery algorithm is one of the simplest known solutions to the mutual exclusion
problem for the general case of n (multiple) process.
• The Bakery algorithm is a critical section solution for n processes. The Bakery
algorithm preserves the First Come First Serve (FCFS) property.
• This uses the concept which is used in bakeries to serve customers. When a customer
enters in a bakery, he/she is given a token in ascending order of number.
• While providing the service, the customer with smallest token number is served first.
1. It is generalization for n processes.
2. Each process has an id. Process ids are ordered.
3. Before entering its critical section, a process receives a number. The holder of the
smallest number enters its critical section.
• The common data structures are used in Bakery algorithm:
Boolean choosing [n];
int number [n];
• Initially, above data structures are initialized to false and 0 respectively. For this we
define the following notation:
(a, b) < (c, d) if a < c or if a = c and b < d
max(a0, . . ., an-1) is a number, k, such that k >= ai for i = 0, . . ., n - 1
4.6
Operating Systems - I Synchronization
number[i] := 0;
remainder section
until false;
Explanation:
• Firstly the process sets its “choosing” variable to be TRUE indicating its intent to enter
critical section. Then it gets assigned the highest ticket number corresponding to other
processes.
• Then the “choosing” variable is set to FALSE indicating that it now has a new ticket
number. This is in-fact the most important and confusing part of the algorithm.
• It is actually a small critical section in itself! The very purpose of the first three lines is
that if a process is modifying its TICKET value then at that time some other process
should not be allowed to check its old ticket value which is now obsolete.
• This is why inside the for loop before checking ticket value we first make sure that all
other processes have the “choosing” variable as FALSE.
• After that we proceed to check the ticket values of processes where process with least
ticket number/process id gets inside the critical section. The exit section just resets the
ticket value to zero.
Synchronization Hardware Solution:
• Sometimes the problems of the critical section are also resolved by hardware. Some
operating system offers lock functionality where a process acquires a lock when
entering the critical section and releases the lock after leaving it.
• So when another process is trying to enter the critical section, it will not be able to
enter as it is locked. It can only do so if it is free by acquiring the lock itself.
• In this topic we present some hardware instructions that are available on many
systems and show how they can be used effectively in solving the critical section
problem.
4.7
Operating Systems - I Synchronization
critical section
lock = false;
remainder section
} while(1);
Fig. 4.6: Mutual-Exclusion Implementation with TestAndSet
• The Swap instruction defined as shown in Fig. 4.7 which operates on the contents of
two words like the TestAndSet instruction, it is executed automatically.
void Swap(Boolean &a, Boolean &b)
{
boolean temp = a;
a = b;
b = temp;
}
Fig. 4.7: The Definition of the Swap Instruction
4.8
Operating Systems - I Synchronization
• If the machine supports Swap instruction then mutual exclusion can be provided. The
structure of process Pi is shown in Fig. 4.8. Each process has a local Boolean variable
key.
do
{
key = true;
while (key == true)
Swap(lock,key);
critical section
lock = false;
remainder section
} while(1);
Fig. 4.8: Mutual-Exclusion Implementation with the Swap Instruction
Semaphore Solution:
• Semaphore is simply a variable that is non-negative and shared between threads. It is
another algorithm or solution to the critical section problem.
• It is a signaling mechanism and a thread that is waiting on a semaphore, which can be
signaled by another thread.
• It uses two atomic operations, namely, Wait and Signal for the process synchro-
nization.
Example:
Wait ( S ):
while ( S <= 0 );
S = S - 1;
Signal ( S ):
S = S + 1;
• The various hardware-based solutions to the critical region problem are complicated
to use. This problem can be solved using synchronization tool semaphore.
• Semaphore is introduced by Edsger W. Dijkstra in 1965. It restricts the sequence or
order of execution of the statements.
• A semaphore is a variable used to control access to a common resource by
multiple processes and avoid critical section problem.
• A semaphore is an integer variable, which can be accessed only through two
operations Wait() and Signal().
4.9
Operating Systems - I Synchronization
Critical Section
Exit
Signal S
Fig. 4.9: Concept of Semaphore
Advantages of Semaphores:
1. Semaphores allow only one process into the critical section. They follow the
mutual exclusion principle strictly and are much more efficient than some other
methods of synchronization.
2. There is no resource wastage because of busy waiting in semaphores as processor
time is not wasted unnecessarily to check if a condition is fulfilled to allow a
process to access the critical section.
• We can use semaphore to deal with the n processes. The semaphore can be used in
various synchronization problems.
• The two type’s semaphores are binary semaphore and counting semaphore. A binary
semaphore is restricted to values of zero (0) or one (1), while a counting semaphore
can assume any non-negative integer value. The counting semaphores can range over
an unrestricted domain.
4.10
Operating Systems - I Synchronization
1. Binary Semaphore:
• Binary semaphore is a semaphore with an integer value that can range only between 0
and 1. Binary semaphore is also known as mutex locks, as the locks can provide
mutual exclusion.
• All the processes can share the same mutex semaphore that is initialized to 1. Then, a
process has to wait until the lock becomes 0.
• Then, the process can make the mutex semaphore 1 and start its critical section. When
it completes its critical section, it can reset the value of mutex semaphore to 0 and
some other process can enter its critical section.
• We can use binary semaphores to deal with critical section problem for multiple
processes.
• The n (multiple) processes share a semaphore mutex initialized to 1. Each process Pi is
organized as shown below:
do
{
Wait (mutex)
//Critical section
Signal (mutex)
//remainder section
}
while(TRUE);
• In OS a semaphore is used to solve number of synchronization problems. For example,
consider process P1 and P2 are concurrently running and share a common semaphore
synch initialized to zero.
• The Process P1 with a statement S1 and Process P2 with a statement S2. Suppose we
requires that S2 can be executed only after S1 has completed then we can implement
this scheme readily by letting P1 and P2 share a common semaphore synch, initialized
to zero (0) and by inserting the statements in the following table:
Process P1 Process P2
S1; Wait(synch);
Signal(synch); S2;
• Because synch is initialized to 0, the Process P2 will execute statement S2 only after the
Process P1 has invoked Signal(synch) which after statement S1.
2. Counting Semaphore: [April 17]
• The value of counting semaphore can range over an unrestricted domain. It can be
used to control access to a given resource consisting of finite number of instances.
• The semaphore is initialized to number of resources available. Each process that
wishes to use resource performs Wait() operation. When process releases resource, it
performs Signal() operation.
4.11
Operating Systems - I Synchronization
4.3.2 Implementation
• The critical aspect of semaphore is that they be executed automatically. We must
guarantee that no two processes can execute Wait() and Signal() operations on the
semaphore at the same time. This is critical section problem.
• While one process is in its critical section, another process that tries to enter its critical
section must loop continuously in the entry code. This continuous looping is problem
in multiprogramming environment.
• In multiprogramming environment, only one CPU is shared among processes.
A process which is waiting utilizes the CPU only to execute wait loop which is nothing
but wastage of CPU cycles. This is called as problem of busy waiting.
• To overcome need of busy waiting; Wait() and Signal() operations are modified. With
each semaphore, there is an associated waiting queue.
• Each entry in a waiting queue has two data items namely, value (of type integer) and
pointer to next record in the list
• C structure of semaphore is as follows:
typedef struct
{
int value;
struct process *list;
}semaphore;
• Two operations are explained below:
1. block: Place the process invoking the operation on the appropriate waiting queue.
2. wakeup: Remove one of processes in the waiting queue and place it in the ready
queue.
• A process which is waiting is blocked and added to list of blocked processes. The
process which is blocked by operation Wait(), will be restarted by wakeup operation
which is included in Signal() operation.
4.12
Operating Systems - I Synchronization
Producer:
• Creates item and adds to the buffer.
• Do not want to overflow the buffer.
Consumer:
• Removes items from buffer (consumes it).
• Do not want to get ahead of producer.
Waiting
producer producer
V(supply) V(supply)
Waiting
consumer consumer
P(supply) P(supply)
full queue empty queue
4.14
Operating Systems - I Synchronization
Explanation:
• While one process is producing or consuming, mutex value is made 0 and after
producing and consuming is over; it is set back to 1. Therefore, mutual exclusion is
preserved.
• For producer, if empty=0, then process cannot continue. It is made to wait and after
producing is over; full is incremented.
• For consumer, exactly reverse is the case. If full =0; then process cannot continue. At
the end, empty is incremented.
4.4.2 The Reader/Writer Problem
• The readers-writers problem is a classical problem of process synchronization, it
relates to a data set such as a file that is shared between more than one process at a
time.
• Among these various processes, some are Readers - which can only read the data set;
they do not perform any updates, some are Writers - can both read and write in the
data sets.
Multiple Readers
Waiting Writers
reader1
reader2 File
Waiting Readers
reader3
One Writer
Waiting Writers
writer1 writer2
writer2 File
Waiting Readers
o When done eating, a philosopher puts both chopsticks back down on the table and
begins thinking.
o Since the philosophers are sharing chopsticks, it is not possible for all of them to be
eating at the same time.
o Now consider each philosopher as process and chopsticks are as resources. It is a
simple presentation of the need to allocate several resources among several
processes in a deadlock-free and starvation-free manner.
Philosopher
1
Philosopher Philosopher
5 2
Philosopher Philosopher
4 3
4.19
Operating Systems - I Synchronization
P2:
while true do
{
L3 : ................
L4 : ................
Y = Y + 1;
X = Y - 1;
V(SY);
V(SX);
}
In order to avoid deadlock, the correct operators at L1, L2, L3 and L4 are respectively
(A) P(SY), P(SX); P(SX), P(SY)
(B) P(SX), P(SY); P(SY), P(SX)
(C) P(SX), P(SX); P(SY), P(SY)
(D) P(SX), P(SY); P(SX), P(SY)
Answer: (D)
Explanation:
Option A: In line L1 ( p(Sy) ) i.e. process p1 wants lock on Sy that is held by process p2
and line L3 (p(Sx)) p2 wants lock on Sx which held by p1. So here circular and wait
condition exist means deadlock.
Option B: In line L1 ( p(Sx) ) i.e. process p1 wants lock on Sx that is held by process p2
and line L3 (p(Sy)) p2 wants lock on Sx which held by p1. So here circular and wait
condition exist means deadlock.
Option C: In line L1 ( p(Sx) ) i.e. process p1 wants lock on Sx and line L3 (p(Sy)) p2
wants lock on Sx . But Sx and Sy can’t be released by its process.
Problem 5: The following program consists of 3 concurrent processes and 3 binary
semaphores. The semaphores are initialized as S0 = 1, S1 = 0, S2 = 0.
Process P0 Process P1 Process P2
while (true) wait (S1); wait (S2);
{ release(S0); release(S0);
wait (S0);
print '0';
release (S1);
release (S2);
}
4.20
Operating Systems - I Synchronization
PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. Which means sharing system resources by processes in such a way that,
concurrent access to shared data is handled thereby minimizing the chance of
inconsistent data?
(a) Process synchronization (b) Thread synchronization
(c) Process virtualization (d) All of the above mentioned
2. When several processes access the same data concurrently and the outcome of the
execution depends on the particular order in which the access takes place, is called
as,
(a) dynamic condition (b) race condition
(c) essential condition (d) critical condition
3. If a process is executing in its critical section, then no other processes can be
executing in their critical section. This condition is called as,
(a) mutual exclusion (b) critical exclusion
(c) synchronous exclusion (d) asynchronous exclusion
4. Which one of the following is a synchronization tool?
(a) thread (b) socket
(c) semaphore (d) None of the Above
5. Process synchronization can be done on,
(a) hardware level (b) software level
(c) both (a) and (b) (d) None of the Above
6. Which are used for signaling among processes and can be readily used to enforce a
mutual exclusion discipline.
(a) Semaphores (b) Messages
(c) Monitors (d) None of the Above
7. Semaphores provide a primitive yet powerful and flexible tool for enforcing
mutual exclusion and for coordinating processes called as,
(a) binary semaphore (b) monitor
(c) counting semaphore (d) None of the Above
4.21
Operating Systems - I Synchronization
47. The initial value of the semaphore that allows only one of the many processes to
enter their critical sections is _______.
(a) 8 (b) 1
(c) 6 (d) 0
48. In implementation of semaphores, for a single processor system, it is possible to
inhibited _______.
(a) Deadlock (b) Interrupts
(c) Lock Step (d) None
49. Using semaphores, each process has a critical section used to access the _______.
(a) Computers (b) Processors
(c) Resources (d) Users
50. In readers and writers problem, readers are processes that are not required to
_______.
(a) Include (b) Exclude
(c) Describe (d) None
Answers
1. (a) 2. (b) 3. (a) 4. (c) 5. (c) 6. (a) 7. (b) 8. (c) 9. (a) 10. (b)
11. (a) 12. (b) 13. (a) 14. (a) 15. (b) 16. (b) 17. (c) 18. (b) 19. (a) 20. (c)
21. (c) 22. (c) 23. (b) 24. (a) 25. (b) 26. (c) 27. (c) 28. (a) 29. (a) 30. (b)
31. (b) 32. (a) 33. (c) 34. (b) 35. (c) 36. (b) 37. (a) 38. (b) 39. (c) 40. (a)
41. (b) 42. (b) 43. (a) 44. (a) 45. (c) 46. (c) 47. (b) 48. (b) 49. (c) 50. (b)
Q.II Fill in the Blanks:
1. Synchronization was introduced to handle problems that arose while _______
process executions.
2. Process Synchronization is the task of coordinating the execution of processes in a
way that no two processes can have access to the _______ shared data and
resources.
3. A _______ section is a segment of code which can be accessed by a signal process at
a specific point of time.
4. In the critical section, only a _______ process can be executed. Other processes,
waiting to execute their critical section, need to wait until the current process
completes its execution.
5. _______ exclusion is a special type of binary semaphore which is used for
controlling access to the shared resource.
6. A _______ condition typically occurs when two or more threads try to read, write
and possibly make the decisions based on the memory that they are accessing
concurrently.
7. The regions of a program that try to access shared resources and may cause race
conditions are called critical_______.
8. To avoid race condition among the processes, we need to assure that only _______
process at a time can execute within the critical section.
4.26
Operating Systems - I Synchronization
9. In _______ process execution of one process does not affects the execution of other
processes.
10. A race condition is a situation that may occur _______ a critical section.
11. A semaphore is a shared variable/integer variable to _______ the progress of
interacting processes.
12. A semaphore uses two atomic operations, _______ and _______ for process
synchronization.
13. _______ semaphores can only be either 0 or 1.
14. _______ synchronization is the task of coordinating the execution of processes in a
way that no two processes can have access to the same shared data and resources.
15. _______ Semaphore value can range over an unrestricted domain. It is used to
control access to a resource that has multiple instances.
16. Number of synchronization problems like bounded buffer problem , the reader
writer problem, the dining philosopher problem etc. can be solved by using
_______.
Answers
1. multiple 2. same 3. critical 4. single
5. Mutual 6. race 7. section 8. one
9. independent 10. inside 11. synchronize 12. wait, signal
13. Binary 14. Process 15. Counting 16. semaphores
Q.III State True or False:
1. Synchronization was introduced to handle problems that arose while single
process executions.
2. A cooperative process is the one which can affect the execution of other process or
can be affected by the execution of other process.
3. Process synchronization is the task of coordinating the execution of processes in a
way that no two processes can have access to the same shared data and resources.
4. A critical section is a segment of code which can be accessed by a signal process at
a specific point of time. The section consists of shared data resources that required
to be accessed by other processes.
5. A critical section is a segment of code that can be accessed by only one signal
process at a certain instance in time.
6. A semaphore is simply a variable that stores an integer value.
7. The critical section code can be accessed by only multiple process at a time.
8. In the critical section, only a single process can be executed. Other processes,
waiting to execute their critical section, need to wait until the current process
completes its execution.
9. To avoid race condition among the processes, we need to assure that only one
process at a time can execute within the critical section.
10. When more than one processes are executing the same code or accessing the same
memory or any shared variable in that condition there is a possibility that the
4.27
Operating Systems - I Synchronization
output or the value of the shared variable is wrong so for that all the processes
doing the race to say that my output is correct this condition known as a race
condition.
11. The entry to the critical section is handled by the wait() function.
12. Race condition contains shared variables which need to be synchronized to
maintain consistency of data variables.
13. A semaphore is basically a synchronizing tool and is accessed only through two
standard atomic operations, wait and signal.
14. Counting semaphores takes more than one value.
15. Any solution to the critical section problem must satisfy three requirements
namely, mutual exclusion, progress and bounded waiting.
16. Binary semaphores are also known as mutex locks, as the locks can provide
mutual exclusion.
17. The exit from a critical section is controlled by the Wait() function.
18. Four elements of critical section are Entry section, Critical section, Exit section and
Reminder section.
Answers
1. (F) 2. (T) 3. (T) 4. (T) 5. (T)
6. (T) 7. (F) 8. (T) 9. (T) 10. (T)
11. (T) 12. (F) 13. (T) 14. (T) 15. (T)
16. (T) 17. (F) 18. (T)
Q.IV Answers the following Questions:
(A) Short Answer Questions:
1. What is synchronization?
2. Define semaphore.
3. List types of semaphore.
4. What is critical section problem?
5. What is race condition?
6. List classical problem for synchronization.
7. What is dining philosopher problem?
8. Give reader writer problem in synchronization.
9. List signals for semaphore.
(B) Long Answer Questions:
1. Define synchronization. Why it is needed in OS? Explain in detail.
2. What is semaphore? Enlist its types.
3. Define critical section problem. Explain in detail.
4. Write note on: Race condition.
5. What are the types of semaphore? Explain its usage and implementation.
4.28
Operating Systems - I Synchronization
6. Discuss the following problems in concurrent programming and explain the use of
semaphore in these problems:
(i) The producer-consumer problem
(ii) The reader-writer problem
(iii) Dining Philosopher problem
7. Define synchronization. Explain in detail.
8. Compare binary and counting semaphores.
9. Give background for synchronization.
10. What is purpose of semaphores? Also explain usages of semaphore.
11. Define the terms:
(i) Race condition.
(ii) Bounded wait.
(iii) Critical section.
(iv) Binary semaphore.
(v) Counting Semaphore.
12. Explain Bounded buffer problem? Give structure of producer and consumer.
13. How to implement an semaphore? Describe in detail.
14. Which two standard atomic operations can access semaphore value?
15. How semaphore can be used to solve the Dining Philosopher problem of
concurrency control?
16. Which three requirements must be satisfied while designing a solution to the
critical section problem? Explain each in detail.
17. Give Peterson’s solution to solve critical section problem.
18. Explain the role of Wait() and Signal() operations used in semaphores.
April 2017
1. "Counting semaphore can be implemented by using binary semaphore". True/Fal
Justify. [1 M]
Ans. Refer to Section 4.3.1 Point (2).
2. What is race condition? [1 M]
Ans. Refer to Section 4.1.
3. What is critical section problem? Explain two general approaches to handle critical
section in operation system. [5 M]
Ans. Refer to Section 4.2.
October 2017
1. Which two standard atomic operations can access semaphore value? [1 M]
Ans. Refer to Section 4.3.
2. What is critical section problem? How is it solved? [5 M]
Ans. Refer to Section 4.2.
April 2018
1. "A race condition exists when processes are running simultaneously". True/False.
Justify. [1 M]
Ans. Refer to Section 4.1.
2. What is Semaphore? Explain bounded-buffer problems. [5 M]
Ans. Refer to Section 4.3.
3. What is critical section problem? Give Peterson's solution to solve critical section
problem. [4 M]
Ans. Refer to Section 4.2.
October 2018
1. What are the types of semaphores? [1 M]
Ans. Refer to Section 4.3.1.
2. Explain critical section problem. [5 M]
Ans. Refer to Section 4.2.
3. Discuss the bounded buffer problem with its solution. [4 M]
Ans. Refer to Section 4.4.1.
April 2019
1. Write a short note on Dining philosopher problem. [5 M]
Ans. Refer to Section 4.4.3.
2. What is mutual exclusion? [1 M]
Ans. Refer to Page 4.4.
■■■
4.30
CHAPTER
5
Memory Management
Objectives …
To learn Basic Concepts in Memory Management such as Hardware, Physical and
Logical Address Space, Basic Method for Memory Utilization like Dynamic Loading and
Linking etc.
To know various Memory Management Schemes like Contiguous Allocation, Paging and
Segmentation etc.
To understand Virtual Memory Management Technique
5.0 INTRODUCTION
• Computer memory is the storage space in the computer, where data is to be processed
and instructions required for processing are stored.
• In a computer system, there may be multiple processes executing at the same time and
every process that needs to execute, requires a certain amount of memory.
• To improve the utilization of the CPU and the speed of the computer's response to its
users, we must keep several processes in memory i.e., we must share memory. Thus,
multi-programming creates need of memory management.
• Memory management is important function of operating system which helps in
allocating the main memory space to the processes and their data at the time of their
execution. The main task of memory management is efficient memory utilization.
• Memory management keeps track of each and every memory location, regardless of
either it is allocated to some process or it is free.
• There are various memory management schemes and techniques for managing
memory.
• In selecting memory management scheme, various issues like hardware design,
protection, binding of symbolic addresses to physical addresses, distinguishing logical
and physical addresses, dynamic loading and linking etc. must be considered.
5.1 BACKGROUND
• The main purpose of a computer system is to execute programs (set of instructions).
These programs together with the data they access must be in main memory during
execution.
5.1
Operating Systems - I Memory Management
5.2
Operating Systems - I Memory Management
Fig. 5.1: A Base Register and a Limit Register Define a Logical Address Space
• Protection of memory space is accomplished by comparing every address generated
by user’s program with these registers.
• Any attempt made by user’s program to access operating system’s memory or other
user’s memory results in a trap to the operating system as shown in Fig. 5.2.
• Thus, this method prevents a user program from modifying code or data of operating
system or other users.
• The base and limit registers can be loaded by operating system using special privileged
instruction in kernel mode.
base base + limit
no no
Source
program
Compiler or Compile
Assembler time
Other Object
object module
modules
Linkage
editor
System
library Load Load
module time
Dynamically
loaded Loader
system
library
Execution
Dynamic In-memory binary time (Run
linking memory image time)
compile time where the process will reside in memory, then absolute code can be
generated. If starting location changes, then it will be necessary to recompile this
code. In MS-DOS, .COM files are bound at compile time. Compile time address
binding will be done before loading the program into memory. The compiler
requires interacts with an OS memory manager to perform compile-time address
binding.
2. Load Time Address Binding: Load time address binding will be done after
loading the program into memory and address binding will be done by loader. If it
is not known at compile time where the process will reside memory, then compiler
generates re-locatable code. Then binding is delayed until load time. If the starting
address is changed, only reload the code to changed address.
3. Execution Time or Dynamic Address Binding: If the process can moved during
its execution from one memory segment to another, then binding must be delayed
until run time. Special hardware is needed to implement this scheme. Dynamic
address binding will be postponed even after loading the program into memory.
The program will be kept on changing the locations in memory until the time of
program execution. The dynamic address binding done by the processor at the
time of program execution.
5.1.3 Logical versus Physical Address Space [Oct. 16, April 19]
• Address uniquely identifies a location in the main memory. Memory locations
determine how the computer’s memory is organized in execution of program.
• Address space is the amount of memory allocated for all possible addresses for a
computational task. An address space is a range of valid addresses in memory that are
available for a program or process.
• Address space may refer to a range of either physical or virtual addresses accessible to
a processor or reserved for a process.
• An address generated by the CPU is commonly known as logical address or virtual
address.
• The address seen by the memory unit that is one loaded into the memory address
register of the memory is commonly referred as the physical address.
• Logical address is generated by CPU while a program is running and used as a
reference to access the physical memory location by CPU. Physical address identifies a
physical location of required data in a memory.
• The compile time and load time address binding generates the identical logical and
physical addresses.
• However, the execution time address binding scheme generates differing logical and
physical addresses as process moves from one memory segment to another.
• The set of all logical addresses generated by a program is known as logical address
space. The set of all physical addresses corresponding to these logical addresses is
physical address space.
5.5
Operating Systems - I Memory Management
• The run time mapping from virtual address to physical address is done by a hardware
device known as Memory Management Unit (MMU). The runtime mapping from
virtual to physical address is done by the MMU.
• The operating system takes care of mapping the logical addresses to physical
addresses at the time of memory allocation to the program. Different methods like
contiguous allocation, paging, segmentation etc. can perform such mapping.
• The simplest memory mapping scheme is shown in the Fig. 5.4. Here, base register is
known as relocation register.
Relocation
register
Logical 14000 Physical
address address Memory
CPU +
346 14346
MAR
MMU
5.6
Operating Systems - I Memory Management
• In dynamic loading a routine is not loaded until it is called. All routines are kept on
disk in re-locatable load format. Only main routine is loaded into memory and is
executed.
Advantages of Dynamic Loading:
1. In dynamic loading unused routine is never loaded.
2. Dynamic loading also provides better memory space utilization.
3. No special support from the operating system is required, can be implemented
through program design.
• A library may be replaced by a new version, and all programs that reference the
library will automatically use the new version.
• Without dynamic linking support, such programs would need to be re-linked to gain
access to the new library.
• More than one version of a library may be loaded into memory, and each program
uses its version information to decide which copy of library to use.
• Major changes increments the version number and minor changes retain the same
version number.
• Thus, only programs that are compiled with new library version are affected by the
incompatible changes incorporated in it.
• Other programs linked before the new library was installed will continue using the
older library. This system is also known as shared libraries.
• Shared libraries are loaded into memory by programs when they start. When a shared
library is loaded properly, all programs that start later automatically use the already
loaded shared library.
• Shared libraries serve as sharing a single copy of library among multiple programs,
hence they are called shared libraries, and the process of linking them with multiple
programs is called dynamic linking.
• Advantages of shared libraries are as follows:
1. Load time might be reduced because the shared library code might already be in
memory.
2. Run-time performance can be enhanced.
operating
system
process P1
1 swap out
process P2
2 swap in
user
space backing store
main memory
Fig. 5.5: Swapping of Two Processes using a Disk as a Backing Store
Example: Suppose the user process's size is 2048KB and is a standard hard disk where
swapping has a data transfer rate of 1Mbps. Now we will calculate how long it will take to
transfer from main memory to secondary memory.
Solution: User process size is 2048 Kb
Data transfer rate is 1 Mbps = 1024 kbps
Process size
Time = Transfer rate
= 2048 / 1024
= 2 seconds
= 2000 milliseconds
Now taking swap-in and swap-out time, the process will take 4000 milliseconds.
Advantages of Swapping:
1. It helps the CPU to manage multiple processes within a single main memory.
2. It helps to create and use virtual memory.
3. Swapping allows the CPU to perform multiple tasks simultaneously. Therefore,
processes do not have to wait very long before they are executed.
4. It improves the main memory utilization.
Disadvantages of Swapping:
1. In the case of heavy swapping activity, if the computer system loses power, the
user might lose all the information related to the program.
2. Inefficiency may arise in the case where a resources or a variable is commonly
used by the processes which are participating in the swapping process.
• With help of relocation register, this code can be loaded during program execution.
So size of operating system changes dynamically
• When the CPU scheduler selects a process for execution, the dispatcher loads the
relocation and limit register with correct values as a part of context switch.
• Since, every address generated by the CPU is checked against these register we can
protect the operating system and other users programs and data from being modified.
Limit Relocation
Register Register
Logical Physical
address Yes address
CPU < + memory
No
5.11
Operating Systems - I Memory Management
• There are two alternatives for fixed sized memory partitioning namely, equal sized
partitions (Refer Fig. 5.8 (a)) and unequal sized partitions (Refer Fig. 5.8 (b)).
Monitor
••• 2K 1K 2K Q2 2K
••• 3K 4K Q6 6K
Fig. 5.9 (a): MFT with Separate Queues for Each Region
• Since each queue has its own memory region, there is no competition between queues
for memory. Another approach is to throw all jobs into one queue.
• The job scheduler selects the next job to be run and waits until a memory region of
that size is available.
• Suppose that we had FCFS job scheduler and regions of 2K, 6K, and 12K. We would
first assign job1 (5K) to the 6K regions and job2 (2K) to the 2K regions. Since our next
job requires 3K, we need the 6K regions.
• Since 6K regions are being used by job1, we must wait until job1 terminates, then job3
will be allocated the 6K regions. Job4 is then allocated to 12K regions and so on.
Monitor
2K
••• 4K 1K 7K 7K 3K 2K 5K Q 6K
12K
2. The main problem with the fixed partitioning method is how to determine the
number of partitions, and how to determine their sizes.
3. The fixed memory partitioning suffers from problem of internal fragmentation.
216K 2 100 K 5
3 30 K 20
4 70 K 8
256K
Fig. 5.10 5 50 K 15
• Given the job queue and FCFS job scheduling, we can immediately allocate memory to
job1, 2 and 3 creating the memory map of Fig. 5.11 (a).
• We have 26K of external fragmentation. Job2 will terminate at time 5 unit, releasing its
memory allocation shown in Fig. 5.11 (b).
• We then return to our job queue and schedule the next job, job4 to produce the
memory map of Fig. 5.11 (c). Job1 will terminate at time 10 unit to produce Fig. 5.11 (d)
and job5 is then scheduled, producing Fig. 5.11 (e).
5.14
Operating Systems - I Memory Management
0 0 0 0 0
5.15
Operating Systems - I Memory Management
Example: Consider the following memory map and assume a new process P4 comes
with a memory requirement of 3 KB. Locate this process.
(i) First fit algorithm allocates from the 10 KB block.
(ii) Best fit algorithm allocates from the 4 KB block.
(iii) Worst first algorithm allocates from the 16 KB block.
OS
P1
<free> 10 KB
P2
<free> 16 KB
P3
<free> 4 KB
• As processes are loaded and removed from memory, the free memory space is broken
into little pieces. It happens after sometimes that processes cannot be allocated to
memory blocks considering their small size and memory blocks remains unused. This
problem is known as fragmentation.
• Fragmentation is of two types namely, external fragmentation and internal
fragmentation.
• External fragmentation arises when free memory, areas existing in systems are too
small to be allocated to processes. Internal fragmentation exists when memory
allocated to a program is not fully utilized by it.
5.17
Operating Systems - I Memory Management
• In internal fragmentation, the process is allocated a memory block of size more than
the size of that process. Due to this some part of the memory is left unused and this
cause internal fragmentation.
• When internal fragmentation occurs, a process that needs 57 bytes of memory, for
example, may be allocated a block that contains 60 bytes, or even 64.
• The extra bytes that the client does not need go to waste, and over time these tiny
chunks of unused memory can build up and create large quantities of memory that
cannot be put to use by the allocator.
• Because all of these useless bytes are inside larger memory blocks, the fragmentation
is considered internal.
Operating
System
60 byte
57 byte
User process
3 byte Unused space in block
Fig. 5.12: Internal Fragmentation
• In external fragmentation the total memory space is enough to satisfy a request or
to reside a process in it, but it is not contiguous, so it cannot be used.
• For example, there is a hole of 20K and 10K is available in multiple partition
allocation schemes. The next process request for 30K of memory.
• Actually 30K of memory is free which satisfy the request but hole is not contiguous.
To there is an external fragmentation of memory
Operating
System
40 K
20 K Hole of 20 K
50 K
10 K Hole of 10 K
Fig. 5.13: External Fragmentation
• Compaction is a method used to overcome the external fragmentation problem. All
free blocks are brought together as one large block of free space. [April 19]
• The collection of free space from multiple non-contiguous blocks into one large free
block in a system's memory is called compaction.
5.18
Operating Systems - I Memory Management
• The goal compaction is to shuffle the memory contents to place all free memory
together in one large block. For example the memory map of (e) can be compacted as
shown in Fig. 5.14.
• The three holes of sizes 10K, 30K and 26K can be compacted into one hole of 66K.
Compaction is not always possible.
• In Fig. 5.14 we moved job4 and job3 for these programs to be able to work in their new
locations, all internal addresses must be relocated.
• Compaction is possible only if relocation is dynamic, at execution time, using base and
limit registers.
• The simplest compaction algorithm is to simply move all jobs towards one end of
memory, all holes move in the other direction, producing on large hole of available
memory.
• Compaction can be quite expensive. Compaction changes the allocation of memory to
make free space contiguous and hence useful. Compaction also consumes system
resources.
0 0
Monitor Monitor
40K 40K
Job 5 Job 5
90K 10K
100K 90K
170K 160K
30K Job 3
200K 190K
Job 3
230K 66K
26K
256K 256K
Fig. 5.14: Compaction
Difference between Internal Fragmentation and External Fragmentation:
Sr. No Internal Fragmentation Internal Fragmentation
1. It occurs when fixed sized memory It occurs when variable size memory
blocks are allocated to the processes. space is allocated to the processes
dynamically.
2. In this fragmentation the fixed size In this fragmentation the memory
partition is assigned to a process space in the system can easily satisfy
whose size is less than the size of the the requirement of the processes, but
partition due to which the rest of the this available memory space is non-
space in the partition becomes contiguous, so it cannot be utilized
unusable. further.
contd. …
5.19
Operating Systems - I Memory Management
3. It mainly refers to the unused space It mainly refers to the unused blocks
in the partition that resides in the of the memory that are not
allocated region contiguous and hence are unable to
satisfy the requirements of the
process.
4. Internal fragmentation can be External fragmentation can be
eliminated by allocating memory to eliminated by compaction technique.
processes dynamically.
5.4 PAGING
• Paging is a memory management scheme that permits the physical address space of a
process to be non-contiguous.
• Paging is a memory management technique by which a computer stores and retrieves
data from secondary storage for use in main memory.
5.4.1 Basic Method [Oct. 18]
• The basic method for implementation of paging involves breaking physical memory
into fixed-sized blocks called frames (f) and break logical memory into blocks of the
same size called pages (p).
• When a process is to be executed its pages are loaded in to available frames from
backing store. The backing store is also divided in to fixed-sized blocks of same size as
the memory frames.
• The hardware support for paging is shown in Fig. 5.15.
f
Logical Physical
address address f0000...0000
CPU p d r d
f1111...1111
p
Physical
memory
f
Page table
Fig. 5.15: Paging Hardware
5.20
Operating Systems - I Memory Management
• Every address generated by the CPU is divided into two parts namely Page number (p)
and Page offset (d).
• The page number (p) is used as an index into a Page Table. A page table is used to map
a page on a frame. Page table has an entry for each page.
• The page table has the base address of each page in physical memory; which is
combined with page offset (d) to define the physical memory address which is sent to
memory unit.
• The paging model of memory is shown in Fig. 5.16.
Frame number
Page 0 0
0 1
Page 1 1 4 1 Page 0
2 3
Page 2 3 7 2
Page 3 Page table
3 Page 2
Logical memory
4 Page 1
7 Page 3
Physical memory
Fig. 5.16: Paging Model of Logical and Physical Memory
• Page size and frame is same and decided by hardware and is generally a power of 2
varying between 512 bytes and 16MB per page.
m n
• If the size of logical address is 2 and page size is 2 , then the high-order m-n bits of a
logical address designate the page number and n low-order bits designate the page
offset. Thus logical address is as follows:
page number page offset
p d
m-n n
Where, p is an index into page table and d is displacement within page.
• For example, consider the memory as shown in Fig. 5.14 using page size of 4 bytes and
physical memory of 32 bytes (8 pages).
• Logical address 0 is page 0 and offset 0. Page 0 is in frame 5. The logical address 0
maps to physical address 20 i.e. [(5 × 4) + 0].
• Logical address 3 is page 0 and offset 3 maps to physical address 23 i.e. [(5 × 4) + 3].
• Logical address 4 is page 1 and offset 0 and page 1 is mapped to frame 6. So logical
address 4 maps to physical address 24 i.e. [(6 × 4) + 0].
5.21
Operating Systems - I Memory Management
• Logical address 13 is page 3 and offset 1 and page 3 is mapped to frame 2. So logical
address 13 maps to physical address 9 i.e. [(2 × 4) + 1].
0a 0
1b
2c
3d
4 i
4 e j
5 f k
6 g 0 5
l
7 h 1 6
8 m
8 i 2 1 n
9 j o
10 k 3 2 p
11 l Page table 12
12 m
13 n
14 o
15 p 16
Logical memory
20 a
b
c
d
24 e
f
g
h
28
Physical memory
Fig. 5.17: Paging Example for a 32-word Memory with 4-word Pages
• Fig. 5.18 shows allocation of free frames.
page 0 17 page 0 17
page 1 page 1
page 2 page 2
page 3 18 page 3 18 Page 2
19 19
New job page table
20 20 Page 3
0 14
1 13
21 21
2 18
3 20
(a) (b)
Fig. 5.18: Allocation of Free Frames (a) Before and (b) After
Advantages of Paging: [Oct. 17]
1. Paging allows jobs to be allocated in non-contiguous memory locations.
2. In paging memory used more efficiently.
5.22
Operating Systems - I Memory Management
3. Paging does not require any support for dynamic relocation because paging itself
is a form of dynamic relocation.
4. Paging support higher degree of multiprogramming.
Disadvantages of Paged Memory Allocation:
1. Page address mapping hardware usually increases the cost of the operating
system.
2. Internal fragmentation still exists, though in last page.
3. Paging requires the entire job to be stored in memory location.
4. In paging the size of page is crucial (not too small, not too large).
• Every access to any of page has to go through the page table. Therefore,
implementation of the page table directly affects the performance of this kind of
memory management.
• Though there are different ways on which page table can be implemented, generally
the method depends upon the size of the page table.
1. Page Table Implemented as Dedicated Registers:
o The simplest method is that the page table is implemented as a set of dedicated
registers.
o These registers must be built with very high speed logic for making paging address
translation efficient. In dedicated registers implementation every access to
memory must go through the paging map.
o The use of registers for page table is satisfactory if the page table is small.
2. Page Table Implemented in the Main Memory:
o This method is used when page table is big or large. Page table is stored in the
main memory.
o PTBR (Pointer To Base Register) points to the base address of page table. The PTBR
is mainly a processor register and is managed by the operating system.
o In case, location of the page table is changed in the memory, only the contents of
PTBR are changed.
o This method gives reasonably slow access. The memory access is to be done twice,
once for accessing page table for getting frame number and secondly for accessing
actual frame.
3. Page Table Implemented in Associative Registers:
o The problem with page table implementation in the main memory approach is the
time required to access memory location.
o This problem can be solved by using special, small, fast lookup hardware cache,
called a Translation Look-aside Buffer (TLB) or associative registers as shown in
Fig. 5.19.
5.23
Operating Systems - I Memory Management
o Each entry in the TLB consists of two parts namely Key and Value. When
associative memory is presented with an item, the item is compared with all keys
simultaneously. If the item is found, the corresponding value field is returned.
Logical address
TLB hit
Physical
address
f d Physical
memory
TLB
p {
f
TLB miss
Page table
Fig. 5.19: Paging hardware with TLB
o The TLB is used with page tables in the following way:
The TLB contains only a few of the page-table entries. When a logical address is
generated by the CPU, its page number is presented to the TLB. If page number
is found, its frame number is immediately available and is used to access
memory.
If the page number is not in the TLB (TLB miss) a memory reference to the
page table must be made. In addition, we add the page number and frame
number into TLB.
If the TLB already full, the OS have to must select one for replacement. Some
TLBs allow entries to be wire down, meaning that they cannot be removed
from the TLB, for example kernel code.
Some TLBs store address-space identifiers (ASIDs) in each TLB entry. An ASID
uniquely identifies each process and is used to provide address-space
protection for that process.
When the TLB attempts to resolve virtual page numbers, it ensures that the
ASID for the currently running process matches the ASID associated with the
virtual page. If the ASIDs do not match, the attempt is treated as a TLB miss.
In addition to providing address-space protection, an ASID allows the TLB to
contain entries for several different processes simultaneously.
If the TLB does not support separate ASIDs, then every time a new page table is
selected (for instance, with each context switch), the TLB must be flushed (or
erased) to ensure that the next executing process does not use the wrong
translation information.
5.24
Operating Systems - I Memory Management
Otherwise, the TLB could include old entries that contain valid virtual
addresses but have incorrect or invalid physical addresses left over from the
previous process.
The percentage of times that a particular page number is found in the TLB is
called as hit ratio.
An 80-percent hit ratio means that we find the desired page number in TLB 80
percent of time. If it takes 20 nanoseconds to search the TLB and 100
nanoseconds to access memory, then mapped memory access takes 120
nanoseconds when page is found in TLB.
If TLB miss is there, then 220 nanoseconds will require for desired bytes. To
find the effective memory access-time, weight each case by probability.
Effective memory access time = 0.8 × (100 + 20) + 0.2 × (120 + 100) = 140
nanoseconds.
If our hit ratio is 98%, the effective memory access time = 0.98 × (100 + 20) +
0.02 × (120 + 100) = 122 nanoseconds.
5.4.3 Protection
• Memory protection in a paged environment is supported by protection bits associated
with each frame. Normally, these bits are kept in page table.
• One bit can define a page to be read-write or read only. Every reference to memory
goes through page table to find correct frame number.
• At same time, protection bits can be checked to verify that no writes are being made to
a read only page. An attempt to write to a read only page causes hardware trap to the
operating system.
• We can easily expand this approach to provide a finer level of protection. We can
create hardware to provide read-only, read-write, or execute-only protection or by
providing separate protection bits for each kind of access.
• We can allow any combination of these accesses. Illegal attempts will be trapped to the
operating system.
• One additional bit is attached to each entry in the page table i.e., a valid-invalid bit.
Illegal addresses are trapped by using the valid-invalid bit.
o When bit is set to “valid” indicates that the associated page is in the process’ logical
address space, and is thus a legal or valid page.
o When bit is set to “invalid” indicates that the page is not in the process’s logical
address space.
• Consider an example as shown in Fig. 5.20.
o Address space is 14 bits, (Address is from 0 to 16,383).
o Page size is 2K (2048 words)
o Program size is 0 to 10,468 (pages from 0 to 5).
5.25
Operating Systems - I Memory Management
ed 1 0
3
ed 2 1 data 1
4
6 2 data 3
ed 3
1 3 ed 1
data 1 page table
4 ed 2
for P1 ed 1
process P1
3 5
ed 2 4 6 ed 3
ed 3 6
7 7 data 2
data 2 page table 8
ed 1 for P2
process P2 9
3
ed 2 4 10
ed 3 6 11
2
data 3 page table
for P3
process P3
• Other heavily used programs can also be shared like compilers, window systems, run-
time libraries, database systems, and so on.
• To be sharable, the code must be reentrant. The read-only nature of shared code
should not be left to the correctness of the code; the operating system should enforce
this property.
• A segment is a logical unit such as main program, procedure, function, method, object,
local variables, global variables, common block, stack, symbol table, arrays etc., as
shown in Fig. 5.22.
• Thus, user prefers to view memory as collection of variable-sized segments, with no
necessary ordering among segments.
• A process in segmentation is divided into segments. In segmentation, the entire logical
address space is considered as a collection of segments with each segment having a
number and a length.
• The length of a segment may range from 0 to some maximum value as specified by
the hardware and may also change during the execution.
• For simplicity, segments are numbered and referred by segment number rather than
segment name. Thus, Logical address is contains, <segment number, offset>.
• The segment number tells the specific segment of the process from which the CPU
wants to read the data. The segment offset tells the exact word in that segment which
the CPU wants to read.
• A segment table stores the base address of each segment in the main memory. The use
of segment table is shown in Fig. 5.23.
• A register called Segment Table Base Register (STBR) which holds the base value of the
segment table. The segment table is also stored in the main memory itself.
s
limit base
CPU s d segment
table
Yes
< +
No
1400
Subroutine Stack Segment 0
Segment 3 2400
Segment 0
Limit Base
Symbol
table 0 1000 1400 3200
Segment 4 1 400 6300
SQRT 2 400 4300 Segment 3
3 1100 3200
Main 4 1000 4700
Segment 1 program 4300
Segment 2
Segment 2 Segment table 4700
Segment 4
5700
6300
Segment 1
Logical address space 6700
Physical memory
Fig. 5.24: Example of Segmentation
5.29
Operating Systems - I Memory Management
5.31
Operating Systems - I Memory Management
Page 1
Page 2
•
•
•
•
•
•
Page n
5.32
Operating Systems - I Memory Management
• Virtual memory makes the task of programming much easier because the
programmer no longer needs to worry about the amount of physical memory
available.
• Virtual memory is commonly implemented by demand paging. It can also be
implemented by demand segmentation.
5.6.2 Demand Paging [April 17, 19]
• Demand paging is a method of virtual memory management.
• With demand-paged virtual memory, pages are only loaded when they are demanded
during program execution; pages that are never accessed are thus never loaded into
physical memory.
• A demand-paging system is similar to a paging system with swapping, where processes
reside in secondary memory (usually a disk) as shown in Fig. 5.27. When we want to
execute a process, we swap it into memory.
• Rather than swapping the entire process into memory, however, we use a swapper is
also known as lazy swapper or pager. A lazy swapper never swaps a page into
memory unless that page will be needed.
• When a process is to be swapped in, the pager guesses which pages will be used before
the process is swapped out again. Instead of swapping in a whole process, the pager
brings only those necessary pages into memory.
• Thus, it avoids reading into memory pages that will not be used in anyway, decreasing
the swap time and the amount of physical memory needed.
• A swapper manipulates entire processes, whereas a pager is concerned with the
individual pages of a process. We thus use pager, rather than swapper, in connection
with demand paging.
Swap out 0 1 2 3
Program 4 5 6 7
A
8 9 10 11
12 13 14 15
Program 16 17 18 19
B Swap in
20 21 22 23
Main
memory
Fig. 5.27: Transfer of a Paged Memory to Contiguous Disk Space
5.33
Operating Systems - I Memory Management
• The hardware to support demand paging is same as hardware for paging and
swapping:
1. Page Table: This table has the ability to mark an entry invalid through a valid-
invalid bit or special value of protection bits.
2. Secondary Memory: It is usually high speed disk; also known as swap device. It
holds those pages that are not in main memory.
• To distinguish between the pages that are in memory and the pages that are on the
disk, valid-invalid bit is used.
• When this bit is set to “valid”, the associated page is legal and in main memory. When
this bit is set to “invalid”, page may not be valid or page is valid but currently on the
disk as shown in the Fig. 5.28.
0
frame valid-invalid bit 1
0 A 2
0 4 v
1 B 3
C 1 i
2 4 A
2 6 v
3 D
3 i 5
4 E C A B
4 i 6
5 F v
5 9 7 C D E
6 G 6 i
8
H i F G H
7 7 9 F
Logical Page table 10
memory
11
12
13
14
15
Physical
memory
Fig. 5.28: Page Table with Valid-Invalid Bit
• Whenever, process tries to access a page which is not into memory, and then page
fault occurs.
• The paging hardware, which translates the address through page table, will notice that
invalid bit is set and cause trap to the operating system.
• A page fault occurs when a program accesses a page that has been mapped in address
space, but has not been loaded in the physical memory.
• When the page (data) requested by a program is not available in the memory, it is
called as a page fault.
5.34
Operating Systems - I Memory Management
• The steps for handling page fault are as follows and shown in Fig. 5.29.
Step 1: Check an internal table for the process, to determine whether the reference
was a valid or it was an invalid memory access.
Step 2: If the reference was invalid, the process is terminated. If it was valid, but we
not yet brought in memory, we now page it in.
Step 3: Find a free frame, possibly from the free frame list.
Step 4: Schedule a disk operation to read the desired page into the newly allocated
frame.
Step 5: When the disk read operation is complete, the process's page table is updated
with the new frame number, and the invalid bit is changed to indicate that
this is now a valid page reference. The page is now in memory.
Step 6: Restart the instruction that was interrupted by the illegal address trap. The
process can now access the page as though it had always been in memory.
• Therefore, the operating system reads the desired page into memory and restarts the
process as though the page had always been in memory.
Page is on
3 backing store
Operating
system
2 Trap
1 Reference
Load M
i
6
Restart Page table
instruction
Free frame
5 Reset page 4
table
Bring in
missing page
Physical
memory
Fig. 5.29: Steps in Handling a Page Fault
Pure Demand Paging:
• In demand paging, when process starts execution with no pages in memory, when
operating system sets the instruction pointer to the first instruction of the process,
process immediately faults for the page.
5.35
Operating Systems - I Memory Management
• After this page is brought into memory, the process continues to execute, faulting as
necessary until every page that it needs is in memory. Thus, it never brings a page
into memory until it is required.
Advantages of Demand Paging:
1. Large virtual memory.
2. More efficient use of memory.
3. Unconstrained multiprogramming. There is no limit on degree of multi-
programming.
Disadvantages of Demand Paging:
1. Number of tables and amount of processor overhead for handling page interrupts
are greater than in the case of the simple paged management techniques.
2. Due to the lack of an explicit constraint on a job address space size.
5.6.3 Performance of Demand Paging
• Demand paging can significantly affect the performance of a system. For most
computer systems, memory access time ranges from 10 to 200 nanoseconds.
• As long as we have no page faults, the effective access time is equal to memory access
time. If page fault occurs, we must first read the relevant page from disk and access
desired word.
• The performance of demand paging is often measured in terms of the effective access
time.
• Let p be probability of page fault.
If p = 0 then no page fault occurs.
If p = 1, every reference is a fault.
Effective Access Time (EAT) = (1 – p) × Memory access + p (Page Fault Overhead + Swap
Page Out + Swap Page In + Restart Overhead)
• To compute the effective access time, we must know how much time is needed to
service a page fault.
• Three major components affect the page fault service-time:
1. Service the page-fault interrupt,
2. Read in the page, and
3. Restart the process.
• Let us assume that Memory access time = 200 nanoseconds.
• Average page-fault service time = 8 milliseconds
EAT = (1 – p) × 200 + p (8 milliseconds)
= (1 – p) × 200 + p × 8,000,000
= 200 + p × 7,999,800
• If one access out of 1,000 causes a page fault, then EAT = 8.2 microseconds. This is a
slowdown by a factor of 40 because of demand paging. Thus effective access time is
directly proportional to page-fault rate.
5.36
Operating Systems - I Memory Management
• The approach of page replacement is, if no frame is free, we find one that is not
currently being used and free it.
• We can free a frame by writing its contents to swap space and changing the page table
(and all other tables) to indicate that the page is no longer in memory.
• We can now use the freed frame to hold the page for which the process faulted. We
modify the page-fault service routine to include page replacement.
• Fig. 5.31 shows steps in page replacement.
Frame Valid-invalid bit
swap out
Change victim
2 to invalid
o i 1 page
f v
4 f victim
Reset page
Page table table for 3 swap
new page desired
page in
Physical
memory
Fig. 5.31
• Steps in page replacement are listed below:
1. Find the location of the desired page on the disk.
2. Find a free frame:
(i) If there is a free frame, use it.
(ii) If there is no free frame, use a page-replacement algorithm to select a victim
frame.
(iii) Write the victim frame to the disk; change the page and frame tables
accordingly.
3. Read the desired page into the newly freed frame; change the page and frame
tables.
4. Restart the user process.
• In an operating system that uses paging for memory management, a page replacement
algorithm is needed to decide which page needs to be replaced when new page comes
in.
• Page replacement algorithms are the techniques using which an Operating System
decides which memory pages to swap out, write to disk when a page of memory needs
to be allocated.
5.38
Operating Systems - I Memory Management
• Page replacement algorithm decides which page to remove, also called swap out when
a new page needs to be loaded into the main memory.
• The different page replacement algorithms are FIFO (First In First Out) page
replacement algorithm, Optimal page replacement algorithm, LRU (Least Recently
Used) page replacement algorithm and so on.
• Page replacement algorithms works on the string of memory references, called as
reference string.
• It also needs the available number of page frames. Then the number of page faults is
determined for a particular reference string.
• Referenced string is a se0.quence of pages being referenced. For example, consider the
sequence of addresses 123, 215, 600, 1234, 76, 96. If page size is 100, then the reference
string is 1,2,6,12,0,0
FIFO (First In First Out) Page Replacement Algorithm: [April 16, 17, Oct. 17]
• FIFO is one of the simplest page replacement algorithms. In this algorithm, all the
available frames are given to the pages from reference string serially i.e. first frame to
first page, second frame to second page and so on.
• When all the available frames are over at that time, the first frame is selected as victim
frame and at next time the second frame and so on.
• In FIFO algorithm, the operating system maintains a queue that keeps track of all the
pages in memory.
• When a page must be replaced, the oldest page is chosen. FIFO queue is created to hold
all pages in memory. We replace the page at the head of the queue. When a page is
brought into memory, we insert it at the tail of the queue.
For example: Consider the reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 and
Number of frames are 3.
1 2 3 1 2 3 5 1 2
Page frame 0 1 1 1 4 4 4 5 5 5
Page frame 1 2 2 2 1 1 1 1 1
Page frame 2 3 3 3 2 2 2 2
Page faulted Y Y Y Y Y Y Y N N
3 4 5
Page frame 0 5 5 5
Page frame 1 3 3 3 Total no. of page faults = 9
Page frame 2 2 4 4
Page faulted Y Y N
Fig. 5.32: Example of FIFO Page Replacement Algorithm
5.39
Operating Systems - I Memory Management
Explanation:
o Number of page frames given are 3, numbered page frame 0, page frame 1 and
page frame 2.
o Reference string is scanned serially.
o If the page is not present in one of the three page frames, then a page fault is
counted and a page is brought into the page frame.
o In the above examples, first 3 pages 1, 2, 3 are brought in page frames 0, 1 and 2
respectively.
o When the page faulted for page 4, that time page frame containing page 1 is freed
and 4 is brought in page frame 0.
o Page frame 0 was selected as victim frame because that was containing first of the
first 3 pages.
o Next victim frame will be page frame 1 and so on. Total number of page faults in
above example is 9.
Advantages of FIFO Page Replacement Algorithm:
1. It is simple to implement.
2. It is easiest algorithm
3. Easy to understand and execute.
Disadvantages of FIFO Page Replacement Algorithm:
1. It is not very effective.
2. System needs to track of each frame.
3. Its performance is not always good.
4. It suffers from Belady’s anomaly.
5. Bad replacement choice increases the page fault rate and slow process execution.
Belady's Anomaly:
• Belady’s anomaly is problem occurs in FIFO page replacement algorithm. Belady’s
anomaly is the phenomenon of increasing the number of page faults on increasing the
number of frames in main memory.
• For example: We will again take the previous reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3,
4, 5 and the number of available page frames be 4.
1 2 3 4 1 2 5 1
Page frame 0 1 1 1 1 1 1 5 5
Page frame 1 2 2 2 2 2 2 1
Page frame 2 3 3 3 3 3 3
Page frame 3 4 4 4 4 4
Page faulted Y Y Y Y N N Y Y
2 3 4 5
Page frame 0 5 5 4 4
Page frame 1 1 1 1 5 Total no. of page faults = 10
Page frame 2 2 2 2 2
Page frame 3 4 3 3 3
Page faulted Y Y Y Y
5.40
Operating Systems - I Memory Management
• In above example, page fault rate is increased though number of page frames is
increased.
• For some page replacement algorithms, the page fault rate may increase as the
number of allocated frames increases. Such exception is known as Belady's Anomaly.
Optimal Page Replacement Algorithm: [Oct. 16, 17, April 19]
• In optimal page replacement algorithm, the page that will not be required for longest
period of time is replaced.
• Page fault rate in this case is less as compared to FIFO, hence the name optimal.
• An optimal page replacement algorithm has the lowest page fault rate of all algorithms
and would never suffer from Belady’s anomaly.
• An optimal page-replacement algorithm exists, and has been called OPT or MIN.
• With optimal page replacement algorithm, the page replacement decision requires
complete reference string to be prepared before the execution of the algorithm starts
because every page replacement needs to refer the reference string for the
requirement of a particular page in future.
• Practically, this situation is difficult to achieve. Therefore, this algorithm is difficult to
implement.
For example: Reference string is 1, 3, 3, 2, 5, 4, 5, 4, 1, 4, 2, 2, 5 and Number of page
frames are 3.
1 3 3 2 5 4 5 4
Page frame 0 1 1 1 1 1 1 1 1
Page frame 1 3 3 3 5 5 5 5
Page frame 2 2 2 4 4 4
Page faulted Y Y N Y Y Y N N
1 4 2 2 5
Page frame 0 1 1 2 2 2
Page frame 1 5 5 5 5 5 Total no. of page faults = 6
Page frame 2 4 4 4 4 4
Page faulted N N Y N N
Fig. 5.34: Optimal Page Replacement Algorithm
Example: Consider the reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. Calculate the page
fault using Optimal Page Replacement using 3 frames.
Solution:
Reference String → 1 2 3 4 1 2 5 1 2 3 4 5
Frames
↓
1 *1 1 1 1 1 1 1 1 1 *3 3 3
2 *2 2 2 2 2 2 2 2 2 *4 4
3 *3 *4 4 4 *5 5 5 5 5 5
Page Fault Y Y Y Y N N Y N N Y Y N
Total Page faults = 7
Fig. 5.35
5.41
Operating Systems - I Memory Management
4 2 2 5
Page frame 0 5 2 2 2
Page frame 1 4 4 4 4 Total no. of page faults = 8
Page frame 2 1 1 1 5
Page faulted N Y N Y
Fig. 5.36: Least Recently Used (LRU) Page Replacement Algorithm
Explanation:
o First 3 pages 1, 3 and 2 are brought into page frames 0, 1 and 2 respectively. When
page fault for page 5 occurs, that time frame 0 is selected as victim frame.
o If you observe the reference string upto page 5, page number 2 is nearest to page 5
and page number 1 is farthest from page 5. The farthest page is the one which is
least recently used.
o Therefore, page frame 0 is selected as victim frame (containing the farthest page)
th
and 5 page is brought into it.
o To bring page number 4 in the memory, frame 1 is selected as victim frame,
because it contains the page 3 farthest from page number 4 (farthest in the past)
and so on.
5.42
Operating Systems - I Memory Management
2 7
a b
1 2
0 1
7 0
4 4
Stack Stack
before after
a b
Fig. 5.37: Use of a Stack to Record the Most Recent Page References
Advantages of LRU Page Replacement Algorithm:
1. LRU is actually quite a good algorithm.
2. It never suffers from Belady’s anomaly.
3. LRU algorithm is very feasible to implement.
Disadvantages of LRU Page Replacement Algorithm:
1. LRU algorithm is that it requires additional data structure and hardware support.
2. Its implementation is not very easy.
Example: Consider the reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. Calculate the page
fault using LRU Page Replacement using 3 frames.
Reference String → 1 2 3 4 1 2 5 1 2 3 4 5
Frames
↓
1 *1 1 1 *4 4 4 *5 5 5 *3 3 3
2 *2 2 2 *1 1 1 1 1 1 *4 4
3 *3 3 3 *2 2 2 2 2 2 *5
Page Fault Y Y Y Y Y Y Y N N Y Y Y
Total Page Faults = 10.
5.43
Operating Systems - I Memory Management
SOLVED PROBLEMS
Problem 1: Consider the page reference string 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2. how many
page faults occur for the following replacement algorithm, assuming 3 frames? (i) FIFO
(ii) LRU (iii) Optimal.
Solution: Using FIFO Page Replacement Algorithm:
Reference String → 2 3 2 1 5 2 4 5 3 2 5 2
Frames
↓
1 *2 2 2 2 *5 5 5 5 *3 3 3 3
2 *3 3 3 3 *2 2 2 2 2 *5 5
3 *1 1 1 *4 4 4 4 4 *2
Page Fault Y Y N Y Y Y Y N Y N Y Y
Total Number of Page fault using three frames=9.
Using LRU Page Replacement Algorithm:
Reference String → 2 3 2 1 5 2 4 5 3 2 5 2
Frames
↓
1 *2 2 2 2 2 2 2 2 *3 3 3 3
2 *3 3 3 *5 5 5 5 5 5 5 5
3 *1 1 1 *4 4 4 *2 2 2
Page Fault Y Y N Y Y N Y N Y Y N N
Total Number of Page fault using three frames=7.
Using Optimal Page Replacement Algorithm:
Reference String → 2 3 2 1 5 2 4 5 3 2 5 2
Frames
↓
1 *2 2 2 2 2 2 *4 4 4 4 4 4
2 *3 3 3 3 3 3 3 3 *2 2 2
3 *1 *5 5 5 5 5 5 5 5
Page Fault Y Y N Y Y N Y N N Y N N
Total Number of Page fault using three frames=6.
Problem 2: Consider the page reference string 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 3. How many
page faults will occur for FIFO and LRU page replacement algorithms? Assuming 3 frames,
all are initially empty.
5.44
Operating Systems - I Memory Management
5.46
Operating Systems - I Memory Management
4. Logical address = 2, 77
Segment No. = 2
Offset = 77
Base address = 75
Physical address = 75 + 77
= 152
5. Logical address = 4,444
Segment No. = 4
Offset = 444
Base address = 2510
Physical address = 2510 + 444
= 2954
Problem 5: Consider the page reference string 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3,
2, 1, 2, 3.
How many page faults would occur for the LRU and optimal page replacement
algorithms, assuming 4 frames? All frames are initially empty.
Solution:
1. LRU Replacement Method:
Reference String 1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3
Frame 1 1 1 1 1 1 1 1 1 1 1 1 1 1 6 6 6 6 6 6
Frame 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Frame 3 3 3 3 3 5 5 5 5 5 3 3 3 3 3 3 3 3
Frame 4 4 4 4 4 6 6 6 6 6 7 7 7 7 1 7 7
Page Fault Y Y Y Y N N Y Y N N N Y Y Y N N Y N N
Frame 1 1 1 1 1 1 1 1 1 1 1 1 1 7 7 7 7 1 1 1
Frame 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Frame 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
Frame 4 4 4 4 5 6 6 6 6 6 6 6 6 6 6 6 6
Page Fault Y Y Y Y N N Y Y N N N N Y N N N Y N N
5.47
Operating Systems - I Memory Management
Frame 3 1 1 1 3 3 3 3 3 3 3 3 1 1 1 1 1 1
Page Fault Y Y Y Y N Y N Y N N Y N N Y N N Y N N
Frame 1 7 7 7 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
Frame 2 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
Frame 3 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
Page Fault Y Y Y Y Y Y Y Y Y Y N N Y Y N N Y Y Y
2. Logical address = 40
Page No. = 40/8 = 5
Page offset = 40 % 8 = 0
Frame No = 5
Base address = 5 × 8 = 40
Physical address = 40 + 0 = 40
3. Logical address = 23
Page No. = 23/8 ⇒ 2
Page offset = 23 % 8 ⇒ 7
Frame No. = 1
Base address = 1 × 8 = 8
Physical address = 8 + 7 ⇒ 15
4. Logical address = 36
Page No. = 36/8 = 4
Page offset = 36 % 8 = 4
Frame No. = 10
Base address = 10 × 4 = 40
Physical address = 40 + 4 ⇒ 44
5. Logical address = 68
Page No. = 68/8 = 8
Page offset = 4
We can't find frame number since page 8 is not loaded in page table, so we can not
find physical address.
Problem 8: Consider the page reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. How many
page faults would occur for the LRU and FIFO page replacement algorithms? Assume three
frames.
Solution: LRU Page Replacement Algorithm:
Reference
1 2 3 4 1 2 5 1 2 3 4 5
String
Frame 1 1 1 1 4 4 4 5 5 5 3 3 3
Frame 2 2 2 2 1 1 1 1 1 1 4 4
Frame 3 3 3 3 2 2 2 2 2 2 5
Page Fault Y Y Y Y Y Y Y N N Y Y Y
5.49
Operating Systems - I Memory Management
Frame 1 1 1 1 4 4 4 5 5 5 5 5 5
Frame 2 2 2 2 1 1 1 1 1 3 3 3
Frame 3 3 3 3 2 2 2 2 2 4 4
Page Fault Y Y Y Y Y Y Y N N Y Y N
Frame 1 7 7 7 2 2 2 2 4 4 4 0 0 0 1 1
Frame 2 0 0 0 0 0 0 0 0 3 3 3 3 3 3
Frame 3 1 1 1 3 3 3 2 2 2 2 2 2 2
Page Fault Y Y Y Y N Y N Y Y Y Y N N Y N
Frame 1 7 7 7 2 2 2 2 2 2 2 2 2 2 2 2
Frame 2 0 0 0 0 0 0 4 4 4 0 0 0 1 1
Frame 3 1 1 1 3 3 3 3 3 3 3 3 3 3
Page Faults Y Y Y Y N Y N Y N N Y N N Y N
5.50
Operating Systems - I Memory Management
Problem 13: Consider a computer system with 40-bit virtual addressing and page size
of sixteen kilobytes. If the computer system has a one-level page table per process and
each page table entry requires 48 bits, then what is the size of the per-process page table?
Solution:
40
Size of memory = 2
14
Page size = 16KB = 2
40 14 26
No of pages= size of Memory/Page size = 2 / 2 = 2
26 6
Size of page table = 2 * 48/8 bytes = 2 * 6 MB = 384 MB
Problem 14: Let the page fault service time be 10ms in a computer with average
6
memory access time being 20ns. If one page fault is generated for every 10 memory
accesses, what is the effective access time for the memory?
Solution:
Let P be the page fault rate
Effective Memory Access Time = p * (page fault service time) +
(1 - p) * (Memory access time)
6 6 6
= ( 1/(10 ) )* 10 * (10 ) ns + (1 - 1/(10 )) * 20 ns
= 30 ns (approx)
Problem 15: In a paged memory, the page hit ratio is 0.40. The time required to access
a page in secondary memory is equal to 120 ns. The time required to access a page in
primary memory is 15 ns.
Solution:
Average access time = hit ratio * primary memory access time + (1 – hit ratio) *
secondary memory access time
Average access time = 0.4 * 15 + 0.6 * 120
Average access time = 6 + 72
Average access time = 78.
Problem 16: What is the size of the physical address space in a paging system which
has a page table containing 64 entries of 11 bit each (including valid and invalid bit) and a
page size of 512 bytes?
Solution:
Size of Physical Address = Paging bits + Offset bits
Paging bits = 11 – 1 = 10 (As 1 valid bit is also included)
Offset bits = log2 page size = 9
Size of Physical Address = 10 + 9 = 19 bits.
Problem 17: If there are 32 segments, each size 1 k bytes, then how many bits will be
in the logical address?
Solution:
Data: Logical address size = L bytes
No of segments = n = 32
10
Size of each segment = s = k byte = 1 × 2 bytes
Formula:
Number of bits = log2 L
Calculation:
L = n × s = bytes
10 15
L = 32 × 2 = 2 bytes
15
Number of bits = log2 2 = 15
5.52
Operating Systems - I Memory Management
Problem 18: A memory management system has 64 pages with 512 bytes page size.
Physical memory consists of 32 page frames. How many number of bits required in logical
and physical address?
Solution:
We know that Number of pages = virtual memory space / page size.
Number of frames = physical memory space / frame size.
Page size is equal to frame size.
According to question and given data:
virtual memory space = Number of pages * page size
4 12
i.e. virtual memory space = 6 * 5 B
6 9
virtual memory space = 2 * 2 B
15
i.e., = 2 B.
So, 15 bits are required for virtual memory space.
Physical memory space = Number of frames * frame size.
2 12
Physical memory space = 3 * 5 B
5 9
Physical memory space = 2 * 2 B
14
i.e., = 2
So, 14 bits are required for physical memory space.
PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. Which is an important function of operating system that helps in allocating the
main memory space to the processes and their data at the time of their execution?
(a) Memory management (b) Device management
(c) CPU management (d) I/O management
2. A memory buffer used to accommodate a speed differential is called as,
(a) stack pointer (b) cache
(c) accumulator (d) disk buffer
3. Which one of the following is the address generated by CPU?
(a) physical address (b) absolute address
(c) logical address (d) none of the mentioned
4. Run time mapping from virtual to physical address is done by,
(a) Memory management unit (b) CPU
(c) PCI (d) None of the mentioned
5. Memory management technique in which system stores and retrieves data from
secondary storage for use in main memory is called?
(a) fragmentation (b) paging
(c) mapping (d) none of the mentioned
6. The address of a page table in memory is pointed by,
(a) stack pointer (b) page table base register
(c) page register (d) program counter
5.53
Operating Systems - I Memory Management
5.55
Operating Systems - I Memory Management
75. In which one of the following page replacement algorithms it is possible for the
page fault rate to increase even when the number of allocated frames increases?
(a) LRU (Least Recently Used) (b) OPT (Optimal Page Replacement)
(c) MRU (Most Recently Used) (d) FIFO (First In First Out)
Answers
1. (a) 2. (b) 3. (c) 4. (a) 5. (b) 6. (d) 7. (a) 8. (a) 9. (c) 10. (a)
11. (a) 12. (c) 13. (a) 14. (d) 15. (a) 16. (a) 17. (c) 18. (b) 19. (a) 20. (a)
21. (a) 22. (a) 23. (a) 24. (a) 25. (d) 26. (b) 27. (a) 28. (a) 29. (b) 30. (b)
31. (b) 32. (c) 33. (b) 34. (d) 35. (b) 36. (c) 37. (c) 38. (d) 39. (b) 40. (a)
41. (a) 42. (d) 43. (a) 44. (a) 45. (b) 46. (b) 47. (d) 48. (c) 49. (c) 50. (b)
51. (c) 52. (d) 53. (b) 54. (a) 55. (a) 56. (c) 57. (a) 58. (b) 59. (c) 60. (b)
61. (a) 62. (c) 63. (c) 64. (a) 65. (c) 66. (b) 67. (a) 68. (c) 69. (c) 70. (b)
71. (c) 72. (c) 73. (b) 74. (d) 75. (d)
Q.II Fill in the Blanks:
1. The task of subdividing memory between the O/S and processes is performed
automatically by the O/S and is called _______ management.
2. The Memory Management task of moving the process image between different
areas of memory as required to support swapping is referred to as _______.
3. The practice in which a program and data are organized in such a way that
various modules can be assigned the same region of memory is called _______.
4. In almost all modern multiprogramming systems, memory is managed using a
sophisticated technique known as _______ memory.
5. The 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
_______ fragmentation.
6. The problem of internal fragmentation can be lessened in system employing a
fixed-partition memory management scheme by using _______ size partitions.
7. In the Dynamic Partitioning technique of memory management, the process of
shifting processes so they occupy a single contiguous block in memory is called
_______.
8. In the Dynamic Partitioning technique of memory management, the placement
algorithm that chooses the block that is closest in size to the request is called
_______.
9. In the Dynamic Partitioning technique of memory management, the phenomenon
that results in unused blocks of memory outside of existing partitions is called
_______ fragmentation.
10. Programs that employ _______ addresses in memory are loaded using dynamic run-
time loading.
5.61
Operating Systems - I Memory Management
11. In a system that employs a paging memory management scheme, the _______ table
shows the frame location for each page of the process.
12. _______ refers to the technique of dividing the physical memory space into multiple
blocks. Each block has specific length and is known as a segment and each
segment has a starting address called the base address.
13. _______ is a technique in which the main memory of computer system is organized
in the form of equal sized blocks called pages.
14. _______ is the technique involves performing two tasks called swapping in and
swapping out.
15. The set of all logical addresses generated by a program is referred to as a _______
address space.
16. Dynamic _______ is the technique through which a computer program at runtime
loads a library into memory.
17. Static _______ is done at the time of compilation of the program.
18. _______ memory is a memory management technique that provides an idealized
abstraction of the storage resources that are actually available on a given machine
which creates the illusion to users of a very large (main) memory.
19. Segment Table Base Register (STBR) is used to point the segment table's _______ in
the memory.
20. A _______ paging mechanism is very much similar to a paging system with
swapping where processes stored in the secondary memory and pages are loaded
only on demand, not in advance.
21. Page _______ algorithms are the techniques using which an Operating System
decides which memory pages to swap out, write to disk when a page of memory
needs to be allocated.
22. The string of memory references is called _______ string.
23. An _______ page-replacement algorithm has the lowest page-fault rate of all
algorithms. An optimal page-replacement algorithm exists, and has been called
OPT or MIN.
24. Least Recently Used (LRU) page replacement algorithm keeps track of page usage
over a _______ period of time.
25. In First In First Out (FIFO) algorithm the OS maintains a _______ that keeps track of
all the pages in memory, with the oldest page at the front and the most recent page
at the back.
26. Dynamic _______ defers much of the linking process until a program starts running.
27. The _______ mapping from virtual to physical address is done by the Memory
Management Unit (MMU) which is a hardware device.
5.62
Operating Systems - I Memory Management
Answers
1. memory 2. relocation 3. overlaying 4. virtual
5. internal 6. unequal 7. compacting 8. best-fit
9. external 10. relative 11. page 12. Segmentation
13. Paging 14. Swapping 15. logical 16. loading
17. linking 18. Virtual 19. location 20. demand
21. replacement 22. reference 23. optimal 24. short
25. queue 26. linking 27. runtime
Q.III State True or False:
1. The concept of virtual memory is based on one or both of two basic techniques
namely, segmentation and paging.
2. A major problem with the largely obsolete Fixed Partitioning memory
management technique is that of external fragmentation.
3. The problem of internal fragmentation can be lessened in a system employing a
fixed-partition memory management scheme by using unequal size partitions.
4. In the Dynamic Partitioning technique of memory management, the best fit
placement algorithm is usually the best performer of the available algorithms.
5. In the Dynamic Partitioning technique of memory management, compaction refers
to shifting the processes into a contiguous block, resulting in all the free memory
aggregated into in a single block.
6. In the Dynamic Partitioning technique of memory management, the first-fit
placement algorithm scans memory from the location of the last placement and
chooses the first available block it finds that satisfies the request.
7. The modify (M) bit is a control bit in a page table entry that indicates whether the
contents of the corresponding page have been altered since the page was last
loaded into memory.
8. A Page Fault occurs when the desired page table entry is not found in the
Translation Look-aside Buffer (TLB).
9. One of the advantages to the programmer of virtual memory using segmentation is
that it simplifies the handling of growing data structures by allowing the segment
to grow or shrink as necessary.
10. In a combined paging/segmentation system, a user’s address space is broken up
into a number of fixed-size pages, which in turn are broken up into a number of
segments.
11. The set of all physical addresses corresponding to these logical addresses is
referred to as a physical address space.
12. In internal fragmentation the total memory space is enough to satisfy a request or
to reside a process in it, but it is not contiguous, so it cannot be used.
13. Paging technique plays an important role in implementing virtual memory.
14. Segmentation is a memory management technique in which each job is divided
into several segments of different sizes, one for each module that contains pieces
that perform related functions.
15. A physical address is generated by CPU while a program is running.
5.63
Operating Systems - I Memory Management
16. The hardware device called Memory-Management Unit is used for mapping logical
address to its corresponding physical address.
17. A logical address identifies the physical location of a specific data element in
memory.
18. Paging is a memory management technique in which process address space is
broken into blocks of the same size called pages.
19. A computer's address space is the total amount of memory that can be addressed
by the computer.
20. Linking is a method that helps OS to collect and merge various modules of code
and data into a single executable file. In dynamic linking method, libraries are
linked at execution time.
21. In swapping any process must be in the memory for its execution, but can be
swapped temporarily out of memory to a backing store and then again brought
back into the memory to complete its execution.
22. Memory allocation is a process by which computer programs are assigned
memory or space.
23. Segment Table Length Register (STLR) indicates the number of segments used by a
CPU.
24. The segment table has two entries for each segment i.e. segment base address and
segment limit.
25. A demand paging system is quite similar to a paging system with swapping where
processes reside in secondary memory and pages are loaded only on demand, not
in advance.
26. In the fixed-sized partition, the memory is divided into fixed-sized blocks and each
block contains exactly two process.
27. The internal fragmentation occurs when the process does not fully utilize the
memory allocated to it.
28. A program segment contains the program's main function, utility functions, data
structures, and so on.
29. Page replacement algorithms are the techniques using which an Operating System
decides which memory pages to swap out, write to disk when a page of memory
needs to be allocated.
30. Reference strings are generated artificially or by tracing a given system and
recording the address of each memory reference.
31. Page replacement algorithms help to decide which page must be swapped out from
the main memory to create a room for the incoming page.
32. FIFO replaces the oldest page that has been present in the main memory for the
longest time.
33. Optimal Page Replacement algorithm is the best page replacement algorithm as it
gives the least number of page faults.
34. Least Recently Used (LRU) page replacement algorithm keeps track of page usage
over a short period of time. It works on the idea that the pages that have been most
heavily used in the past are most likely to be used heavily in the future too.
5.64
Operating Systems - I Memory Management
Answers
1. (T) 2. (F) 3. (T) 4. (F) 5. (T)
6. (F) 7. (T) 8. (F) 9. (T) 10. (F)
11. (T) 12. (F) 13. (T) 14. (T) 15. (F)
16. (T) 17. (F) 18. (T) 19. (T) 20. (T)
21. (T) 22. (T) 23. (F) 34. (T) 25. (T)
26. (F) 27. (T) 28. (T) 29. (T) 30. (T)
31. (T) 32. (T) 33. (T) 34. (T)
Q.IV Answers the following Questions:
(A) Short Answer Questions:
1. What is memory management?
2. What is logical and physical address?
3. What is logical and physical address space?
4. What address binding?
5. What is dynamic loading?
6. Define dynamic linking.
7. What is meant by shared libraries?
8. Define swapping.
9. What is fragmentation?
10. What is paging?
11. What is meant by page replacement?
12. What is virtual memory?
13. What segmentation?
14. Define demand paging.
15. What is page and page table?
16. What is page replacement algorithm.
17. What is FIFO?
18. Compare LRU and MFU with any two points.
(B) Long Answer Questions:
1. What is memory management? List its functions.
2. Describe logical and physical address explain in detail. Also differentiate them.
3. Define address binding? Describe with diagrammatically.
4. Write short note on: Dynamic linking and Shared libraries.
5. With the help of diagram describe swapping.
6. Describe segmentation in detail.
7. What is fragmentation? Explain with its types.
5.65
Operating Systems - I Memory Management
5.66
Operating Systems - I Memory Management
October 2016
1. Define physical address space. [1 M]
Ans. Refer to Section 5.1.3.
2. Consider page reference string: 7, 5, 6, 2, 9, 5, 7, 6, 2, 7, 6, 5, 2, 7, 2, 7, 8. Assume 3
frames. Find the number of page faults according to:
(i) Optimal page replacement algorithm.
(ii) LRU page replacement algorithm. [5 M]
Ans. Refer to Section 5.6.4.1.
3. Explain MVT with advantage and disadvantages. [4 M]
Ans. Refer to Section 5.3.2.2.
April 2017
1. What is the role of valid and invalid bit in demand paging? [1 M]
Ans. Refer to Section 5.6.2.
2. Consider page reference string: 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 3. Assume 3 frames. Find the
number of page faults according to:
(i) FIFO page replacement algorithm.
(ii) LRU page replacement algorithm. [5 M]
Ans. Refer to Section 5.6.4.1.
3. What is external fragmentation? What are various ways to avoid external
fragmentation? [4 M]
Ans. Refer to Section 5.3.3.
October 2017
1. What is the advantage of paging with segmentation model? [1 M]
Ans. Refer to Section 5.4.
2. What is fragmentation? Explain internal and external fragmentation in
detail. [5 M]
Ans. Refer to Section 5.3.3.
3. Consider the page reference string: 7, 5, 6, 2, 9, 5, 7, 6, 2, 7, 6, 5, 2, 7, 2, 7, 8. How
many page faults will occur for the following page replacement algorithm?
Assume 3 frames: (i) FIFO, (ii) Optimal Replacement. [1 M]
Ans. Refer to Section 5.6.4.1.
April 2018
1. Using segmentation, find the physical address from the logical address 2,280,
having segment size 285 with base address 3000. [1 M]
Ans. Refer to Section 5.5.
2. List various dynamic allocation memory management methods. [1 M]
Ans. Refer to Page 5.16.
3. What is fragmentation? Explain types of fragmentation with suitable
example. [5 M]
Ans. Refer to Section 5.3.3.
5.67
Operating Systems - I Memory Management
5.68