You are on page 1of 150

Operating Systems

Why to study OS:

1. To understand how computer works


2. To know Resource management
3. To learn how to write concurrent code
4. Career Subject + Gate(10M)+ Other competitive exams

Pre- requisites of OS:


1. Data structures, Computer organization and architecture, C- programming

Course Objectives
1. To understand the services provided by and the design of an operating system.
2. To understand the structure and organization of the file system.
3. To understand what a process is and how processes are synchronized and scheduled.
4. To understand different approaches to memory management.
5. Students should be able to use system calls for managing processes, memory and the file
system.
6. Students should understand the data structures and algorithms used to implement an OS.

Syllabus:

Unit1: Introduction to OS, OS structure& Strategies, Process, thread, scheduling algorithms

Unit2:

Memory management- How to allocate memory, How to replace page in main memory, Virtual
memory
File system- File concept, Directory, Implementations

Unit3: Process synchronization and Deadlocks


Unit4: Device Management- Disk, RAID, I/O system
Unit 5: Case studies: Linux& Windows xp

Textbooks:
1. Silberschatz, Galvin, Gagne, Operating System Concepts (8th Edition), John Wiley and
Sons, 2008.
2. Modern Operating Systems ,3rd edition by Andrew S Tanenbaum
Definition of Operating System: An operating system is a program that acts as an
interface between the user and the computer hardware and controls the execution of all kinds of
programs like file management, memory management, process management, handling input and
output, and controlling peripheral devices such as disk drives and printers.
Some popular Operating Systems include Linux Operating System, Windows Operating System,
VMS, OS/400, AIX, z/OS, etc.

Computer Hardware:

memory

Aritmetic & logical


Input unit Output

Control unit
• The application programs such as compliers, games, firefox, Ms-office, libre office, etc
defines the way in which these resources are used to solve the computing problems of users
• Resource manager: A computer has many resources that may require to solve a problem:
CPU time, memory space, storage space, I/O devices and so on. OS acts as the manager of
these resources and allocates them to specific programs & users as per the necessary.

Types of Operating Systems:


Operating systems are there from the very first computer generation and they keep evolving with
time. In this chapter, we will discuss some of the important types of operating systems which are
most commonly used.

Batch operating system

The users of a batch operating system do not interact with the computer directly.
Each user prepares his job on an off-line device like punch cards and submits it to the computer
operator.
To speed up processing, jobs with similar needs are batched together and run as a group.
The programmers leave their programs with the operator and the operator then sorts the programs
with similar requirements into batches.

The problems with Batch Systems are as follows −

• Lack of interaction between the user and the job.

• CPU is often idle, because the speed of the mechanical I/O devices is slower than the CPU.

• Difficult to provide the desired priority.


Multiprogramming Operating System:
Multiprogramming is an extension to the batch processing where the CPU is kept always busy.
Each process needs two types of system time: CPU time and IO time.

In multiprogramming environment, for the time a process does its I/O, The CPU can start the
execution of other processes. Therefore, multiprogramming improves the efficiency of the
system.

Multiprocessing Operating System:

In Multiprocessing, Parallel computing is achieved. There are more than one processors present in
the system which can execute more than one process at the same time. This will increase the
throughput of the system.

Time-sharing operating systems

Time-sharing is a technique which enables many people, located at various terminals, to use a
particular computer system at the same time.
Time-sharing or multitasking is a logical extension of multiprogramming. Processor's time which
is shared among multiple users simultaneously is termed as time-sharing.
Multiple jobs are executed by the CPU by switching between them, but the switches occur so
frequently. Thus, the user can receive an immediate response. For example, in a transaction
processing, the processor executes each user program in a short burst or quantum of computation.
That is, if n users are present, then each user can get a time quantum. When the user submits the
command, the response time is in few seconds at most.
The operating system uses CPU scheduling and multiprogramming, to provide each user with a
small portion of a time. Computer systems that were designed primarily as batch systems have
been modified to time-sharing systems.
Advantages of Timesharing operating systems are as follows −

• Provides the advantage of quick response.

• Avoids duplication of software.

• Reduces CPU idle time.


Disadvantages of Time-sharing operating systems are as follows −

• Problem of reliability.

• Question of security and integrity of user programs and data.

• Problem of data communication.

Distributed operating System

Distributed systems use multiple central processors to serve multiple real-time applications and
multiple users. Data processing jobs are distributed among the processors accordingly.
The processors communicate with one another through various communication lines (such as high-
speed buses or telephone lines). These are referred as loosely coupled systems or distributed
systems. Processors in a distributed system may vary in size and function. These processors are
referred as sites, nodes, computers, and so on.
The advantages of distributed systems are as follows −

• With resource sharing facility, a user at one site may be able to use the resources available at
another.

• Speedup the exchange of data with one another via electronic mail.

• If one site fails in a distributed system, the remaining sites can potentially continue
operating.

• Better service to the customers.

• Reduction of the load on the host computer.

• Reduction of delays in data processing.

Network operating System

A Network Operating System(tightly) runs on a server and provides the server the capability to
manage data, users, groups, security, applications, and other networking functions. The primary
purpose of the network operating system is to allow shared file and printer access among multiple
computers in a network, typically a local area network (LAN), a private network or to other
networks.
Real Time operating System

A real-time system is defined as a data processing system in which the time interval required to
process and respond to inputs is so small that it controls the environment. The time taken by the
system to respond to an input and display of required updated information is termed as
the response time. So in this method, the response time is very less as compared to online
processing.
Real-time systems are used when there are rigid time requirements on the operation of a processor
or the flow of data and real-time systems can be used as a control device in a dedicated application.
A real-time operating system must have well-defined, fixed time constraints, otherwise the system
will fail. For example, Scientific experiments, medical imaging systems, industrial control
systems, weapon systems, robots, air traffic control systems, etc.
There are two types of real-time operating systems.

Hard real-time systems

Hard real-time systems guarantee that critical tasks complete on time. In hard real-time systems,
secondary storage is limited or missing and the data is stored in ROM. In these systems, virtual
memory is almost never found.

Soft real-time systems

Soft real-time systems are less restrictive. A critical real-time task gets priority over other tasks
and retains the priority until it completes. Soft real-time systems have limited utility than hard real-
time systems. For example, multimedia, virtual reality

Operating System structures:


Operating system can be implemented with the help of various structures. The structure of the OS
depends mainly on how the various common components of the operating system are
interconnected and melded into the kernel. Depending on this we have following structures of the
operating system:
Simple structure:
Such operating systems do not have well defined structure and are small, simple and limited
systems. The interfaces and levels of functionality are not well separated. MS-DOS is an example
of such operating system. In MS-DOS application programs are able to access the basic I/O
routines. These types of operating system cause the entire system to crash if one of the user
programs fails.
Diagram of the structure of MS-DOS is shown below.

Layered structure:

An OS can be broken into pieces and retain much more control on system. In this structure the OS
is broken into number of layers (levels).
The bottom layer (layer 0) is the hardware and the topmost layer (layer N) is the user
interface.
These layers are so designed that each layer uses the functions of the lower level layers
only. This simplifies the debugging process as if lower level layers are debugged and an
error occurs during debugging then the error must be on that layer only as the lower level
layers have already been debugged.
The main disadvantage of this structure is that careful planning of the layers is necessary as a layer
can use only lower level layers. UNIX is an example of this structure.
Unix
consists of
2
separable
parts.The
Kernel and
the system
programs.
The kernel
is further
seperated
into series
of
interfaces
and device
drivers.Th
e kernel
provides
OS
functions
through
system
calls.

Another example of layered approach is “The OS”. It was defined in six layers

Layer 5: User programs


Layer 4: Buffering
Layer3: Device drivers
Layer2: Memory management
Layer1: Cpu scheduling
Layer0: Hardware
Micro-kernel:
This structure designs the operating system by removing all non-essential components (like device
drivers,file systems etc)from the kernel and implementing them as system and user programs.

This result in a smaller kernel called the micro-kernel.Advantages of this structure are that all new
services need to be added to user space and does not require the kernel to be modified.

Thus it is more secure and reliable as if a service fails then rest of the operating system remains
untouched. Mac OS is an example of this type of OS.

OS Components:An operating system is a large and complex system that can only be
created by partitioning into small pieces. These pieces should be a well-defined portion of
the system, which carefully defined inputs, outputs, and functions.

What are OS Components ?


•File Management

•Process Management
•I/O Device Management
•Main Memory management
•Secondary-Storage management
•Security Management

1. File Management:A file is a


set of related information which is
should define by its creator. It
commonly represents programs, both
source and object forms, and data. Data files can be numeric, alphabetic, or alphanumeric.

Function of file management in OS


The operating system has the following important given activities in connections with file
management:
•File and directory creation and deletion.
•For manipulating files and directories.
•Mapping files onto secondary storage.
•Backup files on stable storage media.

2. Process ManagementThe process management component is a procedure for managing


the many processes that are running simultaneously on the operating system.Every software
application program has one or more processes associated with them when they are running.

Functions of process management in OS

•Process creation and deletion.


•Suspension and resumption.
•Synchronization process
•Communication process
3. I/O Device Management: One of the important use of an operating system that helps
you to hide the variations of specific hardware devices from the user.

Functions of I/O management in OS

•It offers buffer caching system


•It provides general device driver code
•It provides drivers for particular hardware devices

4. Main Memory management: Main Memory is a large array of storage or bytes,


which has an address. The memory management process is conducted by using a sequence
of reads or writes of specific memory addresses. In order to execute a program , it should be
mapped to absolute addresses and loaded inside the Memory.

Functions of Memory management in OS

An Operating System performs the following functions for Memory Management:


•It helps you to keep track of primary memory.
•Determine what part of it are in use by whom, what part is not in use.
•In a multiprogramming system, the OS takes a decision about which process will get
Memory and how much.
•Allocates the memory when a process requests
•It also de-allocates the Memory when a process no longer requires or has been terminated.

5. Secondary-Storage Management:
The most important task of a computer system is to execute programs. These programs,
along with the data, helps you to access, which is in the main memory during execution.This
Memory of the computer is very small to store all data and programs permanently. The
computer system offers secondary storage to back up the main Memory. Today modern
computers use hard drives/SSD as the primary storage of both programs and data. However,
the secondary storage management also works with storage devices, like a USB flash drive,
and CD/DVD drives.
Functions of Secondary storage management in OS
•Storage allocation
•Free space management
•Disk scheduling

6. Security Management
The various processes in an operating system need to be secured from each other's activities.
For that purpose, various mechanisms can be used to ensure that those processes which want
to operate files, memory CPU, and other hardware resources should have proper
authorization from the operating system.

Process Control Block (PCB)

A Process Control Block is a data structure maintained by the Operating System for every process.
The PCB is identified by an integer process ID (PID). A PCB keeps all the information needed to
keep track of a process as listed below in the table

Process State

The current state of the process i.e., whether it is ready, running, waiting, or whatever.

Process privileges
This is required to allow/disallow access to system resources.

Process ID

Unique identification for each of the process in the operating system.

Pointer

A pointer to parent process.

Program Counter

Program Counter is a pointer to the address of the next instruction to be executed for this process.

CPU registers

Various CPU registers where process need to be stored for execution for running state.

CPU Scheduling Information

Process priority and other scheduling information which is required to schedule the process.

Memory management information

This includes the information of page table, memory limits, Segment table depending on memory
used by the operating system.

Accounting information

This includes the amount of CPU used for process execution, time limits, execution ID etc.

IO status information

This includes a list of I/O devices allocated to the process.


The architecture of a PCB is completely dependent on Operating System and may contain different
information in different operating systems. Here is a simplified diagram of a PCB

Context Switching:
A context switching is a process that involves switching of the CPU from one process or task to
another. In this phenomenon, the execution of the process that is present in the running state is
suspended by the kernel and another process that is present in the ready state is executed by the
CPU.

It is one of the essential features of the multitasking operating system. The processes are switched
so fastly that it gives an illusion to the user that all the processes are being executed at the same
time.

But the context switching process involved a number of steps that need to be followed. You can't
directly switch a process from the running state to the ready state. You have to save the context of
that process. If you are not saving the context of any process P then after some time, when the
process P comes in the CPU for execution again, then the process will start executing from starting.
But in reality, it should continue from that point where it left the CPU in its previous execution. So,
the context of the process should be saved before putting any other process in the running state.

A context is the contents of a CPU's registers and program counter at any point in time. Context
switching can happen due to the following reasons:

•When a process of high priority comes in the ready state. In this case, the execution of the
running process should be stopped and the higher priority process should be given the CPU
for execution.

•When an interruption occurs then the process in the running state should be stopped and the
CPU should handle the interrupt before doing something else.

•When a transition between the user mode and kernel mode is required then you have to
perform the context switching.

Steps involved in Context Switching


The process of context switching involves a number of steps. The following diagram depicts the
process of context switching between the two processes P1 and P2.
In the above figure, the process P1 is in the running state and the process P2 is in the ready state.
Now, when some interruption occurs then you have to switch the process P1 from running to the
ready state after saving the context and the process P2 from ready to running state. The following
steps will be performed:

1.Firstly, the context of the process P1 i.e. the process present in the running state will be
saved in the Process Control Block of process P1 i.e. PCB1.

2.Now, you have to move the PCB1 to the relevant queue i.e. ready queue, I/O queue,
waiting queue, etc.

3.From the ready state, select the new process that is to be executed i.e. the process P2.

4.Now, update the Process Control Block of process P2 i.e. PCB2 by setting the process
state to running. If the process P2 was earlier executed by the CPU, then you can get the
position of last executed instruction so that you can resume the execution of P2.

5.Similarly, if you want to execute the process P1 again, then you have to follow the same
steps as mentioned above(from step 1 to 4).

The time involved in the context switching of one process by other is called the Context
Switching Time.

Advantage of Context Switching


Context switching is used to achieve multitasking.Multitasking gives an illusion to the users that
more than one process are being executed at the same time. But in reality, only one task is being
executed at a particular instant of time by a processor. Here, the context switching is so fast that the
user feels that the CPU is executing more than one task at the same time.
The disadvantage of Context Switching
The disadvantage of context switching is that it requires some time for context switching i.e. the
context switching time. Time is required to save the context of one process that is in the running
state and then getting the context of another process that is about to come in the running state.
During that time, there is no useful work done by the CPU from the user perspective. So, context
switching is pure overhead in this condition.

Process Scheduling:
Process scheduling allows OS to allocate a time interval of CPU execution for each process.
Another important reason for using a process scheduling system is that it keeps the CPU busy all
the time. This allows you to get the minimum response time for programs.

Process Scheduling Queues:

Process Scheduling Queues help you to maintain a distinct queue for each and every process states
and PCBs. All the process of the same execution state are placed in the same queue. Therefore,
whenever the state of a process is modified, its PCB needs to be unlinked from its existing queue,
which moves back to the new state queue.
Three types of operating system queues are:

1.Job queue– It helps you to store all the processes in the system.
2.Ready queue– This type of queue helps you to set every process residing in the main
memory, which is ready and waiting to execute.
3.Device queues – It is a process that is blocked because of the absence of an I/O device.

In the above-given Diagram,


•Rectangle represents a queue.
•Circle denotes the resource
•Arrow indicates the flow of the process.
1.Every new process first put in the Ready queue .It waits in the ready queue until it is
finally processed for execution. Here, the new process is put in the ready queue and wait
until it is selected for execution or it is dispatched.
2.One of the processes is allocated the CPU and it is executing.
3.The process should issue an I/O request
4.Then, it should be placed in the I/O queue.
5.The process should create a new subprocess
6.The process should be waiting for its termination.
7.It should remove forcefully from the CPU, as a result interrupt. Once interrupt is
completed, it should be sent back to ready queue.
Two State Process Model
Two-state process models are:
•Running
•Not Running
Running
In the Operating system, whenever a new process is built, it is entered into the system, which should
be running.
Not Running
The process that are not running are kept in a queue, which is waiting for their turn to execute. Each
entry in the queue is a point to a specific process.
Scheduling Objectives
Here, are important objectives of Process scheduling
•Maximize the number of interactive users within acceptable response times.
•Achieve a balance between response and utilization.
•Avoid indefinite postponement and enforce priorities.
•It also should give reference to the processes holding the key resources.
Type of Process Schedulers
A scheduler is a type of system software that allows you to handle process scheduling.
There are mainly three types of Process Schedulers:
1.Long Term
2.Short Term
3.Medium Term
Long Term Scheduler
Long term scheduler is also known as a job scheduler. This scheduler regulates the program and
select process from the queue and loads them into memory for execution. It also regulates the
degree of multi-programing.
However, the main goal of this type of scheduler is to offer a balanced mix of jobs, like Processor,
I/O jobs., that allows managing multiprogramming.
Medium Term Scheduler
Medium-term scheduling is an important part of swapping. It enables you to handle the swapped
out-processes. In this scheduler, a running process can become suspended, which makes an I/O
request.
A running process can become suspended if it makes an I/O request. A suspended processes can't
make any progress towards completion. In order to remove the process from memory and make
space for other processes, the suspended process should be moved to secondary storage.
Short Term Scheduler
Short term scheduling is also known as CPU scheduler. The main goal of this scheduler is to boost
the system performance according to set criteria. This helps you to select from a group of processes
that are ready to execute and allocates CPU to one of them. The dispatcher gives control of the CPU
to the process selected by the short term scheduler.

Type of Process Schedulers:


A scheduler is a type of system software that allows you to handle process scheduling.
There are mainly three types of Process Schedulers:
1.Long Term
2.Short Term
3.Medium Term
Long Term Scheduler
Long term scheduler is also known as a job scheduler. This scheduler regulates the program and
select process from the queue and loads them into memory for execution. It also regulates the
degree of multi-programing.

Processes can be described as either:


I/O-bound process – spends more time doing I/O than computations, many short CPU bursts
CPU-bound process – spends more time doing computations; few very long CPU bursts

However, the main goal of this type of scheduler is to offer a balanced mix of jobs, like Processor,
I/O jobs., that allows managing multiprogramming.

Medium Term Scheduler

Medium-term scheduling is an important part of swapping. It enables you to handle the swapped
out-processes. In this scheduler, a running process can become suspended, which makes an I/O
request.
A running process can become suspended if it makes an I/O request. A suspended processes can't
make any progress towards completion. In order to remove the process from memory and make
space for other processes, the suspended process should be moved to secondary storage.
Short Term Scheduler:

Short term scheduling is also known as CPU scheduler. The main goal of this scheduler is to boost
the system performance according to set criteria. This helps you to select from a group of processes
that are ready to execute and allocates CPU to one of them. The dispatcher gives control of the CPU
to the process selected by the short term scheduler.

Process Times in Scheduling:

1. Arrival Time
The time at which the process enters into the ready queue is called the arrival time.
2. Burst Time
The total amount of time required by the CPU to execute the whole process is called the Burst
Time. This does not include the waiting time. It is confusing to calculate the execution time for a
process even before executing it hence the scheduling problems based on the burst time cannot be
implemented in reality.
3. Completion Time
The Time at which the process enters into the completion state or the time at which the process
completes its execution, is called completion time.
4. Turnaround time
The total amount of time spent by the process from its arrival to its completion, is called
Turnaround time.
Turn Around Time = Completion Time – Arrival Time (or)
Turn Around time = Burst time + Waiting time.
5. Waiting Time
The Total amount of time for which the process waits for the CPU to be assigned is called waiting
time.
Waiting Time = Turn Around Time – Burst Time.
6. Response Time
The difference between the arrival time and the time at which the process first gets the CPU is
called Response Time.
Response Time = Time at which process first gets the CPU – Arrival time.

