OS-Module-1 (6 Files Merged)
OS-Module-1 (6 Files Merged)
INTRODUCTION TO OPERATING
SYSTEMS
Program
Software
Types of software
1) System software
2) Application software
System software
Example system software are: Operating System, Compiler, Linker, Loader, Assembler, debugger, driver and so on.
System Software is generally installed in the computer when the operating system is installed.
The user does not interact with the system software because it works in the background.
Application software
Software which depends on other software for services is called as application software.
Example application software are: VLC player, web browser, word processor and so on.
Computer
Some of the hardware resources are: processor, HD, RAM, keyboard, monitor and so on.
Some of the software resources are: operating system, compiler, interpreter, loader, assembler and so on.
Operating System
An Operating System is a system software that manages the computer hardware.
Operating System acts as an intermediary between the computer user and the computer hardware.
Some operating systems are designed to provide convenient communication between user and computer system, others
for efficient utilization of resources, and others to provide both.
The hardware (the central processing unit, the memory and the I/O devices) provides the computing resources for the
computer system.
The system programs (compilers, assemblers and so on) and application programs (word processors, spreadsheets, web
browsers and so on) help the user programs in their execution.
The operating system controls the hardware and coordinates the use of hardware among various user programs.
The role of operating system is fully explained by considering two views: user view and system view.
User view
Computer systems are divided into four categories from user point of view.
1) Personal computers
Used by single person for home applications. The Operating System used in personal computers should focus on the
following
2) Thin clients
A thin client is a system which do not have any processing capability and storage capacity.
Thin clients are connected to a server system which has storage capacity and execution capability.
The programs typed in the thin clients are stored and executed in server system.
The operating system used in thin clients should concentrate on resource utilization.
The server system provides service to all client systems connected to it.
The Operating System used in client system should use resources of the client system as well as resources available at the
server system.
4) Home devices or devices used in automobiles
The Operating System used in home devices should work automatically without any intervention of user.
System view
From system point of view, the Operating System can be viewed as resource allocator.
The Operating System allocates the resources of the computer system to the programs that are currently running in the
system.
Computer System Organization
A modern computer system consists of one or more CPUs and number of devices.
The CPUs and devices are connected to the system bus as shown in diagram.
The devices are connected to the system bus through device controllers.
A device controller maintains some local buffer storage and a set of special-purpose registers.
The device controller is responsible for moving the data between the devices that it controls and its local buffer storage.
When the computer system is booted, a program called bootstrap program starts running.
Role of bootstrap program is locating the operating system, loading the operating system into RAM and starting the
execution of operating system.
When the operating system is started then the operating system controls the operations of computer system.
Interrupts
To start an I/O operation, the device driver loads the appropriate registers in the device controller.
The device controller, in turn, examines the contents of these registers to determine what action to take (such as “read a
character from the keyboard”).
The controller starts the transfer of data from the device to its local buffer.
Once the transfer of data is complete, the device controller informs the device driver that it has finished its operation by
raising an interrupt (a signal).
The device driver then returns the data if the operation was a read.
For other operations, the device driver returns status information such as “write completed successfully” or “device busy”.
When an interrupt is raised, the CPU stops what it is doing and immediately transfers execution to the interrupt service
routine.
The interrupt service routine executes; on completion, the CPU resumes the interrupted computation.
Following figure summarizes the interrupt-driven I/O cycle.
Storage Structure
Registers
They are used to store the most frequently used data and instructions.
Registers have the fastest access time and the smallest storage capacity, typically ranging from 16 to 64 bits.
Cache Memory
Cache memory is a small, fast memory unit located close to the CPU.
It stores frequently used data and instructions that have been recently accessed from the main memory.
Cache memory is designed to minimize the time it takes to access data by providing the CPU with quick access to frequently
used data.
Main Memory
Main memory, also known as RAM (Random Access Memory), is the primary memory of a computer system.
Secondary Storage
Secondary storage, such as hard disk drives (HDD) and solid-state drives (SSD), is a non-volatile memory unit that has a
larger storage capacity than main memory.
It is used to store data and instructions that are not currently in use by the CPU.
Secondary storage has the slowest access time and is typically the least expensive type of memory in the memory hierarchy.
Magnetic Disk
Magnetic disks are simply circular plates that are fabricated with either a metal or a plastic or a magnetized material.
The Magnetic disks work at a high speed inside the computer and these are frequently used.
Magnetic Tape
Magnetic tape is simply a magnetic recording device that is covered with a plastic film. It is generally used for the backup of
data.
In the case of a magnetic tape, the access time for a computer is a little slower and therefore, it requires some amount of
time for accessing the strip.
Operating System Operations
Following are the major operations or functions of an operating system
1. Process Management
2. Memory Management
3. File Management
4. I/O device Management
5. Protection
6. Security
7. Networking
Process Management
To create a new process, the operating system has to do the following activities
1) Move the program from secondary memory (HD) to primary memory (RAM)
2) Allocate CPU or processor to the program in primary memory
To delete a process, the Operating System has to move the program form primary memory (RAM) to secondary memory
(HD).
Suspending a process is temporarily stopping the execution of process due to several reasons like requesting for the input
from the user.
Before suspending the process, the Operating System has to save the status of the execution of the process.
Resuming a suspended process is restarting the execution of the suspended process when the process becomes ready for
execution.
Before resuming the process, the Operating System has to restore the saved status of the process.
Providing synchronization
A resource that can be shared by a number of processes at a time is called Sharable resource.
A resource that can be used by only one process at a time is called Non-sharable resource.
When a number of processes requests for a non-sharable resource at the same time, the Operating System has to provide
synchronous access to the resource by allocating the resource to only one process at a time.
Providing communication
Two methods used by operating system for providing communication between processes are:
1) Shared memory
2) Message passing
Handling deadlocks
A set of processes is said to be in deadlock state if each process in the set is waiting for another process in the same set of
processes.
The Operating System has to allocate the resources to processes such that the system should not go into the deadlock
state.
If a deadlock state occurs in the computer system, then the operating system has to detect the occurrence of deadlock
state and recover the computer system from deadlock state.
CPU Scheduling
When a number of programs are ready for execution, the operating system decides an order for executing the program.
Memory Management
Operating system has to manage both primary memory (RAM) and secondary memory (Hard disk).
For managing the primary memory, the Operating System is responsible for
RAM
The hard disk contains number of storage blocks.
At any time, some of the blocks contains programs or data and remaining blocks are free.
For managing the secondary memory, the Operating System is responsible for
The Operating System is responsible for the following activities for managing files
A computer system contains a number of input and output devices like keyboard, mouse, monitor, printer and so on.
The Operating System used in a computer which is connected to a network must provide support for
Protection: restricting access to the resources of the system from the processes that are running in the system.
For example, if two or more processes request the CPU at the same time then the Operating System has to restrict the
access to the CPU by allocating the CPU to only one process at a time.
If the computer system is running with batch processing operating system then the execution of programs in the computer
system is as follows:
The programs that users want to execute and the input data required for executing the programs are collected into a batch.
The batch of programs is then loaded into computer system for execution.
There is no interaction between users and computer during execution of the programs.
The outputs generated after execution of programs is collected into a batch and then distributed to users.
When only one program is loaded into main memory and if that program requires any i/o operation during its execution
then the CPU will be in idle state until the i/o operation is completed.
To increase the utilization of resources, a number of programs is loaded into main memory.
During execution of a program, if the program requires any i/o operation then the CPU is switched to another program so
that the CPU is busy at all times.
Multiprogramming operating system allows loading of a number of programs at a time into RAM.
Multiprogramming operating system switches the CPU from current program to another program when the current process
is completed or goes to the waiting state.
Timesharing operating system loads a number of programs at a time into main memory.
Allow each program to execute for a certain amount of time only. After that the CPU is switched to another program.
Timesharing operating system switches the CPU from current program to another program in the following cases:
A time-shared operating system allows many users to share the computer simultaneously.
Distributed operating system manages a group of independent computers and makes them appear to be a single computer.
Resource sharing
For example, if a printer is connected to the network then all systems in the network can share the printer.
Reliability of resources
If any resource of any system fails then the resource of other system in the network can be used.
Speed up of computations
Programs can be executed in less time by dividing the program into number of parts and executing these parts on different
systems in the network.
Communication between systems
If a number of persons is involved in the development of any project then there should be some communication between
the systems that are being used by the persons.
Providing Transparency
The distributed operating system hides the details of execution of the program from the user.
Real time operating systems are used in computer systems which executes real time applications.
For example, if there is a satellite which sends some data for every 100 seconds and if a computer system receives and
stores that data then the operating system used in that computer system is a real time operating system.
In this example, the operating system has to receive and store the data within 99 seconds.
Windows CE and Symbian are examples for real time operating systems.
The operating systems designed for being used in embedded computer systems are known as embedded operating systems.
Embedded operating systems are designed to operate on small machines like PDAs with less autonomy.
Embedded operating systems are able to operate with a limited number of resources.
A mobile operating system controls a mobile device and its design supports wireless communication and mobile
applications.
Blackberry OS, Google's Android and Apple's iOS are some of the most known names of mobile operating systems.
System calls
The operating system provides services to the user programs through system calls.
Some system calls which directly interacts with hardware devices are developed in assembly language.
System calls are executed in kernel of operating system when a user program requests the operating system for any service.
Kernel is the major part in the operating system (like a heart in human body).
The API contains set of functions/methods using which the programmer develops programs.
When any API method is executed in the program then the corresponding system call is invoked.
For example, when a user program calls a scanf() function for reading data from keyboard then the system call ‘read()’ is
invoked and executed in the kernel of the operating system.
There are two main reasons for writing programs using API instead of system calls directly.
1) Program written using API can be executed on any system that supports the same API.
2) Working with system calls is more difficult than API.
The run-time support system for most programming languages provides a system-call interface that serves as the link to
system calls made available by the operating system.
During execution of user program, when any API function is executed then the system-call interface identifies the
corresponding system call and activates that system call within the operating system.
The system-call interface also returns the status of the system call and any return values.
Types of system calls
1. end, abort
2. load, execute
3. create process, terminate process
4. get process attributes , set process attributes
5. wait, signal
File management system calls
1. create, delete
2. open, close
3. read, write, reposition
4. get file attributes, set file attributes
protection
The value of mode bit is ‘0’ for kernel mode and ‘1’ for user mode.
When the system is booted, the system is running in kernel mode.
When the operating system starts any user program then the mode of system is switched to user mode.
During execution, if the user program requests any service from the operating system then the mode is changed to kernel
mode.
After executing the system call, the mode is switched to user mode by setting mode bit to ‘1’ before passing the control to
user program.
To protect the operating system code, some of the machine instructions are designated as privileged instructions.
These privileged instructions are executed only in kernel mode.
The instruction used to change the mode bit is an example for privileged instruction.
When any privileged instruction is executed in user mode then the hardware informs to the operating system.
Virtualization
Virtualization is a technology that allows us to abstract the hardware of a single computer into several different execution
environments, thereby creating the illusion that each separate environment is running on its own private computer.
These environments can be viewed as different individual operating systems that may be running at the same time and may
interact with each other.
A user of a virtual machine can switch among the various operating systems.
Virtualization allows operating systems to run as applications within other operating systems.
With virtualization, an operating system that is natively compiled for a particular CPU architecture runs within another
operating system also native to that CPU.
VMware created a new virtualization technology in the form of an application that ran on Windows.
That application ran one or more guest copies of Windows or other native x86 operating systems, each running its own
applications. (See Figure 1.16.)
Windows was the host operating system, and the VMware application was the virtual machine manager (VMM).
The VMM runs the guest operating systems, manages their resource use, and protects each guest from the others.
On laptops and desktops, a VMM allows the user to install multiple operating systems to run applications written for
operating systems other than the native host.
For example, an Apple laptop running macOS on the x86 CPU can run a Windows 10 guest to allow execution of Windows
applications.
Module 2
PROCESS MANAGEMENT
Process
A program is a set of statements or instructions which collectively implements a task.
The memory allocated by the operating system for a process consists of four parts as shown in following figure.
The text part contains instructions or statements of the process.
Stack contains temporary data like function parameters, return values and so on.
Heap part contains the objects that are dynamically created during execution of process.
Process States
A process at any particular time will be in any one of the following states
1) New - The process is about to be created but not yet created. It is the program that is present in secondary memory that
will be picked up by the Operating System to create the process.
2) Ready - The process is loaded into the main memory. The process here is ready to run and is waiting to get the CPU time
for its execution.
4) Waiting – The process requests access to I/O or needs input from the user. The process continues to wait in the main
memory and does not require CPU.
5) Exit – The execution of process is completed. The resources allocated to the process will be released or deallocated.
When the process is ready for execution on allocation of CPU then the process moves from new state to ready state.
When CPU is allocated to the process then the process moves from ready state to running state.
During execution of the process:
1) If the process is completed then the process moves from running state to exit state.
2) If any i/o operation is requested or the process waits for an event then the process moves from running state to waiting
state.
3) If the allocated time is over or an interrupt occurs then the process moves from running state to ready state.
When the requested i/o operation is completed or the event is completed then the process moves from waiting state to
ready state.
Memory management information: indicates the starting and ending positions or addresses of process in the RAM.
Accounting information: indicates process id, amount of CPU time required and so on.
I/O status information: indicates the list of i/o devices allocated to the process, the list of open files and so on.
Context switch
Context switch is switching the CPU from one process to other process.
Operating system switches the CPU from the current process to another process when:
When the Operating System switches the CPU from one process to other process then the Operating System has to update
the PCBs of the processes.
The following diagram shows state change in process control block when context switch occurs.
Initially CPU is allocated to process P0.
While executing process P0, if process P0 invokes a system call or requests any i/o operation or an interrupt occurs then
While executing process P1, if process P1 invokes a system call or requests any i/o operation or an interrupt occurs then
When number of programs are ready for execution then the operating system has to decide an order for executing the
programs (i.e. the operating system has to schedule the execution of programs).
Scheduling queues
1) Ready queue
2) Device queues
Ready queue
The ready queue contains the PCBs of processes that are ready for execution.
Device queue
A device queue is maintained for each device. The device queue of a device contains the PCBs of the processes that are
waiting for that device.
Queuing diagram
The queuing diagram shows how a process moves between different queues during its life time.
In the queuing diagram, each rectangle represents a queue and each circle represents a resource.
When a process enters into the system then it is put into ready queue.
A process waits in the ready queue until the CPU is allocated to the process.
When CPU is allocated to the process then the execution of the process begins.
In this case, execution of the process is suspended and it is put into device queue of the device for which it made the
request.
The process waits in device queue till the completion of the i/o operation.
After the completion of i/o operation, the process is put into the ready queue.
In this case, execution of the process is suspended and is put into the ready queue.
3) The process may create a child process.
In this case, execution of the process is suspended and the process waits for the completion of child process.
After completion of child process, the process is put into the ready queue.
In this case, execution of the process is suspended and the process waits for completion of processing of the interrupt.
After processing the interrupt, the process is put into ready queue.
Schedulers
The operating system uses different types of schedulers to select processes form these queues.
The following process state diagram shows when the operating system activates these schedulers.
When a process requests any i/o operation during its execution then the process moves to the waiting state.
When number of processes in the RAM are in waiting state then the operating system moves some of the waiting processes
from RAM to hard disk in order to load new processes into RAM.
When a waiting process is moved from RAM to hard disk then the process is said to be in the blocked state.
After completion of the i/o operation, the process goes to the ready state from either the waiting or the blocked state.
Long term scheduler selects one process (program) from the list of new processes (programs) in the hard disk and loads that
process into RAM.
Medium term scheduler selects one process from the list of waiting processes in the hard disk and loads that process into
RAM.
Short term scheduler or CPU scheduler
Short term scheduler selects one process from the ready queue and allocates the CPU to that process.
The Dispatcher module switches the CPU to the process selected from the ready queue.
The time required to switch the CPU to the selected process is called Dispatching Latency.
Short term scheduler uses various scheduling algorithms (CPU scheduling algorithms) for selecting a process from the ready
queue.
1) Preemptive
2) Non-preemptive
In preemptive scheduling, the CPU can be switched from current process to other process forcibly.
In non-preemptive scheduling, the CPU cannot be switched from current process to other process until the current process
releases the CPU.
Scheduling Criteria
CPU utilization: the percentage of time for which the CPU is busy.
Turnaround time: difference between the time at which the process has arrived into system and the time at which the
process has completed its execution.
Waiting time: the sum of periods for which the process is waiting in the ready queue.
Response time: difference between the time at which the process has arrived into system and the time at which the first
response has come out from the process.
A good scheduling algorithm should maximize CPU utilization, throughput and minimize turnaround time, waiting time and
response time.
With FCFS scheduling, the processes are executed in the order in which they enter into the system.
Consider the following set of processes
The order of execution of these processes with FCFS is indicated with following Gantt chart
Gantt chart:
P1
P1 P2 P3 P2P4
0 8 13 16 22 P3
P4
Process Burst time Arrival time Turnaround time Waiting time
P1 8 0 8-0=8 8-8=0
P2 5 0 13-0=13 13-5=8
P3 3 0 16-0=16 16-3=13
P4 6 0 22-0=22 22-6=16
The order of execution of these processes with FCFS is indicated with following Gantt chart
Gantt chart:
P1 P2 P3 P4
0 8 13 16 22
Disadvantages:
With SJF algorithm, the process with least burst time is executed first.
If the burst time of two or more processes is same then the processes are executed in FCFS order.
Non-preemptive SJF
The order of execution of these processes with non-preemptive SJF is indicated with following Gantt chart
Gantt chart:
0 3 8 14 22
The order of execution of these processes with non-preemptive SJF is indicated with following Gantt chart
Gantt chart:
0 8 11 16 22
With preemptive SJF, current process is allowed to execute till the arrival of next process.
At the arrival of next process, CPU is allocated to the process with shortest remaining burst time.
The order of execution of these processes with preemptive SJF is indicated with following Gantt chart
Gantt chart:
0 3 8 14 22
The order of execution of these processes with preemptive SJF is indicated with following Gantt chart
Gantt chart:
0 1 2 4 5 9 15 22
Disadvantages:
It can be used only when the burst times of processes are known in advance.
With priority algorithm, the process with highest priority is executed first.
If the priority of two or more processes is same then the processes are executed in FCFS order.
The order of execution of these processes with non-preemptive Priority is indicated with following Gantt chart
Gantt chart:
0 3 11 16 22
The order of execution of these processes with non-preemptive Priority is indicated with following Gantt chart
Gantt chart:
0 8 11 16 22
With Preemptive Priority, current process is allowed to execute until the arrival of next process.
At the arrival of next process, CPU is allocated to the process with highest priority.
Process Burst time Arrival time Priority
P1 8 0 2
P2 5 0 3
P3 3 0 1
P4 6 0 4
The order of execution of these processes with preemptive Priority is indicated with following Gantt chart
Gantt chart:
0 3 11 16 22
The order of execution of these processes with preemptive Priority is indicated with following Gantt chart
Gantt chart:
0 1 2 4 5 11 16 22
A process may continuously wait for the CPU. This situation is called starvation.
If there is a continuous flow of higher priority processes into the system then the lower priority processes will never get the
CPU.
Aging is increasing the priority of processes which have been waiting for long time.
Round Robin (RR) scheduling algorithm
RR is similar to FCFS.
With RR, the processes are executed in FCFS order and each process is allowed to execute for the time which is specified by
quantum time.
If the process is completed within the time specified by quantum time then it will be taken out from the ready queue.
quantum time=4
The order of execution of these processes with RR is indicated with following Gantt chart
Gantt chart:
0 4 8 11 15 19 20 22
Quantum time=4
The order of execution of these processes with RR is indicated with following Gantt chart
Gantt chart:
0 4 8 11 15 19 20 22
Disadvantages:
2) If quantum time is less then more number of context switches will occur. More number of context switches leads to
wastage of time.
3) If quantum time is high then the processes have to wait for more time.
Multilevel Queue Scheduling
Initially, the processes are distributed to ready queues based on some property of processes like burst time, priority and so
on.
As an example, the processes with small burst time are placed in first ready queue, the processes with larger burst time are
placed in last ready queue.
Different scheduling algorithms can be used to execute the processes in different queues.
Initially, CPU is allocated to ready queue1 and the processes in ready queue1 are executed in the order specified by
corresponding scheduling algorithm.
After completion of all processes in ready queue1, CPU is allocated to ready queue2.
The processes in ready queue2 are executed in the order specified by corresponding scheduling algorithm.
After completion of all processes in ready queue2, CPU is allocated to ready queue3.
While executing any process in ready queue2, if any new process arrives into ready queue1 then the process in ready
queue2 is preempted and the CPU is switched to the process arrived into ready queue1.
Similarly, while executing any process in ready queue3, if any new process arrives into either ready queue1 or ready queue2
then the process in ready queue3 is preempted and the CPU is switched to the process arrived into ready queue1 or ready
queue2.
Multilevel Feedback Queue Scheduling
The processes in ready queue1 are executed in the order specified by corresponding scheduling algorithm.
After completion of all processes in ready queue1, the CPU is allocated to ready queue2.
The processes in ready queue2 are executed in the order specified by corresponding scheduling algorithm.
After completion of all processes in ready queue1 and ready queue2, the CPU is allocated to ready queue3.
While executing any process in ready queue2, if any new process arrives into ready queue1 then the process in ready
queue2 is preempted and the CPU is switched to the process arrived into ready queue1.
Similarly, while executing any process in ready queue3, if any new process arrives into either ready queue1 or ready queue2
then the process in ready queue3 is preempted and the CPU is switched to the process arrived into ready queue1 or ready
queue2.
If a process in ready queue1 is not completed then it will be moved to ready queue2.
If a process in ready queue2 is not completed then it will be moved to ready queue3.
If a process in last ready queue is not completed then it will be moved to ready queue1.
Operations on Processes
There are two basic operations on processes:
1) Process creation
2) Process termination
Process Creation
The process which creates new processes is called the parent process and the created processes are called the child
processes.
Each process is associated with a unique integer number called process identifier which is used to identify the processes.
43
44
Each process requires some resources.
A child process obtains its required resources in any one of the following ways:
The parent process has to distribute some resources and share some resources among its child processes.
In addition to resources, the parent process may pass some data to its child processes.
For example, if a child process is created for displaying an image file on the monitor then the parent process has to pass the
address of file to the child process.
The child process may be a duplicate of the parent process (child has the same program and data as parent) or the child has
a new program.
When a parent process creates a child process then the parent and child processes can execute concurrently or the parent
process waits for the completion of some or all of its child processes.
In UNIX operating system, the fork() system call is used to create a new process.
45
Fork() System Call
The process which invokes the fork() system call is called the parent process.
After a new child process is created, both processes will execute the next instruction following the fork() system call.
Changes made to the values of variables by parent process will not be reflected in the child process and vice versa.
Output:
Hello world!
Hello world!
Calculate the number of times hello is printed
void forkexample()
{
if (fork() == 0)
printf("Hello from Child!\n");
else
printf("Hello from Parent!\n"); Output:
} Hello from Child!
Hello from Parent!
int main()
{ (or)
forkexample();
return 0; Hello from Parent!
} Hello from Child!
Predict the output of the following program
Process hierarchy
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
void forkexample()
{
int x = 1;
if (fork() == 0)
printf("Child has x = %d\n", ++x);
Output:
else
Parent has x = 0
printf("Parent has x = %d\n", --x); Child has x = 2
}
int main() (or)
{
forkexample(); Child has x = 2
return 0; Parent has x = 0
}
Predict output of below program
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
fork();
((fork() && fork()) || fork());
fork();
printf("forked\n");
return 0;
}
#include<stdio.h>
int main()
{
fork(); /* A */
(( fork() /* B */ && fork() /* C */ ) || fork(); /* D */ ) Output:
fork(); /* E */
printf("forked\n"); Forked
return 0; Forked
} Forked
Forked
Process hierarchy Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
forked
How many times fork() is invoked in the following program?
6 times
Mention the process hierarchy and output.
void main()
{
if(fork() && fork() || fork()) Process hierarchy
{
fork();
fork();
print(“OS”);
}
}
Output:
OS
OS
OS
OS
OS
OS
OS
OS
OS
OS
OS
OS
Process Termination
The operating system releases all resources from the completed process.
A parent process may terminate its child process when the child process uses its allocated resources for long time (or) the
task executed by the child process is no longer required (or) the parent is exiting.
Some operating systems do not allow a child process to exist when the parent process is terminated.
In this case, when the parent process terminates then the operating system terminates all children of the process.
56
Inter-process Communication
In a computer system, number of processes may be executing concurrently.
Sharing resources: for example, if two or more processes require data from same file (resource).
when a program has to be completed in less time then the program is divided into number of parts and the parts are
executed concurrently on the processors (processing units) of the system.
The parts must communicate with each other as they belong to the same program.
1) Shared memory
2) Message passing 57
Shared Memory
The processes can exchange information by reading and writing data to the shared memory.
Any process which wants to communicate with the process creating the shared memory must attach the shared memory to
its address space.
Generally, the operating system prevents one process from accessing other process memory.
58
Producer – consumer problem
The producer process stores items into the buffer and the consumer process takes items from the buffer.
Two pointers (in and out) are maintained for the buffer.
The in pointer points to the next free slot in the buffer and out pointer points to the slot from which item can be taken out
from the buffer.
The buffer, sizeofbuffer and count must be stored in shared memory as these are accessed by both processes.
The producer process has to wait until a free slot is available in the buffer.
The consumer process has to wait until an item is available in the buffer.
Using shared memory, large amount of data can be exchanged between processes in less time.
59
Message passing
Two operations called send(message), receive(message) and a communication link are used for exchanging the messages.
1) Naming
2) Synchronization
3) Buffering 60
1) Naming
A direct communication link or an indirect communication link is used between the communicating processes.
Direct Communication:
With direct communication, the sender process has to mention the name of receiver process.
Similarly, the receiver process has to mention the name of sender process.
In Direct Communication:
1) A communication link is established between every pair of processes that want to communicate.
2) A link is associated with exactly two processes.
3) There exists exactly one link between every pair of processes.
61
Indirect communication:
The processes can communicate by sending and receiving messages from mailboxes.
In Indirect Communication:
1) A link is established between a pair of processes if both processes have a shared mailbox.
2) There exists more number of links between a pair of processes.
2) Synchronization
Communication between processes takes place through calls to send() and receive() operations.
Message passing is either blocking or non blocking – also known as synchronous and asynchronous.
62
Blocking send: the sending process is blocked until the message is received by the receiver or mailbox.
Non blocking send: the sending process continues its operations after sending the message.
Non blocking receive: the receiver does not block for the message to receive.
The sender and receiver can use any combination of send() and receive() operations.
3) Buffering
In both direct and indirect communication, the messages are stored in a buffer.
Buffer with zero capacity: in this case, the sender must block until the receiver receives the message.
Buffer with bounded capacity: the buffer has a finite length. In this case, the sender can continue its execution after sending
a message if the buffer is not full.
Buffer with unbounded capacity: the buffer has infinite capacity. In this case, the sender never blocks.
63
If the processes running in different systems want to communicate then message passing is easier to implement than shared
memory.
It takes more time to exchange messages as messages are exchanged through kernel.
64
Multithreaded Programming
A thread is part of a process.
The following figure shows the difference between a single threaded process and a multithreaded process.
65
A thread is associated with a program counter, a set of registers and a stack.
The threads belonging to a process shares the code, data and resources of the process.
For example, in a word processor, a thread displays graphics, a thread responds to key strokes and a thread checks spelling
and grammar.
In a web server, there are number of threads one for each client request.
When a client makes a request, a thread is created by the server for processing that request.
66
Benefits of multithreaded programs
1) Responsiveness
In a single threaded process, if the thread blocks then the user does not get response.
In a multithreaded process, if one or more threads are blocked then the user gets response from other threads of the
process.
2) Resource sharing
In multithreading, all threads of a process shares resources allocated to that process automatically.
3) Economy
The operating system has to allocate number of resources to a process when it creates a process.
When a thread is created no need to allocate resources for the thread as the thread shares resources of the process in which
it is created. 67
Process switching is costly compared to thread switching.
4) Scalability
In a multiprocessor system, a single threaded process can use only one processor.
A multithreaded process can use all processors for running the threads in parallel.
Multithreading models
Two types of threads can be found in the computer system: User threads and Kernel threads
User threads (threads created in an application program) are created and managed by user.
Creation of user level threads and switching between user level threads is easy.
Kernel threads (threads created in kernel) are managed by the operating system.
Kernel level threads are slower compared to user level threads due to management overhead.
68
Creation of kernel level threads and switching between kernel level threads is costly and time consuming.
A kernel level thread provide services to one or more user level threads.
Multithreading models indicate the relationship between user and kernel threads.
1) Many-to-One model
2) One-to-One model
3) Many-to-Many model
69
1) Many-to-One model
Number of user threads is mapped to one kernel thread as shown in following figure.
The kernel thread provide services to only one user thread at a time.
If the kernel thread is blocked then the entire user process is blocked.
70
2) One-to-One model
The user process is not blocked when one thread makes a blocking system call.
The user threads are mapped to less or equal number of kernel threads as shown in following figure
When a thread makes a blocking system call, the kernel can schedule another thread for execution.
72
One variation of many-to-many model is two-level model.
In two-level model, user threads are mapped to less or equal number of kernel threads and a user thread can be bound to a
kernel thread.
73
Module 3.1
PROCESS SYNCHRONIZATION
Producer – Consumer Problem
• There is a Buffer with some number of slots.
• One item can be stored in each slot of the buffer.
• In and out are pointers to the buffer.
• Count is a variable which indicates the number of items in the buffer.
• Producer is the process which inserts items into the buffer.
• In points to the next free slot of buffer into which the producer process inserts the next item.
• In pointer is moved to the next position after inserting an item into the buffer.
• Count is incremented by 1 after inserting an item into the buffer.
• Consumer is the process which removes items from the buffer.
• Out points to the slot from which the consumer process removes the item.
• Out pointer is moved to the next position after removing an item from the buffer.
• Count is decremented by 1 after removing an item from the buffer.
2
Producer Process Consumer Process
The code of producer process is: The code of consumer process is:
while (true) while(true)
{ {
while (count == buffer_size) while (count == 0)
; ;
Buffer[in] = item; item = Buffer[out];
in = (in + 1) % buffer_size; out = (out+1) % buffer_size;
count = count + 1; count = count -1;
} }
3
Producer Process Consumer Process
The code of producer process is: The code of consumer process is:
while (true) while(true)
{ {
P1: while (count == buffer_size) C1: while (count == 0)
P2: ; C2: ;
P3: Buffer[in] = item; C3: item = Buffer[out];
P4: in = (in + 1) % buffer_size; C4: out = (out+1) % buffer_size;
P5: register1 = count; C5: register2 = count;
P6: register1 = register1 + 1; C6: register2 = register2 - 1;
P7: count = register1; C7: count = register2;
} }
Serial Execution of processes
Processes are executed one after another.
CPU is switched to next process only after completion of the currently running process.
N number of processes can be serially executed in N! number of ways.
For example, two processes p1 and p2 can be executed serially in two ways:
1) p1, p2 2) p2, p1
Three processes p1, p2 and p3 can be executed serially in six ways:
1) p1, p2, p3 2) p1, p3, p2 3) p2, p1, p3 4) p2, p3, p1 5) p3, p1, p2 6) p3, p2, p1
5
Independent processes
Processes running in the computer system are said to be independent processes if they are not exchanging or sharing any
data.
Cooperating processes
Processes running in the computer system are said to be cooperating processes if they are exchanging or sharing any data.
Correct output is generated when the independent processes are executed either serially or concurrently.
Correct output is generated when the cooperating processes are executed serially.
7
Another example concurrent execution of producer & consumer processes that leads to wrong result is:
P1, P2, P3, P4, P5, P6, C1, C2, C3, C4, C5, C6, C7, P7.
8
Race condition
Race condition is getting different outputs for different concurrent executions of cooperating processes.
Ex: What are the different values that A and B get after the execution of the processes P1, P2 concurrently?
Initial values of A and B are 6 and 4 respectively.
9
Concurrent execution sequence1: I1, I2, I11, I12
A=6-4=2
B=4+2=6
A=2+1=3
B=6-1=5
A=3, B=5 Process P1: Process P2:
register1 = count;
register1 = register1 + 1;
count = register1;
register2 = count;
register2 = register2 - 1;
count = register2;
Critical Section Problem
When two or more cooperating processes are executing concurrently, then at a time only one process should execute the
statements in its critical section.
(or)
When one process is already executing the statements in its critical section, no other process is allowed to execute the
statements in its critical section.
(or)
No two processes are allowed to execute the statements in their critical section at the same time.
When the execution of cooperating processes is restricted in this way then the correct output is generated even if the
cooperating processes are executed concurrently.
Each process must request permission to start the execution of statements in its critical section.
The critical section may be followed by an exit section which allows the other process to start the execution of statements
in its critical section.
Entry Section
Critical Section
Exit Section
.
.
}
Solutions for Critical Section Problem (or) Solutions for Implementing Mutual Exclusion
Mutual Exclusion
No two processes should execute the statements in their critical section at the same time.
Progress
At any time, at least one process should be in the running state or active. No deadlock should occur.
Bounded waiting
All processes should be given equal chance to execute the statements in their critical sections.
17
Peterson’s Solution
It is applicable to two processes only.
o int turn;
o boolean flag[2];
turn variable indicates whose turn it is to execute the statements in its critical section.
turn=1 - indicates that the 1st process (producer process) can execute the statements in its critical section.
turn=2 - indicates that the 2nd process (consumer process) can execute the statements in its critical section.
flag indicates whether a process is ready to execute the statements in its critical section.
flag[1]=true - indicates that the 1st process (producer process) is ready to execute the statements in its critical section.
flag[2]=true - indicates that the 2nd process (consumer process) is ready to execute the statements in its critical section.
18
The following procedure is used to solve the critical section problem:
When the 1st process (producer process) wants to execute the statements in its critical section then the 1st process
(producer process) sets flag[1] to true.
If the 2nd process (consumer process) is also ready to execute the statements in its critical section, then the 1st process
(producer process) allows the 2nd process (consumer process) to execute the statements in its critical section by setting the
turn value to 2.
The 1st process (producer process) waits until the 2nd process (consumer process) completes the execution of statements in
its critical section.
Then the 1st process (producer process) starts executing the statements in its critical section.
The 1st process (producer process) sets flag[1] to false after completing the execution of statements in its critical section.
Same procedure is followed by the 2nd process (consumer process) when it wants to execute the statements in its critical
section.
19
Peterson’s solution to the critical section problem of producer-consumer is as follows:
flag[1]=true; flag[2]=true;
Entry turn=2; Entry turn=1;
Section while(flag[2]==true && turn==2) Section while(flag[1]==true && turn==1)
; ;
boolean lock;
When a process wants to execute the statements in its critical section, the process checks the value of lock variable.
If the value of lock variable is true, then the process is not allowed to execute the statements in its critical section.
If the value of lock variable is false, then the process is allowed to execute the statements in its critical section.
Before executing the statements in the critical section, the process must set the value of lock variable to true.
After executing the statements in the critical section, the process must set the value of lock variable to false.
21
while (true)
{
.
.
Acquire lock;
Critical Section
Release lock;
.
.
}
Test_and_Set()
‘Test_and_Set’ is a hardware instruction which performs operations on the ‘lock’ variable to solve the critical section
problem.
During the execution of Test_and_Set(), the CPU is not switched to any other process.
‘Swap’ is another hardware instruction which is used to solve the critical section problem.
25
The solution to critical section problem of producer-consumer is
1) Binary semaphore
2) Counting semaphore
Binary semaphore is also called as ‘mutex’ variable as binary semaphore is used to implement mutual exclusion.
Counting semaphore is used to provide synchronous access to a resource when the resource is requested by a number of
processes at a time.
Initial value of counting semaphore is equal to the number of instances of the resource.
A queue is associated with each semaphore variable.
1) wait()
2) signal()
wait(s)
{
s=s-1;
if(s<0)
{
Suspend the execution of the process which has invoked the wait() operation;
Insert that process in the queue associated with the semaphore variable s;
}
}
The definition of signal() operation is:
signal(s)
{
s=s+1;
if(s<=0)
{
remove a process from the queue associated with the semaphore variable s;
restart the execution of that removed process;
}
}
To solve the critical section problem, a process should invoke wait() operation on the semaphore variable before starting the
execution of statements in its critical section and should invoke signal() operation after executing the statements in its
critical section.
Readers-Writers Problem
Some processes named ‘Readers’ only read data from the database.
At a time, any number of Readers can read data from the database without any conflict.
But, when a Writer is writing data to the database then other Writers and Readers are not allowed to access the database.
read_count variable keeps track of how many Readers are currently reading from the database.
rw_mutex is used by both Readers and Writers to synchronously access the database.
while(true)
{
wait(rw_mutex);
signal(rw_mutex);
}
The code of a Reader process is:
while(true)
{
wait(mutex);
read_count = read_count + 1;
if(read_count == 1)
wait(rw_mutex);
signal(mutex);
wait(mutex);
read_count = read_count - 1;
if(read_count == 0)
signal(rw_mutex);
signal(mutex);
}
Dining Philosopher’s Problem
The philosophers share a circular table surrounded by five chairs, each belonging to one philosopher.
In the center of the table is a bowl of rice, and the table is laid with five single chopsticks as shown in the diagram:
When a philosopher feels hungry then the philosopher tries to pick up the two chopsticks that are closest to him.
When a hungry philosopher has both chopsticks at the same time, he eats without releasing the chopsticks.
When he is finished eating, he puts down both chopsticks and starts thinking again.
semaphore chopstick[5];
A philosopher releases his chopsticks by executing the signal() operation on the appropriate semaphores.
The structure of philosopher i is:
while (true)
{
wait(chopstick[i]);
wait(chopstick[(i+1) % 5]);
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
Some example resources are: RAM, HD, CPU, compiler, file and so on.
A computer system may have one or more instances of each resource type.
For example, a computer system may have 3 CPUs, 2 printers and so on.
A number of processes may be executing in the computer system at any particular time.
Operating system allocates the resources to the processes running in the computer system.
When a process requires a resource then the process makes a request to the operating system.
The operating system allocates the requested resource if that resource is free otherwise the process has to wait.
Deadlock
A set of processes is said to be in deadlock state if each process in the set is waiting for an event (releasing of resource) that
can be caused by another process in the set.
Ex:
Necessary conditions for deadlock
The following four conditions must hold for the occurrence of deadlock state.
(or)
In a deadlock state, the following four conditions are satisfied.
Mutual Exclusion
Mutual exclusion condition indicates that a non-sharable resource should be used by only one process at a time.
Each process is holding some resources and waiting for other resources.
No-Preemption
Circular Wait
Nodes in the resource allocation graph represent both processes and resources.
Edges indicate both allocation of resources to processes and request to resources by the processes.
Processes are indicated using circles, resources are indicated using boxes.
When a process Pi requests an instance of resource type Rj, a request edge is inserted in the resource-allocation graph.
When this request can be fulfilled, the request edge is instantaneously transformed to an allocation edge.
When the process no longer needs access to the resource, it releases the resource.
There is a cycle:
There is a cycle:
There is no deadlock.
Conclusions from the above examples are:
1) If there is no cycle in the Resource Allocation Graph then there is no deadlock state.
2) If each resource has only one instance then the presence of cycle in the resource allocation graph indicates the
occurrence of deadlock state.
3) If the resources have multiple instances then the presence of cycles in the resource allocation graph may or may not
indicate the occurrence of deadlock state.
Methods for handling deadlocks
1) Deadlock prevention
2) Deadlock avoidance
3) Deadlock detection
4) Deadlock recovery
Deadlock prevention provides a set of methods to ensure that at least one of the necessary conditions cannot hold.
These methods prevent deadlocks by constraining how requests for resources can be made.
Deadlock avoidance requires that the operating system be given additional information in advance concerning the resources
currently available, the resources currently allocated to each process, and the future requests and releases of each process.
With this additional knowledge, the operating system can decide for each request whether the request can be satisfied or
must be delayed.
Deadlock detection detects the occurrence of deadlock state in the computer system.
Deadlock recovery recovers the computer system from the deadlock state when it occurs.
Deadlock prevention
For a deadlock to occur, each of the four necessary conditions must hold.
We can prevent the occurrence of a deadlock by ensuring that at least one of these conditions cannot hold.
Mutual Exclusion
To ensure that mutual exclusion condition cannot hold, a resource can be allocated to a number of processes at a time.
For non-sharable resources, it is not possible to ensure that mutual exclusion condition cannot hold.
Any one of the following two mechanisms is used to ensure that hold and wait condition cannot hold.
1) Allocate all required resources to the process before starting the execution of the process.
2) Allocate a subset of required resources to the process. Release this subset of resources from the process after using them
and allocate another subset of resources. Repeat this procedure till the completion of process.
No preemption
The following two mechanisms are used to ensure that no preemption condition cannot hold:
1) When a process requests for any resource which is currently allocated to a running process then the requesting process
will go the waiting state and the resources that are currently allocated to the requesting process are released.
2) When a process request for a resource which is currently allocated to a waiting process then the resource from the
waiting process is released and allocated to the requesting process.
The following mechanism is used to ensure that circular wait condition cannot hold:
An ordering (R1, R2, R3, …..) is given to the resources and the processes are allowed to request the resources in an increasing
order.
When a process request for a resource whose number is less than the number of any resource already allocated to the
process then that request is rejected by the operating system.
For example, if a process P1 is holding resources R2 and R5 and if it requests for resource R7 then the request is accepted.
To apply deadlock avoidance method, the following information must be known in advance:
Resource-Allocation-Graph Algorithm
Resource-Allocation-Graph algorithm is used when each resource has only one instance.
In addition to the request and allocation edges, a claim edge is also included in the graph.
A claim edge Pi→Rj indicates that process Pi may request resource Rj at some time in the future.
This edge resembles a request edge in direction but is represented in the graph by a dashed line.
When process Pi requests resource Rj, the claim edge Pi → Rj is converted to a request edge.
Similarly, when a resource Rj is released by Pi, the allocation edge Rj →Pi is reconverted to a claim edge Pi → Rj.
Before process Pi starts executing, all its claim edges must already appear in the resource-allocation graph.
The request can be granted only if converting the request edge Pi → Rj to an allocation edge Rj → Pi does not result in the
formation of a cycle in the resource-allocation graph.
If all processes can be completed in any order with available resources then the system is said to be in safe state. Otherwise,
the system is said to be in unsafe state.
Safe Sequence
The order in which the processes can be completed is called the safe sequence.
When any process request for resources then the algorithm checks whether the allocation of requested resources will lead
to (or) result in the safe state.
If the allocation of requested resources to the process will lead to safe state, then the operating system accepts the request.
Otherwise, rejects the request.
Four data structures are used in Banker’s algorithm for deadlock avoidance.
Available vector indicates the number of copies of each resource type available in the system.
Max matrix indicates the total number of copies of each resource type required by each process.
Allocation matrix indicates the number of copies of each resource type allocated to each process.
Need matrix indicates the number of copies of each resource type still required by each process.
This algorithm is used to check whether the system is in safe state or not.
1. Find a process Pi which is not marked as finished and Needi <= Available. If no such process exists then go to step 3.
Go to step 1.
3. If all processes are marked as finished, then indicate that the system is in safe state. Otherwise, indicate that the system is
in unsafe state.
Ex: n=4, m=2
Above state is safe state as all processes can be completed in the order: P1, P3, P0, P2.
At first, process P1 is selected as it is not marked as finished and its need1<=Available ((1,2)<=(3,3)).
Process P1 is marked as finished, the resources allocated to process P1 are released and then added to the available vector.
Available
5 3
Next, process P3 is selected as it is not marked as finished and its need3<=Available ((0,1)<=(5,3)).
Process P3 is marked as finished, the resources allocated to process P3 are released and then added to the available vector.
Available
7 4
Next, process P0 is selected as it is not marked as finished and its need0<=Available ((7,4)<=(7,4)).
Process P0 is marked as finished, the resources allocated to process P0 are released and then added to the available vector.
Available
7 5
Next, process P2 is selected as it is not marked as finished and its need2<=Available ((6,0)<=(7,5)).
Process P2 is marked as finished, the resources allocated to process P2 are released and then added to the available vector.
Available
10 5
Resource request algorithm
When a process Pi makes a Requesti for resources then the resource request algorithm is invoked to decide whether to
accept or reject the request.
1. If Requesti <= Available then go to step 2. Otherwise, the process Pi has to wait.
2. Pretend that the requested resources are allocated to the process and update the values of matrices as
3. Check whether the resulting state is safe or not by calling the safety algorithm. If the resulting state is safe, then accept
the request. Otherwise, reject the request and restore the old state.
Ex: n=4, m=2
The above state is safe as the processes can be completed in the order: P1, P3, P0, P2.
Request1 = (1, 0)
Then the operating system calls the resource request algorithm to decide whether the request can be accepted or not.
The resource request algorithm checks whether Request1<=Available or not.
Now, the safety algorithm is called to determine whether the resulting state is safe or not.
The safety algorithm returns that the state is safe as the processes can be completed in the order: P1, P3, P0, P2.
Request0 = (0, 2)
Then the operating system calls the resource request algorithm to decide whether the request can be accepted or not.
The resource request algorithm checks whether Request0<=Available or not.
Now, the safety algorithm is called to determine whether the resulting state is safe or not.
The safety algorithm returns that the state is unsafe as it is not possible to complete all processes.
Deadlock detection method is used to check the occurrence of deadlock state in the computer system.
Two methods are used for detecting the occurrence of deadlock state.
1) Wait-for-graph algorithm
2) Banker’s algorithm
Wait-for-graph algorithm
Wait-for-graph algorithm is used when each resource has only one instance.
Wait-for-graph contains only the process nodes and they are indicated by circles.
If a process Pi is waiting for a resource held by process Pj, then an edge is drawn from process Pi to process Pj in the wait-for-
graph.
Cycle 1: P1->P3->P5->P1
Cycle 2: P1->P3->P4->P5->P1
Banker’s algorithm is used when the resources have number of instances or copies.
Available vector indicates the number of available copies of each resource type.
Allocation matrix indicates the number of copies of each resource type that are currently allocated to each process.
Request matrix indicates the number of copies of each resource type that are required by each process.
Algorithm
1. Find a process Pi which is not marked as finished and Requesti <= Available. If no such process exist then go to step 3.
Go to step 1.
3. If all processes are marked as finished, then indicate that deadlock state has not occurred. If some of the processes are
not marked as finished, then indicate that deadlock has occurred. The processes which are not marked as finished are in the
deadlock sate.
Ex:
Allocation Request Available
A B A B A B
P0 0 1 P0 0 0 0 0
P1 2 0 P1 2 0
P2 3 0 P2 0 0
P3 2 1 P3 1 0
At first, process P0 is selected as it is not marked as finished and its Request0 <= Available.
Available
0 1
Next, process P2 is selected as it is not marked as finished and its Request2 <= Available.
Available
3 1
Next, process P1 is selected as it is not marked as finished and its Request1 <= Available.
Available
5 1
Finally, process P3 is selected as it is not marked as finished and its Request3 <= Available.
Available
7 2
Deadlock state has not occurred in the system as all processes are marked as finished.
Ex: Allocation Request Available
A B C A B C A B C
P0 0 1 0 P0 0 0 0 0 0 0
P1 2 0 0 P1 2 0 2
P2 3 0 3 P2 0 0 1
P3 2 1 1 P3 1 0 0
P4 0 0 2 P4 0 0 2
First, process P0 is selected as it is not marked as finished and its Request0 <= Available.
Available
0 1 0
So, the processes P1, P2, P3 and P4 are not marked as finished and it indicates the occurrence of deadlock state in the
system.
Any of the following two techniques is used to recover from the deadlock state:
In this method, the processes which are involved in the deadlock state are terminated.
In the first mechanism, all processes that are involved in the deadlock state are terminated or aborted.
Advantages
1. Deadlock can be recovered in less amount of time.
2. No need to apply the deadlock detection algorithm for number of times.
Disadvantage
1. Loosing of computations of all processes.
In the second mechanism, first one of the processes in the deadlock state is terminated.
If the deadlock state still exist, then one more process in the deadlock state is terminated.
This procedure is repeated until the system is recovered from the deadlock state.
Advantage
1. Less wastage in computation time compared to the first mechanism as only some processes in the deadlock state are
terminated.
Disadvantage
1. Need of applying the deadlock detection algorithm for number of times.
2. Some criteria have to be used to select a process for termination at each step.
The general criteria used to select a process for termination are:
1) Execution time
The process that has been executed for less amount of time is selected for termination.
2) No of Resources
The process which is holding less number of resources is selected for termination.
3) Priority
In this method, resources will be preempted successively from the deadlocked processes until the system is recovered to
the normal state.
To use this mechanism for recovering from the deadlock state, the following factors are considered.
Selecting a victim: one of the deadlocked processes has to be selected and then one of the resources allocated to that
process needs to be selected based on some criteria like execution time, number of resources allocated, priority and so on.
Rollback: When a resource is preempted from a deadlocked process then that process needs to be restated from the
beginning.
Starvation: resources should not be preempted from the same process in each step.
Ex: n=5, m=4
A B C D A B C D
A B C D
P0 0 0 1 2 P0 0 0 1 2
1 5 2 0
P1 1 7 5 0 P1 1 0 0 0
P2 2 3 5 6 P2 1 3 5 4
P3 0 6 5 2 P3 0 6 3 2
P4 0 6 5 6 P4 0 0 1 4
Need
A B C D
P0 0 0 0 0
P1 0 7 5 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2
Process P1 makes a request
Request1 = (0,4,2,0)
Performance of computer system is high when the CPU is kept busy at all times.
A number of processes or programs is loaded into main memory at a time in order to keep the CPU busy at all times.
During the execution of a process, if the process waits for an input/output then the CPU is switched to other process in the
main memory.
3
Logical address
When the user wants to execute his/her program then the operating system loads the program into main memory.
CPU don’t know the location of program inside the main memory.
To execute a statement of the program, CPU generates the logical address of the statement.
For example, CPU generates the logical address 0 to execute the 1st statement in the program.
CPU generates the logical address 1 to execute the 2nd statement in the program.
The logical address range is 0 to n-1, where n is the number of statements in the program.
The set of logical addresses generated by the CPU during the execution of the program is called as logical address space.
For example, if the program has 10 statements then the logical address space includes the logical addresses 0, 1, 2, 3, 4, 5, 6,
7, 8, 9.
Physical address
Physical address refers to a location inside the main memory where a statement of the program is loaded.
The set of physical addresses of locations inside main memory where the statements of the program are loaded.
For example, if a program has 10 statements and are loaded into main memory locations whose address starts from 20340
then the physical address space includes the physical addresses 20340, 20341, 20342, 20343, 20344, 20345, 20346, 20347,
20348, 20349.
5
Memory Management Unit (MMU)
The MMU maintains two registers: Base or relocation register and limit register.
The relocation register is used to store the starting physical address of the process.
The limit register is used to store the number of statements in the process.
6
Before starting the execution of a process, the starting physical address of the process is loaded into Relocation or Base
Register and the number of statements in the process is loaded into the Limit Register.
For example, if a process P1 is loaded into main memory at the address 1200 and the number of statements in the process
P1 is 5, when the execution of process P1 is started, the operating system loads the address 1200 into Relocation Register
and value 5 into Limit Register.
While executing the process, the logical address generated by the CPU for executing a statement is compared with the value
in Limit Register.
If the logical address value is less than the value in Limit Register then the logical address is added to the value in Relocation
Register to generate the corresponding physical address.
7
Memory Management Techniques
The operating system uses the following techniques to allocate space for programs in the main memory.
Operating system loads the entire program as a single unit into the main memory.
8
Equal size fixed partition technique
Before loading any program into main memory, the operating system divides the space in main memory into equal size parts.
Only one program can be loaded into each partition of main memory.
Operating system can load a program into any free partition of main memory.
Main Memory
9
Advantage
To load a program, the operating system can select any free partition of main memory.
Disadvantages
1) Wastage of space inside the partition when the size of program is less than the size of partition.
2) When the size of program to be loaded is greater than the size of partition then it is not possible to load the program into
main memory.
Before loading any program into main memory, the operating system divides the space in main memory into different size
parts.
Only one program can be loaded into each partition of main memory.
Operating system has to select a suitable partition of main memory for loading a program.
10
Main Memory
Advantage
Internal Fragmentation is less when compared with equal size fixed partition technique.
Disadvantages
1) Operating System has to select a suitable partition for loading a program into main memory.
2) Wastage of space inside the partition when the size of program is less than the size of partition. 11
3) When the size of program to be loaded is greater than the size of partition then it is not possible to load the program into
main memory.
Operating system divides the space in main memory into parts at the time of loading programs into main memory.
Operating System is loaded into one part and the other part is used for loading user programs.
After loading a program say P1, the space in main memory is divided into three parts.
After loading another program say P2, the space in main memory is divided into four parts as shown in below figure.
12
Advantage
Disadvantages
Main Memory
13
Consider the above position of main memory which is the result of loading some programs into main memory and removing
the completed programs from main memory.
If the operating system wants to load a program say P20 with size 250 KB, then it is not possible to load that program into
main memory as there is no free part with size greater than or equal to 250KB in the main memory.
Compaction
Compaction technique moves all free spaces in the main memory together.
After applying compaction technique to the above position of main memory, the position of main memory is as shown
below
Main Memory
14
With unequal size fixed partition and dynamic partition techniques, the operating system has to select a suitable partition in
the main memory for loading a program.
Operating System uses any of the following placement algorithms to select the suitable partition for loading the program.
Placement Algorithms
1) FIRST FIT
2) BEST FIT
3) WORST FIT
First fit algorithm scans the main memory either from the from starting or from the position where recent placement
happened and selects the first free partition whose size is greater than or equal to the size of program to be loaded.
Best fit algorithm scans the main memory from starting to ending and selects the free partition whose size is very close to
the size of program to be loaded.
Worst fit algorithm scans the main memory from starting to ending and selects the largest free partition.
15
Consider the following position of main memory.
To load the program P10 with size 150 KB, the partition selected by operating system with different algorithms is indicated in
below diagram.
16
Assume the memory is divided into six partitions with size 200 KB, 400 KB, 100 KB, 600 KB, 300 KB, 500 KB (in order).
Calculate the total internal fragmentation when the processes with size 336 KB, 96 KB, 173 KB, 415 KB, 245 KB (in order) are
placed with
i) First Fit algorithm
ii) Best Fit algorithm
Paging Technique
Before loading any program into main memory, the space in main memory is divided into equal size frames.
To load a program into main memory, the program is divided into equal size pages.
The pages of program are loaded into free frames of main memory.
After loading all pages of program, a page table is created for the program.
Number of entries in the page table of the program is equal to the number of pages in the program.
The entries of page table are filled with the frame numbers in which the pages of the program are loaded.
The following diagram shows how programs are loaded into main memory with the paging technique.
19
Mapping Logical Address to Physical Address in Paging technique (or) Hardware support for Paging technique
20
To execute a statement of the program loaded into main memory using the paging technique, the CPU generates the logical
address of that statement.
This logical address consists of two parts: page number (p) and offset (d).
The page number is used as an index into the page table of the program to find the corresponding frame number.
The identified frame number is appended with offset of logical address to get the physical address of the statement.
The statement in the main memory at the generated physical address is transferred to the CPU for execution.
21
22
Consider a logical address space of 256 pages with 64 words per page mapped onto a physical memory of 4096 frames.
i. How many bits are required in the logical address?
ii. How many bits are required in the physical address?
Size of logical address = size of page number field + size of offset field = 8 bits (256=28) + 6 bits (64=26 ) = 14 bits
Size of physical address = size of frame number field + size of offset field = 12 bits (4096=212) + 6 bits (64=26) = 18 bits
Ex: Consider a machine with 128 MB physical memory and a 32 bit virtual address space. If the page size is 8 KB.
i) Find the number of bits in physical address
ii) Find the number of frames in the Main memory
iii) Find the number of bits allocated for offset
iv) Find the number of entries in page table
Size of physical memory/main memory/primary memory/RAM = 128 MB = 128 X 220 B = 27 X 220 B = 227 B
Size of virtual address/logical address = 32 bits
Size of program = 232 B
Size of page = 8 KB = 8 X 210 B = 23 X 210 B = 213 B
Size of frame = 213 B
i) Number of bits in physical address or size of physical address = 27 bits
ii) Number of frames in main memory = size of main memory/size of frame = 227 B / 213 B = 214 frames
iii) Number of bits allocated for offset = 13 bits
iv) Number of entries in page table=Number of pages in the program=size of program/size of page=232 B / 213 B = 219 entries
Ex: In a virtual memory system, size of virtual address is 64-bit, size of physical address is 60-bit, page size is 8 Kbyte and size
of each page table entry is 32 bytes. The main memory is byte addressable.
i) Calculate the Main memory size.
ii) Calculate the number of frames in the main memory.
iii) Calculate the page table size.
iv) Find the number of bits used for offset.
Ex: Calculate the size of memory if its address consists of 22 bits and the memory is 2-byte addressable.
Ex: Calculate the number of bits required in the address for memory having size of 16 GB. Assume the memory is 4-byte
addressable.
Ex: Consider a system with byte-addressable memory, 32 bit logical addresses, 4 kilobyte page size and page table entries of
4 bytes each. Calculate the size of the page table in the system in megabytes.
Ex: Consider a machine with 64 MB physical memory and a 32 bit virtual address space. If the page size is 4 KB, what is the
approximate size of the page table?
Ex: In a virtual memory system, size of virtual address is 32-bit, size of physical address is 30-bit, page size is 4 Kbyte and size
of each page table entry is 32-bit. The main memory is byte addressable. Which one of the following is the maximum
number of bits that can be used for storing protection and other information in each page table entry?
Translation Look-aside Buffer (TLB)
The operating system stores the page table of a process in the registers of computer system when the number of entries in
the page table is less. Otherwise, stores the page table in the main memory.
When the page table of process is stored in the registers, then one memory access is enough for getting a statement of the
process from main memory.
When the page table of process is stored in the main memory, then two memory accesses are required for getting a
statement of the process from main memory.
To get a statement from the main memory, frame number of that statement needs to be obtained first.
To get frame number of the statement, page table of the process stored in the main memory has to be accessed and it
requires one memory access.
One more memory access is to get the statement from the identified frame of the main memory.
To get a statement of the process from main memory with one memory access or to reduce the time for access, information
about frequently executed pages is stored in the translation look-aside buffer (TLB).
TLB contains a number of entries.
Each entry of the TLB contains a page number and its corresponding frame number.
TLB is associative in nature. All entries of TLB are searched at a time in parallel.
The following diagram shows the usage of TLB in paging.
When the CPU generates logical address of a statement, the page number of logical address is first searched in the TLB.
If the page number is found in the TLB (TLB hit) then the corresponding frame number is appended with the offset of logical
address to generate the corresponding physical address.
Otherwise (TLB miss), the page number of logical address is used as an index into the page table, the corresponding frame
number is identified and then appended with the offset of logical address to generate the physical address.
Hit ratio is the percentage of times that a particular page number is found in the TLB.
An 80 percent hit ratio means that we find the desired page number in the TLB 80 percent of the time.
Effective memory access time = probability of finding the page in the TLB x time to access the statement from the main
memory + probability of not finding the page in the TLB x time to access the statement from the main memory
If the hit ratio is 80 percent and it takes 20 nanoseconds to search the TLB, 100 nanoseconds to access the memory then the
effective memory access time is
Effective access time = 0.80 x (20+100) + 0.20 x (20+100+100) = 0.8 x 120 + 0.2 x 220 = 140 nanoseconds
If the hit ratio is 98 percent and it takes 20 nanoseconds to search the TLB, 100 nanoseconds to access the memory then the
effective memory access time is
Operating system divides the space in main memory at the time of loading programs into main memory.
To load a program into main memory, the operating system divides the program into a number of segments of either equal
or unequal size.
The operating system then loads the segments of program into main memory wherever free space is available.
The operating system then creates a segment table for the program.
The number of entries in the segment table is equal to the number of segments in the program.
Each entry of the segment table contains a base which indicates the starting address of the segment in the main memory,
and a limit/length which indicates the number of statements in that segment.
The following figure depicts how a program say P1 is loaded into main memory using segmentation technique.
31
The program P1 has 2000 statements and is divided into four segments.
The number of statements in the four segments are 500, 400, 300 and 800 respectively.
The segments are loaded into main memory and then a segment table is created for the program.
To execute a statement of the program, the CPU generates the logical address of the statement.
The logical address consists of two parts: segment number and offset.
The segment number is used as an index into the segment table to identify the entry in the segment table.
Then, the offset value of logical address is compared with the limit value in the identified entry.
If the offset value is less than the limit value, then the offset value is added to the base value to generate the physical
address.
If the offset value is greater then the limit value, then an error is reported.
33
Ex: Consider the following segment table:
What are the physical addresses for the following logical addresses?
a) 0, 512
b) 1, 47
c) 2, 13
d) 3, 185
In module 4, we discussed various memory-management strategies (contiguous allocation, paging, segmentation) used in
computer systems for loading programs into main memory.
All these strategies require that an entire program be in memory before it can execute.
Virtual memory is a technique that allows the execution of programs that are not completely in memory.
One major advantage of this scheme is that programs can be larger than physical memory.
Virtual memory allows to load more number of programs into main memory, thereby increases performance of computer
system.
Virtual memory also allows processes to share files and libraries, and to implement shared memory.
Demand Paging
With demand-paged virtual memory, pages are loaded only when they are demanded during program execution.
Pages that are never accessed are thus never loaded into physical memory.
The procedure used in demand paging technique to load programs into main memory is as follows:
Before loading any program into main memory, the space in main memory is divided into a number of equal size frames.
To load a program into the main memory, the program is divided into a number of equal size pages.
Loads the required pages of the program into the free frames of main memory.
The Number of entries in the page table is equal to the number of pages in the program.
A valid-invalid bit is stored in each entry of the page table to indicate whether the page has been loaded into main memory or
not.
If the page is loaded into main memory, then the corresponding frame number is stored in the page table entry and the valid-
invalid bit is set to ‘v’.
If the page is not loaded into main memory, then the frame number field is empty and the valid-invalid bit is set to ‘i’.
To execute a statement of the program, CPU generates the logical address of the statement.
The logical address has two parts: page number and offset.
The page number of logical address is used as an index into the page table of the program.
If the entry of page table contains a frame number, then that frame number is appended with the offset of logical address to
generate the physical address.
If the entry of page table does not contain any frame number, then that situation is called a page fault.
A page fault occurs when the required page is not present in the main memory.
When a page fault occurs, the operating system has to load the required page into the main memory.
Operating system performs the following activities to load the required page when a page fault occurs.
1. Verifies whether any free frame is available in the main memory.
2. If any free frame is available, then loads the required page into the free frame and updates the page table of program.
3. Otherwise, replaces one of the pages in the main memory with the required page and updates the page table of
program. Operating System uses a page replacement algorithm for replacing one of the pages in the main memory.
4. Instructs the CPU to generate the same logical address again.
The following diagram depicts the activities performed by operating system to handle the page fault.
Free-Frame List
When a page fault occurs, the operating system must bring the desired page from secondary storage into main memory.
To resolve page faults, most operating systems maintain a free-frame list, a pool of free frames for satisfying such requests.
Zero-fill-on-demand frames are “zeroed-out” before being allocated, thus erasing their previous contents.
Page fault time is the time required to service the page fault i.e. the time required to load the required page into main
memory from secondary storage.
Ex: if the probability of occurring age fault is 0.2, memory access time is 100 nano seconds and the page fault time is 2000
nano seconds then the effective access time is
Effective access time=(1 - 0.2) X 100 + 0.2 X 2000 = 0.8 X 100 + 400 = 80 + 400 = 480 nano seconds
Page Replacement
During the execution of a program, if a page fault occurs then the operating system has to load the required page into the
main memory.
If the main memory is not having a free frame then the operating system has to replace one of the pages in the main
memory with the required page.
To select a page in main memory for replacement, the operating system uses different page replacement algorithms.
Page replacement algorithms
The following are different page replacement algorithms used by operating systems:
Reference string
In a program the pages will be executed in any order and each page may be executed for any number of times.
Reference string indicates the order in which the pages of a program are executed or referenced.
10
First in first out page replacement algorithm
The page that was loaded for the first time into main memory is replaced by the requested page.
Ex1:-
12
Ex3:
13
Ex4:
14
Generally, the number of page faults will be decreased by increasing the number of frames in the main memory.
With FIFO algorithm, in some cases, the number of page faults increases when the number of frames in the main memory
are increased.
Advantage:
Disadvantages:
15
Optimal page replacement algorithm
The page that will not be used for the longest period of time will be replaced by the requested page.
Ex1:
16
Ex2:
17
Ex3:
18
Ex4:
19
Advantages:
Disadvantage:
If the operating system doesn’t know the order in which the pages will be referred, then it is not possible to calculate future
references for the pages and not possible to use this algorithm.
20
Least Recently Used Page Replacement Algorithm
The page that has not been used for the longest period of time is replaced by the requested page.
Ex1:
21
Ex2:
22
Advantages:
A reference count is maintained with each page loaded in the main memory.
Reference count of a page indicates the number of times that page is referenced or executed.
If two or more pages have the same least reference count, then out of the pages with least reference count the page that
23
was loaded first is replaced.
Ex1:
If two or more pages have the same highest reference count, then out of the pages with highest reference count the page
that was loaded first is replaced.
Ex1:
When a page fault occurs, if no frames are free, two page transfers (one for the page-out and one for the page-in) are
required as shown in the below figure.
This situation doubles the page-fault service time and increases the effective access time accordingly.
This overhead can be reduced by using a modify bit (or dirty bit).
When this scheme is used, each page has a modify bit associated with it.
The modify bit for a page is set whenever any byte in the page is written into, indicating that the page has been modified.
If the bit is set, then the page must be written to the disk.
If the modify bit is not set, then the page is not required to be written to the disk. The page can be discarded.
A file is a container of information that is stored in the secondary storage device (Hard disk).
Data files may contain numeric or text data, photos, music, video, and so on..
In general, a file is a sequence of bits, bytes, lines, or records, the meaning of which is defined by the file’s creator and user.
A file is named, for the convenience of its users, and is referred to by its name.
2) Identifier: it is a unique number assigned by operating system. The operating system identifies a file by its identifier.
5) Size: indicates the current size of the file in bytes or words or blocks.
7) Time, date and user identification: indicates the owner of file and the date and time on which the file is created and last
modified.
Directory
The information about all files is kept in the directory structure, which resides on the same device as the files themselves.
A directory entry consists of the file’s name and its unique identifier.
File Operations
1) Creating a file: to create a new file, the operating system has to allocate space for the file and create an entry for the file
in the directory.
2) Opening a file: a file has to be opened to perform any operations on the file. Upon successful opening of the file, a file
handle is returned using which operations are performed on the file.
3) Writing a file: to write to a file, file handle and the information to be written should be specified. The operating system
identifies the file and writes the information into the file at the position specified by file pointer and moves the file pointer
after completing the write operation.
4) Reading a file: to read from a file, file handle should be specified. The operating system identifies the file and then reads
the data from the position pointed by the file pointer and moves the file pointer after completing the read operation.
5) Repositioning within a file: repositions the file pointer. This operation is also called as file seek.
6) Deleting a file: the operating system search for the file, releases the file space and erase the directory entry of the file.
7) Truncating a file: the contents of the file are deleted but the space allocated to the file is not released.
File type helps the operating system to decide what operations can be performed on the file.
The file name is split into two parts: name and extension, separated by a period character.
The following table depict some common file types, extensions and the content in that files.
Allocation Methods
Many files are stored on the same disk.
The main problem is how to allocate space to these files so that storage space is utilized effectively and files can be accessed
quickly.
To allocate space to these files three major methods are in wide use: contiguous, linked, and indexed.
An operating system uses one method for all files within a file-system type.
Contiguous Allocation
If the file is n blocks long and starts at location b, then it occupies blocks b, b+1, b+2, ... ,b+n-1.
The directory entry for each file indicates the address of the starting block and the number of blocks allocated for that file.
Advantages:
1) Easy to implement
2) Supports both sequential and direct access to the blocks of file (For direct access to block i of a file that starts at block b,
we can immediately access block b + i ).
Disadvantages:
1) External fragmentation
As files are allocated and deleted, the free disk space is broken into pieces.
External fragmentation exists whenever free space is broken into chunks and when the largest contiguous chunk is
insufficient for a request.
Compaction technique compact all free space into one contiguous space.
When the file is created, the total amount of space it will need must be found and allocated.
If too little space is allocated to a file then the file cannot be extended.
If more space is allocated then some space may be wasted (internal fragmentation).
To minimize these drawbacks, some operating systems use a modified contiguous-allocation scheme.
In this scheme, a contiguous chunk of space is allocated initially; then, if that space is not enough, another chunk of
contiguous space, known as an extent is added.
The directory entry of the file now contains address of the starting block, block count, plus address of first block of the next
extent.
Linked Allocation
With linked allocation, free blocks at any position of the disk can be allocated to a file.
For example, a file of five blocks may start at block 9 and continue at block 16, then block 1, then block 10, and finally block
25.
Each block allocated to the file contains a pointer to the next block allocated to the file.
The directory entry of a file contains a pointer to the first and last blocks of the file.
Advantages:
1) There is no external fragmentation with linked allocation, and any free block on the free-space list can be used to satisfy a
request.
2) The size of a file need not be declared when that file is created. A file can continue to grow as long as free blocks are
available.
Disadvantages:
1) Does not support direct access. To find the ith block of a file, we must start at the beginning of that file and follow the
pointers until we get to the ith block.
One solution to this problem is to collect blocks into multiples, called clusters and to allocate clusters rather than blocks.
Pointers then use a much smaller percentage of the file's disk space.
Cluster mechanism improves disk throughput and decreases the space needed for block allocation and free-list
management.
But, this approach increases internal fragmentation, because more space is wasted when a cluster is partially full than when
a block is partially full.
3) Another problem with linked allocation is reliability. If a block allocated to a file is corrupted then it is not possible to
access the remaining blocks of the file.
This simple but efficient method of disk-space allocation was used by the MS-DOS operating system.
A section of storage at the beginning of the disk is reserved for the FAT.
The FAT has one entry for each block in the disk and is indexed by block number.
Initially, all entries in FAT are filled with 0 to indicate that all blocks in the disk are initially free.
To store a file in the disk, free blocks at any position of the disk can be allocated to the file as in linked allocation technique.
In the directory entry, name of the file and number of the first block allocated to the file is stored.
In the FAT, the entry indexed by the first block number that is allocated to the file contains the number of the next block
allocated to the file.
This chain continues until it reaches the last block, which has a special end-of-file value as the table entry.
Following figure shows how the blocks allocated to the file are linked with FAT.
Allocating a new block to a file is a simple matter of finding the first 0-valued table entry and replacing the previous end-of-
file value with the address of the new block.
Free blocks at any position of the disk can be allocated to a file as in linked allocation method.
The addresses of blocks allocated to the file are stored into another block called index block.
Each file has its own index block, which is an array of disk-block addresses.
The ith entry in the index block points to the ith block of the file.
The directory entry of the file contains the address of the index block.
To access the ith block, we use the pointer in the ith entry of index block.
Advantages:
1) There is no external fragmentation with indexed allocation, and any free block on the free-space list can be used to satisfy
a request.
2) The size of a file need not be declared when that file is created. A file can continue to grow as long as free blocks are
available.
3) Blocks of the file can be accessed directly. ith block of the file can be accessed directly using the pointer in the ith entry of
index block.
4) If one of the blocks allocated to the file is corrupted, still the remaining blocks of the file can be accessed.
Disadvantages:
Consider a file which occupies only one or two blocks. With linked allocation, we lose the space of only one pointer per
block.
With indexed allocation, an entire index block must be allocated, even if only one or two pointers will be non-null.
2) Size of file is limited to number of pointers in the index block.
Linked scheme
If one index block is not enough to store the addresses of blocks allocated to a file then a number of index blocks are
allocated to the file and they are linked together.
The last entry in first index block contains the address of second index block.
This chain is repeated till the last index block of the file.
Multilevel index
This scheme uses a first-level index block to point to a set of second-level index blocks, which in turn point to the file blocks.
To access a block, the operating system uses the first-level index to find a second-level index block and then uses that block
to find the desired data block.
This approach could be continued to a third or fourth level, depending on the file size.
Combined scheme
In this scheme, the first 15 pointers of the index block are stored in the file's inode.
The first 12 of these pointers point to direct blocks; that is, they contain addresses of blocks that contain data of the file.
Thus, the data for small files (of not more than 12 blocks) do not need a separate index block.
The first pointer points to a single indirect block, which is an index block containing not data but the addresses of blocks that
do contain data.
The second pointer points to a double indirect block, which contains the address of a block that contains the addresses of
blocks that contain pointers to the actual data blocks.
The operating system maintains a free-space list to keep track of free blocks in the disk.
The free-space list contains the addresses or numbers of free blocks in the disk.
To allocate blocks to a file, the operating system searches the free-space list and identifies the required number of free
blocks and allocates that blocks to the file.
The allocated blocks are then removed from the free-space list.
When a file is deleted, the blocks allocated to that file are added to the free-space list.
1) Bit Vector
2) Linked List
3) Grouping
Bit Vector
Each block is represented by one bit. If the block is free then the bit is 0; if the block is allocated then the bit is 1.
For example, consider a disk with 32 blocks (0 to 31) where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26, and 27 are
free and the rest of the blocks are allocated.
11000011000000111001111110001111
Advantage:
To store the Bit Vector, more space is required when the size of disk is large.
For example, if the size of disk is 1-TB (240 bytes) and size of each block in the disk is 4-KB (212 bytes) then the size of Bit
Vector is
The address of first free block is stored in a special location in the disk.
The first free block contains a pointer to the next free block, and so on.
For example, consider a disk where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26, and 27 are free and the rest of the
blocks are allocated.
In this situation, the address of block 2 is stored in the special location in the disk.
Block 2 will contain a pointer to block 3, which will point to block 4, which will point to block 5, which will point to block 8,
and so on.
Advantage:
Disadvantage:
The addresses of n free blocks are stored in the first free block.
The last block contains the addresses of another n free blocks, and so on.
Block number 10 contains the addresses of blocks 11, 12, 13, 17, 18, 25.
Advantage:
The bulk of secondary storage for modern computers is provided by hard disk drives (HDDs) and nonvolatile memory (NVM)
devices.
we describe the basic mechanisms of these devices and explain how operating systems translate their physical properties to
logical storage via address mapping.
The set of tracks that are at one arm position makes up a cylinder.
Each sector has a fixed size and is the smallest unit of transfer.
The sector size was commonly 512 bytes until around 2010.
The storage capacity of common disk drives is measured in gigabytes and terabytes.
A disk drive with the cover removed is shown in the following figure.
When the disk is in use, a drive motor spins it at high speed (60 to 250 times per second).
The time required to access data from the disk is called access time.
The access time consists of the time necessary to move the disk arm to the desired cylinder, called the seek time and the
time necessary for the desired sector to rotate to the disk head, called the rotational latency.
Disks can transfer several megabytes of data per second, and they have seek times and rotational latencies of several
milliseconds.
Mapping logical blocks to physical sectors
In the logical view of disk, the disk is collection of blocks. Block is the smallest unit of transfer.
In the physical view of disk, the disk is collection of sectors. Sector is the smallest unit of transfer.
Sector 0 is the first sector of the first track on the outermost cylinder.
The mapping proceeds in order through that track, then through the rest of the tracks in that cylinder, and then through the
rest of the cylinders from outermost to innermost.
‘c’ is the cylinder number, ‘t’ is the track number and ‘s’ is the sector number.
Ex: A hard disk has 63 sectors per track, 10 platters and 1000 cylinders.
1) Find the corresponding block number of the address <400, 16, 29>
1) Capacity of disk pack = Number of surfaces x Number of tracks per surface x Number of sectors per track x Storage
capacity of one sector
2) Average disk access time = Average Seek time + Average Rotational delay or latency + Transfer time
6) Data transfer rate = Number of heads x Capacity of one track x Number of rotations in one second
Capacity of disk pack = Number of surfaces x Number of tracks per surface x Number of sectors per track x Number of bytes
per sector = 16 x 128 x 256 x 512 bytes = 228 bytes = 256 MB
Data transfer rate = Number of heads x Capacity of one track x Number of rotations in one second = 16 x (256 x 512 bytes) x
(3600 / 60) rotations/sec = 24 x 28 x 29 x 60 bytes/sec = 60 x 221 bytes/sec = 120 MBps
Average rotational delay = 1/2 x Time taken for one full rotation = 1/2 x (1/3600) minutes = 1/2 x (60/3600) sec = 1/2 X 1/60
sec = 0.0083 sec = 0.0083 X 1000 msec = 8.3 msec
Average access time = Average seek time + Average rotational delay + Transfer time = 11.5 msec + 8.3 msec + 0 = 19.8 msec
Formatting overhead = Total number of sectors x overhead per sector = (16 x 128 x 256) x 32 bytes = 219 x 25 bytes = 224 bytes
= 16 MB
Formatted disk space = Total disk space – Formatting overhead = 256 MB - 16 MB = 240 MB
Ex2: What is the average access time for transferring 512 bytes of data with the following specifications-
• Average seek time = 5 msec
• Disk rotation = 6000 RPM
• Data rate = 40 KB/sec
Average rotational delay = 1/2 x Time taken for one full rotation = 1/2 x 1/6000 minutes = 1/2 X 60/6000 sec = 1/2 X 1/100
sec = 1/200 sec = 0.005 sec = 0.005 X 1000 msec = 5 msec
Transfer time = Number of bytes to be transferred / Data transfer rate = 512 bytes / 40 KB per sec = 29 bytes / 40 X 210 bytes
per sec = 0.0125 sec = 0.0125 X 1000 = 12.5 msec
Average access time = Average seek time + Average rotational delay + Transfer time = 5 msec + 5 msec + 12.5 msec = 22.6
msec
Ex3: Consider a typical disk that rotates at 15000 RPM and has a transfer rate of 50 x 106 bytes/sec. If the average seek time
of the disk is twice the average rotational delay. What is the average time (in milliseconds) to read or write a 512 byte sector
of the disk?
Average rotational delay = 1/2 x Time taken for one full rotation = 1/2 x 1/15000 minutes = 1/2 X 60/15000 sec = 2 msec
Transfer time = Number of bytes to be transferred / Data transfer rate = 512 bytes / (50 x 106 bytes/sec) = 10.24 x 10-6 sec =
0.01024 msec
Average time to read or write 512 bytes = Average seek time + Average rotational delay + Transfer time = 4 msec + 2 msec +
0.01024 msec = 6.11 msec
Ex4: A hard disk system has the following parameters-
• Number of tracks = 500
• Number of sectors per track = 100
• Number of bytes per sector = 500
• Time taken by the head to move from one track to another adjacent track = 1 msec
• Rotation speed = 600 RPM
What is the average time taken for transferring 250 bytes from the disk?
Average rotational delay = 1/2 x Time taken for one full rotation = 1/2 x 1/600 minutes = 1/2 x 60/600 sec = 1/200 sec =
0.005 sec = 0.005 X 1000 msec = 50 msec
Capacity of one track = Number of sectors per track x Number of bytes per sector = 100 x 500 bytes = 50000 bytes
Data transfer rate = Number of heads x Capacity of one track x Number of rotations in one second = 1 x 50000 bytes x (600 /
60) = = 50000 x 10 bytes/sec = 5 x 105 bytes/sec
Transfer time = (250 bytes / 5 x 105 bytes) sec = 50 x 10-5 sec = 0.5 msec
Average time taken to transfer 250 bytes = Average seek time + Average rotational delay + Transfer time = 249.5 msec + 50
msec + 0.5 msec = 300 msec
Ex5: A certain moving arm disk storage with one head has the following specifications-
• Number of tracks per surface = 200
• Disk rotation speed = 2400 RPM
• Track storage capacity = 62500 bits
Average latency = 1/2 x Time taken for one full rotation = 1/2 x 1/2400 minutes = 1/2 X 60 / 2400 sec = 1/2 X 1/40 sec = 1/80
sec = 0.0125 sec = 0.0125 X 1000 msec = 12.5 msec
Data transfer rate = Number of heads x Capacity of one track x Number of rotations in one second = 1 x 62500 bits x (2400 /
60) = 2500000 bits/sec = 2.5 x 106 bits/sec
Disk Scheduling
To use the disk efficiently, the operating system has to minimize the access time and maximize the bandwidth.
The access time and bandwidth are improved by managing the order in which the disk I/O requests are serviced.
This algorithm is simple, but it does not provide the fastest service.
Consider a disk queue with requests for I/O to blocks on the cylinders 98, 183, 37, 122, 14, 124, 65, 67.
If the disk head is initially at cylinder 53, it will first move from 53 to 98, then to 183, 37, 122, 14, 124, 65, and finally to 67.
The SSTF algorithm selects the request with the least seek time from the current head position.
In other words, SSTF chooses the pending request closest to the current head position.
For the example request queue (98, 183, 37, 122, 14, 124, 65, 67), the closest request to the initial head position (53) is at
cylinder 65.
After processing the request at cylinder 65, the next closest request is at cylinder 67.
Continuing, we service the request at cylinder 14, then 98, 122, 124, and finally 183.
This scheduling method results in a total head movement of only 236 cylinders.
Suppose that we have two requests in the queue, for cylinders 14 and 186, and while the request for 14 is being serviced, a
new request near 14 arrives.
This new request will be serviced next, making the request at 186 wait.
While this request is being serviced, another request close to 14 could arrive.
A continual stream of requests near one another can cause the request for cylinder 186 to wait indefinitely.
SCAN Scheduling
In the SCAN algorithm, the disk arm starts at one end of the disk and moves towards the other end, servicing requests along
the way, until it gets to the other end of the disk.
At the other end, the direction of head movement is reversed, and servicing continues.
The head continuously scans back and forth across the disk.
Before applying SCAN algorithm to schedule the requests, we need to know the direction of head movement in addition to
the head's current position.
Assuming that the disk arm is moving towards 0 and that the initial head position is again 53, the head will next service 37
and then 14.
At cylinder 0, the arm will reverse and will move towards the other end of the disk, servicing the requests at 65, 67, 98, 122,
124, and 183.
More requests will be at the other end of the disk and these requests have also waited the longest.
C-SCAN Scheduling
Circular SCAN (C-SCAN) is a variant of SCAN and provides a more uniform wait time.
Like SCAN, C-SCAN moves the head from one end of the disk to the other, servicing requests along the way.
When the head reaches the other end, it immediately returns to the beginning of the disk without servicing any requests on
the return trip.
Both SCAN and C-SCAN move the disk arm across the full width of the disk.
In practice, the arm goes only as far as the final request in each direction.
Then, it reverses direction immediately, without going all the way to the end of the disk.
Versions of SCAN and C-SCAN that follow this pattern are called LOOK and C-LOOK scheduling, because they look for a
request before continuing to move in a given direction.