Priority Scheduling:
Priority Scheduling is a method of scheduling processes that is based on priority. In this algorithm,
the scheduler selects the tasks to work as per the priority.
The processes with higher priority should be carried out first, whereas jobs with equal priorities are
carried out on a round-robin or FCFS basis. Priority depends upon memory requirements, time
requirements, etc.

Types of Priority Scheduling:

Preemptive Scheduling:
Priority scheduling divided into two main types:
In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is
important to run a task with a higher priority before another lower priority task, even if the lower
priority task is still running. The lower priority task holds for some time and resumes when the
higher priority task finishes its execution.

Non-Preemptive Scheduling:

In this type of scheduling method, the CPU has been allocated to a specific process. The process
that keeps the CPU busy, will release the CPU either by switching context or terminating. It is the
only method that can be used for various hardware platforms. That's because it doesn't need special
hardware (for example, a timer) like preemptive scheduling.
Characteristics of Priority Scheduling
•A CPU algorithm that schedules processes based on priority.
•It used in Operating systems for performing batch processes.
•If two jobs having the same priority are READY, it works on a FIRST COME, FIRST
SERVED basis.
•In priority scheduling, a number is assigned to each process that indicates its priority level.
•Lower the number, higher is the priority.
•In this type of scheduling algorithm, if a newer process arrives, that is having a higher
priority than the currently running process, then the currently running process is preempted.

Example of Non-Preemptive Priority Scheduling


•Consider following five processes P1 to P5. Each process has its unique priority, burst
time, and arrival time.

Process Priority Burst time Arrival time

P1 1 4 0
P2 2 3 0
P3 1 7 6
P4 3 4 11
P5 2 2 12

Gantt Chart:

P1 P2 P3 P5 P4
0 4 7 14 16 20

Preemptive Priority Scheduling:


In Preemptive Priority Scheduling, at the time of arrival of a process in the ready queue, its Priority
is compared with the priority of the other processes present in the ready queue as well as with the
one which is being executed by the CPU at that point of time. The One with the highest priority
among all the available processes will be given the CPU next.
The difference between preemptive priority scheduling and non preemptive priority scheduling is
that, in the preemptive priority scheduling, the job which is being executed can be stopped at the
arrival of a higher priority job.
Example
There are 7 processes P1, P2, P3, P4, P5, P6 and P7 given. Their respective priorities, Arrival
Times and Burst times are given in the table below.
Burst
Process Id Priority Arrival Time
Time

1 2(L) 0 1

2 6 1 7

3 3 2 3

4 5 3 6

5 4 4 5

6 10(H) 5 15

7 9 15 8

Gantt Chart:
CPU Scheduling:
CPU Scheduling is a process of determining which process will own CPU for execution while
another process is on hold.
The selection process will be carried out by the CPU scheduler. It selects one of the processes in
ready Queue that are ready for execution.

Types of CPU Scheduling

Preemptive Scheduling
In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is
important to run a task with a higher priority before another lower priority task, even if the lower
priority task is still running. The lower priority task holds for some time and resumes when the
higher priority task finishes its execution.

Non-Preemptive Scheduling
In this type of scheduling method, the CPU has been allocated to a specific process. The process
that keeps the CPU busy will release the CPU either by switching context or terminating. It is the
only method that can be used for various hardware platforms. That's because it doesn't need special
hardware (for example, a timer) like preemptive scheduling.
To determine if scheduling is preemptive or non-preemptive, consider these four parameters:
1.A process switches from the running to the waiting state.
2.Specific process switches from the running state to the ready state.
3.Specific process switches from the waiting state to the ready state.
4.Process finished its execution and terminated.
Only conditions 1 and 4 apply, the scheduling is called non- preemptive.
All other scheduling are preemptive.
There are mainly seven types of process scheduling algorithms
1.First Come First Serve (FCFS)
2.Shortest-Job-First (SJF) Scheduling
3.Shortest Remaining Time
4.Priority Scheduling
5.Round Robin Scheduling
6.Preemptive priority
7.Multilevel Queue Scheduling

1. First Come First Serve(FCFS) Scheduling:


First come first serve(FCFS) scheduling algorithm simply schedules the jobs according to their
arrival time. The job which comes first in the ready queue will get the CPU first. The lesser the
arrival time of the job, the sooner will the job get the CPU. FCFS scheduling may cause the
problem of starvation if the burst time of the first process is the longest among all the jobs.

Advantages of FCFS

•Simple
•Easy
•First come, First serve

Disadvantages of FCFS

1.The scheduling method is non preemptive, the process will run to the completion.
2.Due to the non-preemptive nature of the algorithm, the problem of starvation may occur.
3.Although it is easy to implement, but it is poor in performance since the average waiting
time is higher as compare to other scheduling algorithms.
4.Convoy effect:one slow process slows down the performance of the entire set of processes,
and leads to wastage of CPU time and other devices.

Examp
le
Let's
take an
exampl
e of The FCFS scheduling algorithm. In the Following schedule, there are 5 processes with process
ID P0, P1, P2, P3 and P4. P0 arrives at time 0, P1 at time 1, P2 at time 2, P3 arrives at time 3 and
Process P4 arrives at time 4 in the ready queue. The processes and their respective Arrival and Burst
time are given in the following table.
The Turnaround time and the waiting time are calculated by using the following formula
1. Turn Around Time = Completion Time – Arrival Time.
2. Waiting Time =Turnaround time -Burst Time
The average waiting Time is determined by summing the respective waiting time of all the
processes and divided the sum by the total number of processes.
Waiting
Process ID Arrival Time Burst Time Completion Time Turn Around Time
Time

0 0 2 2 2 0

1 1 6 8 7 1

2 2 4 12 8 4

3 3 9 21 18 9

4 4 12 33 29 17

Avg Waiting Time=31/5


Gantt Chart:

P0 P1 P2 P3 P4

0 2 8 12 21 33

Shortest Job First (SJF) Scheduling


SJF scheduling algorithm, schedules the processes according to their burst time.The process with
the lowest burst time, among the list of available processes in the ready queue, is going to be
scheduled next.
However, it is very difficult to predict the burst time needed for a process hence this algorithm is
very difficult to implement in the system.

Advantages of SJF

1.Maximum throughput
2.Minimum average waiting and turnaround time

Disadvantages of SJF

1.May suffer with the problem of starvation


2.It is not implementable because the exact Burst time for a process can't be known in
advance.
There are different techniques available by which, the CPU burst time of the process can be
determined. We will discuss them later in detail.
Example
In the following example, there are five jobs named as P1, P2, P3, P4 and P5. Their arrival time and
burst time are given in the table below.
Waiting
PID Arrival Time Burst Time Completion Time Turn Around Time
Time

1 1 7 8 7 0

2 3 3 13 10 7

3 6 2 10 4 2

4 7 10 31 24 14

5 9 8 21 12 4

Since, No Process arrives at time 0 hence; there will be an empty slot in the Gantt chart from time 0
to 1 (the time at which the first process arrives).
According to the algorithm, the OS schedules the process which is having the lowest burst time
among the available processes in the ready queue.
Till now, we have only one process in the ready queue hence the scheduler will schedule this to the
processor no matter what is its burst time.
This will be executed till 8 units of time. Till then we have three more processes arrived in the ready
queue hence the scheduler will choose the process with the lowest burst time.
Among the processes given in the table, P3 will be executed next since it is having the lowest burst
time among all the available processes.
So that's how the procedure will go on in shortest job first (SJF) scheduling algorithm.

Avg Waiting Time = 27/5


Techniques to Predict Burst Time-

There are several techniques which try to predict the burst time for the processes so that the
algorithm can be implemented.

These techniques are-

Static Techniques-
There are two static techniques-

1.Based on process size


2.Based on process type

1. Based on Process Size-

•This technique predicts the burst time for a process based on its size.
•Burst time of the already executed process of similar size is taken as the burst time for the
process to be executed.

Example-

•Consider a process of size 200 KB took 20 units of time to complete its execution.
•Then, burst time for any future process having size around 200 KB can be taken as 20 units.
NOTE

• The predicted burst time may not always be right.


• This is because the burst time of a process also depends on what kind of a process it
is.

2. Based on Process Type-

•This technique predicts the burst time for a process based on its type.
•The following figure shows the burst time assumed for several kinds of processes.

Dynamic Techniques-
There are two dynamic techniques-

1.Based on simple averaging


2.Based on exponential averaging
1. Based on Simple Averaging-

•Burst time for the process to be executed is taken as the average of all the processes that are
executed till now.
•Given n processes P1, P2, … , Pn and burst time of each process Pi as ti, then predicted burst
time for process Pn+1 is given as-

2. Based on Exponential Averaging-

•Given n processes P1, P2, … , Pn and burst time of each process Pi as ti. Then, predicted burst
time for process Pn+1 is given as-

where-

•α is called smoothening factor (0<= α <=1)


•Smoothening factor (α) –It controls the relative weight of recent and past history in our
prediction.
•If α = 0, Τn+1 = Τn i.e. no change in value of initial predicted burst time.
•If α = 1, Τn+1 = tn i.e. predicted Burst-Time of new process will always change according to
actual Burst-time of nth process.
•If α = 1/2, recent and past history are equally weighted.

•tn = actual burst time of process Pn


•Tn = Predicted burst time for process Pn
Problem
Calculate the predicted burst time using exponential averaging for the fifth process if the predicted
burst time for the first process is 10 units and actual burst time of the first four processes is 4, 8, 6
and 7 units respectively. Given α = 0.5.

Solution-
Given-

•Predicted burst time for 1st process = 10 units


•Actual burst time of the first four processes = 4, 8, 6, 7
•α = 0.5
Predicted Burst Time for 2nd Process-
Predicted burst time for 2nd process

=α x Actual burst time of 1st process + (1-α) x Predicted burst time for 1st process

= 0.5 x 4 + 0.5 x 10

=2+5

= 7 units

Predicted Burst Time for 3rd Process-


Predicted burst time for 3rd process

=α x Actual burst time of 2nd process + (1-α) x Predicted burst time for 2nd process

= 0.5 x 8 + 0.5 x 7

= 4 + 3.5

= 7.5 units

Predicted Burst Time for 4th Process-


Predicted burst time for 4th process

=α x Actual burst time of 3rd process + (1-α) x Predicted burst time for 3rd process

= 0.5 x 6 + 0.5 x 7.5

= 3 + 3.75
= 6.75 units

Predicted Burst Time for 5th Process


Predicted burst time for 5th process

=α x Actual burst time of 4th process + (1-α) x Predicted burst time for 4th process

= 0.5 x 7 + 0.5 x 6.75

= 3.5 + 3.375

= 6.875 units

Round Robin Scheduling:

•It is similar to FCFS scheduling, but preemption is added to switch between processes. A
small unit of time, called a time quantum or time slice, is defined. A time quantum is
generally from 10 to 100 milliseconds.

•The ready queue is treated as a circular queue. The CPU scheduler goes around the
ready queue, allocating the CPU to each process for a time interval of up to 1 time quantum.

•To implement RR scheduling, we keep the ready queue as a FIFO queue of processes. New
processes are added to the tail of the ready queue.

•The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt
after 1 time quantum, and dispatches the process.

•One of two things will then happen. The process may have a CPU burst of less than 1 time
quantum. In this case, the process itself will release the CPU voluntarily. The scheduler will
then proceed to the next process in the ready queue. Otherwise, if the CPU burst of the
currently running process is longer than 1 time quantum, the timer will go off and will cause
an interrupt to the operating system.

•A context switch will be executed, and the process will be put at the tail of the ready queue.
The CPU scheduler will then select the next process in the ready queue.

Example:Consider the set of 5 processes whose arrival time and burst time are

given below-
Process Arrival Burst
Id time time

P1 0 5

P2 1 3

P3 2 1

P4 3 2

P5 4 3

If the CPU scheduling policy is Round Robin with time quantum = 2 unit,

calculate the average waiting time and average turn around time.

Ready Queue:

P5, P1, P2, P5, P4, P1, P3, P2, P1

P1 P2 P3 P1 P4 P5 P2 P1 P5

0 2 4 5 7 9 11 12 13 14

Shortest Remaining Time first( SRTF) Scheduling:

This is also called as Preemptive SJF scheduling algorithm.the preemption of running process is
based on the arrival of a new shorter process.

Example:
Consider five jobs P1, P2, P3, P4, P5 and P6. Their arrival time and burst time are given below in
the table.
Arrival Burst Completion Turn Around Waiting Response
Process ID
Time Time Time Time Time Time

1 0 8 20 20 12 0

2 1 4 10 9 5 1

3 2 2 4 2 0 2

4 3 1 5 2 1 4

5 4 3 13 9 6 10

6 5 2 7 2 0 5

Gantt Chart:

P1 P2 P3 P3 P4 P6 P2 P5 P1

0 1 2 3 4 5 7 10 13 20

Multilevel Queue (MLQ) CPU Scheduling:

It may happen that processes in the ready queue can be divided into different classes where each
class has its own scheduling needs.

For example, a common division is a foreground (interactive) process and background


(batch)processes.

These two classes have different scheduling needs. For this kind of situation Multilevel Queue
Scheduling is used.
Ready Queue is divided into separate queues for each class of processes. For example, let us take
three different types of process System processes, Interactive processes and Batch Processes. All
three process have there own queue.
All three different type of processes have there own queue. Each queue have its own Scheduling
algorithm. For example, queue 1 and queue 2 uses Round Robin while queue 3 can use FCFS to
schedule there processes.
Scheduling among the queues :There are two ways to determine this Scheduling among the queues
is necessary.

1.Fixed priority preemptive scheduling method – Each queue has absolute priority over lower
priority queue. Let us consider following priority order queue 1 > queue 2 > queue
3.According to this algorithm no process in the batch queue(queue 3) can run unless queue 1
and 2 are empty. If any batch process (queue 3) is running and any system (queue 1) or
Interactive process(queue 2) entered the ready queue the batch process is preempted.
2.Time slicing– In this method each queue gets certain portion of CPU time and can use it to
schedule its own processes.For instance, queue 1 takes 50 percent of CPU time queue 2 takes
30 percent and queue 3 gets 20 percent of CPU time.
Example :
Consider below table of four processes under Multilevel queue scheduling.Queue number denotes
the queue of the process.
Priority of queue 1 is greater than queue 2. queue 1 uses Round Robin (Time Quantum = 2) and
queue 2 uses FCFS.
Gantt chart:

P1 P2 P1 P2 P3 P4 P3

0 2 4 6 7 10 15 20

Multi Threading

Thread: A thread is a basic unit of CPU utilization, consisting of a program counter, a stack,
and a set of registers, ( and a thread ID. )

Traditional ( heavyweight ) processes have a single thread of control - There is one


program counter, and one sequence of instructions that can be carried out at any given time.

multi-threaded applications have multiple threads within a single process, each having
their own program counter, stack and set of registers, but sharing common code, data, and
certain structures such as open files.
Figure 4.1 - Single-threaded and multithreaded processes

Threads are very useful in modern programming whenever a process has multiple tasks to
perform independently of the others.

This is particularly true when one of the tasks may block, and it is desired to allow the other
tasks to proceed without blocking.

For example in a word processor, a background thread may check spelling and grammar
while a foreground thread processes user input ( keystrokes ), while yet a third thread loads
images from the hard drive, and a fourth does periodic automatic backups of the file being
edited.

Another example is a web server - Multiple threads allow for multiple requests to be satisfied
simultaneously, without having to service requests sequentially or to fork off separate
processes for every incoming request. ( The latter is how this sort of thing was done before
the concept of threads was developed. A daemon would listen at a port, fork off a child for
every incoming request to be processed, and then go back to listening to the port. )
Figure 4.2 - Multithreaded server architecture
4.1.2 Benefits

There are four major categories of benefits to multi-threading:

Responsiveness - One thread may provide rapid response while other threads are
blocked or slowed down doing intensive calculations.

Resource sharing - By default threads share common code, data, and other resources,
which allows multiple tasks to be performed simultaneously in a single address
space.

Economy - Creating and managing threads ( and context switches between them ) is
much faster than performing the same tasks for processes.

Scalability, i.e. Utilization of multiprocessor architectures - A single threaded


process can only run on one CPU, no matter how many may be available, whereas
the execution of a multi-threaded application may be split amongst available
processors. ( Note that single threaded processes can still benefit from multi-
processor architectures when there are multiple processes contending for the CPU,
i.e. when the load average is above some certain threshold. )
4.2 Multicore Programming

A recent trend in computer architecture is to produce chips with multiple cores, or CPUs
on a single chip.

A multi-threaded application running on a traditional single-core chip would have to


interleave the threads, as shown in Figure 4.3. On a multi-core chip, however, the threads
could be spread across the available cores, allowing true parallel processing, as shown in
Figure 4.4.
Figure 4.3 - Concurrent execution on a single-core system.

Figure 4.4 - Parallel execution on a multicore system

For operating systems, multi-core chips require new scheduling algorithms to make better
use of the multiple cores available.

4.3 Multithreading Models


7. There are two types of threads to be managed in a modern system: User threads and
kernel threads.
8. User threads are supported above the kernel, without kernel support. These are the
threads that application programmers would put into their programs.
9. Kernel threads are supported within the kernel of the OS itself. All modern OSes
support kernel level threads, allowing the kernel to perform multiple simultaneous tasks
and/or to service multiple kernel system calls simultaneously.
10. In a specific implementation, the user threads must be mapped to kernel threads, using
one of the following strategies.
4.3.1 Many-To-One Model

In the many-to-one model, many user-level threads are all mapped onto a single kernel
thread.

Thread management is handled by the thread library in user space, which is very efficient.

However, if a blocking system call is made, then the entire process blocks, even if the other
user threads would otherwise be able to continue.

Because a single kernel thread can operate only on a single CPU, the many-to-one model
does not allow individual processes to be split across multiple CPUs.
Green threads for Solaris and GNU Portable Threads implement the many-to-one model in
the past, but few systems continue to do so today.

Figure 4.5 - Many-to-one model


4.3.2 One-To-One Model

The one-to-one model creates a separate kernel thread to handle each user thread.

One-to-one model overcomes the problems listed above involving blocking system calls and
the splitting of processes across multiple CPUs.

However the overhead of managing the one-to-one model is more significant, involving
more overhead and slowing down the system.

Most implementations of this model place a limit on how many threads can be created.

Linux and Windows from 95 to XP implement the one-to-one model for threads.

Figure 4.6 - One-to-one model


4.3.3 Many-To-Many Model
The many-to-many model multiplexes any number of user threads onto an equal or smaller
number of kernel threads, combining the best features of the one-to-one and many-to-one
models.

Users have no restrictions on the number of threads created.

Blocking kernel system calls do not block the entire process.

Processes can be split across multiple processors.

Individual processes may be allocated variable numbers of kernel threads, depending on the
number of CPUs present and other factors.

Figure 4.7 - Many-to-many model

One popular variation of the many-to-many model is the two-tier model, which allows either
many-to-many or one-to-one operation.

IRIX, HP-UX, and Tru64 UNIX use the two-tier model, as did Solaris prior to Solaris 9.
Figure 4.8 - Two-level model
4.4 Thread Libraries

Thread libraries provide programmers with an API for creating and managing threads.

Thread libraries may be implemented either in user space or in kernel space. The former
involves API functions implemented solely within user space, with no kernel support.

There are three main thread libraries in use today:


1.POSIX Pthreads - may be provided as either a user or kernel library, as an
extension to the POSIX standard.
2.Win32 threads - provided as a kernel-level library on Windows systems.
3.Java threads - Since Java generally runs on a Java Virtual Machine, the
implementation of threads is based upon whatever OS and hardware the JVM is
running on, i.e. either Pthreads or Win32 threads depending on the system

Thread Scheduling
Scheduling of threads involves two boundary scheduling,

•Scheduling of user level threads (ULT) to kernel level threads (KLT) via leightweight
process (LWP) by the application developer.
•Scheduling of kernel level threads by the system scheduler to perform different unique os
functions.
Leightweight Process (LWP) :
Thread Scheduling: Scheduling of threads involves two boundary scheduling,

•Scheduling of user level threads (ULT) to kernel level threads (KLT) via leightweight
process (LWP) by the application developer.
•Scheduling of kernel level threads by the system scheduler to perform different unique os
functions.
• Light-weight process are threads in the user space that acts as an interface for the ULT to
access the physical CPU resources. Thread library schedules which thread of a process to run on
which LWP and how long. The number of LWP created by the thread library depends on the type of
application. Each LWP is attached to a separate kernel-level thread.

In real-time, the first boundary of thread scheduling is beyond specifying the scheduling policy and
the priority. It requires two controls to be specified for the User level threads: Contention scope, and
Allocation domain. These are explained as following below.

1. Contention Scope :
The word contention here refers to the competition or fight among the User level threads to access
the kernel resources. Thus, this control defines the extent to which contention takes place. It is
defined by the application developer using the thread library. Depending upon the extent of
contention it is classified as Process Contention Scope and System Contention Scope.
1.Process Contention Scope (PCS) –
The contention takes place among threads within a same process. The thread library schedules
the high-prioritized PCS thread to access the resources via available LWPs (priority as
specified by the application developer during thread creation).
2.System Contention Scope (SCS) –
The contention takes place among all threads in the system. In this case, every SCS thread is
associated to each LWP by the thread library and are scheduled by the system scheduler to
access the kernel resources.
In LINUX and UNIX operating systems, the POSIX Pthread library provides a function
Pthread_attr_setscope to define the type of contention scope for a thread during its creation.
It takes two values.

PTHREAD_SCOPE_SYSTEM

PTHREAD_SCOPE_PROCESS

Advantages of PCS over SCS :

•If all threads are PCS, then context switching, synchronization, scheduling everything takes
place within the userspace. This reduces system calls and achieves better performance.
•PCS is cheaper than SCS.
•PCS threads share one or more available LWPs. For every SCS thread, a separate LWP is
associated.For every system call, a separate KLT is created.

Multilevel Feedback Queue Scheduling


In a multilevel queue-scheduling algorithm, processes are permanently assigned to a queue on entry
to the system. Processes do not move between queues. This setup has the advantage of low
scheduling overhead, but the disadvantage of being inflexible.

Multilevel feedback queue scheduling, however, allows a process to move between queues. The
idea is to separate processes with different CPU-burst characteristics. If a process uses too much
CPU time, it will be moved to a lower-priority queue. Similarly, a process that waits too long in a
lower-priority queue may be moved to a higher-priority queue. This form of aging prevents
starvation.
An example of a multilevel feedback queue can be seen in the below figure.

In general, a multilevel feedback queue scheduler is defined by the following parameters:

•The number of queues.

•The scheduling algorithm for each queue.

•The method used to determine when to upgrade a process to a higher-priority queue.

•The method used to determine when to demote a process to a lower-priority queue.

•The method used to determine which queue a process will enter when that process needs
service. Although a multilevel feedback queue is the most general scheme, it is also the
most complex.

Inter Process Communication (IPC)


A process can be of two types:

• Independent process.

• Co-operating process.
An independent process is not affected by the execution of other processes while a co-operating
process can be affected by other executing processes. Though one can think that those processes,
which are running independently, will execute very efficiently, in reality, there are many situations
when co-operative nature can be utilised for increasing computational speed, convenience and
modularity. Inter process communication (IPC) is a mechanism which allows processes to
communicate with each other and synchronize their actions. The communication between these
processes can be seen as a method of co-operation between them. Processes can communicate with
each other through both:
1. Shared Memory
2. Message passing
The Figure 1 below shows a basic structure of communication between processes via the shared
memory method and via the message passing method.

An operating system can implement both method of communication. First, we will discuss the
shared memory methods of communication and then message passing. Communication between
processes using shared memory requires processes to share some variable and it completely
depends on how programmer will implement it. One way of communication using shared memory
can be imagined like this: Suppose process1 and process2 are executing simultaneously and they
share some resources or use some information from another process. Process1 generate information
about certain computations or resources being used and keeps it as a record in shared memory.
When process2 needs to use the shared information, it will check in the record stored in shared
memory and take note of the information generated by process1 and act accordingly. Processes can
use shared memory for extracting information as a record from another process as well as for
delivering any specific information to other processes.
Let’s discuss an example of communication between processes using shared memory method.
i) Shared Memory Method
Ex: Producer-Consumer problem
There are two processes: Producer and Consumer. Producer produces some item and Consumer
consumes that item. The two processes share a common space or memory location known as a
buffer where the item produced by Producer is stored and from which the Consumer consumes the
item, if needed. There are two versions of this problem: the first one is known as unbounded buffer
problem in which Producer can keep on producing items and there is no limit on the size of the
buffer, the second one is known as the bounded buffer problem in which Producer can produce up
to a certain number of items before it starts waiting for Consumer to consume it. We will discuss
the bounded buffer problem. First, the Producer and the Consumer will share some common
memory, then producer will start producing items. If the total produced item is equal to the size of
buffer, producer will wait to get it consumed by the Consumer. Similarly, the consumer will first
check for the availability of the item. If no item is available, Consumer will wait for Producer to
produce it. If there are items available, Consumer will consume it.
ii) Messaging Passing Method
Now, We will start our discussion of the communication between processes via message passing. In
this method, processes communicate with each other without using any kind of shared memory. If
two processes p1 and p2 want to communicate with each other, they proceed as follows:

•Establish a communication link (if a link already exists, no need to establish it again.)

•Start exchanging messages using basic primitives.


We need at least two primitives:
–send(message, destinaion) or send(message)
–receive(message, host) or receive(message)

The message size can be of fixed size or of variable size. If it is of fixed size, it is easy for an OS
designer but complicated for a programmer and if it is of variable size then it is easy for a
programmer but complicated for the OS designer. A standard message can have two parts: header
and body.
The header part is used for storing message type, destination id, source id, message length, and
control information. The control information contains information like what to do if runs out of
buffer space, sequence number, priority. Generally, message is sent using FIFO style.

Memory management

•Memory consists of a large array of words or bytes, each with its own address. The
CPU fetches instructions from memory according to the value of the program
counter. These instructions may cause additional loading from and storing to specific
memory addresses.

•A typical instruction-execution cycle, for example, first fetches an instruction from


memory. The instruction is then decoded and may cause operands to be fetched from
memory. After the instruction has been executed on the operands, results may be
stored back in memory.

•The CPU can only access its registers and main memory. It cannot, for example,
make direct access to the hard drive, so any data stored there must first be transferred
into the main memory chips before the CPU can work with it.

•Memory accesses to registers are very fast, generally one clock tick, and a CPU
may be able to execute more than one machine instruction per clock tick.

•Memory accesses to main memory are comparatively slow, and may take a number
of clock ticks to complete. This would require intolerable waiting by the CPU if it
were not for an intermediary fast memory cache built into most modern CPUs. The
basic idea of the cache is to transfer chunks of memory at a time from the main
memory to the cache, and then to access individual memory locations one at a time
from the cache.

•User processes must be restricted so that they only access memory locations that
"belong" to that particular process. This is usually implemented using a base register
and a limit register for each process.
•Every memory access made by a user process is checked against these two registers,
and if a memory access is attempted outside the valid range, then a fatal error is
generated. The OS obviously has access to all existing memory locations, as this is
necessary to swap users' code and data in and out of memory. It should also be
obvious that changing the contents of the base and limit registers is a privileged
activity, allowed only to the OS kernel.
Figure 8.1 - A base and a limit register define a logical addresss space

Figure 8.2 - Hardware address protection with base and limit registers

Note: Source code produces symbolic addresses


ex: Array1, x, y, count
A compiler will bind these symbolic addresses to Absolute addresses
Symbolic address was x, absolute (physical address) is 0x04
Relocatable addresses
Symbolic address was x, relocatable address is “16 bytes from the beginning of this file”

Address Binding:
• User programs typically refer to memory addresses with symbolic names such as "i",
"count", and "averageTemperature".

• These symbolic names must be mapped or bound to physical memory addresses,


which typically occurs in several stages:

• Compile Time- If it is known at compile time where a program will reside in


physical memory, then absolute code can be generated by the compiler,
containing actual physical addresses. However if the load address changes at
some later time, then the program will have to be recompiled.

• Load Time - If the location at which a program will be loaded is not known at
compile time, then the compiler must generate relocatable code, which
references addresses relative to the start of the program. If that starting
address changes, then the program must be reloaded but not recompiled.

• Execution Time- If a program can be moved around in memory during the


course of its execution, then binding must be delayed until execution time.
This requires special hardware, and is the method implemented by most
modern OSes.

Logical Versus Physical Address Space:

•The address generated by the CPU is a logical address, whereas the address actually
seen by the memory hardware is a physical address.

•In some case the logical address is also known as a virtual address, and the
two terms are used interchangeably by our text.
•The set of all logical addresses used by a program composes the logical
address space, and the set of all corresponding physical addresses composes
the physical address space.
•The run time mapping of logical to physical addresses is handled by the memory-
management unit, MMU.
•The MMU can take on many forms. One of the simplest is a modification of
the base-register scheme described earlier.
•The base register is now termed a relocation register, whose value is added
to every memory request at the hardware level.
•Note that user programs never see physical addresses. User programs work entirely
in logical address space, and any memory references or manipulations are done using
purely logical addresses. Only when the address gets sent to the physical memory
chips is the physical memory address generated.
Figure 8.4 - Dynamic relocation using a relocation register

Swapping
•A process must be loaded into memory in order to execute.
•If there is not enough memory available to keep all running processes in memory at
the same time, then some processes who are not currently using the CPU may have
their memory swapped out to a fast local disk called the backing store.
Standard Swapping
• If compile-time or load-time address binding is used, then processes must be
swapped back into the same memory location from which they were swapped out. If
execution time binding is used, then the processes can be swapped back into any
available location.

• Swapping is a very slow process compared to other operations.

• To reduce swapping transfer overhead, it is desired to transfer as little information as


possible, which requires that the system know how much memory a process is using,
as opposed to how much it might use. Programmers can help with this by freeing up
dynamic memory that they are no longer using.

• It is important to swap processes out of memory only when they are idle, or more to
the point, only when there are no pending I/O operations. (Otherwise the pending I/O
operation could write into the wrong process's memory space.)
• Most modern OSes no longer use swapping, because it is too slow and there are
faster alternatives available. (e.g. Paging.) However some UNIX systems will still
invoke swapping if the system gets extremely full, and then discontinue swapping
when the load reduces again.

Figure 8.5 - Swapping of two processes using a disk as a backing store

Memory allocation methods:In operating systems, Memory Management is the function


responsible for allocating and managing computer’s main memory. Memory Management function
keeps track of the status of each memory location, either allocated or free to ensure effective and
efficient use of Primary Memory.

There are two Memory Management Techniques:Contiguous, and Non-Contiguous.


Contiguo
us
memory
allocation
is a
classical
memory
allocation
model. In
this
system
assigns
consecutive memory blocks (that is, memory blocks having consecutive addresses) to a process.

Contiguous memory allocation is one of the oldest memory allocation methods. When a process
needs to execute, memory is requested by the process. The size of the process is compared with the
amount of contiguous main memory available to execute the process.

If sufficient contiguous memory is found, the memory is allocated and the process starts its
execution. Otherwise, the process is added to a queue of waiting processes until sufficient free
contiguous memory is available.
Fragmentation
As processes are loaded and removed from memory, the free memory space is broken into little
pieces. It happens after sometimes that processes cannot be allocated to memory blocks considering
their small size and memory blocks remains unused. This problem is known as Fragmentation.
Fragmentation is of two types −

External fragmentation

Total memory space is enough to satisfy a request or to reside a process in it, but it is not
contiguous, so it cannot be used.

Internal fragmentation

2 Memory block assigned to process is bigger. Some portion of memory is left unused, as it
cannot be used by another process.
The following diagram shows how fragmentation can cause waste of memory and a compaction
technique can be used to create more free memory out of fragmented memory −

External fragmentation can be reduced by compaction or shuffle memory contents to place all free
memory together in one large block. To make compaction feasible, relocation should be dynamic.
The internal fragmentation can be reduced by effectively assigning the smallest partition but large
enough for the process.

Fixed Partition Scheme:


In the fixed partition scheme, memory is divided into fixed number of partitions. Fixed means
number of partitions are fixed in the memory. In the fixed partition, in every partition only one
process will be accommodated. Degree of multi-programming is restricted by number of partitions
in the memory. Maximum size of the process is restricted by maximum size of the partition. Every
partition is associated with the two registers.
•Base Register: It has staring address
•Limit register: Total memory needed
Internal Fragmentation is found in fixed partition scheme.
To overcome the problem of internal fragmentation, instead of fixed partition scheme, variable
partition scheme is used.
Variable Partition Scheme:
In the variable partition scheme, initially memory will be single continuous free block. Whenever
the request by the process arrives, accordingly partition will be made in the memory. If the smaller
processes keep on coming then the larger partitions will be made into smaller partitions.

External Fragmentation is found in variable partition scheme.


To overcome the problem of external fragmentation, compaction technique is used or non-
contiguous memory management techniques are used.

Memory Allocation Algorithms

• For bothFixed and variable partitioning scheme, the operating system must keep list of each
memory location noting which are free and which are busy. Then as new jobs come into the
system, the free partitions must be allocated.

• There are many different strategies for finding the "best" allocation of memory to processes,
including the three most commonly discussed:

1. First-Fit Memory Allocation


2. Best-Fit Memory Allocation
3. Worst-Fit Memory Allocation
First-Fit Memory Allocation: In this method, first job claims the
first available memory with space more than or equal to it’s size.
The operating system doesn’t search for appropriate partition but
just allocate the job to the nearest memory partition available with
sufficient size.

Example: First fit for Fixed partitioning


Input : blockSize[] = {100, 500, 200, 300, 600};
processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 2
2 417 5
3 112 3
4 426 Not Allocated

Internal fragmentation: 288+183+88= 559

variable partitioning
Example :Input : blockSize[] = {100, 500, 200, 300, 600};
processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no
1 212 2
2 417 5
3 112 2
4 426 Not Allocated
Internal fragmentation: 359
Advantages of First-Fit Memory Allocation:
It is fast in processing. As the processor allocates the nearest available memory partition to the job,
it is very fast in execution.
Disadvantages of First-Fit Memory Allocation :
It wastes a lot of memory. The processor ignores if the size of partition allocated to the job is very
large as compared to the size of job or not. It just allocates the memory. As a result, a lot of memory
is wasted and many jobs may not get space in the memory, and would have to wait for another job
to complete.
Best Fit Allocation Algorithm: Allocate the smallest hole that is big enough to satisfy the request.
This saves large holes for other process requests that may need them later, but the resulting unused
portions of holes may be too small to be of any use, and will therefore be wasted. Keeping the free
list sorted can speed up the process of finding the right hole.

Examples:
Input : blockSize[] = {100, 500, 200}
processSize[] = {95, 417, 112, 426}
Output :
process no process size Block
1 95 1
2 417 2
3 112 3
4 426 Not allocated
Internal Fragmentation: 176
*Note: If a variable partitioning is used also, P4 process with size 426 won’t be allocated with
memory.

Advantages of Best-Fit Allocation :


Memory Efficient. The operating system allocates the job minimum possible space in the memory,
making memory management very efficient. To save memory from getting wasted, it is the best
method.
Disadvantages of Best-Fit Allocation :
It is a Slow Process. Checking the whole memory for each job makes the working of the operating
system very slow. It takes a lot of time to complete the work.

Worst fit memory allocation:


In this allocation technique the process traverse the whole memory and always search for largest
hole/partition, and then the process is placed in that hole/partition.It is a slow process because it has
to traverse the entire memory to search largest bole.

Example:
Fixed Partitioning
Input : blockSize[] = {100, 500, 200, 300, 600};
processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 5
2 417 2
3 112 4
4 426 Not Allocated
Internal Fragmentation: 659

Variable Partitioning:

Input : blockSize[] = {100, 500, 200, 300, 600};


processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 5
2 417 2
3 112 5
4 426 Not Allocated
Internal Fragmentation: 359

Advantages of Worst-Fit Allocation :


Since this process chooses the largest hole/partition, therefore there will be large internal
fragmentation. Now, this internal fragmentation will be quite big so that other small processes can
also be placed in that left over partition.
Disadvantages of Worst-Fit Allocation :
It is a slow process because it traverse all the partitions in the memory and then selects the largest
partition among all the partitions, which is a time consuming process.

Non-contiguous memory allocation: In non-contiguous memory allocation, it is allowed to


store the processes in non-contiguous memory locations. There are different techniques used to load
processes into memory, as follows:
1. Paging
2. Segmentation
3. Virtual memory paging(Demand paging)

Paging

Main memory is divided into a number of equal-sized blocks, are called frames. Each process is
divided into a number of the equal-size block of the same length as frames, are called Pages. A
process is loaded by loading all of its pages into available frames (may not be contiguous).
The process of Translation from logical to physical addresses

• Every address generated by the CPU is divided into two parts: a page number (p) and a page
offset (d).The page number is used as an index into a page table.

• The page table contains the base address of each page in physical memory. This base
address is combined with the page offset to define the physical memory address that is sent
to the memory unit.
• If the size of logical address space is 2^m and a page size is 2^n addressing units (bytes
or ·words), then the high-order m – n bits of a logical address designate the page number
and then low-order bits designate the page offset.

Hardwar
e Support
for
Paging:
Each
operating
system has
its own
methods
for storing
page
tables.
Most
operating
systems
allocate a
page table
for each
process. A pointer to the page table is stored with the other register values (like the instruction
counter) in the process control block. When the dispatcher is told to start a process, it must reload
the user registers and define the correct hardware page table values from the stored user page table.

Implementation of Page Table


• Generally, Page table is kept in main memory. The Page Table Base Register (PTBR) points
to the page table. And Page-table length register (PRLR) indicates the size of the page table.
• In this scheme, every data/instruction access requires two memory accesses. One for the
page table and one for the data/instruction.
• The two memory access problem can be solved by the use of a special fast-lookup hardware
cache called associative memory or translation look-aside buffers (TLBs).

Paging Hardware With TLB


The TLB is an associative and high-speed memory. Each entry in the TLB consists of two parts: a
key (or tag) and a value. The TLB is used with page tables in the following way.

• The TLB contains only a few of the page-table entries. When a logical address is generated
by the CPU, its page number is presented to the TLB.
• If the page number is found (known as a TLB Hit), its frame number is immediately
available and is used to access memory. It takes only one memory access.
• If the page number is not in the TLB (known as a TLB miss), a memory reference to the
page table must be made. When the frame number is obtained, we can use it to access
memory. It takes two memory accesses.
• In addition, it stores the page number and frame number to the TLB, so that they will be
found quickly on the next reference.
• If the TLB is already full of entries, the operating system must select one for replacement by
using replacement algorithm.

• The percentage of times that a particular page number is found in the TLB is called the hit
ratio. The effective access time (EAT) is obtained as follows:

• EAT= HR x (TLBAT + MAT) + MR x (TLBAT + 2 x MAT)


Where HR: Hit Ratio, TLBAT: TLB access time, MAT: Memory access time, MR: Miss
Ratio.
Memory protection in Paged Environment:

• Memory protection in a paged environment is accomplished by protection bits that are


associated with each frame. These bits are kept in the page table.

• One bit can define a page to be read-write or read-only. This protection bit can be checked to
verify that no writes are being made to a read-only page. An attempt to write to a read-only
page causes a hardware trap to the operating system (or memory-protection violation).

• One more bit is attached to each entry in the page table: a valid-invalid When this bit is set
to “valid,” this value indicates that the associated page is in the process’ logical-address
space, and is a legal (or valid) page. If the bit is set to “invalid,” this value indicates that the
page is not in the process’ logical-address space.

• Illegal addresses are trapped by using the valid-invalid bit. The operating system sets this bit
for each page to allow or disallow accesses to that page.
(Valid (v) or invalid (i) bit in a page table)

Segmentation:
Segmentation is a memory management technique in which, the memory is divided into the variable
size parts. Each part is known as segment which can be allocated to a process.
The details about each segment are stored in a table called as segment table. Segment table is stored
in one (or many) of the segments.
Segment table contains mainly two information about segment:

1.Base: It is the base address of the segment


2.Limit: It is the length of the segment.
Paging as our main memory management technique. Paging is more close to Operating system
rather than the User. It divides all the process into the form of pages regardless of the fact that a
process can have some relative parts of functions which needs to be loaded in the same page.
Operating system doesn't care about the User's view of the process. It may divide the same function
into different pages and those pages may or may not be loaded at the same time into the memory. It
decreases the efficiency of the system.
It is better to have segmentation which divides the process into the segments. Each segment contain
same type of functions such as main function can be included in one segment and the library
functions can be included in the other segment.
Translation of Logical address into physical address by segment table:

CPU generates a logical address which contains two parts:

1.Segment Number
2.Offset
The Segment number is mapped to the segment table. The limit of the respective segment is
compared with the offset. If the offset is less than the limit then the address is valid otherwise it
throws an error as the address is invalid.
In the case of valid address, the base address of the segment is added to the offset to get the physical
address of actual word in the main memory.

Advantages of Segmentation

1.No internal fragmentation


2.Average Segment Size is larger than the actual page size.
3.Less overhead
4.It is easier to relocate segments than entire address space.
5.The segment table is of lesser size as compare to the page table in paging.
Disadvantages

1.It can have external fragmentation.


2.it is difficult to allocate contiguous memory to variable sized partition.
3.Costly memory management algorithms.

Virtual Memory:
Virtual Memory is a space where large programs can store themselves in form of pages while their
execution and only the required pages or portions of processes are loaded into the main memory.
This technique is useful as large virtual memory is provided for user programs when a very small
physical memory is there.
It maps memory addresses used by a program, called virtual addresses, into physical addresses in
computer memory.
1.All memory references within a process are logical addresses that are dynamically translated
into physical addresses at run time. This means that a process can be swapped in and out of
main memory such that it occupies different places in main memory at different times during
the course of execution.
2.A process may be broken into number of pieces and these pieces need not be continuously
located in the main memory during execution. The combination of dynamic run-time address
translation and use of page or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or segments are present
in the main memory during execution. This means that the required pages need to be loaded into
memory whenever required. Virtual memory is implemented using Demand Paging or Demand
Segmentation.

DemandPaging :
The process of loading the page into memory on demand (whenever page fault occurs) is
known as demand paging.
The process includes the following steps :
If CPU try to refer a page that is currently not available in the main memory, it generates an
interrupt indicating memory access fault.

1.The OS puts the interrupted process in a blocking state. For the execution to proceed the OS
must bring the required page into the memory.
2.The OS will search for the required page in the logical address space.
3.The required page will be brought from logical address space to physical address space. The
page replacement algorithms are used for the decision making of replacing the page in
physical address space.
4.The page table will updated accordingly.
5.The signal will be sent to the CPU to continue the program execution and it will place the
process back into ready state.
Hence whenever a page fault occurs these steps are followed by the operating system and the
required page is brought into memory.

Advantages :

•More processes may be maintained in the main memory: Because we are going to load only
some of the pages of any particular process, there is room for more processes. This leads to
more efficient utilization of the processor because it is more likely that at least one of the more
numerous processes will be in the ready state at any particular time.
•A process may be larger than all of main memory: One of the most fundamental restrictions
in programming is lifted. A process larger than the main memory can be executed because of
demand paging. The OS itself loads pages of a process in main memory as required.
•It allows greater multiprogramming levels by using less of the available (primary) memory
for each process.
Page Fault Service Time :
The time taken to service the page fault is called as page fault service time. The page fault service
time includes the time taken to perform all the above six steps.
Let Main memory access time is: m

Page fault service time is: s

Page fault rate is : p

Then, Effective memory access time = (p*s) + (1-p)*m

Swapping:
Swapping a process out means removing all of its pages from memory, or marking them so that
they will be removed by the normal page replacement process. Suspending a process ensures that it
is not runnable while it is swapped out. At some later time, the system swaps back the process from
the secondary storage to main memory. When a process is busy swapping pages in and out then this
situation is called thrashing.
Thrashing :
At any given time, only few pages of any process are in main memory and therefore more processes
can be maintained in memory. Furthermore time is saved because unused pages are not swapped in
and out of memory. However, the OS must be clever about how it manages this scheme. In the
steady state practically, all of main memory will be occupied with process’s pages, so that the
processor and OS has direct access to as many processes as possible. Thus when the OS brings one
page in, it must throw another out. If it throws out a page just before it is used, then it will just have
to get that page again almost immediately. Too much of this leads to a condition called Thrashing.
The system spends most of its time swapping pages rather than executing instructions. So a good
page replacement algorithm is required.

Causes of Thrashing :
1.High degree of multiprogramming: If the number of processes keeps on increasing in the
memory than number of frames allocated to each process . So, less number of frames will be
available to each process. Due to this, page fault will occur more frequently and more CPU
time will be wasted in just swapping in and out of pages and the utilization will keep on
decreasing.
Forexample:
Let free frames = 400
Case 1: Number of process = 100
Then, each process will get 4 frames.
Case 2: Number of process = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes are increased,frames per process
are decreased. Hence CPU time will be consumed in just swapping pages.
2.Lacks of Frames:If a process has less number of frames then less pages of that process will
be able to reside in memory and hence more frequent swapping in and out will be required.
This may lead to thrashing. Hence sufficient amount of frames must be allocated to each
process in order to prevent thrashing.
Recovery of Thrashing :

•Do not allow the system to go into thrashing by instructing the long term scheduler not to
bring the processes into memory after the threshold.
•If the system is already in thrashing then instruct the mid term schedular to suspend some of
the processes so that we can recover the system from thrashing

Page Replacement Algorithms in Operating Systems:


In an operating system that uses paging for memory management, a page replacement algorithm is
needed to decide which page needs to be replaced when new page comes in.

Page Fault –A page fault happens when a running program accesses a memory page that is mapped
into the virtual address space, but not loaded in physical memory.
Since actual physical memory is much smaller than virtual memory, page faults happen. In case of
page fault, Operating System might have to replace one of the existing pages with the newly needed
page. Different page replacement algorithms suggest different ways to decide which page to
replace. The target for all algorithms is to reduce the number of page faults.

Page Replacement Algorithms :


1. First In First Out (FIFO)
This is the simplest page replacement algorithm. In this algorithm, the operating system keeps
track of all pages in the memory in a queue, the oldest page is in the front of the queue. When
a page needs to be replaced page in the front of the queue is selected for removal.
Example: Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames.Find number of
page faults.

Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3
Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. —>1
Page Fault.
6 comes,it is also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page
Fault.
Finally when 3 come it is not avilable so it replaces 0, 1 page fault
Belady’s anomaly: Belady’s anomaly proves that it is possible to have more page faults when
increasing the number of page frames while using the First in First Out (FIFO) page
replacement algorithm.
For example, if we consider reference string 3, 2, 1,0, 3, 2, 4, 3, 2, 1, 0, 4 and 3 slots, we get 9
total page faults, but if we increase slots to 4, we get 10 page faults.
Example with 3 frames: Total page faults are 9.
3 2 1 0 3 2 4 3 2 1 0 4

1 1 1 2 2 2 2 2 0 0

2 2 2 3 3 3 3 3 1 1 1

3 3 3 0 0 0 4 4 4 4 4 4

H H H
Example with 4 frames: Total page faults are 10.
3 2 1 0 3 2 4 3 2 1 0 4

0 0 0 0 0 0 1 1 1

1 1 1 1 1 1 2 2 2 2

2 2 2 2 2 2 3 3 3 3 4

3 3 3 3 3 3 4 4 4 4 0 0

H H
Advantages :

It is simple and easy to understand & implement.


Disadvantages :
The process effectiveness is low.
When we increase the number of frames while using FIFO, we are giving more
memory to processes. So, page fault should decrease, but here the page faults are
increasing. This problem is called as Belady’s anomaly.

2. Optimal Page replacement :


In this algorithm, pages are replaced which would not be used for the longest duration of time
in the future.
Example: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4 page frame.
Find number of page fault.

Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —>4 Page faults
0 is already there so —>0 Page fault.
when 3 came it will take the place of 7 because it is not used for the longest duration of time
in the future.—>1 Page fault.
0 is already there so —>0 Page fault..
4 will takes place of 1 —>1 Page Fault.

Now for the further page reference string —>0 Page fault because they are already available
in the memory.
Optimal page replacement is perfect, but not possible in practice as the operating system
cannot know future requests.

Total no.of page faults :9

3. Least Recently Used :


In this algorithm page will be replaced which is least recently used.
Example: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4 page
frames.Find number of page faults.
Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available
in the memory.
Example:
File System Interface
File:
A file is a named collection of related information that is recorded on secondary storage such as
magnetic disks, magnetic tapes and optical disks. In general, a file is a sequence of bits, bytes, lines
or records whose meaning is defined by the files creator and user.
File Attributes: A file's attributes vary from one operating system to another but typically consist
of these:
3.Name:Name is the symbolic file name and is the only information kept in human readable form.
4.Identifier: This unique tag is a number that identifies the file within the file system; it is in non-
human-readable form of the file.
5.Type: This information is needed for systems which support different types of files or its
format.
6.Location: This information is a pointer to a device which points to the location of the file on the
device where it is stored.
7.Size: The current size of the file (which is in bytes, words, etc.) which possibly the maximum
allowed size gets included in this attribute.
8.Protection: Access-control information establishes who can do the reading, writing, executing,
etc.
9.Date, Time & user identification: This information might be kept for the creation of the file,
its last modification and last used. These data might be useful for in the field of protection,
security, and monitoring its usage.
File Operations:
For defining a file properly, we need to consider the operations that can be performed on files.
The operating system can provide system calls to create, write, read, reposition, delete, and
truncate files. There are six basic file operations within an Operating system. These are:
Creating a file:There are two steps necessary for creating a file. First, space in the file system
must be found for the file. We discuss how to allocate space for the file. Second, an entry for the
new file must be made in the directory.
Writing a file: To write to a file, you make a system call specify about both the name of the file
along with the information to be written to the file.
Reading a file: To read from a file, you use a system call which specifies the name of the file
and where within memory the next block of the file should be placed.
Repositioning inside a file: The directory is then searched for the suitable entry, and the
'current-file-position' pointer is relocating to a given value. Relocating within a file need not
require any actual I/O. This file operation is also termed as 'file seek.'
Deleting a file: For deleting a file, you have to search the directory for the specific file.
Deleting that file or directory release all file space so that other files can re-use that space.
Truncating a file: The user may wish for erasing the contents of a file but keep the attributes
same. Rather than deleting the file and then recreate it, this utility allows all attributes to remain
unchanged — except the file length — and let the user add or edit the file content.
Windows ( and some other systems ) use special file extensions to indicate the type of each
file:

File Access Methods:


The file contains the information but when it required to used this information can be access by the
access methods and reads into the computer memory. Some system provides only one access
method and some provide more than on access method to access the file,
1. Sequential Access Method
A sequential access is that in which the records are accessed in some sequence, i.e., the information
in the file is processed in order, one record after the other. This access method is the most primitive
one.
The idea of Sequential access is based on the tape model which is a sequential access device. We
consider Sequential access method is best because most of the records in a file are to be processed.
For example, transaction files.
Example:Compilers usually access files in this fashion.
2. Direct or Random Access Methods
Sometimes it is not necessary to process every record in a file. It is not necessary to process all the
records in the order in which they are present in the memory. In all such cases, direct access is used.
The disk is a direct access device which gives us the reliability to random access of any file block.
In the file, there is a collection of physical blocks and the records of that blocks.
Eg. Databases are often of this type since they allow query processing that involves immediate
access to large amounts of information. All reservation systems fall into this category.
Not all operating systems support direct access files. The sequential and direct access of the file is
defined at the time of creation and accessed accordingly later. The direct access of a sequential file
is not possible but Sequential access to a direct access file is possible.
3. Index Access Method
An indexed file is a computer file with an index that allows easy random access to any record given
its file key. The key is an attribute that uniquely identifies a record. We can say that If more than
one index is present the other ones are alternate indexes. The creation of the indexes is done with
the file but maintained by the system.
4. Index sequential Access Method
The index sequential access method is a modification of the direct access method. Basically, it is
kind of combination of both the sequential access as well as direct access. The main idea of this
method is to first access the file directly and then it accesses sequentially. In this access method, it
is necessary for maintaining an index. The index is nothing but a pointer to a block. The direct
access of the index is made to access a record in a file. The information which is obtained from this
access is used to access the file. Sometimes the indexes are very big. So to maintain all these
hierarchies of indexes are built in which one direct access of an index leads to information of
another index access.
The main advantage in this type of access is that both direct and sequential access of files is
possible with the help of this method.

Directory Structure:
A directory is a container that is used to contain folders and file. It organizes files and folders into a
hierarchical manner.
There are several logical structures of a directory, these are given below.

Single-level directory :
Single level directory is simplest directory structure.In it all files are contained in same
directory which make it easy to support and understand.
A single level directory has a significant limitation, however, when the number of files
increases or when the system has more than one user. Since all the files are in the same
directory, they must have the unique name . if two users call their dataset test, then the unique
name rule violated.

Advantages:

Since it is a single directory, so its implementation is very


easy.
If the files are smaller in size, searching will become faster.
The operations like file creation, searching, deletion, updating
are very easy in such a directory structure.
Disadvantages:

There may chance of name collision because two files can not
have the same name.
Searching will become time taking if the directory is large.
In this can not group the same type of files together.
Two-level directory :
As we have seen, a single level directory often leads to confusion of files names among
different users. the solution to this problem is to create a separate directory for each user.
In the two-level directory structure, each user has there own user files directory (UFD). The
UFDs has similar structures, but each lists only the files of a single user. system’s master file
directory (MFD) is searches whenever a new user id=s logged in. The MFD is indexed by
username or account number, and each entry points to the UFD for that user.

Advantages:
A user is not allowed to share files with other users.
Still it not very scalable, two files of the same type cannot be grouped together in the same
user.
Different users can have same directory as well as file name.
Searching of files become more easy due to path name and
user-grouping.
Disadvantages:
A user is not allowed to share files with other users.

Still it not very scalable, two files of the same type cannot be grouped together in the same
user.
Tree-structured directory :
Once we have seen a two-level directory as a tree of height 2, the natural generalization is to
extend the directory structure to a tree of arbitrary height.
This generalization allows the user to create there own subdirectories and to organize on their
files accordingly.

A tree structure is the most common directory structure. The tree has a root directory, and
every file in the system have a unique path.

Advantages:

Very generalize, since full path name can be given.


Very scalable, the probability of name collision is less.
Searching becomes very easy, we can use both absolute path as well as relative.
Disadvantages:

Every file does not fit into the hierarchical model, files may be saved into multiple
directories.
We can not share files.
It is inefficient, because accessing a file may go under multiple directories.
4. Acyclic graph directory :
An acyclic graph is a graph with no cycle and allows to share subdirectories and files. The
same file or subdirectories may be in two different directories. It is a natural generalization of
the tree-structured directory.
It is used in the situation like when two programmers are working on a joint project and they
need to access files. The associated files are stored in a subdirectory, separating them from
other projects and files of other programmers, since they are working on a joint project so they
want the subdirectories to be into their own directories. The common subdirectories should be
shared. So here we use Acyclic directories.

It is the point to note that shared file is not the same as copy file . If any programmer makes
some changes in the subdirectory it will reflect in both subdirectories.

Advantages:

We can share files.


Searching is easy due to different-different paths.
Disadvantages:

We share the files via linking, in case of deleting it may create the problem,
If the link is softlink then after deleting the file we left with a dangling pointer.
In case of hardlink, to delete a file we have to delete all the reference associated with
it.
5. General Graph Directory Structure:
In general graph directory structure, cycles are allowed within a directory structure where
multiple directories can be derived from more than one parent directory.
The main problem with this kind of directory structure is to calculate total size or space that
has been taken by the files and directories.

Advantages:

It allows cycles.
It is more flexible than other directories structure.
Disadvantages:

It is more costly than others.


It needs garbage collection.

File System Structure:


File System provide efficient access to the disk by allowing data to be stored, located and retrieved
in a convenient way. A file System must be able to store the file, locate the file and retrieve the file.
Most of the Operating Systems use layering approach for every task including file systems. Every
layer of the file system is responsible for some activities.
The image shown below, elaborates how the file system is divided in different layers, and also the
functionality of each layer.

When an application program asks for a file, the first request is directed to the logical file system.
The logical file system contains the Meta data of the file and directory structure. If the application
program doesn't have the required permissions of the file then this layer will throw an error. Logical
file systems also verify the path to the file.

•Generally, files are divided into various logical blocks. Files are to be stored in the hard
disk and to be retrieved from the hard disk. Hard disk is divided into various tracks and
sectors. Therefore, in order to store and retrieve the files, the logical blocks need to be
mapped to physical blocks. This mapping is done by File organization module. It is also
responsible for free space management.
•Once File organization module decided which physical block the application program
needs, it passes this information to basic file system. The basic file system is responsible for
issuing the commands to I/O control in order to fetch those blocks.
•I/O controls contain the codes by using which it can access hard disk. These codes are
known as device drivers. I/O controls are also responsible for handling interrupts.
File system implementation:
We can implement file system by using two types data structures
1.On-disk Structures
Generally they contain information about total number of disk blocks, free disk blocks, location of
them and etc. Below given are different on-disk structures :
1.Boot Control Block –
It is usually the first block of volume and it contains information needed to boot an operating
system.In UNIX it is called boot block and in NTFS it is called as partition boot sector.
2.Volume Control Block
It contains number and sizes of blocks in volume, free block count and list, free FCB count
and list; called superblock in UFS;in master file table in NTFS.
3.Directory structure (one per file system)
It is the collection of directories for organizing files – maps file names to FCBs (inodes in
UNIX)
4.File control block (one per file) –it contains attributes of file: size, location,
owner,protections,reference counts;called inode in UNIX
2. In-Memory Structure :
They are maintained in main-memory and these are helpful for file system management for caching.
Several in-memory structures given below :
5.Mount Table –
It contains information about each mounted volume.
6.Directory-Structure cache –
This cache holds the directory information of recently accessed directories.
7.System wide open-file table –
It contains the copy of FCB of each open file.
8.Per-process open-file table –
It contains information opened by that particular process and it maps with appropriate system
wide open-file.
Directo
ry
Implem
entation
:
There is
the
number
of
algorith
ms by
using
which,
the
directori
es can
be
impleme
nted. However, the selection of an appropriate directory implementation algorithm may
significantly affect the performance of the system.
The directory implementation algorithms are classified according to the data structure they are
using. There are mainly two algorithms which are used in these days.
1. Linear List
In this algorithm, all the files in a directory are maintained as singly linked list. Each file contains
the pointers to the data blocks which are assigned to it and the next file in the directory.
Characteristics

1.When a new file is created, then the entire list is checked whether the new file name is
matching to a existing file name or not. In case, it doesn't exist, the file can be created at the
beginning or at the end. Therefore, searching for a unique name is a big concern because
traversing the whole list takes time.
2.The list needs to be traversed in case of every operation (creation, deletion, updating, etc)
on the files therefore the systems become inefficient.
2. Hash Table
To overcome the drawbacks of singly linked list implementation of directories, there is an
alternative approach that is hash table. This approach suggests to use hash table along with the
linked lists.
A key-value pair for each file in the directory gets generated and stored in the hash table. The key
can be determined by applying the hash function on the file name while the key points to the
corresponding file stored in the directory.
Now, searching becomes efficient due to the fact that now, entire list will not be searched on every
operating. Only hash table entries are checked using the key and if an entry found then the
corresponding file will be fetched using the value.

File Allocation Methods


On secondary storage, a file consists of a collection of blocks. The operating system or file
management system is responsible for allocating blocks to files.The approach taken for file
allocation may influence the approach taken for free space management.Space is allocated to a file
as one or more portions (contiguous set of allocated blocks).
The allocation methods define how the files are stored in the disk blocks. There are three main disk
space or file allocation methods.

Contiguous Allocation
Linked Allocation
Indexed Allocation
The main idea behind these methods is to provide:

Efficient disk space utilization.


Fast access to the file blocks.
All the three methods have their own advantages and disadvantages as discussed below:

1. Contiguous Allocation:
In this scheme, each file occupies a contiguous set of blocks on the disk. For example, if a file
requires n blocks and is given a block b as the starting location, then the blocks assigned to the file
will be: b, b+1, b+2,……b+n-1. This means that given the starting block address and the length of
the file (in terms of blocks required), we can determine the blocks occupied by the file.
The directory entry for a file with contiguous allocation contains

Address of starting block


Length of the allocated portion.
The file ‘mail’ in the following figure starts from the block 19 with length = 6 blocks. Therefore, it
occupies 19, 20, 21, 22, 23, 24 blocks.

Advantages:
Both the Sequential and Direct Accesses are supported by this. For direct access, the address
of the kth block of the file which starts at block b can easily be obtained as (b+k).
This is extremely fast since the number of seeks are minimal because of contiguous allocation
of file blocks.

Disadvantages:

This method suffers from both internal and external fragmentation. This makes it inefficient
in terms of memory utilization.
Increasing file size is difficult because it depends on the availability of contiguous memory
at a particular instance.

2. Linked List Allocation:


In this scheme, each file is a linked list of disk blocks which need not be contiguous. The disk
blocks can be scattered anywhere on the disk.
The directory entry contains a pointer to the starting and the ending file block. Each block contains
a pointer to the next block occupied by the file.
The file ‘jeep’ in following image shows how the blocks are randomly distributed. The last block
(25) contains -1 indicating a null pointer and does not point to any other block.

Advantages:

This is very flexible in terms of file size. File size can be increased easily since the system
does not have to look for a contiguous chunk of memory.
This method does not suffer from external fragmentation. This makes it relatively better in
terms of memory utilization.
Disadvantages:
11. Because the file blocks are distributed randomly on the disk, a large number of seeks
are needed to access every block individually. This makes linked allocation slower.
12. It does not support random or direct access. We can not directly access the blocks of a
file. A block k of a file can be accessed by traversing k blocks sequentially (sequential
access ) from the starting block of the file via block pointers.
13. Pointers required in the linked allocation incur some extra overhead.

3. Indexed Allocation:
In this scheme, a special block known as the Index block contains the pointers to all the blocks
occupied by a file. Each file has its own index block. The ith entry in the index block contains the
disk address of the ith file block. The directory entry contains the address of the index block as
shown in the image:

Advantages:

This supports direct access to the blocks occupied by the file and therefore provides fast
access to the file blocks.
It overcomes the problem of external fragmentation.
Disadvantages:

The pointer overhead for indexed allocation is greater than linked allocation.
For very small files, say files that expand only 2-3 blocks, the indexed allocation would
keep one entire block (index block) for the pointers which is inefficient in terms of memory
utilization. However, in linked allocation we lose the space of only 1 pointer per block.
For files that are very large, single index block may not be able to hold all the pointers.

Free space management in Operating System:


The system keeps tracks of the free disk blocks for allocating space to files when they are created.
Also, to reuse the space released from deleting the files, free space management becomes crucial.
The system maintains a free space list which keeps track of the disk blocks that are not allocated to
some file or directory. The free space list can be implemented mainly as:

Bitmap or Bit vector –


A Bitmap or Bit Vector is series or collection of bits where each bit corresponds to a disk
block. The bit can take two values: 0 and 1: 0 indicates that the block is allocated and 1
indicates a free block.
The given instance of disk blocks on the disk in Figure 1 (where green blocks are allocated)
can be represented by a bitmap of 16 bits as: 0000111000000110.

Advantages –

Simple to understand.
Finding the first free block is efficient. It requires scanning the words (a group of 8 bits)
in a bitmap for a non-zero word. (A 0-valued word has all bits 0). The first free block is
then found by scanning for the first 1 bit in the non-zero word.
The block number can be calculated as:
(number of bits per word) *(number of 0-values words) + offset of first 1 bit in the non-zero
word.
For the Figure-1, we scan the bitmap sequentially for the first non-zero word.
The first group of 8 bits (00001110) constitute a non-zero word since all bits are not 0. After the
non-0 word is found, we look for the first 1 bit. This is the 5th bit of the non-zero word. So, offset =
5.
Therefore, the first free block number = 8*0+5 = 5.

Linked List –
In this approach, the free disk blocks are linked together i.e. a free block contains a pointer to
the next free block. The block number of the very first disk block is stored at a separate
location on disk and is also cached in memory.

In Figure-2, the free space list head points to Block 5 which points to Block 6, the next free
block and so on. The last free block would contain a null pointer indicating the end of free list.
A drawback of this method is the I/O required for free space list traversal.

Grouping –
This approach stores the address of the free blocks in the first free block. The first free block
stores the address of some, say n free blocks. Out of these n blocks, the first n-1 blocks are
actually free and the last block contains the address of next free n blocks.
An advantage of this approach is that the addresses of a group of free disk blocks can be found
easily.
Counting –
This approach stores the address of the first free disk block and a number n of free contiguous
disk blocks that follow the first block.
Every entry in the list would contain:
Address of first free disk block
A number n
For example, in Figure-1, the first entry of the free space list would be: ([Address of Block 5],
2), because 2 contiguous free blocks follow block 5.

File Mounting:

Mounting is the attaching of an additional filesystem to the currently accessible filesystem of a


computer.

A filesystem is a hierarchy of directories (also referred to as a directory tree) that is used to


organize files on a computer or storage media (e.g., a CDROM or floppy disk). On computers
running linux or other Unix-like operating systems, the directories start with the root directory,
which is the directory that contains all other directories and files on the system and which is
designated by a forward slash ( / ). The currently accessible filesystem is the filesystem that can
be accessed on a computer at a given time.

The mount point is the directory (usually an empty one) in the currently accessible filesystem to
which a additional filesystem is mounted. It becomes the root directory of the added directory
tree, and that tree becomes accessible from the directory to which it is mounted (i.e., its mount
point). Any original contents of a directory that is used as a mount point become invisible and
inaccessible while the filesystem is still mounted.

The /mnt directory exists by default on all Unix-like systems. It, or usually its subdirectories (such
as /mnt/floppy and /mnt/usb), are intended specifically for use as mount points for removable
media such as CDROMs, USB key drives and floppy disks.

On some operating systems, everything is mounted automatically by default so that users are
never even aware that there is any such thing as mounting. Linux and other Unix-like systems can
likewise be configured so that everything is mounted by default, as a major feature of such
systems is that they are highly configurable.

The only partition that can be accessed immediately after a computer boots (i.e., starts up) is the
root partition, which contains the root directory, and usually at least a few other directories as
well. The other partitions must be attached to this root filesystem in order for an entire, multiple-
partition filesystem to be accessible. Thus, about midway through the boot process, the operating
system makes these non-root partitions accessible by mounting them on to specified directories in
the root partition.

Systems can be set up so that external storage devices can be mounted automatically upon
insertion. This is convenient and is usually satisfactory for home computers. However, it can
cause security problems, and thus it is usually not (or, at least, should not be) permitted for
networked computers in businesses and other organizations. Rather, such devices must be
mounted manually after insertion, and such manual mounting can only be performed by the root
account.

Mounting can often be performed manually by the root user by merely using the mount command
followed by the name of the device to be mounted and its mounting destination (but in some cases
it is also necessary to specify the type of filesystem).

Removing the connection between the mounted device and the rest of the filesystem is referred to
as unmounting. It is performed by running the umount (with no letter n after the first u) command,
likewise followed by the name of the device to be unmounted and its mount point. For example, to
unmount the eighth partition from the root filesystem, the following would be used:

umount /dev/hda8 /dir8

Synchronization
Process Synchronization means sharing system resources by processes in a such a way that,
Concurrent access to shared data is handled thereby minimizing the chance of inconsistent data.
Maintaining data consistency demands mechanisms to ensure synchronized execution of
cooperating processes.
Process Synchronization was introduced to handle problems that arose while multiple process
executions. Some of the problems are discussed below.

Critical Section Problem


A Critical Section is a code segment that accesses shared variables and has to be executed as an
atomic action. It means that in a group of cooperating processes, at a given point of time, only one
process must be executing its critical section. If any other process also wants to execute its critical
section, it must wait until the first one finishes.
Solution to Critical Section Problem
A solution to the critical section problem must satisfy the following three conditions:

1. Mutual Exclusion
Out of a group of cooperating processes, only one process can be in its critical section at a given
point of time.

2. Progress
If no process is in its critical section, and if one or more threads want to execute their critical section
then any one of these threads must be allowed to get into its critical section.

3. Bounded Waiting

After a process makes a request for getting into its critical section, there is a limit for how many
other processes can get into their critical section, before this process's request is granted. So after
the limit is reached, system must grant the process permission to get into its critical section.

Mutex Locks:
A strict software approach called Mutex Locks was introduced to achieve synchronization. In this
approach, in the entry section of code, a LOCK is acquired over the critical resources modified and
used inside critical section, and in the exit section that LOCK is released.

As the resource is locked while a process executes its critical section hence no other process can
access it.

Race condition:
It is a situation in which concurrently executing processes accessing the same shared data item may
affect the consistency of the data item.
Outcome of the execution depends the particular order in which the statements are executed.

Turn Variable-
Turn Variable or Strict Alternation Approach is the software mechanism implemented at user
mode. It is a busy waiting solution which can be implemented only for two processes. In this
approach, A turn variable is used which is actually a lock.
This approach can only be used for only two processes. In general, let the two processes be Pi
and Pj. They share a variable called turn variable. The pseudo code of the program can be given as
following.
For Process Pi

Non-CS
while (turn ! =i);
Critical Section
turn = j;
Non -CS

For Process Pj

Non-CS
while (turn!=j);
Critical Section
turn = i;
Non- CS

The actual problem of the lock variable approach was the fact that the process was entering in the
critical section only when the lock variable is 1. More than one process could see the lock variable
as 1 at the same time hence the mutual exclusion was not guaranteed there.
This problem is addressed in the turn variable approach. Now, A process can enter in the critical
section only in the case when the value of the turn variable equal to the PID of the process.
There are only two values possible for turn variable, i or j. if its value is not i then it will definitely
be j or vice versa.
In the entry section, in general, the process Pi will not enter in the critical section until its value is j
or the process Pj will not enter in the critical section until its value is i.
Initially, two processes Pi and Pj are available and want to execute into critical section.
The turn variable is equal to i hence Pi will get the chance to enter into the critical section. The
value of Pi remains I until Pi finishes critical section.

Pi finishes its critical section and assigns j to turn variable. Pj will get the chance to enter into the
critical section. The value of turn remains j until Pj finishes its critical section.

Analysis of Strict Alternation approach

Let's analyze Strict Alternation approach on the basis of four requirements.


Mutual Exclusion
The strict alternation approach provides mutual exclusion in every case. This procedure works only
for two processes. The pseudo code is different for both of the processes. The process will only
enter when it sees that the turn variable is equal to its Process ID otherwise not Hence No process
can enter in the critical section regardless of its turn.
Progress
Progress is not guaranteed in this mechanism. If Pi doesn't want to get enter into the critical section
on its turn then Pj got blocked for infinite time. Pj has to wait for so long for its turn since the turn
variable will remain 0 until Pi assigns it to j.

Interest Variable-
Interest variable is a synchronization mechanism that provides synchronization among two
processes.
It uses an interest variable to provide the synchronization.
Working-
This synchronization mechanism works as explained in the following scenes-

Scene-01:

Process P0 arrives.
It sets interest[0] = True.
Now, it executes while loop condition- interest [1] == True.
Since interest [1] is initialized to False, so it returns value 0 to the while loop.
The while loop condition breaks.
Process P0 enters the critical section and executes.
Now, even if process P0 gets preempted in the middle, process P1 can not enter the critical
section.
Process P1 can not enter unless process P0 completes and sets the interest [0] = False.

Scene-02:

Process P1 arrives.
It sets interest[1] = True.
Now, it executes while loop condition- interest [0] == True.
Process P0 has already shown its interest by setting interest [0] = True.
So, it returns value 1 to the while loop.
The while loop condition satisfies.
The process P1 is trapped inside an infinite while loop.
The while loop keeps the process P1 busy until the interest [0] value becomes False and its
condition breaks.
Scene-03:

Process P0 comes out of the critical section and sets the interest [0] value to False.
The while loop condition of process P1 breaks.
Now, the process P1 waiting for the critical section enters the critical section and execute.
Now, even if process P1 gets preempted in the middle, process P0 can not enter the critical
section.
Process P0 can not enter unless process P1 completes and sets the interest [1] = False.

Peterson’s solution:

Peterson’s solution provides a good algorithmic description of solving the critical-section problem
and illustrates some of the complexities involved in designing software that addresses the

requirements of mutual exclusion, progress, and bounded waiting.


10.In the peterson solution we will consider 2 process P0 and P1, Turn variable whose value can be
either 0 or 1 and a flag boolean array whose value is initialised to F for both slots initially.
The mistake that we were making in turn variable was that we were never asking the process if it
wants to enter into the critical section or not. That's why it did not show Progress.
In flag variable the mistake we committed was that if both the process makes the value of flag T
then the system was trapped in deadlock. Hence use of flag variable too did not showed us Progress.

Now let's trace the above code and prove if it holds good for all criterias of critical section or not.
1. Suppose P0 wants to enter critical section so flag[0] is set T and turn is set 1. Now while loop
will be executed. It says turn == 1 which is true and flag[1]==T which is false because P1 is not
interested in entering into critical section till this time. So, overall while condition will not execute
because true && false = false and P0 will enter into critical section.

2. Now let say P1 wants to enter into critical section too. So, flag[1] is set T and turn is set 0.
Now while loop will be executed. It says turn == 0 which is true and flag[0]==T which is
true because P0 is already in the critical section. So, overall the while condition will execute
because true && true = true and P1 will be trapped in the while loop.
3. Now let say P0 comes out of critical section and it sets flag[0] to F. So, now P1 can enter
into critical section because flag[0] is F so while loop condition will be false.

4. Now let say P0 again wants to enter into critical section so it sets flag[0]=T and turn=1.
Now while loop will be executed. It says turn == 1 which is true and flag[1]==T which is
also true because P1 is in the critical section. So, overall while condition will not be execute
because true && true = false and P0 will be trapped in the while loop.

5. Now, we say see that at a time only 1 process can enter critical section so Mutual
Exclusion holds good for this solution.

6. Now let's see if it shows progress also or not. Assume the flag variable is set to F again
for both the processes.

7. Suppose P0 wants to enter critical section so flag[0] is set T and turn is set 1. Now while
loop will be executed. It says turn == 1 which is true and flag[1]==T which is
false because P1 is not interested in entering into critical section till this time. So, overall
while condition will not execute because true && false = false and P0 will enter into
critical section.

8. Now let say P0 comes out of critical section and it sets flag[0]=F.

9. P1 is not interested to enter into critical section till this time.

10. Now let say P0 again wants to enter into critical section. So flag[0] is set T and turn is
set 1. Now while loop will be executed. It says turn == 1 which is true and flag[1]==T
which is false because P1 is not interested in entering into critical section . So, overall while
condition will not execute because true && false = false and P0 will enter into critical
section again.

11. So we can observe that a process can access critical section multiple time. But when we
were discussing the Flag variable solution for critical section then we observed that due to
strict alternation between P0 and P1 the processes went into Deadlock. Now, let's see if this
occurs in this solution or not.

12. Assume the flag variable is set to F again for both the processes.

13. Suppose P1 wants to enter critical section so flag[1] is set T and turn is set 0. Since we
want to check if the processes goes into deadlock so we context switch before executing
while loop.

14. Now P0 enters into critical section so flag[0] is set T and turn is set 1. Now while loop
will be executed. It says turn == 1 which is true and flag[1]==T which is true as
well because P1 was context switched after initialising flag[1] to T. So, overall while
condition will execute because true && true = true and P0 will not enter into critical
section.

15. Now context switch will occur and control comes to P1 again and while loop will be
executed. It says turn == 0 which is false and flag[0]==T which is true because P0 has
been initialised flag[0] to T. So, overall while condition will not execute because false &&
true = false and P1 will enter into critical section.

16. So, you can see that it is possible that both the processes are interested in entering into
the critical section at a time but turn variable can have either 0 or 1 value so only either P0
or P1 will enter into critical section. So this solution holds good for Progress as well.

17. Now lets see if it holds good for Bounded Wait or not. Assume flag is initialised to F for
both processes again.

18. Suppose P0 wants to enter critical section so flag[0] is set T and turn is set 1. Now
while loop will be executed. It says turn == 1 which is true and flag[1]==T which is
false because P1 is not interested in entering into critical section till this time. So, overall
while condition will not execute because true && false = false and P0 will enter into
critical section.

19. Now let say P1 wants to enter into critical section too. So, flag[1] is set T and turn is set
0. Now while loop will be executed. It says turn == 0 which is true and flag[0]==T which
is true because P0 is already in the critical section. So, overall the while condition will
execute because true && true = true and P1 will be trapped in the while loop.

20. Now let say P0 comes out of critical section and it sets flag[0] to F. So, now P1 can
enter into critical section because flag[0] is F so while loop condition will be false.

21. Now let say P0 again wants to enter into critical section so it sets flag[0]=T and turn=1.
Now while loop will be executed. It says turn == 1 which is true and flag[1]==T which is
also true because P1 is in the critical section. So, overall while condition will not be execute
because true && true = false and P0 will be trapped in the while loop.

22. So, we can say that we have to wait for 1 round for execution of a process. Thus, This
solution holds good for Bounded Waiting also.

Semaphores:
Semaphores are integer variables that are used to solve the critical section problem by using two
atomic operations, wait and signal that are used for process synchronization.
The definitions of wait and signal are as follows −

Wait
The wait operation decrements the value of its argument S, if it is positive. If S is negative
or zero, then no operation is performed.

wait(S)

while (S<=0);

S--;

Signal
The signal operation increments the value of its argument S.

signal(S)

S++;

Types of Semaphores

There are two main types of semaphores i.e. counting semaphores and binary semaphores. Details
about these are given as follows −

Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate the resource access, where the semaphore count is the
number of available resources. If the resources are added, semaphore count automatically
incremented and if the resources are removed, the count is decremented.
Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to 0 and 1.
The wait operation only works when the semaphore is 1 and the signal operation succeeds
when semaphore is 0. It is sometimes easier to implement binary semaphores than counting
semaphores.
Advantages of Semaphores

Some of the advantages of semaphores are as follows −

Semaphores allow only one process into the critical section. They follow the mutual
exclusion principle strictly and are much more efficient than some other methods of
synchronization.
There is no resource wastage because of busy waiting in semaphores as processor time is not
wasted unnecessarily to check if a condition is fulfilled to allow a process to access the
critical section.
Semaphores are implemented in the machine independent code of the microkernel. So they
are machine independent.
Disadvantages of Semaphores

Some of the disadvantages of semaphores are as follows −

Semaphores are complicated so the wait and signal operations must be implemented in the
correct order to prevent deadlocks.
Semaphores are impractical for last scale use as their use leads to loss of modularity. This
happens because the wait and signal operations prevent the creation of a structured layout
for the system.
Semaphores may lead to a priority inversion where low priority processes may access the
critical section first and high priority processes later.

Producer- Consumer Problem:

The producer consumer problem is a synchronization problem. There is a fixed size buffer and the
producer produces items and enters them into the buffer. The consumer removes the items from the
buffer and consumes them.
A producer should not produce items into the buffer when the consumer is consuming an item from
the buffer and vice versa. So the buffer should only be accessed by the producer or consumer at a
time.
The producer consumer problem can be resolved using semaphores. The codes for the producer and
consumer process are given as follows −
Producer Process

The code that defines the producer process is given below −


do {

PRODUCE ITEM

wait(empty);

wait(mutex);

PUT ITEM IN BUFFER

signal(mutex);

signal(full);

} while(1);

In the above code, mutex, empty and full are semaphores. Here mutex is initialized to 1, empty is
initialized to n (maximum size of the buffer) and full is initialized to 0.
The mutex semaphore ensures mutual exclusion. The empty and full semaphores count the number
of empty and full spaces in the buffer.
After the item is produced, wait operation is carried out on empty. This indicates that the empty
space in the buffer has decreased by 1. Then wait operation is carried out on mutex so that
consumer process cannot interfere.
After the item is put in the buffer, signal operation is carried out on mutex and full. The former
indicates that consumer process can now act and the latter shows that the buffer is full by 1.
Consumer Process

The code that defines the consumer process is given below:

do {

wait(full);

wait(mutex);

REMOVE ITEM FROM BUFFER

signal(mutex);

signal(empty);

CONSUME ITEM

} while(1);

The wait operation is carried out on full. This indicates that items in the buffer have decreased by 1.
Then wait operation is carried out on mutex so that producer process cannot interfere.
Then the item is removed from buffer. After that, signal operation is carried out on mutex and
empty. The former indicates that consumer process can now act and the latter shows that the empty
space in the buffer has increased by 1.

The Readers-Writers Problem


A database is to be shared among several concurrent processes. Some of these processes may want
only to read the database, whereas others may want to update (that is, to read and write) the
database.
We distinguish between these two types of processes by referring to the former as readers and to the
latter as writers. Obviously, if two readers access the shared data simultaneously, no adverse affects
will result. However, if a writer and some other thread (either a reader or a writer) access the
database simultaneously, chaos may ensue.
To ensure that these difficulties do not arise, we require that the writers have exclusive access to the
shared database. This synchronization problem is referred to as the readers-writers problem.
In the solution to the first readers-writers problem, the reader processes share the following data
structures:
semaphore mutex, wrt;
int readcount;
The semaphores mutex and wrt are initialized to 1; readcount is initialized to O. The semaphore wrt
is common to both reader and writer processes.
The mutex semaphore is used to ensure mutual exclusion when the variable readcount is updated.
The readcount variable keeps track of how many processes are currently reading the object. The
semaphore wrt functions as a mutual-exclusion semaphore for the writers. It is also used by the first
or last reader that enters or exits the critical section. It is not used by readers who enter or exit while
other readers are in their critical sections.
The structure of a writer process :
do {
wait (wrt) ;
II writing is performed
signal (wrt) ;
}while (TRUE);

The structure of a reader process:


do {
wait (mutex) ;
readcount++i;
if (readcount==1)
wait (wrt) ;
signal (mutex);
reading is performed
wait (mutex) ;
readcount--;
if (readcount==0)
signal (wrt) ;
signal (mutex);
}while (TRUE) ;

The writer process code can be summarized as:

The wait(writer) function is called so that it achieves the mutual exclusion. The wait()
function will reduce the writer value to "0" and this will block other processes to enter into
the critical section.

The write operation will be carried and finally, the signal(writer) function will be called
and the value of the writer will be again set to "1" and now other processes will be allowed
to enter into the critical section.
This is how we can deal with the process of doing the write operation. Now, let's look at the reader
problem.
The reader process code can be summarized as:

We are using the mutex variable to change something in the readerCount variable. This is
done because if some process is changing something in the readerCount variable, then no
other process should be allowed to use that variable. So, to achieve mutual exclusion, we are
using the mutex variable.

Initially, we are calling the wait(mutex) function and this will reduce the value of the
mutex by one. After that, the readerCount value will be increased by one.

If the readerCount variable is equal to "1" i.e. the reader process is the first process, in
this case, no other process demanding for write operation will be allowed to enter into the
critical section. So, the wait(writer) will be called and the value of the writer variable will be
decreased to "0" and no other process demanding for write operation will be allowed.

After changing the readerCount variable, the value of the mutex variable will be increased
by one, so that other processes should be allowed to change the value of the readerCount
value.
The read operation by various processes will be continued and after that when the read
operation is done, then again we have to change the count the value of the readerCount and
decrease the value by one.

If the readerCount becomes "0", then we have to increase the value of the writer variable
by one by calling the signal(writer) function. This is done because if the readerCount is "0"
then other writer processes should be allowed to enter into the critical section to write the
data in the file.

The Dining-Philosophers Problem


Consider five philosophers who spend their lives thinking and eating. 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 .

When a philosopher thinks, she does not interact with her colleagues. From time to time, a
philosopher gets hungry and tries to pick up the two chopsticks that are closest to her (the
chopsticks that are between her and her left and right neighbors). A philosopher may pick up only
one chopstick at a time. Obviously, she cannot pick up a chopstick that is already in the hand of a
neighbor.
When a hungry philosopher has both her chopsticks at the same time, she eats without releasing her
chopsticks. When she is finished eating, she puts down both of her chopsticks and starts thinking
again.
It is a simple representation of the need to allocate several resources among several processes in a
deadlock-free and starvation-free manner.
One simple solution is to represent each chopstick with a semaphore. A philosopher tries to grab a
chopstick by executing a wait () operation on that semaphore; she releases her chopsticks by
executing the signal () operation on the appropriate semaphores. Thus, the shared data are
semaphore chopstick [5] ;
where all the elements of chopstick are initialized to 1. Although this solution guarantees that no
two neighbors are eating simultaneously, it nevertheless must be rejected because it could create a
deadlock.
Suppose that all five philosophers become hungry simultaneously and each grabs her left chopstick.
All the elements of chopstick will now be equal to O. When each philosopher tries to grab her right
chopstick, she will be delayed forever. Several possible remedies to the deadlock problem are listed
next. we present a solution to the dining-philosophers problem that ensures freedom from
deadlocks.
• Allow at most four philosophers to be sitting simultaneously at the table.
do {
wait (chopstick[i] );
wait(chopstick[(i+l) % 5]);
// eat
signal (chopstick[i] );
signal (chopstick[(i+l) % 5]);
// think
}while (TRUE);

• Allow a philosopher to pick up her chopsticks only if both chopsticks are available (to do this she
must pick them up in a critical section).
• Use an asymmetric solution; that is, an odd philosopher picks up first her left chopstick and then
her right chopstick, whereas an even philosopher picks up her right chopstick and then her left
chopstick.
Finally, any satisfactory solution to the dining-philosophers problem must guard against the
possibility that one of the philosophers will starve to death.
A deadlock-free solution does not necessarily eliminate the possibility of starvation.

Deadlock in Operating System

A process in operating systems uses different resources and uses resources in the following way.
1) Requests a resource
2) Use the resource
2) Releases the resource
Deadlock is a situation where a set of processes are blocked because each process is holding a
resource and waiting for another resource acquired by some other process.
Consider an example when two trains are coming toward each other on the same track and there is
only one track, none of the trains can move once they are in front of each other. A similar situation
occurs in operating systems when there are two or more processes that hold some resources and
wait for resources held by other(s). For example, in the below diagram, Process 1 is holding
Resource 1 and waiting for resource 2 which is acquired by process 2, and process 2 is waiting for
resource 1.

Deadlock can arise if the following four conditions hold simultaneously (Necessary Conditions)
Mutual Exclusion: One or more than one resource are non-shareable (Only one process can use at a
time)
Hold and Wait: A process is holding at least one resource and waiting for resources.
No Preemption: A resource cannot be taken from a process unless the process releases the resource.
Circular Wait: A set of processes are waiting for each other in circular form.

Methods for handling deadlock


There are three ways to handle deadlock
1) Deadlock prevention or avoidance: The idea is to not let the system into a deadlock state.
One can zoom into each category individually, Prevention is done by negating one of above
mentioned necessary conditions for deadlock.
Avoidance is kind of futuristic in nature. By using strategy of “Avoidance”, we have to make an
assumption. We need to ensure that all information about resources which process will need are
known to us prior to execution of the process. We use Banker’s algorithm (Which is in-turn a gift
from Dijkstra) in order to avoid deadlock.
2) Deadlock detection and recovery: Let deadlock occur, then do preemption to handle it once
occurred.

Resource Allocation Graph:


The resource allocation graph is the pictorial representation of the state of a system. As its name
suggests, the resource allocation graph is the complete information about all the processes which
are holding some resources or waiting for some resources.
It also contains the information about all the instances of all the resources whether they are
available or being used by the processes.
In Resource allocation graph, the process is represented by a Circle while the Resource is
represented by a rectangle. Let's see the types of vertices and edges in detail.

Vertices are mainly of two types, Resource and process. Each of them will be represented by a
different shape. Circle represents process while rectangle represents resource.
A resource can have more than one instance. Each instance will be represented by a dot inside the
rectangle.
Edges in RAG are also of two types, one represents assignment and other represents the wait of a
process for a resource. The above image shows each of them.
A resource is shown as assigned to a process if the tail of the arrow is attached to an instance to the
resource and the head is attached to a process.
A process is shown as waiting for a resource if the tail of an arrow is attached to the process while
the head is pointing
towards the
resource.
Example:
Let's consider 3 processes P1, P2 and P3, and two types of resources R1 and R2. The resources are
having 1 instance each.
According to the graph, R1 is being used by P1, P2 is holding R2 and waiting for R1, P3 is waiting
for R1 as well as R2.
The graph is deadlock free since no cycle is being formed in the graph.

Deadlock Prevention

Recall the four necessary and sufficient conditions for deadlock


Mutual exclusion
Hold and wait
No preemption
Circular wait
Deadlock prevention is achieved by ensuring that at least one condition cannot occur.

1. Mutual Exclusion
Mutual section from the resource point of view is the fact that a resource can never be used by more
than one process simultaneously which is fair enough but that is the main reason behind the
deadlock. If a resource could have been used by more than one process at the same time then the
process would have never been waiting for any resource.
However, if we can be able to violate resources behaving in the mutually exclusive manner then the
deadlock can be prevented. we cannot violate mutual exclusion for a process practically.
2. Hold and Wait
Hold and wait condition lies when a process holds a resource and waiting for some other resource to
complete its task. Deadlock occurs because there can be more than one process which are holding
one resource and waiting for other in the cyclic order.
However, we have to find out some mechanism by which a process either doesn't hold any resource
or doesn't wait. That means, a process must be assigned all the necessary resources before the
execution starts. A process must not wait for any resource once the execution has been started.
!(Hold and wait) = !hold or !wait (negation of hold and wait is, either you don't hold or you
don't wait)
This can be implemented practically if a process declares all the resources initially. However, this
sounds very practical but can't be done in the computer system because a process can't determine
necessary resources initially.
Process is the set of instructions which are executed by the CPU. Each of the instruction may
demand multiple resources at the multiple times. The need cannot be fixed by the OS.
The problem with the approach is:
1. Practically not possible.
2. Possibility of getting starved will be increases due to the fact that some process may
hold a resource for a very long time.

3. No Preemption
Deadlock arises due to the fact that a process can't be stopped once it starts. However, if we take the
resource away from the process which is causing deadlock then we can prevent deadlock.
This is not a good approach at all since if we take a resource away which is being used by the
process then all the work which it has done till now can become inconsistent.
Consider a printer is being used by any process. If we take the printer away from that process and
assign it to some other process then all the data which has been printed can become inconsistent and
ineffective and also the fact that the process can't start printing again from where it has left which
causes performance inefficiency.
4. Circular Wait
To violate circular wait, we can assign a priority number to each of the resource. A process can't
request for a lesser priority resource. This ensures that not a single process can request a resource
which is being utilized by some other process and no cycle will be formed.
Among all the methods, violating Circular wait is the only approach that can be implemented
practically.

Deadlock Avoidance:
Deadlock prevention may result in low device utilization and reduced system throughput.
An alternative method for avoiding deadlocks is to require additional information about how
resources are to be requested.With this knowledge of the complete sequence of requests and
releases for each process, the system can decide for each request whether or not the process should
wait in order to avoid a possible future deadlock. Each request requires that in making this decision
the system consider the resources currently available, the resources currently allocated to each
process and the future requests and releases of each process.
Safe State: A state is safe if the system can allocate resources to each process (up to its maximum)
in some order and still avoid a deadlock. More formally, a system is in a safe state only if there
exists a safe sequence. A sequence of processes < P1, P 2 , ... , Pi> is a safe sequence for the current
allocation state it for each Pi, the resource requests that Pi can still can be satisfied by the currently
available resources plus the resources held by all Pj, with j < i. In this situation,if the resources that
Pi needs are not immediately available, then Pi can wait until all Pj have finished. When they have
finished, Pi can obtain all of its needed resources, complete its designated task, return its allocated
resources, and terminate.
When Pi terminates, Pi +1 can obtain its needed resources, and so on. If no such sequence exists,
then the system state is said to be unsafe.A safe state is not a deadlocked state. Conversely, a
deadlocked state is an unsafe state. Not all unsafe states are deadlocks, however An unsafe state
MAY lead to a deadlock.
Example:
we consider a system with 12 magnetic tape drives and three processes: Po, P1, and P 2 . Process P0
requires 10 tape drives, process P1 may need as many as 4 tape drives, and process P 2 may need up
to 9 tape drives. Suppose that, at time t0, process P0 is holding 5 tape drives, process P1 is holding
2 tape drives, and process P2 is holding 2 tape drives. (Thus, there are 3 free tape drives.)

Maximum Needs Current Needs


P0 10 5
P1 4 2
P2 9 2

At time to, the system is in a safe state. The sequence < P1, P0, P 2 > satisfies the safety condition.
Process P1 can immediatly be allocated all its tape drives and then return them (the system will then
have 5 available tape drives); then process Po can get all its tape drives and return them (the system
will then have 10 available tape drives); and finally process P 2 can get all its tape drives and return
thenl (the system will then have all 12 tape drives available). A system can go from a safe state to
an unsafe state. Suppose th.at, at time h, process P2 requests and is allocated one more tape drive.
The system is no longer in a safe state. At this point, only process P j can be al10cated all its tape
drives. When it returns them, the system will have only 4 available tape drives. Since process Po is
allocated 5 tape drives but has a maximum of 10, it may request 5 more tape drives. Since they are
unavailable, process Po must wait.

Similarly, process P 2 may request an additional 6 tape drives and have to wait, resulting in a
deadlock. Our mistake was in granting the request from process P2 for one more tape drive. If we
had made P2 wait until either of the processes had finished and released its resources, then we
could have avoided the deadlock.
Resource-Allocation Graph Algorithm:
•If resource categories have only single instances of their resources, then deadlock states can
be detected by cycles in the resource-allocation graphs.

•In this case, unsafe states can be recognized and avoided by augmenting the resource-
allocation graph with claim edges, noted by dashed lines, which point from a process to a
resource that it may request in the future.

•In order for this technique to work, all claim edges must be added to the graph for any
particular process before that process is allowed to request any resources. (Alternatively,
processes may only make requests for resources for which they have already established
claim edges, and claim edges cannot be added to any process that is currently holding
resources.)

•When a process makes a request, the claim edge Pi->Rj is converted to a request edge.
Similarly when a resource is released, the assignment reverts back to a claim edge.

•This approach works by denying requests that would produce cycles in the resource-
allocation graph, taking claim edges into effect.

•Consider for example what happens when process P2 requests resource R2:

Figure 7.7 - Resource allocation graph for deadlock avoidance

•The resulting resource-allocation graph would have a cycle in it, and so the request cannot
be granted.
Figure 7.8 - An unsafe state in a resource allocation graph

Banker’s Algorithm:
It is a banker algorithm used to avoid deadlock and allocate resources safely to each process in the
computer system. The 'S-State' examines all possible tests or activities before deciding whether the
allocation should be allowed to each process. It also helps the operating system to successfully
share the resources between all the processes. The banker's algorithm is named because it checks
whether a person should be sanctioned a loan amount or not to help the bank system safely simulate
allocation resources.
Banker’s algorithm is named so because it is used in banking system to check whether loan can be
sanctioned to a person or not. Suppose there are n number of account holders in a bank and the total
sum of their money is S. If a person applies for a loan then the bank first subtracts the loan amount
from the total money that bank has and if the remaining amount is greater than S then only the loan
is sanctioned. It is done because if all the account holders comes to withdraw their money then the
bank can easily do it.
In other words, the bank would never allocate its money in such a way that it can no longer satisfy
the needs of all its customers. The bank would try to be in safe state always.
Similarly, it works in an operating system. When a new process is created in a computer system, the
process must provide all types of information to the operating system like upcoming processes,
requests for their resources, counting them, and delays. Based on these criteria, the operating system
decides which process sequence should be executed or waited so that no deadlock occurs in a
system. Therefore, it is also known as deadlock avoidance algorithm.
Following are the important data structures terms applied in the banker's algorithm as follows:
Suppose n is the number of processes, and m is the number of each type of resource used in a
computer system.
1.Available: It is an array of length 'm' that defines each type of resource available in the
system. When Available[j] = K, means that 'K' instances of Resources type R[j] are available
in the system.
2.Max: It is a [n x m] matrix that indicates each process P[i] can store the maximum number
of resources R[j] (each type) in a system.
3.Allocation: It is a matrix of m x n orders that indicates the type of resources currently
allocated to each process in the system. When Allocation [i, j] = K, it means that process
P[i] is currently allocated K instances of Resources type R[j] in the system.
4.Need: It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each
process.Need [ i, j ] = k means process Pi currently need ‘k’ instances of resource type Rj
for its execution.
Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
5.Finish: It is the vector of the order m. It includes a Boolean value (true/false) indicating
whether the process has been allocated to the requested resources, and all resources have
been released after finishing its task.
The Banker's Algorithm is the combination of the safety algorithm and the resource request
algorithm to control the processes and avoid deadlock in a system:
Safety Algorithm:
It is a safety algorithm used to check whether or not a system is in a safe state or follows the safe
sequence in a banker's algorithm:
1. There are two vectors Work and Finish of length m and n in a safety algorithm.
Initialize: Work = Available
Finish[i] = false; for I = 0, 1, 2, 3, 4… n - 1.
2. Check the availability status for each type of resources [i], such as:
Need[i] <= Work
Finish[i] == false
If the i does not exist, go to step 4.
3. Work = Work +Allocation(i) // to get new resource allocation
Finish[i] = true
Go to step 2 to check the status of resource availability for the next process.
4. If Finish[i] == true; it means that the system is safe for all processes.
Resource Request Algorithm
A resource request algorithm checks how a system will behave when a process makes each type of
resource request in a system as a request matrix.
Let create a resource request array R[i] for each process P[i]. If the Resource Requesti [j] equal to
'K', which means the process P[i] requires 'k' instances of Resources type R[j] in the system.
1. When the number of requested resources of each type is less than the Need resources, go to step 2
and if the condition fails, which means that the process P[i] exceeds its maximum claim for the
resource. As the expression suggests:
If Request(i) <= Need
Go to step 2;
2. And when the number of requested resources of each type is less than the available resource for
each process, go to step (3). As the expression suggests:
If Request(i) <= Available
Else Process P[i] must wait for the resource since it is not available for use.
3. When the requested resource is allocated to the process by changing state:
Available = Available - Request
Allocation(i) = Allocation(i) + Request (i)
Needi = Needi- Requesti

When the resource allocation state is safe, its resources are allocated to the process P(i). And if the
new state is unsafe, the Process P (i) has to wait for each type of Request R(i) and restore the old
resource-allocation state.
Example: Consider a system that contains five processes P1, P2, P3, P4, P5 and the three resource
types A, B and C. Following are the resources types: A has 10, B has 5 and the resource type C has
7 instances.
Allocation Max Available
Process
A B C A B C A B C

P1 0 1 0 7 5 3 3 3 2

P2 2 0 0 3 2 2

P3 3 0 2 9 0 2

P4 2 1 1 2 2 2

P5 0 0 2 4 3 3

Answer the following questions using the banker's algorithm:


1.What is the reference of the need matrix?
2.Determine if the system is safe or not.
3.What will happen if the resource request (1, 0, 0) for process P1 can the system accept this
request immediately?
Ans. 2:Context of the need matrix is as follows:
Need [i] = Max [i] - Allocation [i]
Need for P1: (7, 5, 3) - (0, 1, 0) = 7, 4, 3
Need for P2: (3, 2, 2) - (2, 0, 0) = 1, 2, 2
Need for P3: (9, 0, 2) - (3, 0, 2) = 6, 0, 0
Need for P4: (2, 2, 2) - (2, 1, 1) = 0, 1, 1
Need for P5: (4, 3, 3) - (0, 0, 2) = 4, 3, 1
Need
Process
A B C

P1 7 4 3

P2 1 2 2

P3 6 0 0

P4 0 1 1

P5 4 3 1

Hence, we created the context of need matrix.


Ans. 2: Apply the Banker's Algorithm:
Available Resources of A, B and C are 3, 3, and 2.
Now we check if each type of resource request is available for each process.
Step 1: For Process P1:
Need <= Available
7, 4, 3 <= 3, 3, 2 condition is false.
So, we examine another process, P2.
Step 2: For Process P2:
Need <= Available
1, 2, 2 <= 3, 3, 2 condition true
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2
Similarly, we examine another process P3.
Step 3: For Process P3:
P3 Need <= Available
6, 0, 0 < = 5, 3, 2 condition is false.
Similarly, we examine another process, P4.
Step 4: For Process P4:
P4 Need <= Available
0, 1, 1 <= 5, 3, 2 condition is true
New Available resource = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3
Similarly, we examine another process P5.
Step 5: For Process P5:
P5 Need <= Available
4, 3, 1 <= 7, 4, 3 condition is true
New available resource = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5
Now, we again examine each type of resource request for processes P1 and P3.
Step 6: For Process P1:
P1 Need <= Available
7, 4, 3 <= 7, 4, 5 condition is true
New Available Resource = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5
So, we examine another process P2.
Step 7: For Process P3:
P3 Need <= Available
6, 0, 0 <= 7, 5, 5 condition is true
New Available Resource = Available + Allocation
7, 5, 5 + 3, 0, 2 => 10, 5, 7
Hence, we execute the banker's algorithm to find the safe state and the safe sequence like P2,
P4, P5, P1 and P3.
Ans. 3: For granting the Request (1, 0, 2), first we have to check that Request <= Available, that is
(1, 0, 2) <= (3, 3, 2), since the condition is true. So the process P1 gets the request immediately.
Deadlock Detection & Recovery:
Deadlock Detection
1.If resources have single instance:
In this case for Deadlock detection we can run an algorithm to check for cycle in the Resource
Allocation Graph. Presence of cycle in the graph is the sufficient condition for deadlock.

In the above diagram, resource 1 and resource 2 have single instances. There is a cycle R1 →
P1 → R2 → P2. So, Deadlock is Confirmed.

2.If there are multiple instances of resources:


Detection of the cycle is necessary but not sufficient condition for deadlock detection, in this
case, the system may or may not be in deadlock varies according to different situations.
Deadlock Recovery
A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is time
and space consuming process. Real-time operating systems use Deadlock recovery.
Recovery method
1.Killing the process: killing all the process involved in the deadlock. Killing process one by
one. After killing each process check for deadlock again keep repeating the process till system
recover from deadlock.
2.Resource Preemption: Resources are preempted from the processes involved in the
deadlock, preempted resources are allocated to other processes so that there is a possibility of
recovering the system from deadlock. In this case, the system goes into starvation.

Protection
Protection is the mechanism of controlling access to resources for programs, processes and users.
Goals of Protection:
•Obviously to prevent malicious misuse of the system by users or programs.
•To ensure that each shared resource is used only in accordance with system policies, which
may be set either by system designers or by system administrators.
•To ensure that errant programs cause the minimal amount of damage possible.
•Note that protection systems only provide the mechanisms for enforcing policies and
ensuring reliable systems. It is up to administrators and users to implement those
mechanisms effectively.
Domain of Protection:
• A computer can be viewed as a collection of processes and objects ( both HW & SW ).
• The need to know principle states that a process should only have access to those objects it
needs to accomplish its task, and furthermore only in the modes for which it needs access
and only during the time frame when it needs access.
• The modes available for a particular object may depend upon its type.
14.3.1 Domain Structure
•A protection domain specifies the resources that a process may access.
•Each domain defines a set of objects and the types of operations that may be
invoked on each object.
•An access right is the ability to execute an operation on an object.
•A domain is defined as a set of < object, { access right set } > pairs, as shown
below. Note that some domains may be disjoint while others overlap.

Figure 14.1 - System with three protection domains.


•The association between a process and a domain may be static or dynamic.
•If the association is static, then the need-to-know principle requires a
way of changing the contents of the domain dynamically.
•If the association is dynamic, then there needs to be a mechanism for
domain switching.
•Domains may be realized in different fashions - as users, or as processes, or as
procedures. E.g. if each user corresponds to a domain, then that domain defines
the access of that user, and changing domains involves changing user ID.

Access Matrix
•The model of protection that we have been discussing can be viewed as an access matrix, in
which columns represent different system resources and rows represent different protection
domains. Entries within the matrix indicate what access that domain has to that resource.
Figure 14.3 - Access matrix.

•Domain switching can be easily supported under this model, simply by providing "switch"
access to other domains:

Figure 14.4 - Access matrix of Figure 14.3 with domains as objects.

•The ability to copy rights is denoted by an asterisk, indicating that processes in that domain
have the right to copy that access within the same column, i.e. for the same object. There are
two important variations:
•If the asterisk is removed from the original access right, then the right is transferred,
rather than being copied. This may be termed a transfer right as opposed to a copy
right.
•If only the right and not the asterisk is copied, then the access right is added to the
new domain, but it may not be propagated further. That is the new domain does not
also receive the right to copy the access. This may be termed a limited copy right, as
shown in Figure 14.5 below:
Figure 14.5 - Access matrix with copy rights.

•The owner right adds the privilege of adding new rights or removing existing ones:

Figure 14.6 - Access matrix with owner rights.


•Copy and owner rights only allow the modification of rights within a column. The addition
of control rights, which only apply to domain objects, allow a process operating in one
domain to affect the rights available in other domains. For example in the table below, a
process operating in domain D2 has the right to control any of the rights in domain D4.

Modified access matrix

Magnetic Disks
•Traditional magnetic disks have the following basic structure:
•One or more platters in the form of disks covered with magnetic media. Hard disk
platters are made of rigid metal, while "floppy" disks are made of more flexible
plastic.
•Each platter has two working surfaces. Older hard disk drives would sometimes not
use the very top or bottom surface of a stack of platters, as these surfaces were more
susceptible to potential damage.
•Each working surface is divided into a number of concentric rings called tracks. The
collection of all tracks that are the same distance from the edge of the platter, ( i.e. all
tracks immediately above one another in the following diagram ) is called a cylinder.
•Each track is further divided into sectors, traditionally containing 512 bytes of data
each, although some modern disks occasionally use larger sector sizes.
•The data on a hard drive is read by read-write heads.
•The standard configuration ( shown below ) uses one head per surface, each on a
separate arm, and controlled by a common arm assembly which moves all heads
simultaneously from one cylinder to another. ( Other configurations, including
independent read-write heads, may speed up disk access, but involve serious
technical difficulties. )
•The storage capacity of a traditional disk drive is equal to the number of heads ( i.e.
the number of working surfaces ), times the number of tracks per surface, times the
number of sectors per track, times the number of bytes per sector. A particular
physical block of data is specified by providing the head-sector-cylinder number at
which it is located.

Figure 10.1 - Moving-head disk mechanism.

•In operation the disk rotates at high speed, such as 7200 rpm ( 120 revolutions per second. )
The rate at which data can be transferred from the disk to the computer is composed of
several steps:
•The positioning time, a.k.a. the seek time or random access time is the time required
to move the heads from one cylinder to another, and for the heads to settle down after
the move. This is typically the slowest step in the process.
•The rotational latency is the amount of time required for the desired sector to rotate
around and come under the read-write head.This can range anywhere from zero to
one full revolution, and on the average will equal one-half revolution. This is another
physical step and is usually the second slowest step behind seek time.
•The transfer rate, which is the time required to move the data electronically from the
disk to the computer. ( Some authors may also use the term transfer rate to refer to
the overall transfer rate, including seek time and rotational latency as well as the
electronic data transfer rate. )
•Disk heads "fly" over the surface on a very thin cushion of air. If they should accidentally
contact the disk, then a head crash occurs, which may or may not permanently damage the
disk or even destroy it completely. For this reason it is normal to park the disk heads when
turning a computer off, which means to move the heads off the disk or to an area of the disk
where there is no data stored.
•Floppy disks are normally removable. Hard drives can also be removable, and some are
even hot-swappable, meaning they can be removed while the computer is running, and a
new hard drive inserted in their place.
•Disk drives are connected to the computer via a cable known as the I/O Bus. Some of the
common interface formats include Enhanced Integrated Drive Electronics, EIDE; Advanced
Technology Attachment, ATA; Serial ATA, SATA, Universal Serial Bus, USB; Fiber
Channel, FC, and Small Computer Systems Interface, SCSI.

Disk scheduling:
Disk scheduling is done by operating systems to schedule I/O requests arriving for the disk. Disk
scheduling is also known as I/O scheduling.
Disk scheduling is important because:

• Multiple I/O requests may arrive by different processes and only one I/O request can be
served at a time by the disk controller. Thus other I/O requests need to wait in the waiting
queue and need to be scheduled.
• Two or more request may be far from each other so can result in greater disk arm movement.
• Hard drives are one of the slowest parts of the computer system and thus need to be accessed
in an efficient manner.
There are many Disk Scheduling Algorithms but before discussing them let’s have a quick look at
some of the important terms:

• Seek Time:Seek time is the time taken to locate the disk arm to a specified track where the
data is to be read or write. So the disk scheduling algorithm that gives minimum average seek
time is better.
• Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to
rotate into a position so that it can access the read/write heads. So the disk scheduling
algorithm that gives minimum rotational latency is better.
• Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating speed
of the disk and number of bytes to be transferred.
• Disk Access Time: Disk Access Time = Seek Time + Rotational Latency + Transfer Time
Disk Response Time: Response Time is the average of time spent by a request waiting to perform
its I/O operation. Average Response time is the response time of the all requests. Variance
Response Time is measure of how individual request are serviced with respect to average response
time. So the disk scheduling algorithm that gives minimum variance response time is better.
Disk Scheduling Algorithms

1.FCFS: FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests
are addressed in the order they arrive in the disk queue.Let us understand this with the help of
an example.
Example:

Suppose the order of request is- (82,170,43,140,24,16,190)


And current position of Read/Write head is : 50
So, total seek time
=(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16)
=642

Advantages:

•Every request gets a fair chance


•No indefinite postponement
Disadvantages:

•Does not try to optimize seek time


•May not provide the best possible service
2.SSTF: In SSTF (Shortest Seek Time First), requests having shortest seek time are executed
first. So, the seek time of every request is calculated in advance in the queue and then they are
scheduled according to their calculated seek time. As a result, the request near the disk arm
will get executed first. SSTF is certainly an improvement over FCFS as it decreases the
average response time and increases the throughput of system.Let us understand this with the
help of an example.
Example:Suppose the order of request is- (82,170,43,140,24,16,190)

And current position of Read/Write head is : 50

So, total seek time:


=(50-43)+(43-24)+(24-16)+(82-16)+(140-82)+(170-40)+(190-170)
=208

Advantages:

•Average Response Time decreases


•Throughput increases
Disadvantages:

•Overhead to calculate seek time in advance


•Can cause Starvation for a request if it has higher seek time as compared to incoming
requests
•High variance of response time as SSTF favours only some requests.

SCAN:In SCAN algorithm the disk arm moves into a particular direction and services the requests
coming in its path and after reaching the end of disk, it reverses its direction and again services the
request arriving in its path. So, this algorithm works as an elevator and hence also known as
elevator algorithm. As a result, the requests at the midrange are serviced more and those arriving
behind the disk arm will have to wait.

Example:

Input:

Request sequence = {176, 79, 34, 60, 92, 11, 41, 114}
Initial head position = 50
Direction = left (We are moving from right to left)
Therefore, the total seek count is calculated as:
= (50-41)+(41-34)+(34-11)+(11-0)+(60-0)+(79-60)+(92-79)+(114-92)+(176-114) = 226

Advantages:

High throughput
•Low variance of response time
•Average response time
Disadvantages:

•Long waiting time for requests for locations just visited by disk arm
CSCAN: In SCAN algorithm, the disk arm again scans the path that has been scanned, after
reversing its direction. So, it may be possible that too many requests are waiting at the other end or
there may be zero or few requests pending at the scanned area.
These situations are avoided in CSCAN algorithm in which the disk arm instead of reversing its
direction goes to the other end of the disk and starts servicing the requests from there. So, the disk
arm moves in a circular fashion and this algorithm is also similar to SCAN algorithm and hence it is
known as C-SCAN (Circular SCAN).
Example:

Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at


50, and it is also given that the disk arm should move “towards the larger value”.
Seek time is calculated as:

=(199-50)+(199-0)+(43-0)
=391

Advantages –

1.C-SCAN Algorithm is the successor and the improved version of the SCAN scheduling
Algorithm.
2.The Head move from one end to the other of the disk while serving all the requests in
between.
3.The waiting time for the cylinders which were just visited by the head is reduced in C-
SCAN compared to the SCAN Algorithm.
4.Uniform waiting time is provided.
5.Better response time is provided.

Disadvantages –
1.More seek movements are caused in C-SCAN compared to SCAN Algorithm.
2.In C-SCAN even if there are no requests left to be serviced the Head will still travel to the
end of the disk unlike SCAN algorithm.
LOOK: It is similar to the SCAN disk scheduling algorithm except for the difference that the disk
arm in spite of going to the end of the disk goes only to the last request to be serviced in front of the
head and then reverses its direction from there only. Thus it prevents the extra delay which occurred
due to unnecessary traversal to the end of the disk.
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”.
So, the
seek
time is
calculat
ed as:
=(190-50)+(190-16)
=314

Advantages –

1.If there are no requests left to be services the Head will not move to the end of the disk
unlike SCAN algorithm.
2.Better performance is provided compared to SCAN Algorithm.
3.Starvation is avoided in LOOK scheduling algorithm.
4.Low variance is provided in waiting time and response time.
Disadvantages –
1.Overhead of finding the end requests is present.
2.Cylinders which are just visited by Head have to wait for long time.

CLOOK: As LOOK is similar to SCAN algorithm, in similar way, CLOOK is similar to CSCAN
disk scheduling algorithm. In CLOOK, the disk arm in spite of going to the end goes only to the last
request to be serviced in front of the head and then from there goes to the other end’s last request.
Thus, it also prevents the extra delay which occurred due to unnecessary traversal to the end of the
disk.

Example:

Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at


50, and it is also given that the disk arm should move “towards the larger value”
So, the seek time is calculated as:

=(190-50)+(190-16)+(43-16)
=341

Advantages –

1.In C-LOOK the head does not have to move till the end of the disk if there are no requests to
be serviced.
2.There is less waiting time for the cylinders which are just visited by the head in C-LOOK.
3.C-LOOK provides better performance when compared to LOOK Algorithm.
4.Starvation is avoided in C-LOOK.
5.Low variance is provided in waiting time and response time.

Disadvantages –
1.In C-LOOK an overhead of finding the end requests is present.

LOOK: It is similar to the SCAN disk scheduling algorithm except for the difference that the disk
arm in spite of going to the end of the disk goes only to the last request to be serviced in front of the
head and then reverses its direction from there only. Thus it prevents the extra delay which occurred
due to unnecessary traversal to the end of the disk.
Example:
Suppose the requests to be addressed are-82,170,43,142,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”.
So,
the
see
k
tim
e is
calc
ulat
ed
as:

=(190-50)+(190-16)
=314

Advantages –

1.If there are no requests left to be services the Head will not move to the end of the disk
unlike SCAN algorithm.
2.Better performance is provided compared to SCAN Algorithm.
3.Starvation is avoided in LOOK scheduling algorithm.
4.Low variance is provided in waiting time and response time.
Disadvantages –
1.Overhead of finding the end requests is present.
2.Cylinders which are just visited by Head have to wait for long time.

CLOOK: As LOOK is similar to SCAN algorithm, in similar way, CLOOK is similar to CSCAN
disk scheduling algorithm. In CLOOK, the disk arm in spite of going to the end goes only to the last
request to be serviced in front of the head and then from there goes to the other end’s last request.
Thus, it also prevents the extra delay which occurred due to unnecessary traversal to the end of the
disk.

Example:

Suppose the requests to be addressed are-82,170,43,142,24,16,190. And the Read/Write arm is at


50, and it is also given that the disk arm should move “towards the larger value”
So, the seek time is calculated as:

=(190-50)+(190-16)+(43-16)
=341

Advantages –

1.In C-LOOK the head does not have to move till the end of the disk if there are no requests to
be serviced.
2.There is less waiting time for the cylinders which are just visited by the head in C-LOOK.
3.C-LOOK provides better performance when compared to LOOK Algorithm.
4.Starvation is avoided in C-LOOK.
5.Low variance is provided in waiting time and response time.

Disadvantages –
1.In C-LOOK an overhead of finding the end requests is present.

Disk Management

Disk Formatting:
• disk formatting deletes data and removes all the programs installed with in the drive. So it
can be done with caution. We must have the backup of all the data and applications which
we require. No-doubt disk formatting requires time. But the frequent formatting of the disk
decreases the life of the hard-drive.
Figure – Formatting process of disk
1. Low-level Formatting :
Low level formatting is a type of physical formatting. It is the process of marking of cylinders and
tracks of the blank hard-disk. After this there is the division of tracks into sectors with the sector
markers. Now-a-days low-level formatting is performed by the hard-disk manufactures themselves.
2. Partitioning :
As suggesting from the name, partitioning means divisions. Partitioning is the process of dividing
the hard-disk into one or more regions. The regions are called as partitions.
It can be performed by the users and it will affect the disk performance.

3. High-level Formatting :
High-level formatting is the process of writing. Writing on a file system, cluster size, partition label,
and so on for a newly created partition or volume. It is done to erase the hard-disk and again
installing the operating system on the disk-drive

Boot Block:

• Computer ROM contains a bootstrap program ( OS independent ) with just enough code to
find the first sector on the first hard drive, load that sector into memory, and transfer control
over to it.
• The first sector on the hard drive is known as the Master Boot Record, MBR, and contains a
very small amount of code in addition to the partition table. The partition table documents
how the disk is partitioned into logical disks, and indicates specifically which partition is the
active or boot partition.
• The boot program then looks to the active partition to find an operating system, possibly
loading up a slightly larger / more advanced boot program along the way.
• In a dual-boot ( or larger multi-boot ) system, the user may be given a choice of which
operating system to boot, with a default action to be taken in the event of no response within
some time frame.
• Once the kernel is found by the boot program, it is loaded into memory and then control is
transferred over to the OS. The kernel will normally continue the boot process by initializing
all important kernel data structures, launching important system services.

Figure 10.9 - Booting from disk in Windows 2000.

Bad Blocks:
• No disk can be manufactured to 100% perfection, and all physical objects wear out over
time. For these reasons all disks are shipped with a few bad blocks, and additional blocks
can be expected to go bad slowly over time. If a large number of blocks go bad then the
entire disk will need to be replaced, but a few here and there can be handled through other
means.
• In the old days, bad blocks had to be checked for manually. Formatting of the disk or
running certain disk-analysis tools would identify bad blocks, and attempt to read the data
off of them one last time through repeated tries. Then the bad blocks would be mapped out
and taken out of future service. Sometimes the data could be recovered, and sometimes it
was lost forever.
• Modern disk controllers make much better use of the error-correcting codes, so that bad
blocks can be detected earlier and the data usually recovered.

• Note that re-mapping of sectors from their normal linear progression can throw off the disk
scheduling optimization of the OS, especially if the replacement sector is physically far
away from the sector it is replacing. For this reason most disks normally keep a few spare
sectors on each cylinder, as well as at least one spare cylinder. Whenever possible a bad
sector will be mapped to another sector on the same cylinder, or at least a cylinder as close
as possible. Sector slipping may also be performed, in which all sectors between the bad
sector and the replacement sector are moved down by one, so that the linear progression of
sector numbers can be maintained.
• If the data on a bad block cannot be recovered, then a hard error has occurred., which
requires replacing the file(s) from backups, or rebuilding them from scratch.

Redundant Array of Independent Disks


RAID or Redundant Array of Independent Disks, is a technology to connect multiple secondary
storage devices and use them as a single storage media.
RAID consists of an array of disks in which multiple disks are connected together to achieve
different goals. RAID levels define the use of disk arrays.
RAID 0

In this level, a striped array of disks is implemented. The data is broken down into blocks and the
blocks are distributed among disks. Each disk receives a block of data to write/read in parallel. It
enhances the speed and performance of the storage device. RAID 0 does not provide any kind of
redundancy. Which means even if one drive fails, your data is at risk. There is no parity and backup
in Level 0.

RAID 1

RAID 1 uses mirroring techniques. When data is sent to a RAID controller, it sends a copy of data
to all the disks in the array. RAID level 1 is also called mirroring and provides 100% redundancy
in case of a failure.

RAID 2

RAID 2 records Error Correction Code using Hamming distance for its data, striped on different
disks. Like level 0, each data bit in a word is recorded on a separate disk and ECC codes of the data
words are stored on a different set disks. Due to its complex structure and high cost, RAID 2 is not
commercially available.
RAID 3

RAID 3 stripes the data onto multiple disks. The parity (dedicated) bit generated for data word is
stored on a different disk. Parity makes use of a very famous mathematical binary operation called
as “XOR”.
XOR is a mathematical operation that's done to produce one output from two input. Some examples
of XOR operations are as below.

1'st operator 2'nd operator XOR OUPUT


1 1 0
1 0 1
0 1 1
0 0 0

You can simply make a rule while performing XOR binary operation, that if there is a difference in
the operator then the XOR output is 1.
This technique makes it to overcome single disk failures.

RAID 4

In this level, an entire block of data is written onto data disks and then the parity(Distributed) is
generated and stored on a different disk. Note that level 3 uses byte-level striping, whereas level 4
uses block-level striping. Both level 3 and level 4 require at least three disks to implement RAID.
RAID 5

RAID 5 writes whole data blocks onto different disks, but the parity bits generated for data block
stripe are distributed among all the data disks rather than storing them on a different dedicated disk.

RAID 6

RAID 6 is an extension of level 5. In this level, two independent parities are generated and stored in
distributed fashion among multiple disks. Two parities provide additional fault tolerance. This level
requires at least four disk drives to implement RAID.

RAID Level 0 + 1. RAID level 0 + 1 refers to a combination of RAID levels o and 1. RAID 0
provides the performance, while RAID 1 provides the reliability.
RAID level 0 + 1 disks are first striped, and then the striped disks mirrored to another set. This level
generally provides better performance.
RAID level 1 + 0 mirrors disks in pairs, and then stripes the mirrored pairs. The storage capacity,
performance, etc. are all the same, but there is an advantage to this approach in the event of multiple
disk failures, as illustrated below:
In diagram (a) below, the 8 disks have been divided into two sets of four, each of which is striped,
and then one stripe set is used to mirror the other set.
If a single disk fails, it wipes out the entire stripe set, but the system can keep on functioning using
the remaining set.
In diagram (b), the same 8 disks are divided into four sets of two, each of which is mirrored, and
then the file system is striped across the four sets of mirrored disks.
If a single disk fails, then that mirror set is reduced to a single disk, but the system rolls on, and the
other three mirror sets continue mirroring.

Stable-Storage Implementation:
•The concept of stable storage ( first presented in chapter 6 ) involves a storage medium in
which data is never lost, even in the face of equipment failure in the middle of a write
operation.
•To implement this requires two ( or more ) copies of the data, with separate failure modes.
•An attempted disk write results in one of three possible outcomes:
1.The data is successfully and completely written.
2.The data is partially written, but not completely. The last block written may be
garbled.
3.No writing takes place at all.
•Whenever an equipment failure occurs during a write, the system must detect it, and return
the system back to a consistent state. To do this requires two physical blocks for every
logical block, and the following procedure:
1.Write the data to the first physical block.
2.After step 1 had completed, then write the data to the second physical block.
3.Declare the operation complete only after both physical writes have completed
successfully.
•During recovery the pair of blocks is examined.
1.If both blocks are identical and there is no sign of damage, then no further action is
necessary.
2.If one block contains a detectable error but the other does not, then the damaged
block is replaced with the good copy. ( This will either undo the operation or
complete the operation, depending on which block is damaged and which is
undamaged. )
3.If neither block shows damage but the data in the blocks differ, then replace the
data in the first block with the data in the second block. ( Undo the operation. )

I/O Hardware
•I/O devices can be roughly categorized as storage, communications, user-interface, and
other
•Devices communicate with the computer via signals sent over wires or through the air.
•Devices connect with the computer via ports, e.g. a serial or parallel port.
•A common set of wires connecting multiple devices is termed a bus.Buses include rigid
protocols for the types of messages that can be sent across the bus and the procedures for
resolving contention issues.
•Below are the three of the four bus types commonly found in a modern PC:The PCI bus
connects high-speed high-bandwidth devices to the memory subsystem ( and the CPU. )
1.The expansion bus connects slower low-bandwidth devices, which typically
deliver data one character at a time.
2.The SCSI(Small Computer System Interface) bus connects a number of
SCSI devices(printer,scanner,hard drive) to a common SCSI controller.
3.A daisy-chain bus, is when a string of devices is connected to each other
like beads on a chain, and only one of the devices is directly connected to the
host.
A typical PC bus structure

•One way of communicating with devices is through registers associated with each port.
Registers may be one to four bytes in size, and may typically include the following four:
1.The data-in register is read by the host to get input from the device.
2.The data-out register is written by the host to send output.
3.The status register has bits read by the host to ascertain the status of the device,
such as idle, ready for input, busy, error, transaction complete, etc.
4.The control register has bits written by the host to issue commands or to change
settings of the device such as parity checking, word length, or full- versus half-
duplex operation.
Polling:
•One simple means of device handshaking involves polling:
1.The host repeatedly checks the busy bit on the device until it becomes clear.
2.The host writes a byte of data into the data-out register, and sets the write bit in the
command register.
3.The host sets the command ready bit in the command register to notify the device
of the pending command.
4.When the device controller sees the command-ready bit set, it first sets the busy bit.
5.Then the device controller reads the command register, sees the write bit set, reads
the byte of data from the data-out register, and outputs the byte of data.
6.The device controller then clears the error bit in the status register, the command-
ready bit, and finally clears the busy bit, signaling the completion of the operation.
•Polling can be very fast and efficient, if both the device and the controller are fast and if
there is significant data to transfer. It becomes inefficient, however, if the host must wait a
long time in the busy loop waiting for the device, or if frequent checks need to be made for
data that is infrequently there.
Interrupts:
•Interrupts allow devices to notify the CPU when they have data to transfer or when an
operation is complete, allowing the CPU to perform other duties when no I/O transfers need
its immediate attention.
•The CPU has an interrupt-request line that is sensed after every instruction.
•A device's controller raises an interrupt by asserting a signal on the interrupt request
line.
•The CPU then performs a state save, and transfers control to the interrupt handler
routine at a fixed address in memory.
•The interrupt handler determines the cause of the interrupt, performs the necessary
processing, performs a state restore, and executes a return from interrupt instruction
to return control to the CPU.
Interrupt-driven I/O cycle.

Direct Memory Access:

•For devices that transfer large quantities of data ( such as disk controllers ), it is wasteful to
tie up the CPU transferring data in and out of registers one byte at a time.
•Instead this work can be off-loaded to a special processor, known as the Direct Memory
Access, DMA, Controller.
•The host issues a command to the DMA controller, indicating the location where the data is
located, the location where the data is to be transferred to, and the number of bytes of data to
transfer. The DMA controller handles the data transfer, and then interrupts the CPU when
the transfer is complete.
•A simple DMA controller is a standard component in modern PCs, and many bus-
mastering I/O cards contain their own DMA hardware.
•Handshaking between DMA controllers and their devices is accomplished through two
wires called the DMA-request and DMA-acknowledge wires.
•While the DMA transfer is going on the CPU does not have access to the PCI bus
( including main memory ), but it does have access to its internal registers and primary and
secondary caches.
•DMA can be done in terms of either physical addresses or virtual addresses that are
mapped to physical addresses. The latter approach is known as Direct Virtual Memory
Access, DVMA, and allows direct data transfer from one memory-mapped device to another
without using the main memory chips.
•Direct DMA access by user processes can speed up operations, but is generally forbidden
by modern systems for security and protection reasons. ( I.e. DMA is a kernel-mode
operation. )
.

Steps in a DMA transfer.

Applications of Input/Output Interface:


I/O Interface :
There is need of surface whenever any CPU wants to communicate with I/O devices. The interface
is used to interpret address which is generated by CPU. Thus, surface is used to communicate to I/O
devices i.e. to share information between CPU and I/O devices interface is used which is called as
I/O Interface.

Various applications of I/O Interface :


Application of I/O is that we can say interface have access to open any file without any kind of
information about file i.e., even basic information of file is unknown. It also has feature that it can
be used to also add new devices to computer system even it does not cause any kind of interrupt to
operating system. It can also used to abstract differences in I/O devices by identifying general
kinds. The access to each of general kind is through standardized set of function which is called asn
interface.
Each type of operating system has its own category for interface of device-drivers. The device
which is given may ship with multiple device-drivers-for instance, drivers for Windows, Linux,
AIX and Mac OS, devices may is varied by dimensions which is illustrated below.

1.Character-stream or Block :
A character stream or block both transfers data in form of bytes. The difference between both of
them is that character-stream transfers bytes in linear way i.e., one after another whereas block
transfers whole byte in single unit.
2.Sequential or Random Access :
To transfer data in fixed order determined by device, we use sequential device whereas user to
instruct device to seek to any of data storage locations, random-access device is used.
3.Synchronous or Asynchronous :
Data transfers with predictable response times is performed by synchronous device, in
coordination with others aspects of system. An irregular or unpredictable response times not
coordinated with other computer events is exhibits by an asynchronous device.
4.Sharable or Dedicated :
Several processes or threads can be used concurrently by sharable device; whereas dedicated
device cannot.
5.Speed of Operation :
The speed of device has range set which is of few bytes per second to few gega-bytes per
second.
6.Read-write, read only, write-only :
Different devices perform different operations, some supports both input and output, but
others supports only one data transfer direction either input or output.
Network Devices
Because the performance and addressing characteristics of network I/O differ significantly from
those of disk I/O. Most operating systems provide a network I/0 interface,which acts like a cable or
pipeline connecting two networked entities. Data can be put into the socket at one end, and read out
sequentially at the other end. Sockets are normally full-duplex, allowing for bi-directional data
transfer.
A call to select () returns information about which sockets have a packet waiting to be received and
which sockets have room to accept a packet to be sent.

Kernel I/O Subsystem


Kernel I/O Subsystem is responsible to provide many services related to I/O. Following are some of
the services provided.
I/O Scheduling: Scheduling I/O requests can greatly improve overall efficiency. Priorities can also
play a part in request scheduling.Kernel schedules a set of I/O requests to determine a good order in
which to execute them. When an application issues a blocking I/O system call, the request is placed
on the queue for that device. The Kernel I/O scheduler rearranges the order of the queue to improve
the overall system efficiency and the average response time experienced by the applications.
Buffering: Buffering of I/O is performed for ( at least ) 3 major reasons:
1. Speed differences between two devices. A slow device may write data into a buffer, and when the
buffer is full, the entire buffer is sent to the fast device all at once. So that the slow device still has
somewhere to write while this is going on, a second buffer is used, and the two buffers alternate as
each becomes full. This is known as double buffering.( Double buffering is often used in
(animated ) graphics, so that one screen image can be generated in a buffer while the other
( completed ) buffer is displayed on the screen. This prevents the user from ever seeing any half-
finished screen images. )
2. Data transfer size differences. Buffers are used in particular in networking systems to break
messages up into smaller packets for transfer, and then for re-assembly at the receiving side.
3. To support copy semantics. For example, when an application makes a request for a disk write,
the data is copied from the user's memory area into a kernel buffer. Now the application can change
their copy of the data, but the data which eventually gets written out to disk is the version of the
data at the time the write request was made.
Caching:
Caching involves keeping a copy of data in a faster-access location than where the
data is normally stored.
•Buffering and caching are very similar, except that a buffer may hold the only copy of a
given data item, whereas a cache is just a duplicate copy of some other data stored
elsewhere.
Spooling and Device Reservation
•A spool ( Simultaneous Peripheral Operations On-Line ) buffers data for ( peripheral )
devices such as printers that cannot support interleaved data streams.
•If multiple processes want to print at the same time, they each send their print data to files
stored in the spool directory. When each file is closed, then the application sees that print job
as complete, and the print scheduler sends each file to the appropriate printer one at a time.
•Support is provided for viewing the spool queues, removing jobs from the queues, moving
jobs from one queue to another queue, and in some cases changing the priorities of jobs in
the queues.

Error Handling
•I/O requests can fail for many reasons, either transient (buffers overflow) or permanent
(disk crash).
•I/O requests usually return an error bit ( or more ) indicating the problem. UNIX systems
also set the global variable errno to one of a hundred or so well-defined values to indicate
the specific error that has occurred.
•Some devices, such as SCSI devices, are capable of providing much more detailed
information about errors, and even keep an on-board error log that can be requested by the
host.
I/O Protection
•The I/O system must protect against either accidental or deliberate erroneous I/O.Errors
and the issue of protection are closely related. A user process may attempt to issue illegal
I/O instructions to disrupt the normal function of a system. We can use the various
mechanisms to ensure that such disruption cannot take place in the system. To prevent
illegal I/O access, we define all I/O instructions to be privileged instructions. The user
cannot issue I/O instruction directly.

Transforming I/O Requests to Hardware Operations:

•Users request data using file names, which must ultimately be mapped to specific blocks of
data from a specific device managed by a specific device driver.
•DOS uses the colon separator to specify a particular device ( e.g. C:, LPT:, etc. )
•UNIX uses a mount table to map filename prefixes ( e.g. /usr ) to specific mounted devices.
Where multiple entries in the mount table match different prefixes of the filename the one
that matches the longest prefix is chosen. ( e.g. /usr/home instead of /usr where both exist in
the mount table and both match the desired file. )
•Each device file has a major and minor number associated with it, stored and displayed
where the file size would normally go.
•The major number is an index into a table of device drivers, and indicates which device
driver handles this device. ( E.g. the disk drive handler. )
•The minor number is a parameter passed to the device driver, and indicates which specific
device is to be accessed, out of the many which may be handled by a particular device
driver. ( e.g. a particular disk drive or partition. )

•Figure 13.13 illustrates the steps taken to process a ( blocking ) read request:
Figure 13.13 - The life cycle of an I/O request.

You might also like