Overview of Computer Systems and OS
Overview of Computer Systems and OS
Computer System - A computer system is a set of integrated devices the input, output, process, and store data and
information. Basically, computer system mainly consists of hardware, software and data.
Software - Computer software, or only software, is a kind of program that enables a user to perform some specific task or
used to operate a computer. It directs all the peripheral devices on the computer system – what to do and how to perform a
task. PC software plays the role of mediator between the user and computer hardware.
1. APPLICATION SOFTWARE - Application software also called end-user programs or merely an application. The end-user
uses application software for a specific purpose. It is programmed for simple as well as complex tasks.
It can either be installed or accessed online. It can be a single program or a group of small programs that referred to as an
application suite. Application software can be used by the user to complete specific tasks, such as creating word processors
documents, spreadsheets, presentations, graphics, sending the emails, etc.
2. SYSTEM SOFTWARE - System software is a collection of programs designed to operate, control, and extend the
processing capabilities of the computer itself. These software products comprise of programs written in low-level languages,
which interact with the hardware at a very basic level.
System software serves as the interface between the hardware and the end users. The best-known example of system
software is an operating system. Some examples of system software are Operating System, Compilers, Interpreter,
Assemblers, etc.
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.
When you turn on the computer, the operating system program is loaded into the main memory. This program is called
the kernel.
Once initialized, the system program is prepared to run the user programs and permits them to use the hardware
efficiently. For example, if we want to perform some paintings on the screen, then we must use the application software
as paint and hardware as a mouse for drawing an object.
But how the system knows what to do when mouse moves on the screen and when the mouse draws a line on the
system so that operating system is necessary which interact between or which communicates with the hardware and the
software.
Although it‟s easy to feel like a director in front of your desktop or laptop, there‟s a lot going on inside, and the real man
behind the curtain handling the necessary tasks is the operating system.
SYSTEM CALLS - System calls provide an interface to the services made available by an operating system. Types of system
calls:
OS AS A RESOURCE MANAGER
A computer has many resources (Hardware and Software), which may be required to complete a task.
The commonly required resources are Input/output devices, Memory file storage space, and CPU (Central Processing
Unit) time and so on.
The operating system acts as the manager of these resources and allocates them to specific programs and users as
necessary for their tasks.
Therefore we can say an operating system is a resource allocator. This is the main features of an operating system.
When a number of computers connected through a network more than one computers trying for a computer print or a
common resource, then the operating system follow same order and manage the resources in an efficient manner.
Generally, resources sharing in two ways "in time" and "in space".
When a resource is a time-sharing resource, first one of the tasks gets the resource for some time, then another and so
on.
For example, a CPU in the time-sharing system. In the time-sharing system, OS fixes the time slot for the CPU. First one
of the processes gets the CPU, when the time slot expires, the CPU switch to the next process in the ready queue. In this
example, the CPU is a time resource.
The other kind of sharing is the space sharing. In this method, the users share the space of resource.
For example, the main memory consisting of several processes at a time, so the process of sharing the resources.
The main difference between "in time" sharing resources and "in space" sharing resources is "in time" resources are not
divided into units, whereas "in space" sharing resources are divided into units.
FUNCTIONALITIES OF AN OS
MEMORY MANAGEMENT
1. Keeps tracks of primary memory, i.e., what part of it are in use by whom, what parts are not in use.
2. In multiprogramming, the OS decides which process will get memory when and how much.
3. Allocates the memory when a process requests it to do so.
4. De-allocates the memory when a process no longer needs it or has been terminated.
DEVICE MANAGEMENT
An Operating System manages device communication via their respective drivers. It does the following activities for device
management:
1. Keeps tracks of all devices. Program responsible for this task is known as the I/O controller.
2. Decides which process gets the device when and for how much time.
3. Allocates the device in the efficient way.
4. De-allocates devices.
PROCESSOR MANAGEMENT
In multiprogramming environment, the OS decides which process gets the processor when and for how much time. This
function is called process scheduling. An Operating System does the following activities for processor management:
1. Keeps tracks of processor and status of process. The program responsible for this task is known as traffic controller.
2. Allocates the processor (CPU) to a process.
3. De-allocates processor when a process is no longer required.
FILE MANAGEMENT
A file system is normally organized into directories for easy navigation and usage.
These directories may contain files and other directions. An Operating System does the following activities for file
management:
1. Keeps track of information, location, uses, status etc. The collective facilities are often known as file system.
2. Decides who gets the resources.
3. Allocates the resources.
4. De-allocates the resources.
OTHER IMPORTANT ACTIVITIES - Following are some of the important activities that an Operating System performs:
1. Security: By means of password and similar other techniques, it prevents unauthorized access to programs and data.
2. Control over system performance: Recording delays between request for a service and response from the system.
3. Job accounting: Keeping track of time and resources used by various jobs and users.
4. Error detecting aids: Production of dumps, traces, error messages, and other debugging and error detecting aids.
5. Coordination between other software and users: Coordination and assignment of compilers, interpreters, assemblers
and other software to the various users of the computer systems.
When the first electronic computer was developed in 1940, it was created without any operating system. In early times, users
have full access to the computer machine and write a program for each task in absolute machine language. The programmer
can perform and solve only simple mathematical calculations during the computer generation, and this calculation does not
require an operating system.
The first operating system (OS) was created in the early 1950s and was known as GMOS. General Motors has developed OS
for the IBM computer. The second-generation operating system was based on a single stream batch processing system
because it collects all similar jobs in groups or batches and then submits the jobs to the operating system using a punch card
to complete all jobs in a machine. At each completion of jobs (either normally or abnormally), control transfer to the
operating system that is cleaned after completing one job and then continues to read and initiates the next job in a punch
card. After that, new machines were called mainframes, which were very big and used by professional operators.
During the late 1960s, operating system designers were very capable of developing a new operating system that could
simultaneously perform multiple tasks in a single computer program called multiprogramming. The introduction
of multiprogramming plays a very important role in developing operating systems that allow a CPU to be busy every time
by performing different tasks on a computer at the same time. During the third generation, there was a new development of
minicomputer's phenomenal growth starting in 1961 with the DEC PDP-1. These PDP's leads to the creation of personal
computers in the fourth generation.
The fourth generation of operating systems is related to the development of the personal computer. However, the personal
computer is very similar to the minicomputers that were developed in the third generation. The cost of a personal computer
was very high at that time; there were small fractions of minicomputers costs. A major factor related to creating personal
computers was the birth of Microsoft and the Windows operating system. Microsoft created the first window operating
system in 1975. After introducing the Microsoft Windows OS, Bill Gates and Paul Allen had the vision to take personal
computers to the next level. Therefore, they introduced the MS-DOS in 1981; however, it was very difficult for the person to
understand its cryptic commands. Today, Windows has become the most popular and most commonly used operating
system technology. And then, Windows released various operating systems such as Windows 95, Windows 98, Windows XP
and the latest operating system, Windows 7. Currently, most Windows users use the Windows 10 operating system. Besides
the Windows operating system, Apple is another popular operating system built in the 1980s, and this operating system was
developed by Steve Jobs, a co-founder of Apple. They named the operating system Macintosh OS or Mac OS.
Given below is a list of commonly used Operating systems along with their year of release.
Given below are the different types of Operating System along with brief information about each of them:
1. 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. Example: Bank Statements, Payroll system, etc.
Advantages of Batch Operating System:
It is very difficult to guess or know the time required for any job to complete. Processors of the batch systems know how
long the job would be when it is in queue
Multiple users can share the batch systems
The idle time for the batch system is very less
It is easy to manage large work repeatedly in batch systems
2. Time-sharing Operating System - Each task is given some time to execute so that all the tasks work smoothly. Each user
gets the time of CPU as they use a single system. These systems are also known as Multitasking Systems. The task can be
from a single user or different users also. The time that each task gets to execute is called quantum. After this time interval is
over OS switches over to the next task.
Reliability problem
One must have to take care of the security and integrity of user programs and data
Data communication problem
3. Distributed Operating System – These types of the operating system is a recent advancement in the world of computer
technology and are being widely accepted all over the world and, that too, with a great pace. Various autonomous
interconnected computers communicate with each other using a shared communication network. Independent systems
possess their own memory unit and CPU. These are referred to as loosely coupled systems or distributed systems. These
system‟s processors differ in size and function. The major benefit of working with these types of the operating system is that
it is always possible that one user can access the files or software which are not actually present on his system but some
other system connected within this network i.e., remote access is enabled within the devices connected in that network.
Failure of one will not affect the other network communication, as all systems are independent from each other
Electronic mail increases the data exchange speed
Since resources are being shared, computation is highly fast and durable
Load on host computer reduces
These systems are easily scalable as many systems can be easily added to the network
Delay in data processing reduces
4. Network Operating System – These systems run on a server and provide the capability to manage data, users, groups,
security, applications, and other networking functions. These types of operating systems allow shared access of files, printers,
security, applications, and other networking functions over a small private network. One more important aspect of Network
Operating Systems is that all the users are well aware of the underlying configuration, of all other users within the network,
their individual connections, etc. and that‟s why these computers are popularly known as tightly coupled systems.
Advantages of Network Operating System:
Examples of Network Operating System are: Microsoft Windows Server 2003, Microsoft Windows Server 2008, UNIX,
Linux, Mac OS X, Novell NetWare, and BSD, etc.
5. Real-Time Operating System – These types of OSs serve real-time systems. The time interval required to process and
respond to inputs is very small. This time interval is called response time.
Real-time systems are used when there are time requirements that are very strict like missile systems, air traffic control
systems, robots, etc.
Hard Real-Time Systems: These OSs are meant for applications where time constraints are very strict and even the
shortest possible delay is not acceptable. These systems are built for saving life like automatic parachutes or airbags
which are required to be readily available in case of any accident. Virtual memory is rarely found in these systems.
Soft Real-Time Systems: These OSs are for applications where for time-constraint is less strict.
Advantages of RTOS:
Maximum Consumption: Maximum utilization of devices and system, thus more output from all the resources
Task Shifting: The time assigned for shifting tasks in these systems are very less. For example, in older systems, it takes
about 10 microseconds in shifting one task to another, and in the latest systems, it takes 3 microseconds.
Focus on Application: Focus on running applications and less importance to applications which are in the queue.
Real-time operating system in the embedded system: Since the sizes of programs are small, RTOS can also be used in
embedded systems like in transport and others.
Disadvantages of RTOS:
Limited Tasks: Very few tasks run at the same time and their concentration is very less on few applications to avoid
errors.
Use heavy system resources: Sometimes the system resources are not so good and they are expensive as well.
Complex Algorithms: The algorithms are very complex and difficult for the designer to write on.
Device driver and interrupt signals: It needs specific device drivers and interrupts signals to respond earliest to
interrupts.
Thread Priority: It is not good to set thread priority as these systems are very less prone to switching tasks.
Examples of Real-Time Operating Systems are: Scientific experiments, medical imaging systems, industrial control
systems, weapon systems, robots, air traffic control systems, etc.
1. The operating system helps to improve the efficiency of the work and helps to save a lot of time by reducing the
complexity.
2. An operating system is mainly used to hide the complexity of the hardware.
3. Users can easily access the hardware without writing large programs.
4. With the help of an operating system, sharing data becomes easier with a large number of users.
5. We can easily install any game or application on the Operating system easily and can run them
6. An operating system can be refreshed easily from time to time without having any problems.
7. There are various operating systems that are accessible in open source. Example: Unix/Linux.
8. With the help of an operating system, multitasking becomes easier.
1. Expensive - There are some open-source platforms like Linux. But some operating systems are expensive. Also, users can
use free operating systems but generally, there is a bit difficulty to run them than others. On the other hand, operating
systems like Microsoft Windows having GUI functionality and other in-built features are very expensive.
2. Virus Threat - Operating Systems are open to virus attacks and sometimes it happens that many users download the
malicious software packages on their system which pauses the functioning of the Operating system and also slows it down.
3. Complexity - Some operating systems are complex in nature because the language used to establish them is not clear
and well defined. If there occurs an issue in the operating system then the user becomes unable to resolve that issue.
4. System Failure - An operating system is the heart of the computer system if due to any reason it will stop functioning
then the whole system will crashes.
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:
These types of operating system cause the entire system to crash if one of the
user programs fails. MS-DOS is an example of such operating system. In MS-
DOS, application programs are able to access the basic I/O routines.
Layering makes it easier to enhance the operating system as implementation of a layer can be changed easily without
affecting the other layers.
It is very easy to perform debugging and system verification.
3. MICRO-KERNEL - This structure designs the operating system by removing all non-
essential components from the kernel and implementing them as system and user
programs. This result in a smaller kernel called the micro-kernel.
4. MODULAR STRUCTURE OR APPROACH - It is considered as the best approach for an OS. It involves designing of a
modular kernel. The kernel has only set of core components and other services are added as dynamically loadable modules
to the kernel either during run time or boot time.
It resembles layered structure due to the fact that each kernel has defined and protected interfaces. But it is more flexible
than the layered structure as a module can call any other module. For example Solaris OS is organized as shown in the figure.
UNIT 2 - PROCESS AND THREADS
Process
For example, when we write a program in C or C++ and compile it, the compiler
creates binary code. The original code and binary code are both programs. When
we actually run the binary code, it becomes a process.
When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data. The following image shows
a simplified layout of a process inside main memory:
Program
A program is a piece of code which may be a single line or millions of lines. A computer program is usually written by a
computer programmer in a programming language. For example, here is a simple program written in C programming
language:
#include <stdio.h>
int main() {
printf("Hello, World! \n");
return 0;
}
A computer program is a collection of instructions that performs a specific task when executed by a computer. When we
compare a program with a process, we can conclude that a process is a dynamic instance of a computer program.
A part of a computer program that performs a well-defined task is known as an algorithm. A collection of computer
programs, libraries and related data are referred to as software.
PROCESS VS PROGRAM
Process Program
The process is basically an instance of the computer A Program is basically a collection of instructions that mainly
program that is being executed. performs a specific task when executed by the computer.
A process has a shorter lifetime. A Program has a longer lifetime.
A Process requires resources such as memory, CPU, Input- A Program is stored by hard-disk and does not require any
Output devices. resources.
A process has a dynamic instance of code and data A Program has static code and static data.
Basically, a process is the running instance of the code. On the other hand, the program is the executable code.
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:
1 Process State - The current state of the process i.e., whether it is ready, running, waiting, or whatever.
3 Process ID - Unique identification for each of the process in the operating system.
5 Program Counter - Program Counter is a pointer to the address of the next instruction to be executed for this
process.
6 CPU registers - Various CPU registers where process need to be stored for execution for running state.
7 CPU Scheduling Information - Process priority and other scheduling information which is required to schedule the
process.
8 Memory management information - This includes the information of page table, memory limits, Segment table
depending on memory used by the operating system.
9 Accounting information - This includes the amount of CPU used for process execution, time limits, execution ID etc.
10 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:
The PCB is maintained for a process throughout its lifetime, and is deleted once the
process terminates.
When a process executes, it passes through different states. These stages may differ in
different operating systems, and the names of these states are also not standardized. In
general, a process can have one of the following five states at a time.
State Diagram
S.N. State & Description
1 Start: This is the initial state when a process is first started / created.
2 Ready: The process is waiting to be assigned to a processor. Ready processes are waiting to have the processor
allocated to them by the operating system so that they can run. Process may come into this state after Start state
or while running it by but interrupted by the scheduler to assign CPU to some other process.
3 Running: Once the process has been assigned to a processor by the OS scheduler, the process state is set to
running and the processor executes its instructions.
4 Waiting: Process moves into the waiting state if it needs to wait for a resource, such as waiting for user input, or
waiting for a file to become available.
5 Terminated or Exit: Once the process finishes its execution, or it is terminated by the operating system, it is
moved to the terminated state where it waits to be removed from main memory.
When a process executes, it goes through a number of states. The current state of the process tells us about the current
activity of the process. The state of a process may change due to events like I/O requests; interrupt routines, synchronization
of processes, process scheduling algorithms, etc.
When a process is first created by the OS, it initializes the program control block for the process and the new process enters
the system in Not-running state. After some time, the currently running process will be interrupted by some events, and the
OS will move the currently running process from Running state to Not-running state. The dispatcher then selects one process
from Not-running processes and moves the process to the Running state for execution.
For example, if we implement round-robin scheduling then the running process moves to not-running state after the time
quantum. When a process finishes the execution, the process exits the system and the dispatcher again selects a new process
and moves it to Running state.
To overcome this problem, we split the not-running state into two states: Ready State and Waiting (Blocked) State.
1. Ready State: The process in the main memory that is ready for execution.
2. Waiting or Blocked State: The process in the main memory that is waiting for some event.
The OS maintains a separate queue for both Ready State and Waiting State. A process moves from Waiting State to Ready
State once the event it‟s been waiting for completes.
In the five state model, we introduce two new states: new state and terminated state.
Reason for New State - In the previous models, we assumed that the main memory is large enough to accommodate all
programs but this is not true. Modern programs are very large. Loading all processes in the main memory is not possible.
When a new process is made, its program is not loaded in the main memory. The OS only stores some information about the
process in the main memory. The long term scheduler moves the program to the main memory when sufficient space is
available. Such a process is said to be in new state.
Reason for Terminated State In the previous models, when a process finishes execution, its resources are immediately
freed. But there might be some other process that may need its data in the future.
For example, when a child process finishes execution, the OS preserves its data until the parent call wait(). The child process is
still in memory but not available for execution. The child process is said to be in terminated state.
States
2. Waiting/Blocked: Process waiting for some event such as completion of I/O operation, waiting for other processes,
synchronization signal, etc.
4. New: The process that is just being created. The Program Control Block is already being made but the program is not
yet loaded in the main memory. The program remains in the new state until the long term scheduler moves the process
to the ready state (main memory).
1. Long term scheduler - Long term scheduler is also known as job scheduler. It chooses the processes from the pool
(secondary memory) and keeps them in the ready queue maintained in the primary memory.
Long Term scheduler mainly controls the degree of Multiprogramming. The purpose of long term scheduler is to choose a
perfect mix of IO bound and CPU bound processes among the jobs present in the pool.
If the job scheduler chooses more IO bound processes then all of the jobs may reside in the blocked state all the time and
the CPU will remain idle most of the time. This will reduce the degree of Multiprogramming. Therefore, the Job of long term
scheduler is very critical and may affect the system for a very long time.
2. Short term scheduler - Short term scheduler is also known as CPU scheduler. It selects one of the Jobs from the ready
queue and dispatch to the CPU for the execution.
A scheduling algorithm is used to select which job is going to be dispatched for the execution. The Job of the short term
scheduler can be very critical in the sense that if it selects job whose CPU burst time is very high then all the jobs after that,
will have to wait in the ready queue for a very long time.
This problem is called starvation which may arise if the short term scheduler makes some mistakes while selecting the job.
3. Medium term scheduler - Medium term scheduler takes care of the swapped out processes. If the running state
processes needs some IO time for the completion then there is a need to change its state from running to waiting.
Medium term scheduler is used for this purpose. It removes the process from the running state to make room for the other
processes. Such processes are the swapped out processes and this procedure is called swapping. The medium term scheduler
is responsible for suspending and resuming the processes.
It reduces the degree of multiprogramming. The swapping is necessary to have a perfect mix of processes in the ready
queue.
2 Speed is lesser than short term Speed is fastest among other two Speed is in between both short and
scheduler long term scheduler.
3 It controls the degree of It provides lesser control over It reduces the degree of
multiprogramming degree of multiprogramming multiprogramming.
4 It is almost absent or minimal in time It is also minimal in time sharing It is a part of Time sharing systems.
sharing system system
5 It selects processes from pool and loads It selects those processes which It can re-introduce the process into
them into memory for execution are ready to execute memory and execution can be
continued.
Context Switch
When CPU switches to another process, the system must save the state of the old process and load the saved state for the
new process. This is known as context switch. In other words, the process of saving the context of one process and loading
the context of another process is known as Context Switching.
Context-switch time is overhead; the system does no useful work while switching.
Switching speed varies from machine to machine, depending on the memory speed, the number of registers that must
be copied, and the existence of special instructions. A typical speed is a few milliseconds.
Context switch times are highly dependent on hardware
support.
Program Counter
Scheduling information
Base and limit register value
Currently used register
Changed State
I/O State information
Accounting information
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.
5. Waiting Time - The Total amount of time for which the process waits for the CPU to be assigned is called waiting 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.
What is Dispatcher?
It is a module that provides control of the CPU to the process. The Dispatcher should be fast so that it can run on every
context switch. Dispatch latency is the amount of time needed by the CPU scheduler to stop one process and start another.
Context Switching
Switching to user mode
Moving to the correct location in the newly loaded program.
CPU Scheduling
In the uniprogrammming systems like MS DOS, when a process waits for any I/O operation to be done, the CPU remains
idle. This is an overhead since it wastes the time and causes the problem of starvation. However, In Multiprogramming
systems, the CPU doesn't remain idle during the waiting time of the Process and it starts executing other processes.
Operating System has to define which process the CPU will be given.
In Multiprogramming systems, the Operating system schedules the processes on the CPU to have the maximum utilization
of it and this procedure is called CPU scheduling. The Operating System uses various scheduling algorithm to schedule the
processes.
This is a task of the short term scheduler to schedule the CPU for the number of processes present in the Job Pool. Whenever
the running process requests some IO operation then the short term scheduler saves the current context of the process (also
called PCB) and changes its state from running to waiting. During the time, process is in waiting state; the Short term
scheduler picks another process from the ready queue and assigns the CPU to this process. This procedure is called context
switching.
In Multiprogramming, if the long term scheduler picks more I/O bound processes then most of the time, the CPU remains
idle. The task of Operating system is to optimize the utilization of resources.
If most of the running processes change their state from running to waiting then there may always be a possibility of
deadlock in the system. Hence to reduce this overhead, the OS needs to schedule the jobs to get the optimal utilization of
CPU and to avoid the possibility to deadlock.
PROCESS SCHEDULING
The act of determining which process is in the ready state, and should be moved to the running state is known as
Process Scheduling.
When there are two or more runnable processes then it is decided by the operating system which one to run first then it
is referred to as Process Scheduling.
The prime aim of the process scheduling system is to keep the CPU busy all the time and to deliver minimum response
time for all programs.
Good Scheduling Algorithm - Given below are the properties of a Good Scheduling Algorithm:
Preemptive Scheduling
Non-Preemptive Scheduling:
Comparison Chart:
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.
Example of FCFS scheduling - A perfect real life example of FCFS scheduling is buying movie tickets at ticket counter.
In this scheduling algorithm, a person is served according to the queue manner. The person who arrives first in the queue
first buys the ticket and then the next one. This will continue until the last person in the queue purchases the ticket. Using
this algorithm, the CPU process works in a similar manner.
Advantages of FCFS
Simple
Easy
First come, First serve
Disadvantages of FCFS
The scheduling method is non-preemptive; the process will run to the completion.
Due to the non-preemptive nature of the algorithm, the problem of starvation may occur.
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.
Example 1: Let's take an example 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.
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.
Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
0 0 2 2 2 0
1 1 6 8 7 1
2 2 4 12 10 6
3 3 9 21 18 9
4 4 12 33 29 17
(Gantt chart)
Example 2: Calculating Average Waiting Time - For every scheduling algorithm, Average waiting time is a crucial
parameter to judge its performance. AWT or Average waiting time is the average of the waiting times of the processes in the
queue, waiting for the scheduler to pick them for execution. Lower the Average Waiting Time, better the scheduling algorithm.
Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same order, with Arrival Time 0,
and given Burst Time, let's find the average waiting time using the FCFS scheduling algorithm.
For the above given processes, first P1 will be provided with the CPU resources,
The Gantt chart above perfectly represents the waiting time for each process.
FCFS may suffer from the convoy effect if the burst time of the first job is the highest among all. As in the real life, if a
convoy is passing through the road then the other persons may get blocked until it passes completely. This can be simulated
in the Operating System also.
If the CPU gets the processes of the higher burst time at the front end of the ready queue then the processes of lower burst
time may get blocked which means they may never get the CPU if the job in the execution has a very high burst time. This is
called convoy effect or starvation.
Example: In the Example, We have 3 processes named as P1, P2 and P3. The Burt Time of process P1 is highest.
In the First scenario, The Process P1 arrives at the first in the queue although; the burst time of the process is the highest
among all. Since, the Scheduling algorithm, we are following is FCFS hence the CPU will execute the Process P1 first.
In this schedule, the average waiting time of the system will be very high. That is because of the convoy effect. The other
processes P2, P3 have to wait for their turn for 40 units of time although their burst time is very low. This schedule suffers
from starvation.
Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
1 0 40 40 40 0
2 1 3 43 42 39
3 1 1 44 43 42
In the Second scenario, If Process P1 would have arrived at the last of the queue and the other processes P2 and P3 at earlier
then the problem of starvation would not be there.
Following example shows the deviation in the waiting times of both the scenarios. Although the length of the schedule is
same that is 44 units but the waiting time will be lesser in this schedule.
Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
1 1 40 44 43 3
2 0 3 3 3 0
3 0 1 4 4 3
Till now, we were scheduling the processes according to their arrival time (in FCFS scheduling). However, SJF scheduling
algorithm, schedules the processes according to their burst time. Shortest job first (SJF) or shortest job next, is a scheduling
policy that selects the waiting process with the smallest execution time to execute next.
Shortest Job first has the advantage of having a minimum average waiting time among all scheduling algorithms.
It is a Greedy Algorithm.
It may cause starvation if shorter processes keep coming. This problem can be solved using the concept of ageing.
It is practically infeasible as Operating System may not know burst time and therefore it may not sort them. While it is
not possible to predict execution time, several methods can be used to estimate the execution time for a job, such as a
weighted average of previous execution times. SJF can be used in specialized environments where accurate estimates of
running time are available.
Advantages of SJF
Maximum throughput
Minimum average waiting and turnaround time
Disadvantages of SJF
There are different techniques available by which, the CPU burst time of the process can be determined. We will discuss them
later in detail.
Algorithm:
Non-Preemptive SJF
In non-preemptive scheduling, once the CPU cycle is allocated to process, the process holds it till it reaches a waiting state or
terminated. Consider the following five processes each having its own unique burst time and arrival time.
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
Step 1) At time= 1, Process P3 arrives. But, P4 still needs 2 execution units to complete. It will continue execution.
Step 2) At time =2, process P1 arrives and is added to the waiting queue. P4 will continue execution.
Step 3) At time = 3, process P4 will finish its execution. The burst time of P3 and P1 is compared. Process P1 is executed
because its burst time is less compared to P3.
Step 4) At time = 4, process P5 arrives and is added to the waiting queue. P1 will continue execution.
Step 5) At time = 5, process P2 arrives and is added to the waiting queue. P1 will continue execution.
Step 6) At time = 9, process P1 will finish its execution. The burst time of P3, P5, and P2 is compared. Process P2 is executed
because its burst time is the lowest.
Step 8) At time = 11, process P2 will finish its execution. The burst time of P3 and P5 is compared. Process P5 is executed
because its burst time is lower.
Step 11) Let's calculate the average waiting time for above example.
Wait time
P4 = 0-0=0
P1 = 3-2=1
P2 = 9-5=4
P5 = 11-4=7
P3 = 15-1=14
Average Waiting Time= 0+1+4+7+14/5 = 26/5 = 5.2
Preemptive SJF
In Preemptive SJF Scheduling, jobs are put into the ready queue as they come. A process with shortest burst time begins
execution. If a process with even a shorter burst time arrives, the current process is removed or preempted from execution,
and the shorter job is allocated CPU cycle.
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
Step 1) At time= 1, Process P3 arrives. But, P4 has a shorter burst time. It will continue execution.
Step 2) At time = 2, process P1 arrives with burst time = 6. The burst time is more than that of P4. Hence, P4 will continue
execution.
Step 3) At time = 3, process P4 will finish its execution. The burst time of P3 and P1 is compared. Process P1 is executed
because its burst time is lower.
Step 4) At time = 4, process P5 will arrive. The burst time of P3, P5, and P1 is compared. Process P5 is executed because its
burst time is lowest. Process P1 is preempted.
P1 5 out of 6 is remaining 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
Step 5) At time = 5, process P2 will arrive. The burst time of P1, P2, P3, and P5 is compared. Process P2 is executed because
its burst time is least. Process P5 is preempted.
Step 7) At time =7, P2 finishes its execution. The burst time of P1, P3, and P5 is compared. Process P5 is executed because its
burst time is lesser.
Step 8) At time =10, P5 will finish its execution. The burst time of P1 and P3 is compared. Process P1 is executed because its
burst time is less.
Step 9) At time =15, P1 finishes its execution. P3 is the only process left. It will start execution.
Step 10) At time =23, P3 finishes its execution.
Step 11) Let's calculate the average waiting time for above example.
Wait time
P4 = 0-0=0
P1 = (3-2) + 6 =7
P2 = 5-5 = 0
P5 = 4-4+2 =2
P3 = 15-1 = 14
Average Waiting Time = 0+7+0+2+14/5 = 23/5 =4.6
Q.1: Consider the set of 5 processes whose arrival time and burst time are given below:
If the CPU scheduling policy is SJF non-preemptive, calculate the average waiting time and average turnaround time.
Solution:
Now, we know,
Q.2: Consider the set of 5 processes whose arrival time and burst time are given below:
If the CPU scheduling policy is SJF preemptive, calculate the average waiting time and average turnaround time.
Solution:
Now, we know-
Turn Around time = Exit time –Arrival time
Waiting time = Turn Around time –Burst time
Now,
Average Turn Around time = (1+5+4+16+9)/5 = 35/5 = 7 unit
Average waiting time = (0+1+2+10+6)/5 = 19/5 = 3.8 unit
Priority Scheduling
In Priority scheduling, there is a priority number assigned to each process. In some systems, the lower the number, the higher
the priority. While, in the others, the higher the number, the higher will be the priority. The Process with the higher priority
among the available processes is given the CPU. There are two types of priority scheduling algorithm exists. One
is Preemptive priority scheduling while the other is Non Preemptive Priority scheduling.
Example of Priority Scheduling Algorithm - Consider the below table of processes with their respective CPU burst times
and the priorities.
As you can see in the Gantt chart that the processes are given CPU time just on the basis of the priorities.
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.
Once all the jobs get available in the ready queue, the algorithm will behave as non-preemptive priority scheduling, which
means the job scheduled will run till the completion and no preemption will be done.
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.
At time 0, P1 arrives with the burst time of 1 units and priority 2. Since no other process is available hence this will be
scheduled till next job arrives or its completion (whichever is lesser).
At time 1, P2 arrives. P1 has completed its execution and no other process is available at this time hence the Operating
system has to schedule it regardless of the priority assigned to it.
The Next process P3 arrives at time unit 2, the priority of P3 is higher to P2. Hence the execution of P2 will be stopped and P3
will be scheduled on the CPU.
During the execution of P3, three more processes P4, P5 and P6 becomes available. Since, all these three have the priority
lower to the process in execution so PS can't preempt the process. P3 will complete its execution and then P5 will be
scheduled with the priority highest among the available processes.
Meanwhile the execution of P5, all the processes got available in the ready queue. At this point, the algorithm will start
behaving as Non Preemptive Priority Scheduling. Hence now, once all the processes get available in the ready queue, the OS
just took the process with the highest priority and execute that process till completion. In this case, P4 will be scheduled and
will be executed till the completion.
Since P4 is completed, the other process with the highest priority available in the ready queue is P2. Hence P2 will be
scheduled next.
P2 is given the CPU till the completion. Since its remaining burst time is 6 units hence P7 will be scheduled after this.
The only remaining process is P6 with the least priority, the Operating System has no choice unless of executing it. This will
be executed at the last.
The Completion Time of each process is determined with the help of GANTT chart. The turnaround time and the waiting time
can be calculated by the following formula.
Turnaround Time = Completion Time - Arrival Time
Waiting Time = Turn Around Time - Burst Time
Process Id Priority Arrival Time Burst Time Completion Time Turnaround Time Waiting Time
1 2 0 1 1 1 0
2 6 1 7 22 21 14
3 3 2 3 5 3 0
4 5 3 6 16 13 7
5 4 4 5 10 6 1
6 10 5 15 45 40 25
7 9 6 8 30 24 16
In the Non Preemptive Priority scheduling, The Processes are scheduled according to the priority number assigned to them.
Once the process gets scheduled, it will run till the completion. Generally, the lower the priority number, the higher is the
priority of the process. The people might get confused with the priority numbers, hence in the GATE, there clearly mention
which one is the highest priority and which one is the lowest one.
Example: In the Example, there are 7 processes P1, P2, P3, P4, P5, P6 and P7. Their priorities, Arrival Time and burst time are
given in the table.
We can prepare the Gantt chart according to the Non Preemptive priority scheduling.
The Process P1 arrives at time 0 with the burst time of 3 units and the priority number 2. Since No other process has arrived
till now hence the OS will schedule it immediately.
Meanwhile the execution of P1, two more Processes P2 and P3 are arrived. Since the priority of P3 is 3 hence the CPU will
execute P3 over P2.
Meanwhile the execution of P3, All the processes get available in the ready queue. The Process with the lowest priority
number will be given the priority. Since P6 has priority number assigned as 4 hence it will be executed just after P3.
After P6, P4 has the least priority number among the available processes; it will get executed for the whole burst time.
Since all the jobs are available in the ready queue hence All the Jobs will get executed according to their priorities. If two jobs
have similar priority number assigned to them, the one with the least arrival time will be executed.
From the Gantt chart prepared, we can determine the completion time of every process. The turnaround time, waiting time
and response time will be determined.
This algorithm is similar to FCFS scheduling, but in Round Robin (RR) scheduling, preemption is added which enables the
system to switch between processes.
Round Robin scheduling algorithm is one of the most popular scheduling algorithms which can actually be implemented
in most of the operating systems.
This algorithm is mainly designed for time-sharing systems; every process gets executed in a cyclic way.
Each process present in the ready queue is assigned the CPU for that time quantum, if the execution of the process is
completed during that time then the process will terminate else the process will go back to the ready queue and waits
for the next turn to complete the execution.
1. Round Robin Scheduling algorithm resides under the category of Preemptive Algorithms.
2. This algorithm is one of the oldest, easiest, and fairest algorithms.
3. This Algorithm is a real-time algorithm because it responds to the event within a specific time limit.
4. In this algorithm, the time slice should be the minimum that is assigned to a specific task that needs to be processed.
Though it may vary for different operating systems.
5. This is a hybrid model and is clock-driven in nature.
6. This is a widely used scheduling method in the traditional operating system.
Advantages
It can be actually implementable in the system because it is not depending on the burst time.
It doesn't suffer from the problem of starvation or convoy effect.
All the jobs get a fare allocation of CPU.
Disadvantages
The higher the time quantum, the higher the response time in the system.
The lower the time quantum, the higher the context switching overhead in the system.
Deciding a perfect time quantum is really a very difficult task in the system.
RR Scheduling Example: In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6. Their arrival
time and burst time are given below in the table. The time quantum of the system is 4 units.
Ready Queue: Initially, at time 0, process P1 arrives which will be scheduled for the time slice 4 units. Hence in the ready
queue, there will be only one process P1 at starting with CPU burst time 5 units.
P1
5
The P1 will be executed for 4 units first.
Ready Queue: Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives in the ready queue. P1 has not
completed yet, it needs another 1 unit of time hence it will also be added back to the ready queue.
P2 P3 P4 P5 P1
6 3 1 5 1
After P1, P2 will be executed for 4 units of time which is shown in the Gantt chart.
Ready Queue: During the execution of P2, one more process P6 is arrived in the ready queue. Since P2 has not completed
yet hence, P2 will also be added back to the ready queue with the remaining burst time 2 units.
P3 P4 P5 P1 P6 P2
3 1 5 1 4 2
After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time is only 3 seconds.
Ready Queue: Since P3 has been completed, hence it will be terminated and not be added to the ready queue. The next
process will be executed is P4.
P4 P5 P1 P6 P2
1 5 1 4 2
After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which is lesser then the time quantum hence it will be
completed.
Ready Queue: The next process in the ready queue is P5 with 5 units of burst time. Since P4 is completed hence it will not be
added back to the queue.
P5 P1 P6 P2
5 1 4 2
P5 will be executed for the whole time slice because it requires 5 units of burst time which is higher than the time slice.
Ready Queue: P5 has not been completed yet; it will be added back to the queue with the remaining burst time of 1 unit.
P1 P6 P2 P5
1 4 2 1
The process P1 will be given the next turn to complete its execution. Since it only requires 1 unit of burst time hence it will be
completed.
Ready Queue: P1 is completed and will not be added back to the ready queue. The next process P6 requires only 4 units of
burst time and it will be executed next.
P6 P2 P5
4 2 1
P6 will be executed for 4 units of time till completion.
Ready Queue: Since P6 is completed, hence it will not be added again to the queue. There are only two processes present in
the ready queue. The Next process P2 requires only 2 units of time.
P2 P5
2 1
P2 will get executed again, since it only requires only 2 units of time hence this will be completed.
Ready Queue: Now, the only available process in the queue is P5 which requires 1 unit of burst time. Since the time slice is
of 4 units hence it will be completed in the next burst.
P5
1
P5 will get executed till completion.
The completion time, Turnaround time and waiting time will be calculated as shown in the table below.
As, we know,
Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turn Around Time - Burst Time
Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
1 0 5 17 17 12
2 1 6 23 22 16
3 2 3 11 9 6
4 3 1 12 9 8
5 4 5 24 20 15
6 6 4 21 15 11
Thread
A thread is the subset of a process and is also known as the lightweight process. A process can have more than one
thread, and these threads are managed independently by the scheduler. All the threads within one process are
interrelated to each other. Threads have some common information, such as data segment, code segment, files,
etc., that is shared to their peer threads. But contains its own registers, stack, and counter.
A thread is a flow of execution through the process code, with its own program counter that keeps track of which
instruction to execute next, system registers which hold its current working variables, and a stack which contains the
execution history.
A thread shares with its peer threads little information like code segment, data segment and open files. When one
thread alters a code segment memory item, all other threads see that.
What is a Thread? A thread is a path of execution within a process. A process can contain multiple threads.
Process vs. Thread - The primary difference is that threads within the same process run in a shared memory space, while
processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result threads share with other threads their code
section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter
(PC), register set, and stack space.
Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow
of control. Threads have been successfully used in implementing network servers and web server. They also provide a
suitable foundation for parallel execution of applications on shared memory multiprocessors. The following figure shows the
working of a single-threaded and a multithreaded process.
How does thread work? As we have discussed that a thread is a sub process or an execution unit within a process. A
process can contain a single thread to multiple threads. A thread works as follows:
o When a process starts, OS assigns the memory and resources to it. Each thread within a process shares the memory and
resources of that process only.
o Threads are mainly used to improve the processing of an application. In reality, only a single thread is executed at a time,
but due to fast context switching between threads gives an illusion that threads are running parallelly.
If a single thread executes in a process, it is known as a single-threaded and if multiple threads execute simultaneously, then
it is known as multithreading.
Why Multithreading? You can imagine multitasking as something that allows processes to run concurrently, while
multithreading allows sub-processes to run concurrently.
It is the ability of a CPU to manage the use of operating system by executing multiple threads concurrently.
The main idea of multithreading is to achieve parallelism by dividing a process into multiple threads.
A thread is also known as lightweight process.
For example, in a browser, multiple tabs can be different threads.
MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc.
Threads have been successfully used in implementing network servers and web server.
Types of Thread
As the name suggests, the user-level threads are only managed by users, and the kernel does not have its information.
These are faster, easy to create and manage.
The application starts with a single thread.
The kernel takes all these threads as a single process and handles them as one process only.
The user-level threads are implemented by user-level libraries, not by the system calls.
The kernel knows nothing about user level threads and manages them as if they were single threaded processes.
When threads are managed in user space, each process needs its own private thread table to keep track of the threads
in that process.
This table keeps track only of the per-thread properties, such as each thread‟s program counter, stack pointer, register,
state, and so forth.
Advantages of ULT
Disadvantages of ULT
Advantages of KLT
Kernel can simultaneously schedule multiple threads from the same process on multiple processes.
If one thread in a process is blocked; the Kernel can schedule another thread of the same process.
Kernel routines themselves can be multithreaded.
Good for applications that frequently block.
Disadvantages of KLT
Kernel threads are generally slower to create and manage than the user threads.
It requires thread control block so it is an overhead.
Transfer of control from one thread to another within the same process requires a mode switch to the Kernel.
Objectives of Kernel:
Inter process communication (IPC) is a set of programming interfaces that allow a programmer to coordinate activities
among different program processes that can run concurrently in an operating system. In other words, Inter process
communication (IPC) is used for exchanging data between multiple threads in one or more processes or programs.
The processes can be running on the same computer or on different computers connected through a network. This
allows a program to handle many user requests at the same time.
IPC enables one application to control another application, and for several applications to share the same data without
interfering with one another.
IPC is required in all multiprocessing systems, but it is not generally supported by single-process operating systems such
as DOS. A diagram that illustrates inter-process communication is as follows:
WHY IPC? Here, are the reasons for using the inter-process communication protocol for information sharing:
RACE CONDITIONS
A race condition is a situation where two or more control threads are working with the same memory elements, and
simultaneously decide to write different values in the same place.
When more than one processes are executing the same code or accessing the same memory or any shared variable in that
condition there is a possibility that the output or the value of the shared variable is wrong so for that all the processes doing
the race to say that my output is correct this condition known as a race condition.
Suppose that two processes, P1 and P2, share the global variable a. At some point in its execution, P1 updates a to the value
1, and at some point in its execution, P2 updates a to the value 2. Thus, the two tasks are in a race to write variable a.
In this example the "loser" of the race (the process that updates last) determines the final value of a.
This happens when the result of multiple thread execution in the critical section differs according to the order in which
the threads execute.
This condition can be avoided using the technique called Synchronization or Process Synchronization, in which we allow
only one process to enter and manipulates the shared data in Critical Section.
Producer-Consumer problem
The Producer-Consumer problem is a classical multi-process synchronization problem that is we are trying to achieve
synchronization between more than one processes.
There is one Producer in the producer-consumer problem; Producer is producing some items, whereas there is one
Consumer that is consuming the items produced by the Producer. The same memory buffer is shared by both producers and
consumers which is of fixed-size.
The task of the Producer is to produce the item, put it into the memory buffer, and again start producing items. Whereas the
task of the Consumer is to consume the item from the memory buffer.
Let's understand what is the problem? Below are a few points that considered as the problems occur in Producer-Consumer:
o The producer should produce data only when the buffer is not full. In case it is found that the buffer is full, the producer
is not allowed to store any data into the memory buffer.
o Data can only be consumed by the consumer if and only if the memory buffer is not empty. In case it is found that the
buffer is empty, the consumer is not allowed to use any data from the memory buffer.
o Accessing memory buffer should not be allowed to producer and consumer at the same time.
SECTIONS OF A PROGRAM
1. Entry Section: It is part of the process which decides the entry of a particular process. It is part of the process which
decide the entry of a particular process in the Critical Section, out of many other processes.
2. Critical Section: This part allows one process to enter and modify the shared variable. It is the part in which only one
process is allowed to enter and modify the shared variable. This part of the process ensures that only no other process can
access the resource of shared data.
3. Exit Section: Exit section allows the other processes that are waiting in the Entry Section, to enter into the Critical Sections.
It also checks that a process that finished its execution should be removed through this Section. This process allows the other
processes that are waiting in the Entry Section, to enter into the Critical Sections. It checks that a process that after a process
has finished execution in Critical Section can be removed through this Exit Section.
4. Remainder Section: All other parts of the Code, which is not in Critical, Entry, and Exit Section, are known as the
Remainder Section. The other parts of the Code other than Entry Section, Critical Section and Exit Section are known as
Remainder Section.
Critical section is a code segment that can be accessed by only one process at a time.
Critical section contains shared variables which need to be synchronized to maintain consistency of data variables.
If any other process also wants to execute its critical section, it must wait until the first one finishes.
It means that in a group of cooperating processes, at a given point of time, only one process must be executing its
critical section.
The entry to the critical section is mainly handled by wait()function while the exit from the critical section is controlled by
the signal()function.
In the entry section, the process requests for entry in the Critical Section.
In the above diagram, the entry section handles the entry into the critical section. It acquires the resources needed for
execution by the process.
The exit section handles the exit from the critical section. It releases the resources and also informs the other processes
that critical section is free.
The critical section problem needs a solution to synchronize the different processes.
The solution to the critical section problem must satisfy the following conditions:
1. Mutual Exclusion: If a process is executing in its critical section, then no other process is allowed to execute in the critical
section. Mutual exclusion implies that only one process can be inside the critical section at any time. If any other processes
require the critical section, they must wait until it is free.
2. Progress: Progress means that if a process is not using the critical section, then it should not stop any other process from
accessing it. In other words, any process can enter a critical section if it is free. When no process is in the critical section, then
any process from outside that request for execution can enter in the critical section without any delay. Only those processes
can enter that have requested and have finite time to enter the process.
3. Bounded Waiting: Bounded waiting means that each process must have a limited waiting time. It should not wait
endlessly to access the critical section. A bound must exist on the number of times that other processes are allowed to enter
their critical sections after a process has made a request to enter its critical section and before that request is granted.
Mutual Exclusion in Synchronization
During concurrent execution of processes, processes need to enter the critical section (or the section of the program shared
across processes) at times for execution. It might so happen that because of the execution of multiple processes at once, the
values stored in the critical section become inconsistent. In other words, the values depend on the sequence of execution of
instructions – also known as a race condition. The primary task of process synchronization is to get rid of race conditions
while executing the critical section. This is primarily achieved through mutual exclusion.
Mutual exclusion is a property of process synchronization which states that “no two processes can exist in the critical
section at any given point of time”. The term was first coined by Djikstra. Any process synchronization technique being used
must satisfy the property of mutual exclusion, without which it would not be possible to get rid of a race condition.
Example: In the clothes section of a supermarket, two people are shopping for clothes.
Boy A decides upon some clothes to buy and heads to the changing room to try them out. Now, while boy A is inside the
changing room, there is an „occupied‟ sign on it – indicating that no one else can come in. Girl B has to use the changing
room too, so she has to wait till boy A is done using the changing room.
Once boy A comes out of the changing room, the sign on it changes from „occupied‟ to „vacant‟ – indicating that another
person can use it. Hence, girl B proceeds to use the changing room, while the sign displays „occupied‟ again.
The changing room is nothing but the critical section, boy A and girl B are two different processes, while the sign outside the
changing room indicates the process synchronization mechanism being used.
For achieving mutual exclusion, while one process is busy updating shared memory in its critical region, no other process will
enter its critical region and cause trouble.
In computer science, mutual exclusion refers to the requirement of ensuring that no two concurrent processes are in their
critical section at the same time; it is a basic requirement in concurrency control, to prevent race conditions.
1. Mutual exclusion must be enforced only one process at a time is allowed into its critical section, among all processes that
have critical sections for the same resource or shared object.
2. A process that halts in its non-critical section must do so without interfering with other processes.
3. It must not be possible for a process requiring access to a critical section to be delayed indefinitely: no deadlock or
starvation.
4. When no process is in a critical section, any process that requests entry to its critical section must be permitted to enter
without delay.
6. A process remains inside its critical section for a finite time only.
1. PETERSON’S SOLUTION
Peterson‟s Solution is a classical software based solution to the critical section problem. Peterson's algorithm is a concurrent
programming algorithm developed by Gary L. Peterson in a 1981 paper. It is known as a simple algorithm when compared to
others. Peterson proved the algorithm using both the 2-process case and the N-process case.
Peterson's algorithm is used for mutual exclusion and allows two processes to share a single-use resource without conflict. It
uses only shared memory for communication. Peterson's formula originally worked only with two processes, but has since
been generalized for more than two.
1. boolean flag[i]: Initialized to FALSE, initially no one is interested in entering the critical section
2. int turn: The process whose turn is to enter the critical section.
Problem: The producer consumer problem (or bounded buffer problem) describes two processes, the producer and the
consumer, which share a common, fixed-size buffer used as a queue. Producer produce an item and put it into buffer. If
buffer is already full then producer will have to wait for an empty block in buffer. Consumer consume an item from buffer. If
buffer is already empty then consumer will have to wait for an item in buffer. Implement Peterson‟s Algorithm for the two
processes using shared memory such that there is mutual exclusion between them. The solution should have free from
synchronization problems.
Peterson’s algorithm
// otherwise producer will produce an item and put it into buffer (critical Section)
flag[j] = false; // Now, producer is out of critical section
// end of code for producer
Explanation of Peterson’s algorithm - Peterson‟s Algorithm is used to synchronize two processes. It uses two variables, a
bool array flag of size 2 and an int variable turn to accomplish it.
In the solution i represent the Consumer and j represents the Producer. Initially the flags are false. When a process wants to
execute its critical section, it sets its flag to true and turn as the index of the other process. This means that the process wants
to execute but it will allow the other process to run first. The process performs busy waiting until the other process has
finished its own critical section.
After this the current process enters its critical section and adds or removes a random number from the shared buffer. After
completing the critical section, it sets its own flag to false, indication it does not wish to execute anymore.
1. Mutual Exclusion is assured as only one process can access the critical section at any time.
2. Progress is also assured, as a process outside the critical section does not block other processes from entering the critical
section.
3. Bounded Waiting is preserved as every process gets a fair chance.
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.
Semaphore was proposed by Dijkstra in 1965 which is a very significant technique to manage concurrent processes by using
a simple integer value, which is known as a semaphore. Semaphore is simply an integer variable that is shared between
threads. This variable is used to solve the critical section problem and to achieve process synchronization in the
multiprocessing environment. So it is basically a synchronizing tool and is accessed only through two low standard atomic
operations, wait and signal designated by P(S) and V(S) respectively.
In very simple words, the semaphore is a variable that can hold only a non-negative Integer value, shared between all the
threads, with operations wait and signal, which work as follow:
Wait: This operation decrements the value of its argument S, as soon as it would become non-negative(greater than or equal
to 1). This Operation mainly helps you to control the entry of a task into the critical section. In the case of the negative or
zero value, no operation is executed. wait() operation was originally termed as P; so it is also known as P(S) operation. The
definition of wait operation is as follows:
wait(S)
{
while (S<=0);//no operation
S--;
}
Note: When one process modifies the value of a semaphore then, no other process can simultaneously modify that same
semaphore's value. In the above case the integer value of S(S<=0) as well as the possible modification that is S-- must be
executed without any interruption.
Signal: Increments the value of its argument S, as there is no more process blocked on the queue. This Operation is mainly
used to control the exit of a task from the critical section. signal() operation was originally termed as V; so it is also known
as V(S) operation. The definition of signal operation is as follows:
signal(S)
{
S++;
}
Also, note that all the modifications to the integer value of semaphore in the wait() and signal() operations must be executed
indivisibly.
Properties of Semaphores
TYPES OF SEMAPHORE
1. 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.
They are also known as mutex locks, as the locks can provide mutual exclusion. All the processes can share the same mutex
semaphore that is initialized to 1.
2. 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.
Advantages of Semaphore
1. In the Semaphore, only one process is allowed to enter into the critical section. In this, the principle of mutual exclusion
is to be followed strictly. And the semaphore mechanism is a more efficient mechanism than other methods which we
use in process synchronization.
2. It is machine-independent because it is implemented in the microkernel‟s machine-independent code.
3. With the help of the semaphore, the resources are managed flexibly.
4. In semaphore, there is a busy waiting, so there is no wastage of resources and process time.
5. In the semaphore, more threads are permitted to enter into the critical section.
Disadvantages of Semaphore
1. There is a priority inversion in the semaphore, and this is the biggest drawback of the semaphore.
2. In the semaphore, due to program error violation of mutual exclusion, deadlock can be happened.
3. In the semaphore, there are more chances of programmer errors.
4. For the purpose of large-scale use, the semaphore is not a practical method, and due to this, there may be loss of
modularity.
5. The Programming of the semaphore is tough, so there may be a possibility of not achieving mutual exclusion.
6. In the semaphore, the OS has to preserve all calls to wait and signal semaphore.
Prerequisite – Semaphores in operating system, Inter Process Communication Producer consumer problem is a classical
synchronization problem. We can solve this problem by using semaphores.
A semaphore S is an integer variable that can be accessed only through two standard operations : wait() and signal().
The wait() operation reduces the value of semaphore by 1 and the signal() operation increases its value by 1.
wait(S){
while(S<=0); // busy waiting
S--;
}
signal(S){
S++;
}
Problem Statement – We have a buffer of fixed size. A producer can produce an item and can place in the buffer. A
consumer can pick items and can consume them. We need to ensure that when a producer is placing an item in the buffer,
then at the same time consumer should not consume any item. In this problem, buffer is the critical section.
To solve this problem, we need two counting semaphores – Full and Empty. “Full” keeps track of number of items in the
buffer at any given time and “Empty” keeps track of number of unoccupied slots.
Initialization of semaphores –
mutex = 1
Full = 0 // Initially, all slots are empty. Thus full slots are 0
Empty = n // All slots are empty initially
do{
//produce an item
wait(empty);
wait(mutex);
//place in buffer
signal(mutex);
signal(full);
}while(true)
When producer produces an item then the value of “empty” is reduced by 1 because one slot will be filled now. The value of
mutex is also reduced to prevent consumer to access the buffer. Now, the producer has placed the item and thus the value of
“full” is increased by 1. The value of mutex is also increased by 1 because the task of producer has been completed and
consumer can access the buffer.
do{
wait(full);
wait(mutex);
}while(true)
As the consumer is removing an item from buffer, therefore the value of “full” is reduced by 1 and the value is mutex is also
reduced so that the producer cannot access the buffer at this moment. Now, the consumer has consumed the item, thus
increasing the value of “empty” by 1. The value of mutex is also increased so that producer can access the buffer now.
A monitor is a collection of procedures, variables, and data structures that are all grouped together in a special kind of
module or package.
Processes may call the procedures in a monitor whenever they want to, but they cannot directly access the monitor‟s
internal data structures from procedures declared outside the monitor.
Monitor simply defines a lock and a condition variable for managing concurrent access to the shared data.
Lock insures that only one single thread is active in the monitor at a time by providing mutual exclusion.
Condition variable enable thread to go to sleep inside of critical section by releasing their lock at the same time it puts
the thread to go to sleep.
To overcome the timing errors that occurs while using semaphore for process synchronization, the researchers have
introduced a high-level synchronization construct i.e. the monitor type. A monitor type is an abstract data type that is used
for process synchronization.
Being an abstract data type monitor type contains the shared data variables that are to be shared by all the processes and
some programmer-defined operations that allow processes to execute in mutual exclusion within the monitor. A process
can not directly access the shared data variable in the monitor; the process has to access it through procedures defined in
the monitor which allow only one process to access the shared variables in a monitor at a time.
monitor monitor_name
{
//shared variable declarations
procedure P1 ( . . . ) {
}
procedure P2 ( . . . ) {
}
procedure Pn ( . . . ) {
}
initialization code ( . . . ) {
}
}
A monitor is a construct such as only one process is active at a time within the monitor. If other process tries to access the
shared variable in monitor, it gets blocked and is lined up in the queue to get the access to shared data when previously
accessing process releases it.
Conditional variables were introduced for additional synchronization mechanism. The conditional variable allows a process
to wait inside the monitor and allows a waiting process to resume immediately when the other process releases the
resources.
The conditional variable can invoke only two operation wait() and signal(). Where if a process P invokes a wait() operation
it gets suspended in the monitor till other process Q invoke signal() operation i.e. a signal() operation invoked by a process
resumes the suspended process.
Drawback of Monitors
1. Major weakness of monitors is the absence of concurrency if a monitor encapsulates the resources, since only one
process can be active within a monitor at a time.
2. There is the possibility of deadlock in the case of nested monitor calls
3. Monitor concept is its lack of implementation most commonly used programming language.
4. Monitors cannot easily be added if they are not natively supported by the language.
Even though both the semaphores and monitors are used to achieve mutual exclusion in parallel programming
environments, they differ in the techniques used to achieve this task.
In monitors, the code that is used to achieve mutual exclusion is in a single place and is more structured, while code for
semaphores are distributed as wait and signal function calls.
Also, it is very easy to make mistakes when implementing semaphores, while there is a very little chance to make
mistakes when implementing monitors.
Further, monitors use condition variable, while semaphores do not.
UNIT 3 – MEMORY MANAGEMENT
MEMORY
Computer Memory is basically known as a collection of data that is represented in binary format.
Main Memory refers to a physical memory that is the internal memory of the computer.
The word main is used to distinguish it from external mass storage devices such as disk drives.
Main memory is also known as RAM.
The computer is able to change only data that is in the main memory.
Therefore, every program we execute and every file we access must be copied from a storage device into main memory.
1. External Memory or Secondary Memory: Comprising of Magnetic Disk, Optical Disk, and Magnetic Tape i.e. peripheral
storage devices which are accessible by the processor via I/O module.
Comprising of Main Memory, Cache Memory & CPU registers. This is directly accessible by the processor.
a. Cache Memory: A small size of memory that is incredible fast, expensive, and volatile.
b. Random Access Memory: A medium-sized memory of a few gigabytes, medium-priced, medium-fast, and volatile.
c. HDD or SSD: A large-sized memory of up to a terabyte of storage, cheap, slow, and nonvolatile.
MEMORY MANAGEMENT IN OS
Memory Management is the process of controlling and coordinating computer memory, assigning portions known as blocks
to various running programs to optimize the overall performance of the system. It is the most important function of an
operating system which handles or manages primary memory and moves processes back and forth between main memory
and disk during execution.
Memory management keeps track of each and every memory location, regardless of either it is allocated to some
process or it is free.
It also keeps the count of how much memory can be allocated to a process.
It decides which process will get memory at what time.
It tracks whenever some memory gets freed or unallocated and correspondingly it updates the status.
NEED FOR MEMORY MANAGEMENT IN OS - Memory management technique is needed for the following reasons:
This technique helps in placing the programs in memory in such a way so that memory is utilized at its fullest extent.
This technique helps to protect different processes from each other so that they do not interfere with each other's
operations.
It helps to allocate space to different application routines.
This technique allows you to check how much memory needs to be allocated to processes that decide which processor
should get memory at what time.
It keeps the track of each memory location whether it is free or allocated.
This technique keeps the track of inventory whenever memory gets freed or unallocated and it will update the status
accordingly.
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. In Contiguous Technique, executing
process must be loaded entirely in main-memory. Contiguous Technique can be divided into:
CONTIGUOUS MEMORY ALLOCATION
In contiguous memory allocation, whenever any user process request for the memory then a single section of the
contiguous memory block is allocated to that process according to the requirements of the process.
In this, all the available memory space remains together at one place and freely available memory partitions are not
distributed here and there across the whole memory space.
The process of dividing the memory into sections is called memory partitioning.
For better utilization of memory and flow of execution, the memory is partitioned into blocks of different sizes to
accommodate the program.
The partitioned allocation technique was introduced to solve the problem of wasted time and memory.
In partition allocation, main memory is divided into a number of partitions, whereas in fixed size of partitioning allocation,
partition can be of equal or different sizes. This technique is also known as static partitioning.
In this scheme, the system divides the memory into fixed-size partitions. The partitions may or may not be the same size.
The size of each partition is fixed as indicated by the name of the technique and it cannot be changed.
In this partition scheme, each partition may contain exactly one process.
There is a problem that this technique will limit the degree of multiprogramming because the number of partitions will
basically decide the number of processes.
Whenever any process terminates then the partition becomes available for another process.
As the allocation of memory is contiguous, no spanning is allowed.
In fixed partitioning, the partitions are made either before execution or during system configuration.
EXAMPLE - Let's take an example of fixed size partitioning scheme, we will divide a memory size of 15 KB into fixed-size
partitions:
It is important to note that these partitions are allocated to the processes as they arrive and the partition that is
allocated to the arrived process basically depends on the algorithm followed.
If there is some wastage inside the partition then it is termed Internal Fragmentation.
WHAT IS FRAGMENTATION?
Processes are stored and removed from memory, which creates free memory space, which are too small to use by other
processes.
After sometimes, that processes not able to allocate to memory blocks because its small size and memory blocks always
remain unused is called fragmentation.
This type of problem happens during a dynamic memory allocation system when free blocks are quite small, so it is not
able to fulfill any request.
Two types of Fragmentation methods are: External fragmentation and Internal fragmentation
External fragmentation can be reduced by rearranging memory contents to place all free memory together in a single block.
The internal fragmentation can be reduced by assigning the smallest partition, which is still good enough to carry the entire
process.
Internal Fragmentation occurs when the memory blocks of External Fragmentation occurs when the memory blocks of
fixed-size are allocated to the processes. variable-size are allocated to the processes dynamically.
This type of fragmentation mainly occurs when the fixed When the memory space in the system can easily satisfy the
size partition is assigned to a process whose size is less requirement of the processes, but this available memory space
than the size of the partition due to which the rest of the is non-contiguous, So it can‟t be utilized further.
space in the partition becomes unusable.
The difference between allocated memory and memory Unused memory spaces between the non-contiguous memory
required by a process is called Internal fragmentation. fragments that are too small to serve a new process are called
External fragmentation.
It mainly refers to the unused space in the partition that It mainly refers to the unused blocks of the memory that are
resides in the allocated region; as suggested by the name. not contiguous and hence are unable to satisfy the
requirements of the process.
Best-fit block can be used to overcome the problem of Compaction, segmentation, and paging can be used to
Internal fragmentation.
overcome the problem of External fragmentation.
Paging suffers from Internal fragmentation. First-fit and best-fit suffer from external fragmentation.
It supports multiprogramming as multiple processes can be stored inside the main memory.
Management is easy using this scheme.
1. Internal Fragmentation: Suppose the size of the process is lesser than the size of the partition in that case some size of
the partition gets wasted and remains unused. This wastage inside the memory is generally termed as Internal fragmentation.
As we have shown in the above diagram the 70 KB partition is used to load a process of 50 KB so the remaining 20 KB got
wasted.
2. Limitation on the Size of the Process: If in a case size of a process is more than that of a maximum-sized partition then
that process cannot be loaded into the memory. Due to this, a condition is imposed on the size of the process and it is: the
size of the process cannot be larger than the size of the largest partition.
3. External Fragmentation: It is another drawback of the fixed-size partition scheme as total unused space by various
partitions cannot be used in order to load the processes even though there is the availability of space but it is not in the
contiguous fashion.
4. Degree of Multiprogramming is Less: In this partition scheme, as the size of the partition cannot change according to
the size of the process. Thus the degree of multiprogramming is very less and is fixed.
This scheme is also known as dynamic partitioning and came into existence to overcome the drawback i.e. internal
fragmentation that is caused by static partitioning.
EXAMPLE
2. Degree of Multiprogramming is Dynamic: As there is no internal fragmentation in this partition scheme due to which
there is no unused space in the memory. Thus more processes can be loaded into the memory at the same time.
3. No Limitation on the Size of Process: In this partition scheme as the partition is allocated to the process dynamically
thus the size of the process cannot be restricted because the partition size is decided according to the process size.
1. External Fragmentation: As there is no internal fragmentation which is an advantage of using this partition scheme does
not mean there will no external fragmentation. Let us understand this with the help of an example: In the above diagram
process P1(3MB) and process P3(8MB) completed their execution. Hence there are two spaces left i.e. 3MB and 8MB. Let‟s
there is a Process P4 of size 15 MB comes.
But the empty space in memory cannot be allocated as no spanning is allowed in contiguous allocation. Because the rule
says that process must be continuously present in the main memory in order to get executed. Thus it results in External
Fragmentation.
2. Difficult Implementation (Complex Memory Allocation): The implementation of this partition scheme is difficult as
compared to the Fixed Partitioning scheme as it involves the allocation of memory at run time rather than during the system
configuration.
In Fixed partitioning, the list of partitions is made once and will never change but in dynamic partitioning, the allocation and
deallocation is very complex since the partition size will be varied every time when it is assigned to a new process OS has to
keep track of all the partitions
Due to the fact that the allocation and deallocation are done very frequently in dynamic memory allocation and the partition
size will be changed at each time, it is going to be very difficult for OS to manage everything
NON-CONTIGUOUS ALLOCATION
Paging and Segmentation are the two ways which allow a process‟s physical address space to be non-contiguous. It
has advantage of reducing memory wastage but it increases the overheads due to address translation. It slows the execution
of the memory because time is consumed in address translation.
In non-contiguous allocation, Operating system needs to maintain the table which is called Page Table for each process
which contains the base address of the each block which is acquired by the process in memory space. In non-contiguous
memory allocation, different parts of a process are allocated different places in Main Memory. Spanning is allowed which is
not possible in other techniques like Dynamic or Static Contiguous memory allocation. That‟s why paging is needed to
ensure effective memory allocation. Paging is done to remove External Fragmentation.
Working: Here a process can be spanned across different spaces in main memory in non-consecutive manner. Suppose
process P of size 4KB. Consider main memory have two empty slots each of size 2KB. Hence total free space is, 2*2= 4 KB. In
contiguous memory allocation, process P cannot be accommodated as spanning is not allowed.
In contiguous allocation, space in memory should be allocated to whole process. If not, then that space remains unallocated.
But in Non-Contiguous allocation, process can be divided into different parts and hence filling the space in main memory. In
this example, process P can be divided into two parts of equal size – 2KB. Hence one part of process P can be allocated to
first 2KB space of main memory and other part of process P can be allocated to second 2KB space of main memory. Below
diagram will explain in better way:
But, in what manner we divide a process to allocate them into main memory is very important to understand. Process is
divided after analyzing the number of empty spaces and their size in main memory. Then only we divide our process. It is
very time consuming process. Their number as well as their sizes changing every time due to execution of already present
processes in main memory.
In order to avoid this time consuming process, we divide our process in secondary memory in advance before reaching the
main memory for its execution. Every process is divided into various parts of equal size called Pages. We also divide our main
memory into different parts of equal size called Frames. It is important to understand that:
Although their numbers can be different. Below diagram will make you understand in better way: consider empty main
memory having size of each frame is 2 KB, and two processes P1 and P2 are 2 KB each.
Address Binding
Programs are stored on the secondary storage disks as binary executable files.
When the programs are to be executed they are brought into the main memory and placed within a process.
The collection of processes on the disk waiting to enter the main memory forms the input queue.
One of the processes which are to be executed is fetched from the queue and placed in the main memory.
During the execution it fetches instruction and data from main memory.
After the process terminates it returns back the memory space.
During execution the process will go through different steps and in each step the address is represented in different
ways.
In source program the address is symbolic.
The compiler converts the symbolic address to re-locatable address.
The loader will convert this re-locatable address to absolute address.
The process address space is the set of logical addresses that a process references in its code. The operating system takes
care of mapping the logical addresses to physical addresses at the time of memory allocation to the program.
There are three types of addresses used in a program before and after memory is allocated:
The concept of a logical address space that is bound to a separate physical address space is central to proper memory
management.
Logical Address is generated by CPU while a program is running. The logical address is virtual address as it does not exist
physically, therefore, it is also known as Virtual Address. This address is used as a reference to access the physical memory
location by CPU. The term Logical Address Space is used for the set of all logical addresses generated by a program‟s
perspective.
The hardware device called Memory-Management Unit is used for mapping logical address to its corresponding physical
address.
Physical Address identifies a physical location of required data in a memory. The user never directly deals with the physical
address but can access by its corresponding logical address. The user program generates the logical address and thinks that
the program is running in this logical address but the program needs physical memory for its execution, therefore, the logical
address must be mapped to the physical address by MMU before they are used. The term Physical Address Space is used for
all physical addresses corresponding to the logical addresses in a Logical address space.
The runtime mapping from virtual (logical) to physical address is done by the memory management unit (which is a hardware
device. MMU uses following mechanism to convert virtual address to physical address
MMU Example
The value in the base register is added to every address generated by a user process, which is treated as offset at the
time it is sent to memory.
Suppose the base is at 14000, then an attempt by the user to address location 0 is relocated dynamically to 14000; thus
access to location 356 is mapped to 14356.
It is important to note that the user program never sees the real physical addresses.
The Program can create a pointer to location 356 and store it in the memory and then manipulate it after that compare
it with other addresses as number 356.
METHODS INVOLVED IN MEMORY MANAGEMENT
There are various methods and with their help Memory Management can be done intelligently by the Operating System:
Paging is a technique in which the main memory of computer system is organized in the form of equal sized blocks
called pages. In this technique, the address of occupied pages of physical memory is stored in a table, which is known as
page table.
Paging enables the operating system to obtain data from the physical memory location without specifying lengthy memory
address in the instruction. In this technique, the virtual address is used to map the physical address of the data. The length of
virtual address is specified in the instruction and is smaller than physical address of the data. It consists of two different
numbers, first number is the address of page called virtual page in the page table and the second number is the offset value
of the actual data in the page.
A computer can address more memory than the amount physically installed on the system.
This extra memory is actually called virtual memory and it is a section of a hard that's set up to emulate the computer's
RAM.
Paging technique plays an important role in implementing virtual memory.
Paging is a storage mechanism that allows OS to retrieve processes from the secondary storage into the main memory
in the form of pages.
In other words, paging is a memory management technique in which process address space is broken into blocks of the
same size called pages.
Similarly, the main memory is divided into small fixed-sized blocks of (physical) memory called frames.
The size of a frame should be kept the same as that of a page to have maximum utilization of the main memory.
Paging helps to avoid external fragmentation and the need for compaction.
Paging is used for faster access to data, and it is a logical concept.
Paging permits the physical address space of a process to be non-contiguous. It is a fixed-size partitioning scheme.
In paging, the logical address space is divided into fixed-sized blocks known as Pages. The physical address space is also
divided into fixed-size blocks known as Frames.
A page is mapped into a frame. The individual pages and frames are recognized by unique numbers known as the page
number and frame number respectively.
The size of the page is equal to the size of a frame. A page number forms a part of the logical address and a frame number
forms a part of a physical address.
ADDRESS TRANSLATION
Following steps are followed to translate logical address into physical address:
Step-1: Page address is called logical address and represented by page number and the offset.
Page Number is used to specify the specific page of the process from which the CPU wants to read the data and it is
also used as an index to the page table.
Page Offset is mainly used to specify the specific word on the page that the CPU wants to read.
Page Table provides the corresponding frame number (base address of the frame) where that page is stored in the main
memory.
Step-3: Frame address is called physical address and represented by a frame number and the offset.
Where,
Frame Number specifies the specific frame where the required page is stored.
Page Offset specifies the specific word that has to be read from that page.
PAGE TABLE IN OS
Characteristics:
Diagram: The following diagram illustrates the above steps of translating logical address into physical address:
The above diagram indicates the translation of the Logical address into the Physical address.
The PTBR in the above diagram means page table base register and it basically holds the base address for the page table
of the current process.
The PTBR is mainly a processor register and is managed by the operating system.
When the system allocates a frame to any page, it translates this logical address into a physical address and create entry
into the page table to be used throughout execution of the program
When a process is to be executed, its corresponding pages are loaded into any available memory frames
Commonly, each process running on a processor needs its own logical address space.
But there is a problem with this approach and that is with the time required to access a user memory location.
Suppose if we want to find the location i, we must first find the index into the page table by using the value in the PTBR
offset by the page number for i. And this task requires memory access.
It then provides us the frame number which is combined with the page offset in order to produce the actual address.
After that, we can then access the desired place in the memory.
EXAMPLE
For example, if the main memory size is 16 KB and Frame size is 1 KB.
Here, the main memory will be divided into the collection of 16 frames of 1 KB each.
There are 4 separate processes in the system that is A1, A2, A3, and A4 of 4 KB each.
Here, all the processes are divided into pages of 1 KB each so that operating system can store one page in one frame.
At the beginning of the process, all the frames remain empty so that all the pages of the processes will get stored in a
contiguous way.
In this example you can see that A2 and A4 are moved to the waiting state after some time.
Therefore, eight frames become empty, and so other pages can be loaded in that empty blocks.
The process A5 of size 8 pages (8 KB) are waiting in the ready queue.
In this example, you can see that there are eight non-contiguous frames which is available in the memory, and paging
offers the flexibility of storing the process at the different places.
Advantages of Paging
Disadvantages of Paging
Page table requires extra memory space, so may not be good for a system having small RAM.
May cause Internal fragmentation by this technique.
There is an increase in time taken to fetch the instruction since now two memory accesses are required.
Complex memory management algorithm.
Multi-level paging may lead to memory reference overhead.
SEGMENTATION
Segmentation refers to the technique of dividing the physical memory space into multiple blocks. Each block has
specific length and is known as a segment. Each
segment has a starting address called the base
address. The length of the segment determines the
availability memory space in the segment.
1. Virtual memory segmentation - Each process is divided into a number of segments, not all of which are resident at any
one point in time.
2. Simple segmentation - Each process is divided into a number of segments, all of which are loaded into memory at run
time, though not necessarily contiguously.
There is no simple relationship between logical addresses and physical addresses in segmentation. A table stores the
information about all such segments and is called Segment Table.
Segment Table – It maps two-dimensional Logical address into one-dimensional Physical address. It‟s each table entry has:
Base Address: It contains the starting physical address where the segments reside in memory.
Initially, we used the Paging technique for memory management. But instead of the user, it is closer to the operating system
more probably. In paging, all the processes are split into the pages. Sometimes there may be a situation where a process
having some relevant sections of the function requires being loaded on the same page.
The operating system does not mind the process from the user‟s point of view. The operating system split the same function
into the number of pages, and at the same time, the pages may be loaded or not loaded into the memory. Due to this
reason, system efficiency could be decreased.
So, it is better to use the segmentation technique for memory management. In segmentation, the process is split into the
segments. In this, every segment consists of a similar kind of function as a library function contained in one segment, and the
main function contained in the other segment.
Advantages of Segmentation
No internal fragmentation
Average Segment Size is larger than the actual page size
Less overhead
It is easier to relocate segments than entire address space
The segment table is of lesser size as compared to the page table in paging
Disadvantages of Segmentation
Major Limitation of Single Level Paging - A big challenge with single level paging is that if the logical address space is
large, then the page table may take up a lot of space in main memory. For instance, consider that logical address is 32 bit
and each page is 4 KB, the number of pages will be 2^20 pages. The page table without additional bits will be of the size 20
20
bits * 2 or 2.5 MB. Since each process has its own page table, a lot of memory will be consumed when single level paging is
used. For a system with 64-bit logical address even a page table of single process will not fit in main memory. For a process
with a large logical address space, a lot of its page table entries are invalid as a lot of the logical address space goes unused.
Segmented Paging
A solution to the problem is to use segmentation along with paging to reduce the size of page table. Traditionally, a program
is divided into four segments, namely code segment, data segment, stack segment and heap segment.
Segments of a process
The size of the page table can be reduced by creating a page table for each segment. To accomplish this hardware support is
required. The address provided by CPU will now be partitioned into segment no., page no. and offset.
The memory management unit (MMU) will use the segment table which will contain the address of page table(base) and
limit. The page table will point to the page frames of the segments in main memory.
Advantages of Segmented Paging
1. The page table size is reduced as pages are present only for data of segments, hence reducing the memory
requirements.
2. Gives a programmers view along with the advantages of paging.
3. Reduces external fragmentation in comparison with segmentation.
4. Since the entire segment need not be swapped out, the swapping out into virtual memory becomes easier .
Paged Segmentation
1. In segmented paging, not every process has the same number of segments and the segment tables can be large in size
which will cause external fragmentation due to the varying segment table sizes. To solve this problem, we use paged
segmentation which requires the segment table to be paged. The logical address generated by the CPU will now consist
of page no #1, segment no, page no #2 and offset.
2. The page table even with segmented paging can have a lot of invalid pages. Instead of using multi-level paging along
with segmented paging, the problem of larger page table can be solved by directly applying multi-level paging instead
of segmented paging.
Advantages of Paged Segmentation
1. No external fragmentation
2. Reduced memory requirements as no. of pages limited to segment size.
3. Page table size is smaller just like segmented paging,
4. Similar to segmented paging, the entire segment need not be swapped out.
SWAPPING
Swapping is the technique used by an operating system for efficient management of memory space of a computer
system. Swapping involves performing two tasks called swapping in and swapping out. The task of placing the pages
or blocks of data from the hard disk to the main memory is called swapping in. On the other hand, the task of removing
pages or blocks of data from main memory to the hard disk is called swapping out. The swapping technique is useful when
larger program is to be executed or some operations have to be performed on a large file.
Swapping is a mechanism in which a process (inactive programs) can be swapped temporarily out of main memory (or
move) to secondary storage (disk) and make that memory available to other processes. It will be later brought back into
the memory for continue execution.
Though performance is usually affected by swapping process but it helps in running multiple and big processes in
parallel and that's the reason swapping is also known as a technique for memory compaction.
The above diagram shows swapping of two processes where the disk is used as a backing store.
In the above diagram, suppose there is a multiprogramming environment with a round-robin scheduling algorithm;
whenever the time quantum expires then the memory manager starts to swap out those processes that are just finished
and swap another process into the memory that has been freed.
And in the meantime, the CPU scheduler allocates the time slice to some other processes in the memory.
When the process with higher priority finishes then the process with lower priority swapped back in and continues its
execution.
There are two more concepts that come in the swapping technique and these are: swap in and swap out.
Swap In and Swap Out in OS - The concept of swapping consists of two more concepts, swap in and swap out.
Swap Out is the method of removing a process from the main memory or RAM and then adding it to the Hard Disk.
On the other hand, Swap In is the procedure by which any process gets removed from the hard disk and placed it back
into the main memory or RAM.
The total time taken by swapping process includes the time it takes to move the entire process to a secondary disk and
then to copy the process back to memory, as well as the time the process takes to regain main memory.
Let us assume that the user process is of size 2048KB and on a standard hard disk where swapping will take place has a
data transfer rate around 1 MB per second. The actual transfer of the 1000K process to or from memory will take
Now considering in and out time, it will take complete 4000 milliseconds plus other overhead where the process
competes to regain main memory.
ADVANTAGES OF SWAPPING
Allows dynamic relocation. For example, if address binding at execution time is being used, then processes can be swap
in different locations. Else in case of compile and load time bindings, processes should be moved to the same location.
Minimum wastage of CPU time on completion so it can easily be applied to a priority-based scheduling method to
improve its performance.
Swapping makes a CPU perform several tasks simultaneously. Hence, processes do not have to wait for too long before
they are executed.
This technique can be easily applied to priority-based scheduling in order to improve its performance.
DISADVANTAGES OF SWAPPING
There may occur inefficiency in the case if a resource or a variable is commonly used by those processes that are
participating in the swapping process.
If the algorithm used for swapping is not good then the overall method can increase the number of page faults and thus
decline the overall performance of processing.
If the computer system loses power at the time of high swapping activity then the user might lose all the information
related to the program.
Although the process has no significant or effective disadvantage, the overall method depends heavily on the virtual
memory, such dependability results in a significant performance drop.
VIRTUAL MEMORY IN OS
The primary memory (such as RAM) is expensive and limited in the system; however, the secondary memory is cheaper
and large and can be extended easily.
Virtual memory concept offers unique solution for apparent primary memory expansion.
Virtual memory is a storage mechanism which offers user an illusion of having a very big main memory. It is done by
treating a part of secondary memory as the main memory.
It essentially with the help of OS, and the software and hardware installed on the system helps to access more memory
than what actually is installed on the system, by transferring data from the RAM to the disk.
In Virtual memory, the user can store processes with a bigger size than the available main memory.
Instead of loading one long process in the main memory, the OS loads the various parts of more than one process in the
main memory.
In an operating system, the memory is usually stored in the form of units that are known as pages.
This is also known as lazy swapper because page is not swapped into the memory unless it is required.
A demand paging mechanism is very much similar to a paging system with swapping where processes stored in the
secondary memory and pages are loaded only on demand, not in advance.
When a context switch occurs, the operating system does not copy any of the old program‟s pages out to the disk or
any of the new program‟s pages into the main memory.
Instead, it will start executing the new program after loading the first page and fetches the program's pages, which are
referenced.
During the program execution, if the program references a page that may not be available in the main memory because
it was swapped, then the processor considers it as an invalid memory reference as a page fault and transfers control
back from the program to the OS, which demands to store page back into the memory.
PAGE FAULT
In this technique we need some hardware support to distinct between the pages that are in memory and those that are
on the disk. A valid and invalid bit is used for this purpose.
When this bit is set to valid it indicates that the associate page is in memory.
If the bit is set to invalid it indicates that the page is either not valid or is valid but currently not in the disk.
It mainly occurs when any program tries to access the data or the code that is in the address space of the program, but
that data is not currently located in the RAM of the system.
So basically when the page referenced by the CPU is not found in the main memory then the situation is termed as Page
Fault.
Whenever any page fault occurs, then the required page has to be fetched from the secondary memory into the main
memory.
In case if the required page is not loaded into the memory, then a page fault trap arises
The page fault mainly generates an exception, which is used to notify the operating system that it must have to retrieve
the "pages" from the virtual memory in order to continue the execution.
Once all the data is moved into the physical memory the program continues its execution normally.
The main steps involved in demand paging that is in between the page is requested and it is loaded into main memory
are as follows:
1. CPU refers to the page it needs. The referred page is checked in page table whether that is present in main memory or
not. If not an interrupt page fault is generated.
2. OS puts the interrupts process in blocking state and starts the process of fetching the page from memory so that
process can be executed.
4. 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.
6. CPU is informed about the update and asked to go ahead with the execution and process gets back into the ready state
i.e. interrupted process will be restarted.
Number of tables and amount of processor overhead for handling page interrupts are greater than in the case of the
simple paged management techniques.
Due to the lack of explicit constraints on jobs address space size.
PAGE REPLACEMENT
When all the frames are in main memory, it is necessary to bring a new page to satisfy the page fault, replacement policy
is concerned with selecting a page currently in memory to be replaced.
In Virtual Memory Management, Page Replacement Algorithms play an important role. The main objective of all the Page
replacement policies is to decrease the maximum number of page faults
Page Fault: It is basically a memory error, and it occurs when the current programs attempt to access the memory page for
mapping into virtual address space, but it is unable to load into the physical memory then this is referred to as Page fault.
Page replacement algorithms are the techniques using which an OS decides which memory pages to swap out, write to
disk when a page of memory needs to be allocated.
A page replacement algorithm looks at the limited information about accessing the pages provided by hardware, and
tries to select which pages should be replaced to minimize the total number of page misses, while balancing it with the
costs of primary storage and processor time of the algorithm itself.
TYPES OF PAGE REPLACEMENT ALGORITHM
This algorithm helps to decide which pages must be swapped out from the main memory in order to create a room for the
incoming page.
A good page replacement algorithm is one that minimizes the number of page faults.
Example: Consider the following references string with frames initially empty.
The first three references (7,0,1) cases page faults and are brought into the empty frames.
The next references 2 replaces page 7 because the page 7 was brought in first.
Since 0 is the next references and 0 is already in memory i.e. no page faults.
The next references 3 results in page 0 being replaced so that the next references to 0 causer page fault. This will
continue till the end of string.
Analysis:
Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (15 / 20)*100
= 75%
Advantages
Disadvantages
This algorithm does not make the use of the frequency of last used time rather it just replaces the Oldest Page.
There is an increase in page faults as page frames increases.
The performance of this algorithm is the worst.
BELADY S A NOMALY
Belady found that, in FIFO page replacement algorithm, the number of page faults will get increased with the increment
in number of frames.
This is the strange behavior shown by FIFO algorithm in some of the cases. This is an Anomaly called as Belady‟s
Anomaly.
However, in the case of LRU and optimal page replacement algorithms, it is seen that the number of page faults will be
reduced if we increase the number of frames.
As the name suggests, this algorithm works on the principle of “Least Recently Used“.
The page that has not been used for the longest time in the main memory will be selected for replacement.
This algorithm is easy to implement.
This algorithm makes use of the counter along with the even page.
Example:
Analysis:
Advantages of LRU
It is an efficient technique.
With this algorithm, it becomes easy to identify the faulty pages that are not needed for a long time.
It helps in Full analysis.
Disadvantages of LRU
3. OPTIMAL ALGORITHM
Optimal page replacement algorithm is mainly to solve the problem of Belady‟s Anomaly.
This algorithm mainly replaces the page that will not be used for the longest time in the future.
The practical implementation of this algorithm is not possible.
This is because we cannot predict in advance those pages that will not be used for the longest time in the future.
However, it is the best known algorithm and gives the least number of page faults.
Hence, this algorithm can be used to measure the performance of other algorithms.
Example: Consider the following references string with frames initially empty.
The first three references cause faults that fill the three empty frames.
The references to page 2 replaces page 7, because 7 will not be used until reference 18.
The page 0 will be used at 5 and page 1 at 14.
With only 9 page faults, optimal replacement is much better than a FIFO, which had 15 faults.
This algorithm is difficult t implement because it requires future knowledge of reference strings.
Analysis:
Advantages of OPR
Disadvantages of OPR
It is a page replacement algorithm. This algorithm removes a page at random from the lowest numbered non-empty class.
Implicit in this algorithm is that it is better to remove a modified page that has not been referenced in at least one clock tick
than a clean page that is in heavy use.
It is easy to understand, moderately efficient to implement and gives a performance that while certainly not optimal, may be
adequate. When page is modified, a modified bit is set. When a page needs to be replaced, the Operating System divides
pages into 4 classes.
Out of above 4 categories, NRU will replace a Not Referenced, Not Modified page, if such page exists. Note that this
algorithm implies that a Modified but Not Referenced is less important than a Not Modified and Referenced.
Example
Algorithm: From the given reference string NRU will remove a page at random from the lowest numbered nonempty class.
Implicit in this algorithm is that it is better to remove a modified page that has not been referenced in at least one clock tick
(typically 20 msec) than a clean page that is in heavy use.
So, this algorithm may behave like any other algorithm like FIFO, LIFO, LRU, Optimal etc.
In page replacement using a random algorithm (random) every time a page fault occurs, the replaced page is randomly
selected.
This technique does not use any information in determining which pages to replace, all pages in main memory have the
same weight to be selected.
How to select random or arbitrary pages, including the page that is currently referenced (pages that should not be
replaced).
This random technique is very bad, the experiment shows the random algorithm causes a very high frequency of page
faults.
The second chance algorithm (which some people call the clock algorithm) works just like FIFO, but it skips over any
pages with the use bit set (and clears the use bit).
Second chance page replacement algorithm is a modified version of FIFO algorithm that avoids the problem of heavily
used page.
It inspects the R bit if it is 0, the page is both old and unused, so it is replaced immediately.
If the R bit is 1, the bit is cleared, the page is put onto the end of the list of pages, and its load time is updated as though
it had just arrived in memory. Then the search continues.
As the name suggests, the pages are given a second chance. The pages that arrived are stored in a linked list. If the
oldest page in the linked list gets referenced, a page fault occurs and the R bit is cleared. The oldest page is now cleared
from memory and is pushed to the latest clock interval.
When the time arrives to replace a page, the operating system replaces the oldest page that has also not been
referenced.
In a rare situation where all the pages have been referenced, the algorithm goes back to its own roots and performs the
simple FIFO algorithm.
7. CLOCK ALGORITHM
The Second Chance Page Replacement algorithm has a drawback that it constantly moves the pages around on its list.
To solve this, the OS keeps all the page frames on a circular list in the form of a clock.
The hand of the clock (pointer) points to the oldest page.
When a page fault takes place, the page being pointed is inspected.
If its R bit is 0, the page is evicted and the new page is inserted into its place. The pointer moves one position ahead.
On the other hand, if R is 1, then it‟s cleared and the hand is advanced to the next page until a page with a false R (i.e.
R=0) bit is found.
Since this works the way a clock works, it is called Clock Page Replacement Algorithm.
It differs from second chance only in the implementation.
WHAT IS A FILE?
FILE ATTRIBUTES
1. Name:
Only information kept in human-readable form (i.e. the name which is displayed to the user).
Every file carries a name by which the file is recognized in the file system.
One directory cannot have two files with the same name.
2. Identifier:
3. Type:
4. Location:
5. Size:
6. Protection:
FILE TYPE
File type refers to the ability of the operating system to distinguish different types of file such as text files source files and
binary files etc. Many operating systems support many types of files. Operating system like MS DOS and UNIX have the
following types of files
1. Ordinary Files
2. Directory Files
These files contain list of file names and other information related to these files.
3. Special Files
1. Character Special Files: data is handled character by character as in case of terminals or printers
2. Block Special Files: data is handled in blocks as in the case of disks and tapes
TYPES OF FILE
File System in OS General Extension Function
Archive arc, zip, tar Related files grouped into one compressed file
Batch bat, sh Commands to the command interpreter
Executable exe, com, bin Read to run machine language program
Multimedia mpeg, mov, rm For containing audio/video information
Object obj, o Compiled, machine language not linked
Source Code C, java, pas, asm, a Source code in various languages
Text txt, doc Textual data, documents
Word Processor wp, tex, rrf, doc Various word processor formats
A file is a collection of logically related data that is recorded on the secondary storage in the form of sequence of
operations
The content of the files are defined by its creator who is creating the file
The various operations which can be implemented on a file such as read, write, open and close etc. are called file
operations
These operations are performed by the user by using the commands provided by the operating system Some common
operations are as follows:
1. Create Operation
2. Open Operation
3. Write Operation
4. Read Operation
The seek system call re positions the file pointers from the current position to a specific place in the file i.e. forward or
backward depending upon the user's requirement
This operation is generally performed with those file management systems that support direct access files
6. Delete Operation
Deleting the file will not only delete all the data stored inside the file it is also used so that disk space occupied by it is
freed
In order to delete the specified file the directory is searched
When the directory entry is located, all the associated file space and the directory entry is released
7. Truncate Operation
8. Close Operation
When the processing of the file is complete, it should be closed so that all the changes made permanent and all the
resources occupied should be released
On closing it deallocates all the internal descriptors that were created when the file was opened
9. Append Operation
File access mechanism refers to the manner in which the records of a file may be accessed
Let's look at various ways to access files stored in secondary memory
1. Sequential Access
If the user wants to read first word of the file then the
pointer provides that word to the user and increases its
value by 1 word This process continues till the end of the file
Modern word systems do provide the concept of direct access and indexed access but the most used method is
sequential access due to the fact that most of the files such as text files, audio files, video files, etc. need to be
sequentially accessed
2. Direct/Random Access
Suppose every block of the storage stores 4 records and we know that the record
we needed is stored in 10 th block
In that case, the sequential access will not be implemented because it will traverse
all the blocks in order to access the needed record
Direct access will give the required result despite of the fact that the operating
system has to perform some complex tasks such as determining the desired block
number.
3. Indexed Access
WHAT IS A DIRECTORY?
Directory can be defined as the listing of the related files on the disk. In other words, group of files combined is known
as directory.
A single directory may or may not contain multiple files.
Information about files is maintained by Directories. In Windows OS, it is called folders.
The directory may store some or the entire file attributes i.e. a directory contains all information about file, its attributes.
To get the benefit of different file systems on the different operating systems, a hard disk can be divided into the
number of partitions of different sizes. The partitions are also called volumes or mini disks.
Each partition must have at least one directory in which, all the files of the partition can be listed.
Directory itself can be organized in many ways.
The organization must allow us to insert entries, to delete entries, to search for a named entry, and to list all the entries
in the directory.
Naming: It becomes convenient for users as two users can have same name for different files or may have different name for
same file.
Grouping: Logical grouping of files can be done by properties e.g. all java programs, all games etc.
DIRECTORY OPERATIONS
The allowed system calls for managing directories exhibit more variation from system to system than system calls for files.
1. Create: A directory is created. It is empty except for dot and dotdot, which are put there automatically by the system (or in
a few cases, by the mkdir program).
2. Delete: A directory is deleted. Only an empty directory can be deleted. A directory containing only dot and dotdot is
considered empty as these cannot usually be deleted.
3. Opendir: Directories can be read. For example, to list all the files in a directory, a listing program opens the directory to
read out the names of all the files it contains. Before a directory can be read, it must be opened, analogous to opening and
reading a file.
4. Closedir: When a directory has been read, it should be closed to free up internal table space.
Formerly, it was possible to read directories using the usual read system call, but that approach has the disadvantage of
forcing the programmer to know and deal with the internal structure of directories.
In contrast, readdir always returns one entry in a standard format, no matter which of the possible directory structures are
being used.
6. Rename: In many respects, directories are just like files and can be renamed the same way files can be.
7. Link: Linking is a technique that allows a file to appear in more than one directory.
This system call specifies an existing file and a path name, and creates a link from the existing file to the name specified by
the path. In this way, the same file may appear in multiple directories.
A link of this kind, which increments the counter in the file‟s i-node (to keep track of the number of directory entries
containing the file), is sometimes called a hard link.
8. Unlink: A directory entry is removed. If the file being unlinked is only present in one directory (the normal case), it is
removed from the file system.
STRUCTURES OF DIRECTORY IN OS
A directory is a container that is used to contain folders and files. It organizes files and folders in a hierarchical manner.
The single level directory is the simplest directory structure (one big list of all the files on the disk).
The entire system will contain only one directory which is supposed to mention all the files present in the file system.
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 a unique name, if two users call their dataset test, then the
unique name rule violated.
The directory contains one entry per each file present on the file system
This type of directories can be used for a simple system
o Naming problem: Users cannot have same name for two files.
o Grouping problem: Users cannot group files according to their need.
Advantages
Disadvantages
1. There may chance of name collision because two files cannot have the same name
2. The directory may be very big therefore searching for a file may take so much time
3. There are no ways to group same kind of files
As we have seen, a single level directory often leads to confusion of files names among different users, the solution to
this problem is each user has its own user file directory (UFD) in two level directory systems
The system doesn't let a user to enter in the other user's directory without permission
There is one master file directory (MFD) which contains separate directories dedicated to each user
The UFDs have similar structures, but each lists only the files of a single user
When a user job starts or a user logs in, the system‟s master file directory (MFD) is searched. The MFD is indexed by user
name or account number, and each entry points to the UFD for that user.
Path Name: Due to two levels there is a path name for every file to locate that file.
Advantages
Disadvantages
1. Grouping problem is still not solved, for example if user wants to create a directory group for all files that are movies or
music, he can‟t do that.
2. A user is not allowed to share files with other users.
3. Still, it not very scalable, two files of the same type cannot be grouped together in the same user.
A tree structure is the most common directory structure. The tree has a root directory, and every file in the system has a
unique path.
Tree structured directory system overcomes the drawbacks of two level directory system. The similar kind of files can
now be grouped in one directory
Each user has its own directory and it cannot enter in the other user's directory. However, the user has the permission to
read the root's data but he cannot write or modify this. Only administrator of the system has the complete access of root
directory
A file can be accessed by two types of path, either relative or absolute
Absolute path is the path of the file with respect to the root directory of the system while relative path is the path with
respect to the current working directory of the system
Advantages
Disadvantages
1. Every file does not fit into the hierarchical model; files may be saved into multiple directories.
2. We cannot share files.
3. It is inefficient, because accessing a file may go under multiple directories.
The tree structured directory system doesn't allow the same file to exist in multiple directories therefore sharing is major
concern in tree structured directory system. We can provide sharing by making the directory an acyclic graph.
In this system, two or more directory entry can point to the same file or sub directory. That file or sub directory is shared
between the two directory entries
These kinds of directory graphs can be made using links or aliases
We can have multiple paths for a same file Links can either be symbolic (logical) or hard link (physical).
If a file gets deleted in acyclic graph structured directory system then
1. In the case of soft link, the file just gets deleted and we are left with a dangling pointer
2. In the case of hard link, the actual file will be deleted only if all the references to it get deleted.
It is used in the situation like when two programmers are working on a joint project and they need to access files.
It is the point to note that the shared file is not the same as the copy file.
If any programmer makes some changes in the subdirectory it will reflect in both subdirectories.
Advantages
Disadvantages
1. We share the files via linking, in case deleting it may create the problem,
2. If the link is a soft link then after deleting the file we left with a dangling pointer.
3. In the case of a hard link, to delete a file we have to delete all the references associated with it.
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 the total size or space that has been taken by the
files and directories.
Advantages
1. It allows cycles.
2. It is more flexible than other directories structure.
Disadvantages
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 figure 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 metadata i.e. includes all details about a file except the actual contents 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
It also maintains via file control blocks (FCB).
2. File Organization Module:
3. Basic File
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
It manages the memory buffers and caches
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
Device drivers acts as interface between devices and OS, they help to transfer data between disk and main memory
ON-DISK STRUCTURES
Generally they contain information about total number of disk blocks, free disk blocks, location of them and etc.
There are various on-disk data structures that are used to implement a file system. This structure may vary depending
upon the operating system.
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 has information about a particular partition ex:- free block count, block size and block
pointers etc. In UNIX it is called super block and in NTFS it is stored in master file table.
3. Directory Structure – They store file names and associated inode numbers. In UNIX, includes file names and associated
file names and in NTFS, it is stored in master file table.
4. Per-File FCB – It contains details about files and it has a unique identifier number to allow association with directory
entry. In NTFS it is stored in master file table.
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:
2. Directory-Structure cache – This cache holds the directory information of recently accessed directories.
3. System wide open-file table – It contains the copy of FCB of each open file.
4. Per-process open-file table – It contains information opened by that particular process and it maps with appropriate
system wide open-file.
DIRECTORY IMPLEMENTATION
There is the number of algorithms by using which, the directories can be implemented
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 an 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
1. CONTIGUOUS ALLOCATION
In this scheme, each file occupies a contiguous set of blocks on the disk (i.e. assigned disk address is in linear order).
This method is best from the point of view of the individual sequential file.
Multiple blocks can be read in at a time to improve I/O performance for sequential processing.
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 file C in the following figure starts from the block 18 with
length = 8 blocks. Therefore, it occupies18, 19, 20, 21, 22, 23, 24,
25 blocks.
Advantages
1. It is simple to implement
2. We will get Excellent read performance
3. Supports Random Access into files
Disadvantages
1. The disk will become fragmented (both internal and external fragmentation) This makes it inefficient in terms of memory
utilization
2. It may be difficult to have a file grow (i e difficult to increase the size of the file due to the availability of the contiguous
memory block)
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.
Increase in file size is always possible if free disk block is available.
There is no external fragmentation because only one block at a time is needed but there can be internal fragmentation
but it exists only in the last disk block of 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
Disadvantages
3. INDEXED ALLOCATION
Disadvantages
PROTECTION
Protection refers to a mechanism which controls the access of programs, processes, or users to the resources defined by
a computer system.
It deals with the access to certain system resources
There are internal threats associated with protection of the system
Simple queries are handled in protection
It tries to determine the files that could be accessed or permeated by a special user
It implements authorization mechanism
Need of Protection:
SECURITY
Protection and security requires that computer resources such as CPU, softwares, memory etc. are protected
If a computer program is run by an unauthorized user, then he/she may cause severe damage to computer or data
stored in it
This can be done by ensuring integrity, confidentiality and availability in the operating system
So a computer system must be protected against unauthorized access, malicious access to system memory, viruses,
worms etc.
The different methods that may provide protect and security for different computer systems are
1. AUTHENTICATION
Authentication refers to identifying each user of the system and associating the executing programs with those users.
It is the responsibility of the Operating System to create a protection system which ensures that a user who is running a
particular program is authentic.
1. Username/Password: User needs to enter a registered username and password with Operating system to login into the
system.
2. User Card/Key: User need to punch card in card slot, or enter key generated by key generator in option provided by
operating system to login into the system.
3. User Attribute (Fingerprint/Eye Retina Pattern/ Signature): User need to pass his/her attribute via designated input
device used by operating system to login into the system.
One time passwords provide additional security along with normal authentication
In One Time Password system, a unique password is required every time user tries to login into the system
Once a one-time password is used, then it cannot be used again
One time password are implemented in various ways
1. Random Numbers - Users are provided cards having numbers printed along with corresponding alphabets System asks
for numbers corresponding to few alphabets randomly chosen
2. Secret Key - User are provided a hardware device which can create a secret id mapped with user id System asks for such
secret id which is to be generated every time prior to login
3. Network Password - Some commercial applications send one time passwords to user on registered mobile/ email
which is required to be entered prior to login
3. PROGRAM THREATS
If a user program made these process do malicious tasks, then it is known as Program Threats
One of the common example of program threat is a program installed in a computer which can store and send user
credentials via network to some hacker
1. Trojan Horse - Such program traps user login credentials and stores them to send to malicious user who can later on
login to computer and can access system resources
2. Trap Door - If a program which is designed to work as required, have a security hole in its code and perform illegal
action without knowledge of user then it is called to have a trap door
3. Logic Bomb - Logic bomb is a situation when a program misbehaves only when certain conditions met otherwise it
works as a genuine program It is harder to detect
4. Virus - Virus as name suggest can replicate themselves on computer system. They are highly dangerous and can
modify/delete user files, crash systems. A virus is generally a small code embedded in a program. As user accesses the
program, the virus starts getting embedded in other files/ programs and can make system unusable for user
4. SYSTEM THREATS
System threats refer to misuse of system services and network connections to put user in trouble.
System threats can be used to launch program threats on a complete network called as program attack.
System threats create such an environment that operating system resources/ user files are misused.
Following is the list of some well-known system threats.
1. Worm - Worm is a process which can choke down a system performance by using system resources to extreme levels. A
Worm process generates its multiple copies where each copy uses system resources, prevents all other processes to get
required resources. Worms processes can even shut down an entire network.
2. Port Scanning - Port scanning is a mechanism or means by which a hacker can detects system vulnerabilities to make
an attack on the system.
3. Denial of Service - Denial of service attacks normally prevents user to make legitimate use of the system. For example,
a user may not be able to use internet if denial of service attacks browser's content settings.
As per the U S Department of Defense Trusted Computer System's Evaluation Criteria there are four security
classifications in computer systems A, B, C, and D.
This is widely used specifications to determine and model the security of systems and of security solutions.
One of the important jobs of an Operating System is to manage various I/O devices including mouse, keyboards, touch pad,
disk drives, display adapters, USB devices, Bit-mapped screen, LED, Analog-to-digital converter, On/off switch, network
connections, audio I/O, printers etc.
An I/O system is required to take an application I/O request and send it to the physical device, then take whatever response
comes back from the device and send it to the application.
2. Character Devices: A character device is one with which the driver communicates by sending and receiving single
characters (bytes, octets). For example, serial ports, parallel ports, sound cards etc.
Note: Doesn't Really Fit: Some devices don't fit into this division. For instance, clocks aren't block addressable, nor do they
accept character streams. All they do is cause interrupts... at timed intervals. Memory-mapped screens do not fit this division
either.
DEVICE CONTROLLERS
Device drivers are software modules (device-specific code) that can be plugged into an OS to handle a particular device.
Operating System takes help from device drivers to handle all I/O devices.
The Device Controller works like an interface between a device and a device driver.
I/O units (Keyboard, mouse, printer, etc.) typically consist of a mechanical component and an electronic component
where electronic component is called the device controller.
There is always a device controller and a device driver for each device to communicate with the Operating Systems.
A device controller may be able to handle multiple devices. As an interface its main task is to convert serial bit stream to
block of bytes, perform error correction as necessary.
Any device connected to the computer is connected by a plug and socket, and the socket is connected to a device
controller.
Following is a model for connecting the CPU, memory, controllers, and I/O devices where CPU and device controllers all
use a common bus for communication.
The CPU must have a way to pass information to and from an I/O device. There are three approaches available to
communicate with the CPU and Device:
1. Special Instruction I/O: This uses CPU instructions that are specifically made for controlling I/O devices. These
instructions typically allow data to be sent to an I/O device or read from an I/O device
2. Memory mapped I/O - When using memory mapped I/O, the same address space is shared by memory and I/O devices.
The device is connected directly to certain main memory locations so that I/O device can transfer block of data to/from
memory without going through CPU. While using memory mapped I/O, OS allocates buffer in memory and informs I/O
device to use that buffer to send data to the CPU.
I/O device operates asynchronously with CPU, interrupts CPU when finished. The advantage to this method is that every
instruction which can access memory can be used to manipulate an I/O device
Memory mapped I/O is used for most high speed I/O devices like disks, communication interfaces.
Working of DMA:
Initially, when any device has to send data between the device and the memory, the device has to send DMA request
(DRQ) to DMA controller.
The DMA controller sends Hold request (HRQ) to the CPU and waits for the CPU to assert the HLDA.
Then the microprocessor tri-states all the data bus, address bus, and control bus. The CPU leaves the control over bus
and acknowledges the HOLD request through HLDA signal.
Now the CPU is in HOLD state and the DMA controller has to manage the operations over buses between the CPU,
memory, and I/O devices.
I/O SOFTWARES IN OPERATING S YSTEM
1. User Level Libraries - This provides simple interface to the user program to perform input and output. For example,
stdio is a library provided by C and C++ programming languages
2. Kernel Level Modules - This provides device driver to interact with the device controller and device independent I/O
modules used by the device drivers
3. Hardware - This layer includes actual hardware and hardware controller which interact with the device drivers and
makes hardware alive
A key concept in the design of I/O software is that it should be device independent where it should be possible to write
programs that can access any I/O device without having to specify the device in advance.
For example, a program that reads a file as input should be able to read a file on a floppy disk, on a hard disk, or on a CD
ROM, without having to modify the program for each different device.
SECONDARY STORAGE
1. Platters
2. Spindle
3. Read/Write Reads
4. Tracks
5. Sectors
6. Cylinder
1. PLATTERS - Hard disks are organized as a concentric stack of disks. An individual disk is referred to as a platter. Each
platter consists of two surfaces: a lower and an upper surface.
2. SPINDLE - The platters within the hard disk are connected by a spindle
that runs through the middle of the platters.
The spindle moves in a unidirectional manner along its axis (either clockwise
or counterclockwise). The movement of the spindle causes the platters to
rotate as well.
3. READ/WRITEHEAD - Each surface on a platter contains a read/write head that is used to read or write data onto the
disk.
The read/write heads can move back and forth along the surface of a platter. Read/write heads are in turn connected to a
single actuator arm.
4. TRACKS - Each surface of a platter consists of a fixed number of
tracks.
5. SECTORS - Each track is divided into a fixed number of sectors. Sectors divide track sections and store data.
6. CYLINDER - Logical grouping of the track on each disk surface in a disk unit.
In other words, all the tracks with the same radius are known as a cylinder.
DISK SCHEDULING
Disk scheduling is also known as I/O scheduling. As we know, a process needs two types of time, CPU time and I/O time. For
I/O, it requests the OS to access the disk.
On a typical multiprogramming system, there will usually be multiple disk access requests at any point of time. So those
requests must be scheduled to achieve good efficiency. Disk scheduling is similar to process scheduling.
However, the operating system must be fair enough to satisfy each request and at the same time, operating system must
maintain the efficiency and speed of process execution.
The technique that operating system uses to determine the request which is to be satisfied next is called disk scheduling.
Generally, the scheduling refers to a time table for completing any task or a job
Purpose of Disk Scheduling - The main purpose of disk scheduling algorithm is to select a disk request from the queue of
I/O requests and decide the schedule when this request will be processed
1. Less traveling head time: Hard drives are one of the slowest parts of the computer system. So, to access the hard drive in
conveniently or effectively manner, we need disk scheduling.
2. Throughput must be high: 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.
3. Fairness: There may be chances that two or more requests can be distant from each other. Therefore, more disk arm
movement can happen. Thus, we need disk scheduling for such handling case.
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 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.
EXAMPLE - Consider a hard disk with:4 surfaces, 64 tracks/surface, 128 sectors/track, and 256 bytes/sector
2. The disk is rotating at 3600 RPM, what is the data transfer rate?
60 sec -> 3600 rotations
1 sec -> 60 rotations
Data transfer rate = number of rotations per second * track capacity * number of surfaces (since 1 R-W head is used for each
surface)
3. The disk is rotating at 3600 RPM, what is the average access time?
Since, seek time, controller time and the amount of data to be transferred is not given, we consider all the three terms as 0.
FCFS (First Come First Serve) is the easiest or simplest of all the Disk Scheduling Algorithms.
In FCFS, the I/O requests are addressed in the order they arrive in the disk queue.
Advantages
Disadvantages
Question - Suppose a disk contains 200 tracks 0 - 199 and the request queue contains track number 43, 176, 42, 148, 27, 14,
180. The current position of the Read/Write head is 55. Now we have to calculate the total number of track movements of
Read/Write head using FCFS scheduling.
Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199.
The current position of the read/write head is 55. So, we start from 55 then move read/write head in the FCFS order
Total Number of cylinders moved by the head = (176 - 55) + (176 - 42) + (148 - 42) + (148 - 14) + (180 - 14)
= 121+134+106+134+166 = 661
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 (total seek time) and increases the
throughput of system.
Advantages
Disadvantages
1. In SSTF, there may be a chance of starvation for some requests if it has higher seek time as compared to incoming
requests.
2. SSTF is not an optimal algorithm.
3. There are chances of overhead in SSTF disk scheduling because we have to calculate the seek time in advanced.
4. The speed of this algorithm can be decreased because direction could be switched frequently.
Question - Consider a disk that contains 200 tracks (0 199). The request queue includes track number: 82, 170, 43, 140, 24,
16, 190 respectively. The current position of the Read/Write head is 50. Find the number of head movements in cylinders
using SSTF scheduling.
Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199. The current
position of the read/write head is 50. So, we start from 50 then move read/write head in the SSTF order.
Now for further movement of read/write head, we calculate the seek time.
Total number of cylinders moved by the head = (50-16) + (190-16) = 34 + 174 = 208
3. SCAN ALGORITHM
In SCAN algorithm the disk arm moves into a particular direction (direction can be moved towards large value or the
smallest value) 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.
It works in the way an elevator works, elevator moves in a direction completely till the last floor of that direction and
then turns back.
So SCAN disk scheduling algorithm is also called an elevator algorithm.
As a result, the requests at the midrange are serviced more and those arriving behind the disk arm will have to wait.
Advantages
Disadvantages
1. SCAN disk scheduling algorithm takes long waiting time for requests for the cylinders (locations), just visited by the head.
2. In SCAN disk scheduling, we have to move the disk head to the end of the disk even when we don‟t have any request to
service.
Question - Consider a disk containing 200 tracks (0 199) and the request queue includes the track number: 93, 176, 42, 148,
27, 14, 180 respectively. The current position of read//write head is 55, and direction is towards the larger value. Calculate the
total number of cylinders moved by the head using SCAN disk scheduling.
Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199.
The current position of the read/write head is 55 So, we start from 55 then we move towards the larger value as given in the
question
4. C-SCAN ALGORITHM
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 C-SCAN 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).
Advantages
1. In C-SCAN disk scheduling, there are more seek movements as compared to SCAN disk scheduling.
2. In C-SCAN disk scheduling, we have to move the disk head to the end of the disk even when we don‟t have any request to
service.
Question - Consider, a disk contains 200 tracks (0 199) and the request queue contains track number: 82, 170, 43, 140, 24,
16,190 respectively. The current position of R/W head is 50, and the direction is towards the larger value. Calculate the total
number of cylinders moved by head using C SCAN disk scheduling.
Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199.
The current position of the read/write head is 50 So, we start from 50 then we move towards the larger value as given in the
question.
5. LOOK SCHEDULING
Look disk scheduling is similar to the SCAN disk scheduling, but in this scheduling, instead of going till the last track
(stops moving inwards or outwards) , we go till the last request and then change the direction.
Thus it prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.
Advantages
Disadvantages
1. In look disk scheduling, there is more overhead to find the end request.
2. Look disk scheduling is not used in case of more load.
Question - Consider a disk contains 200 tracks (0 199). The request queue includes track number: 82, 170, 43, 140, 24, 16,
190 respectively. The current position of the read/write head is 50. The direction is towards the larger value. Calculate the
total number of cylinders moved by head using look disk scheduling.
Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199.
The current position of the read/write head is 50 So, we start from 50 then we move towards the larger value as given in the
question.
Total Number of cylinders moved by head = (190-50) + (190-16) = 314
6. C-LOOK SCHEDULING
As LOOK is similar to SCAN algorithm, in similar way, C-LOOK is similar to C-SCAN disk scheduling algorithm.
It is different from C-SCAN algorithm in the sense that, C-SCAN force the disk arm to move till the last cylinder
regardless of knowing whether any request is to be serviced on that cylinder or not.
In C-LOOK (Circular-LOOK) scheduling, the disk arm moves and service each request till the head reaches its highest
request, and after that, the disk arm jumps to the lowest cylinder without servicing any request, and the disk arm moves
further and service those requests which are remaining.
Thus, it also prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.
Advantages
Disadvantages
1. In C-Look disk scheduling there may be more overhead to determine the end request.
Question - Consider a disk containing 200 tracks (0 199). The request queue contains the track number: 93, 176, 42, 148, 27,
14,180 respectively. The current position of the R/W head is 55. The direction is towards the larger value. Calculate the total
number of cylinders moved by head using C LOOK disk scheduling.
Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199. The current
position of the read/write head is 55. So, we start from 55 then we move towards the larger value as given in the question
It divides the request queue into sub-queues of length N. In this, a buffer is created for N requests.
By doing this it ensures that all requests belonging to a buffer will be serviced in one go.
Also once the buffer is full no new requests are kept in this buffer and are sent to another one.
Now, when these N requests are serviced, the time comes for another top N requests and this way all get requests get a
guaranteed service.
Therefore, starvation is completely eliminated and by doing this N-Step-SCAN allows better throughput.
OS TERMINALS
The devices such as keyboard, monitors etc. are called as terminals. Every computer system has at least one keyboard and
one display that are used to communicate with it.
Basically there are many forms of terminals or you can also say that terminals come in many forms. Here are the list of three
most important form of terminals:
Basically, RS-232 terminals are the hardware devices that contain a keyboard and a display, communicate using a serial
interface, one bit at a time.
These terminals uses a 9 and 25-pin connector where one pin is for transmitting the data one is for receiving the data,
and one is ground.
The other pins are used for various control functions, but most of which are not used.
In today's computer world, almost all the personal computers uses graphical user interface (GUI).
Some years ago, users were using their personal computers with MS-DOS (Microsoft Disk Operating System) which is a
character-based interface.
A graphical user interface has the following four essential elements: Windows, Icons, Menus & Pointing device.
In short, these four essential elements of GUI also called as WIMP which is the short form of windows, icons, menus,
pointing device.
3. NETWORK TERMINALS
Network terminals are used to connect a remote user to computer over a network, either a local area network (LAN) or a
wide area network (WAN).
There are following two concept that shows how the network terminal should work:
1. The network terminal should have a large amount of computing power and memory just in order to run the complex
protocols to compress the amount of data that are sent over the network.
2. The network terminal should be extremely simple, basically displaying pixels and not doing much thinking in order to
make it very cheap.
A protocol is a set of requests and responses that a sender and receiver agree upon in order to communicate over a network
or other interface.
OS CLOCKS
System Clock is maintained by the kernel of an operating system and is used to set the tasks and processes –their
synchronization and scheduling, settings and managing interrupts, setting timer etc.
The system clock reports seconds and microseconds since a start point from the system boot up procedure.
There are two main reasons that clocks are very essential to the operation of any multi-programmed system.
Clock maintains the time of day and prevents one process from monopolizing the CPU among other things.
The clock software (clock driver) can take the form of a device driver, even though a clock is neither a block device, nor a
character device like disk and mouse.
The exact function of the clock driver may vary depending on operating system.
The exact duties of the clock driver vary among operating systems, but usually include most of the following:
1. Maintaining the time of the day: The clock driver implements the time of day or the real time clock function. It requires
incrementing a counter at each clock tick.
2. Accounting for CPU usage: Another function performed by clock driver is doing CPU accounting. CPU accounting implies
telling how long the process has run.
3. Preventing processes from running too long: As a process is started, the scheduler initializes the quantum counter in
clock ticks for the process. The clock driver decrements the quantum counter by 1, at every clock interrupts. When the
counter gets to zero, clock driver calls the scheduler to set up another process. Thus clock driver helps in preventing
processes from running longer than time slice allowed.
4. Providing watchdog timers for parts of the system itself: Watchdog timers are the timers set by certain parts of the
system. For example, to use a floppy disk, the system must turn on the motor and then wait about 500msec for it to come up
to speed.
5. Handling the alarm system call made by user processes: In many systems, a process can request that the OS give it a
warning after a certain interval. The warning is usually a signal, interrupt, message, or something similar. One application
requiring such warnings is networking, in which a packet not acknowledged within a certain time interval must be
retransmitted. If the clock driver had enough clocks, it could set a separate clock for each request.
Unit 6 - Deadlock in Operating System
Deadlock refers to the condition when two or more processes are waiting
for each other to release a resource indefinitely. A process in nature
requests a resource first and uses it and finally releases it.
System Model
A system may consist of finite number of resources and is distributed among number of processes.
There resources are partitioned into several instances each with identical instances.
In general, a process must request a resource before using it and it must release the resource after using it.
And any process can request as many resources as it requires in order to complete its designated task.
And there is a condition that the number of resources requested may not exceed the total number of resources available
in the system.
Utilization of Resources:
Basically in the normal mode of operation, utilization of resources by a process is in the following sequence:
1. Request: Firstly, the process requests the resource. In a case, if the request cannot be granted immediately (e.g. resource
is being used by any other process), then the requesting process must wait until it can acquire the resource.
2. Use: The process can operate on the resource (e.g. if the resource is a printer then in that case process can print on the
printer).
Example of Deadlock: Consider a system with one printer and one tape drive. If a process Pi currently holds a printer and a
process Pj holds the tape drive. If process Pi request a tape drive and process Pj request a printer then a deadlock occurs.
Multithread programs are good candidates for deadlock because they compete for shared resources.
A deadlock situation can occur if the following 4 conditions occur simultaneously in a system:
1. Mutual Exclusion: Only one process must hold the resource at a time. If any other process requests for the resource,
the requesting process must be delayed until the resource has been released.
2. Hold and Wait: A process must be holding at least one resource and waiting to acquire additional resources that are
currently being hold by the other process.
3. No preemption: Resources can‟t be preempted i.e., only the process holding the resources must release it after the
process has completed its task.
4. Circular Wait: A set {P0, P1 … Pn} of waiting process must exist such that P0 is waiting for a resource i.e. held by P1, P1
is waiting for a resource i.e., held by P2. Pn-1 is waiting for resource held by process Pn and Pn is waiting for the
resource i.e., held by P1.
Example of Deadlock - A real-world example would be traffic, which is going only in one direction. Here, a bridge is
considered a resource. So, when deadlock happens, it can be easily resolved if one car backs up (Preempt resources and
rollback). Several cars may have to be backed up if a deadlock situation occurs. So starvation is possible.
ADVANTAGES OF DEADLOCK
This situation works well for processes which perform a single burst of activity
No preemption needed for Deadlock.
Convenient method when applied to resources whose state can be saved and restored easily
Feasible to enforce via compile-time checks
Needs no run-time computation since the problem is solved in system design
DISADVANTAGES OF DEADLOCK
We can use a protocol to prevent deadlock ensuing that the system will never enter into the deadlock state.
We allow a system to enter into deadlock state, detect it and recover from it.
We ignore the problem and pretend that the deadlock never occur in the system. This is used by OS including UNIX.
To ensure that the deadlock never occur the system can use either deadlock avoidance or a deadlock prevention.
Deadlock prevention is a set of method for ensuing that at least one of the necessary conditions does not occur.
Deadlock avoidance requires the OS is given advance information about which resource a process will request and use
during its lifetime.
If a system does not use either deadlock avoidance or deadlock prevention then a deadlock situation may occur. During
this it can provide an algorithm that examines the state of the system to determine whether a deadlock has occurred
and algorithm to recover from deadlock.
Deadlock Ignorance
Deadlock ignorance is the most widely used approach among all the mechanism. This is being used by many operating
systems mainly for end user uses. In this approach, the operating system assumes that deadlock never occurs. It simply
ignores deadlock. This approach is best suitable for a single end user system where user uses the system only for browsing
and all other normal stuff (or tasks).
Thus ignoring the deadlock method can be useful in many cases but it is not perfect in order to remove the deadlock from
operating system. There is always a tradeoff between correctness and performances.
The operating systems like Windows and Linux mainly focus upon performance.
However, the performance of the system decreases of it uses deadlock handling mechanism all the time of deadlock happens
1 out of 100 times then it is completely unnecessary to use the deadlock handling mechanism all the time.
In these types of systems, the user has to simply restart the computer the case of deadlock. Windows and Linux are mainly
using this approach.
Deadlock prevention
Deadlock happens only when mutual exclusion, hold and wait, no preemption and circular wait occurs simultaneously
The main aim of the deadlock prevention method is to violate any one condition among the four because if any of one
condition is violated then the problem of deadlock will never occur.
The idea behind the approach is very simple that we have to fail one of the four conditions but the difficulty can occur
during the physical implementation of this method in the system.
Let us take an example of a chair, as we know that chair always stands on its four legs. If anyone leg of the chair gets
broken, then definitely it will fall.
The same is the situation with the deadlock if we become able to violate any condition among the four and do not let
them occur together then there can be prevented from the deadlock problem.
We will elaborate deadlock prevention approach by examining each of the four necessary conditions separately. Let's
see how we can prevent each of the conditions.
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.
Spooling - For a device like printer, spooling can work. There is a memory associated with the printer which stores jobs from
each of the process into it. Later, Printer collects all the jobs and print each one of them according to FCFS. By using this
mechanism, the process doesn't have to wait for the printer and it can continue whatever it was doing. Later, it collects the
output when it is produced.
Although, Spooling can be an effective approach to violate mutual exclusion but it suffers from two kinds of problems.
We cannot force a resource to be used by more than one process at the same time since it will not be fair enough and some
serious problems may arise in the performance. Therefore, we cannot violate mutual exclusion for a process practically.
2. Hold and Wait – Hold and wait condition occurs when a process holds a resource and is also waiting for some other
resource in order to complete its execution. Thus if we did not want the occurrence of this condition then we must guarantee
that when a process requests a resource, it does not hold any other resource.
There are some protocols that can be used in order to ensure that the Hold and Wait condition never occurs:
According to the first protocol each process must request and gets all its resources before the beginning of its
execution.
The second protocol allows a process to request resources only when it does not occupy any resource.
We will consider a process that mainly copies data from a DVD drive to a file on disk, sorts the file and then prints the
results to a printer. If all the resources must be requested at the beginning of the process according to the first protocol,
then the process requests the DVD drive, disk file, and printer initially. It will hold the printer during its entire execution,
even though the printer is needed only at the end.
While the second method allows the process to request initially only the DVD drive and disk file. It copies the data from
the DVD drive to the disk and then releases both the DVD drive and the disk file. The process must then again request
the disk file and printer. After copying the disk file to the printer, the process releases these two resources as well and
then terminates.
Utilization of resources may be low, since resources may be allocated but unused for a long period. In the above-
given example, for instance, we can release the DVD drive and disk file and again request the disk file and printer only if
we can be sure that our data will remain on the disk file. Otherwise, we must request all the resources at the beginning
of both protocols.
There is a possibility of starvation. A process that needs several popular resources may have to wait indefinitely
because at least one of the resources that it needs is always allocated to some other process.
3. No Preemption – The third necessary condition for deadlocks is that there should be no preemption of resources that
have already been allocated.
In order to ensure that this condition does not hold the following protocols can be used:
According to the first protocol: “If a process that is already holding some resources requests resource and if the
requested resources cannot be allocated to it, then it must release all the resources currently allocated to it.”
According to the second protocol: “When a process requests some resources, if they are available, then allocate them. If
in case, the requested resource is not available then we will check whether it is being used or is allocated to some other
process waiting for other resources. If that resource is not being used, then the operating system preempts it from the
waiting process and allocates it to the requesting process. And if that resource is being used, then the requesting
process must wait”.
The second protocol can be applied to those resources whose state can be easily saved and restored later for example CPU
registers and memory space, and cannot be applied to resource like printers and tape drivers.
4. Circular Wait
The fourth necessary condition to cause deadlock is circular wait, in order to ensure violate this condition we can do the
following:
Deadlock Avoidance
In this method, the request for any resource will be granted only if the resulting state of the system doesn‟t cause any
deadlock in the system.
In deadlock avoidance, the operating system checks whether the system is in safe state or in unsafe state at every step
which the operating system performs.
Any process continues its execution until the system is in a safe state.
Once the system enters into an unsafe state, the operating system has to take a step back.
With the help of a deadlock-avoidance algorithm, the operating system keeps an eye on each allocation, and make sure
that allocation does not cause any deadlock in the system.
In order to avoid deadlocks, the process must tell OS, the maximum number of resources a process can request to
complete its execution.
The deadlock avoidance algorithm examines the resource allocations so that there can never be a circular wait condition.
Deadlock avoidance can mainly be done with the help of Banker‟s Algorithm.
A state is safe if the system can allocate resources to each process (up to its maximum requirement) in some order and
still avoid a deadlock. Formally, a system is in a safe state only, if there exists a safe sequence.
So a safe state is not a deadlocked state and conversely a deadlocked state is an unsafe state.
In an unsafe state, the operating system cannot prevent processes from requesting resources in such a way that any
deadlock occurs.
It is not necessary that all unsafe states are deadlocks; an unsafe state may lead to a deadlock.
The above figure shows the safe, unsafe and deadlocked state spaces.
Deadlock Avoidance Example: Let us consider a system having 12 magnetic tapes and three processes P1, P2, P3. Process
P1 requires 10 magnetic tapes, process P2 may need as many as 4 tapes, and process P3 may need up to 9 tapes. Suppose at
a time t0, process P1 is holding 5 tapes, process P2 is holding 2 tapes and process P3 is holding 2 tapes. (There are 3 free
magnetic tapes)
Note: In a case, if the system is unable to fulfill the request of all processes then the state of the system is called unsafe.
The main key of the deadlock avoidance method is whenever the request is made for resources then the request must only
be approved only in the case if the resulting state is a safe state
It is better to avoid a deadlock instead of taking action after the deadlock has occurred.
Deadlock avoidance is the simplest and most useful model that each process declares the maximum number of
resources of each type that it may need.
The deadlock avoidance algorithm helps you to dynamically assess the resource-allocation state so that there can never
be a circular-wait situation.
Resource allocation graph is explained to us what is the state of the system in terms of processes and resources.
Like how many resources are available, how many are allocated and what is the request of each process.
Everything can be represented in terms of the diagram.
The graph consists of set of vertices (v) and set of edges (e).
One of the advantages of having a diagram is, sometimes it is possible to see a deadlock directly by using RAG, but then
you might not be able to know looking at the table.
But the tables are better if the system contains lots of process and resource and graph is better if the system contains
less number of process and resource.
We know that nay graph contains vertices and edges. So RAG also contains vertices and edges.
1. Process vertex: Every process will be represented as a process vertex. Generally, the process will be represented with a
circle.
2. Resource vertex: Every resource will be represented as a resource vertex. It is also two types:
- Single instance type resource: It represents as a box, inside the box, there will be one dot. So the number of dots
indicates how many instances are present of each resource type.
- Multi-resource instance type resource: It also represents as a box, inside the box, there will be many dots present.
1. Assign edge: If you already assign a resource to a process then it is called assign edge.
2. Request Edge: It means in future the process want some resource to complete the execution, that is called request
edge.
So, if a process is using a resource, an arrow is drawn from the resource node to the process node. If a process is requesting
a resource, an arrow is drawn from the process node to the resource node.
For example, if process P1 holds resource R1, process P2 holds resource R2 and process P1 is waiting for R2 and process
P2 is waiting for R1, then process P1 and process P2 will be in deadlock.
Here‟s another example that shows Processes P1 and P2 acquiring resources R1 and R2 while process P3 is waiting to acquire
both resources.
In this example, there is no deadlock because there is no circular dependency. So cycle in single instance resource type is the
sufficient condition for deadlock.
Multi-instances RAG
The total number of processes are three; P1, P2 & P3 and the total number of resources are two; R1 & R2.
Allocation Matrix:
For constructing the allocation matrix, just go to the resources and see to which process it is allocated.
R1 is allocated to P1, therefore write 1 in allocation matrix and similarly, R2 is allocated to P2 as well as P3 and for the
remaining element just write 0.
Request Matrix:
In order to find out the request matrix, you have to go to the process and see the outgoing edges.
P1 is requesting resource R2, so write 1 in the matrix and similarly, P2 requesting R1 and for the remaining element write
0.
So now available resource is = (0, 0).
So, there is no deadlock in this RAG. Even though there is a cycle, still there is no deadlock.
Therefore in multi-instance resource cycle is not sufficient condition for deadlock.
So, the available resource is = (0, 0), but requirement are (0, 1), (1, 0) and (1, 0).
So you can‟t fulfill any one requirement. Therefore, it is in deadlock.
Therefore, every cycle in a multi-instance resource type graph is not a deadlock, if there has to be a deadlock, there has
to be a cycle.
So, in case of RAG with multi-instance resource type, the cycle is a necessary condition for deadlock, but not sufficient.
Banker’s Algorithm
The banker‟s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the
allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible
activities, before deciding whether allocation should be allowed to continue.
Why Banker’s algorithm is named so? 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 come 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.
Following data structures are used to implement the Banker‟s algorithm:
Let „n‟ be the number of processes in the system and „m‟ be the number of resource types.
4. Available:
It is a 1-d array of size „m‟ indicating the number of available resources of each type.
Available[j] = k means there are „k‟ instances of resource type Rj
5. Max:
It is a 2-d array of size „n*m‟ that defines the maximum demand of each process in a system.
Max[i, j] = k means process Pi may request at most „k‟ instances of resource type R j.
6. Allocation:
It is a 2-d array of size „n*m‟ that defines the number of resources of each type currently allocated to each process.
Allocation [i, j] = k means process Pi is currently allocated „k‟ instances of resource type R j
7. 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]
Allocation i specifies the resources currently allocated to process Pi and Needi specifies the additional resources that process
Pi may still request to complete its task.
Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional resources that
process
Pi may still request to complete its task.
Banker‟s algorithm consists of Safety algorithm and Resource request algorithm
Safety Algorithm - The algorithm for finding out whether or not a system is in a safe state can be described as follows:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
Resource-Request Algorithm - Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k
instances of resource type Rj. When a request for resources is made by process Pi, the following actions are taken:
EXAMPLE
Output:
We must determine whether this new system state is safe. To do so, we again execute Safety algorithm on the above data
structures.
Hence the new system state is safe, so we can immediately grant the request for process P1 .
Example: Let us consider the following snapshot for understanding the banker's algorithm:
Solution:
1. The Content of the need matrix can be calculated by using the formula given below:
Safe sequence:
The system allocates all the needed resources to each process. So, we can say that the system is in a safe state.
As the processes enter the system, they must predict the maximum number of resources needed which is not impractical
to determine.
In this algorithm, the number of processes remains fixed which is not possible in interactive systems.
This algorithm requires that there should be a fixed number of resources to allocate. If a device breaks and becomes
suddenly unavailable the algorithm would not work.
Overhead cost incurred by the algorithm can be high when there are many processes and resources because it has to be
invoked for every process.
First let's consider the situation when there is one resource type, think of it as units of money (1K dollars), a banker (the OS)
who has a certain number of units in his bank and a number of customers who can loan a certain number of units from the
bank and later pay the loan back (release the resources). The customers have a credit line that cannot exceed the number of
units initially in the bank, and when they have borrowed their max number of units, they will pay their loan back.
The banker will grant a loan request only if it does not lead to an unsafe state. E.g., the banker initially has 10 K, and four
customers A, B, C and D have credit lines 6K, 5K, 4K and 7K respectively. The state when no loans have been made is then:
A06
B05
C04
D07
A16
B15
C24
D47
Suppose B loans one more unit:
This is unsafe: if ALL CUSTOMERS ask their MAXIMUM remaining credit, NONE can be satisfied, and we have deadlock. So,
in this case the banker will not grant B the loan; i.e. back to
A16
B15
C24
D47
This state is safe because with 2K left, C can borrow her max remaining credit:
A16
B15
C44
D47
A16
B15
D47
A16
B15
D77
and terminate
A16
B15
B15
So a safe state is a state where a sequence of ALL processes can get their max required resources (one at the time) and finish
and release all their resources.
Deadlock Detection and Recovery
This approach let the processes fall in deadlock and then periodically check whether deadlock occur in the system or not
by using some algorithms of the resource-allocation graph.
This graph is mainly used to represent the allocations of various resources to different processes.
Do not confuse the deadlock detection algorithm with banker‟s algorithm which is completely different.
If a system does not employ either a deadlock prevention or deadlock avoidance algorithm then a deadlock situation
may occur.
After the detection of deadlock, a number of methods can be used in order to recover from that deadlock.
In this case:
- Apply an algorithm to examine state of system to determine whether deadlock has occurred or not.
- Apply an algorithm to recover from the deadlock.
We have already discussed about deadlock detection method using different algorithms of the resource-allocation graph.
Deadlock Recovery - In order to recover the system from deadlocks, either OS considers resources or processes. Deadlock
can be recovered by:
For Resource:
Preemption (Preempt the Resource):
The resources that are allocated to the process involved in deadlock are taken away (preempted) and are transferred to
other processes with the expectation that it will complete the execution and will release this resource sooner.
In this way, system may recover from deadlock as we may change system state.
Well, choosing a resource which will be snatched is going to be a bit difficult.
The operating system keeps a record of the process state and it can easily make a process roll back to its previous state
due to which deadlock situation can be easily eliminate.
System passes through various states to get into the deadlock state.
A state when the system is not in deadlock is called safe state.
The moment, we get into deadlock (i.e. unsafe state), we will rollback all the allocations to get into the previous safe
state.
For this purpose, OS needs to implement check pointing at every state.
For Process:
Kill the Process: One way is to kill all the process in deadlock or the second way kill the process one by one, and check after
each if still deadlock exits and do the same till the deadlock is removed.
Kill a process - Killing a process can solve our problem but the bigger concern is to decide which process to kill. Generally,
operating system kills a process which has done least amount of work until now.
Kill all process - This is not a suggestible approach but can be implemented if the problem becomes very serious. Killing all
process will lead to inefficiency in the system because all the processes will execute again from starting.
No preemption necessary.
Works well for process that perform a single burst of activity.
Needs no run-time computation.
Feasible to enforce via compile time checks.
Inefficient.
Delay process initiation.
Subject to cyclic restart.
Disallows increment resource requests.
No preemption necessary
Real time system means that the system is subjected to real time, i.e. response
should be guaranteed within a specified time constraint or system should meet
the specified deadline.
Moreover, the system should perform the given task in a fixed time otherwise; it
results in a system failure.
In a RTOS, processing time requirement are calculated in tenths of seconds increments of time.
Response time is the time within which the system takes the input, processes the data, and gives the results.
Hence, a real-time operating system must be fast and responsive for an embedded system, weapon system, robots,
scientific research & experiments and various real-time objects.
1. Manage the processor and other system resources to meet the requirements of an application.
2. Synchronize with and respond to the system events.
3. Move the data efficiently among processes and to perform coordination among these processes.
For example, real-life situations governing an automatic car, traffic signal, nuclear reactor or an aircraft require an immediate
response to complete tasks within a specified time delay.
TYPES OF REAL-TIME OS
Hard real-time systems guarantee that critical tasks complete on time (i.e. purely deterministic).
In this, the time constraint is very short and strict. Even seconds of delay is not acceptable. Therefore, it is compulsory to
complete the task within the given time only.
In a hard real-time system, the secondary storage is either limited or missing, so these system stored data in the ROM.
For example users expected the output for the given input in 10 sec then system should process the input data and give
th th th
the output exactly by 10 second. It should not give the output by 11thsecond or by 9 second, exactly by 10 second it
should give the output.
In the hard real time system meeting the deadline is very important. If deadline is not met, it is not accepted by the
system or may face serious issues like a system failure.
Examples: Automobile control system like as Anti-Lock & Air Bag, Airline Control System, Diagnosis Control System etc.
In this type of RTOS, there is a deadline assigned for a specific job, but a delay for a small amount of time is acceptable.
In this, performance is degraded but not destroyed by failure to meet response-time constraints. That means soft real
time systems are less restrictive.
The usefulness of result produced by a soft real time system decreases gradually with increase in tardiness.
Soft real-time systems have limited utility than hard real-time systems.
Examples: Multimedia transmission and reception, Telecom (cellular) networks, Web sites and services, Smart phones,
Reservation system, Online transaction systems, Livestock price quotation systems, Computer games, etc.
In these systems, although the deadline is given but missing a deadline may not have big impact but could cause
undesired affects, like a huge reduction in quality of a product.
A firm real-time system is one in which a few missed deadlines will not lead to total failure, but missing more than a few
may lead to complete and catastrophic system failure.
The responses for the requests (e.g. replies to a query, result of a task, etc.) are worthless once the deadline for that
particular request has passed (i.e. the usefulness of a result is zero after its deadline).
A hypothetical example can be a storm forecast system (if a storm is predicted before arrival, then the system has done
its job, prediction after the event has already happened or when it is happening is of no value).
Examples are: Automation system in the industry sector like as visual inspection, various multimedia applications etc.
In hard real time system, the size of data file is small or In soft real time system, the size of data file is large.
medium.
In this system response time is in millisecond. In this system response time are higher.
Peak load performance should be predictable. In soft real time system, peak load can be tolerated.
A hard real time system is very restrictive. A Soft real time system is less restrictive.
In case of an error in a hard real time system, the In case of an soft real time system, computation is rolled back
computation is rolled back. to previously established a checkpoint.
Satellite launch, Railway signaling system etc. DVD player, telephone switches, electronic games etc.
CHARACTERISTICS/FEATURES OF RTOS
It has better reliability. It means, RTOS can work in long time without any human interference.
Its result is more predictable because its every action is executed into predefined time frame. Repeating an input will
result in the same output (i.e. deterministic).
Its performance is much better because it can perform more complex tasks without taking more workload.
All software and hardware are small size, which are used in the RTOS (Real Time Operating system). Due to this,
technician does not get more headaches for finding the errors in the RTOS.
It has good stability. So due to this feature, we can upgrade or downgrade to RTOS.
RTOS systems are fast and responsive (i.e. high performance) often executing actions within a small fraction of the time
needed by a general OS.
Kernel helps for storing the states of interrupted tasks for execution at appropriate time frame.
RTOS are frequently used in critical systems when failures can have catastrophic consequences, such as robotics or flight
controllers. To protect those around them, they must have higher security standards and more reliable safety features.
In RTOS, priority-based scheduling is done means that actions assigned a high priority are executed first, and those with
lower priority come after.
ADVANTAGES OF RTOS
1. Maximum consumption: Maximum utilization of devices and systems. Thus more output from all the resources.
2. Task Shifting: Time assigned for shifting tasks in these systems is very less. For example, in older systems, it takes about
10 microseconds. Shifting one task to another and in the latest systems, it takes 3 microseconds.
3. Focus On Application: Focus on running applications and less importance to applications that are in the queue.
4. Real-Time Operating System In Embedded System: Since the size of programs is small, RTOS can also be embedded
systems like in transport and others.
DISADVANTAGES OF RTOS
1. Limited Tasks: Very few tasks run simultaneously, and their concentration is very less on few applications to avoid errors.
2. Use Heavy System Resources: Plenty of resources are used by RTOS, which makes this system expensive.
3. Complex Algorithms: The algorithms are very complex which is difficult to understand.
4. Device Driver and Interrupt signals: It needs specific device drivers and interrupts signals to respond earliest to
interrupts.
5. Thread Priority: It is not good to set thread priority as these systems are very less prone to switching tasks.
APPLICATIONS OF RTOS
CENTRALIZED SYSTEMS: We start with centralized systems because they are the most intuitive and easy to understand and
define.
Centralized systems are systems that use client/server architecture where one or more client
nodes are directly connected to a central server. This is the most commonly used type of
system in many organizations where a client sends a request to a company server and receives
the response.
Example – - Wikipedia. Consider a massive server to which we send our requests and the
server responds with the article that we requested. Suppose we enter the search term „junk
food‟ in the Wikipedia search bar. This search term is sent as a request to the Wikipedia servers
(mostly located in Virginia, U.S.A) which then responds back with the articles based on
relevance. In this situation, we are the client node, Wikipedia servers are the central server.
Presence of a global clock: As the entire system consists of a central node(a server/ a master) and many client nodes(a
computer/ a slave), all client nodes sync up with the global clock(the clock of the central node).
One single central unit: One single central unit which serves/coordinates all the other nodes in the system.
Processors communicate with each other through various communication lines (like high-speed buses or telephone lines).
These are known as loosely coupled systems or distributed systems. Processors in this system may vary in size and function.
They are referred as sites, nodes, computers, and so on.
Distributed operating systems mainly comprise of: Workstations, Database Server Computer Node, Computer Network,
Terminal and File Server
All software and hardware compounds are located remotely. In order for them to communicate with each other, they
pass messages.
It is capable for sharing their computational capacity and I/O files (i.e. resource sharing). Resources are managed by
servers and clients use these resources.
TYPES OF DOS
1. Client-Server: This type of system requires the client to request a resource, and then the server provides that resource
which was requested.
When a client is in contact with one server, the server itself may serve multiple clients at the same time. Both the server and
the client will communicate over a computer network, which is part of a distributed system.
2. Three-Tier: The information about the client is stored in the middle tier, instead of storing it in the client, this is done to
simplify development. This architecture is most common in web applications.
3. N Tier: n tier systems are used when the server or application needs to forward requests to additional enterprise services
on the network.
4. Peer to Peer: This type of system contains nodes that are equal participants in data sharing; furthermore, all the tasks are
equally divided between all the nodes. These nodes will interact with each other as required as “share resources”. To
accomplish this, a network is
5. Middleware - Middleware allows the interoperability in the between of all applications, which are running on other
operating systems. With using these services those applications are capable for transferring all data each other.
CHARACTERISTICS OF A DOS
1. Heterogeneity:
In distributed systems, the components can have differences and variety in programming languages, operating systems,
computer hardware, networks, and implementations by different developers.
2. Resource Sharing:
(Every resource has its own management policies and methods; furthermore every resource is managed by a module,
commonly known as a resource manager).
Resource sharing is the ability to use hardware, software, or data anywhere in the system. The resource manager controls
access, moreover, it also provides naming scheme and controls concurrency in the DOS.
The hardware resources are shared for convenience and reductions in cost, in contrast, the data is shared for consistency
and exchange of information.
3. Openness:
With DOS, the “openness” is related to the extensions and improvements of disturbed systems.
4. Concurrency:
This characteristic of the system enables multiple activities in the system to be executed in the same time. In the
distributed system, the simultaneous execution of tasks happens in different components running on multiple machines,
furthermore, these tasks could execute interactions between each other.
The benefit of concurrency is increases the efficiency while reducing the latency in the system.
In the DOS, the components access and update shared resources (device drivers, variables, databases), moreover, if
concurrent updates are not coordinated it may result in inconsistent analysis.
5. Scalability:
In a DOS, the scalability of a system refers to how it handles the growth as number of users on the system platform
increases. Scaling best occurs by adding more systems to the network.
These components must be designed in a way that allows them to be scalable too.
6. Fault Tolerance:
The system is designed in a way that it is still available to the user despite a failure in the hardware, software, or the
network.
7. Security:
In a DOS, the clients send requests to access data managed by servers and resources in the networks.
Authentication
8. No Global Clock:
In a DOS, computers are connected through a network, and each computer has its own clock (so a system can have
many clocks).
Nodes communicate between each other through message passing, furthermore, their coordination is dependent on
time, as every client/computer has a different time span.
Even if the clocks were in sync, the individual clocks on each component may run at a different rate, leading them to be
out of sync after one local clock cycle.
Though accurate time synchronization is impossible in a DOS because it doesn‟t provide a global clock.
9. Flexibility:
Flexibility in a DOS is enhanced through modular characteristics, and by providing a more enhanced set of high level
services. The quality and completeness of the kernel/microkernel simplifies implementation of such services.
10. Synchronization:
Systems, which are cooperating concurrent process, have an inherent need for synchronization. This ensures that
changes happen in a correct and predictable manner.
The three basic circumstances, which define the scope of synchronization, are:
Synchronization between one or more processes must happen at a single point in order for one or more other processes
to continue.
One or more processes have to wait for an asynchronous (not existing or occurring at the same time) condition in order
to continue.
If synchronization is not done properly it may lead to multiple points of failure modes including loss of consistency,
isolation and durability, deadlock, lick, atomicity, and loss of serializability.
11. Transparency:
DOS are perceived by programmers and users as a whole system, rather than a collection of cooperating components.
Thus meaning the communication between components is hidden. Transparency directly affects the decision making in
every aspect of design of a DOS; it can imply certain requirements and/or restrictions on other design considerations.
ADVANTAGES OF DOS
1. Scalability: As computing occurs on each node independently, it is simple and inexpensive to add more nodes and
functionality as required. These systems are easily scalable as many systems can be easily added to the network.
2. Reliability: Most distributed systems are made from many nodes that work together which ultimately make them fault
tolerant. The system doesn‟t experience any disruptions if a single machine fails.
3. Performance: These systems are regarded to be very efficient as the work load can be broken up and sent to multiple
machines, therefore reducing data processing. It delivers better performance than a single system. Load on host computer
reduces.
4. Data Sharing: Nodes can easily share data with other nodes as they are connected with each other. Since resources are
being shared, computation is highly fast and durable.
5. No domino effect in case of a node failure: The failure of one node in a DOS does not have a domino effect and
enables all other nodes fail. Failure of one will not affect the other network communication, as all systems are independent
from each other.
6. Shareable: Resources, for instance like printers, can be shared with multiple nodes rather than just being constrained to
just one node.
DISADVANTAGES OF DOS
1. Scheduling: The system has to decide which jobs need to be executed, when they should be executed, and where they
should be executed.
2. Latency: The more widely distributed a system is the more latency can be experienced with communications. This
therefore results in teams/developers to make tradeoffs between availability, consistency and latency.
3. Observability: It can be a real challenge to gather, process, present, and monitor hardware usage metrics for large
clusters.
4. Security: It is difficult to place adequate security in DOS, as the nodes and the connections need to be secured.
5. Data Loss: Some data/messages may be lost in the network while moving from one node to another.
6. Complicated Database: In comparison to a single user system, the database connected to a DOS is relatively complicated
and difficult to handle.
7. Overloading: If multiple nodes in DOS send data all at once, then the system network may become overloaded.
8. Expensive: These types of systems are not readily available as they are very expensive.
9. Complex Software: Underlying software is highly complex and is not understood very well compared to other systems.
APPLICATIONS OF DOS
1. Telecommunication Networks: DOS are useful in phone network and cellular networks. Networks such as the Internet,
Wireless sensor networks, routing algorithms are also examples of a DOS.
2. Network Applications: Many network applications utilize DOS, such as Web, P2P networks, multiplayer web based games
and virtual communities. An example of such systems includes: banking systems, and airline reservation booking systems.
3. Real Time Process Control: Real time process control system that operates with a deadline, such examples include aircraft
control systems.
4. Parallel Computation: Systematic computer, which includes cluster computing and grid computing as well as varied
volunteer computing projects are based on DOS.
EXAMPLES OF DOS
LOCUS - In LOCUS operating system, can be accessed local and remote file in simultaneously without any location
hindrance
MICROS - MICROS operating system maintains the balance load of data while allotting the jobs to all nodes of entire
system
IRIX - IRIX operating system is used in the UNIX system V and LINUX
DYNIX - DYNIX operating system is developed for the Symmetry multiprocessor computers
AIX - AIX operating system is designed for IBM RS/ 6000 computers
Solaris - Solaris operating system designed for SUN multiprocessor workstations
Mach - Mach operating system allows the multithreading and multitasking features
OSF/ 1 - OSF/ 1 operating system is compatible with UNIX, and it is designed by Open Foundation Software Company
In most traditional OS, each process has an address space and a single thread of control. It is desirable to have multiple
threads of control sharing one address space but running in quasi-parallel.
Processor Allocation
Processor allocation determines which process is assigned to which processor; it is also called load distribution.
Two categories:
1. Static load distribution-non-migratory, once allocated, cannot move, no matter how overloaded the machine is.
2. Dynamic load distribution-migratory, can move even if the execution started. But algorithm is complex.
Remote Procedure Call (RPC) is a powerful technique for constructing distributed, client-server based applications. It is based
on extending the conventional local procedure calling so that the called procedure need not exist in the same address space
as the calling procedure. The two processes may be on the same system, or they may be on different systems with a network
connecting them.
Remote Procedure Call or RPC is a powerful technique for constructing distributed, client-server-based applications. It is also
known as a function call or a subroutine call. A remote procedure call is when a computer program causes a procedure to
execute in a different address space, coded as a local procedure call, without the programmer explicitly stating the details for
the remote interaction. The programmer writes essentially the same code whether the subroutine is local to the executing
program or remote. This is a form of client-server interaction implemented via a request-response message-passing system.
The RPC model implies location transparency that calling procedures are largely the same, whether local or remote. Usually,
they are not identical, so that local calls can be distinguished from remote calls. Remote calls are usually orders of magnitude
slower and less reliable than local calls, so distinguishing them is important.
RPCs are a form of inter-process communication (IPC), in that different processes have different address spaces. They have
distinct virtual address spaces on the same host machine, even though the physical address space is the same. While if they
are on different hosts, the physical address space is different.
1. The calling environment is suspended, procedure parameters are transferred across the network to the environment where
the procedure is to execute, and the procedure is executed there.
2. When the procedure finishes and produces its results, its results are transferred back to the calling environment, where
execution resumes as if returning from a regular procedure call.
NOTE: RPC is especially well suited for client-server (e.g. query-response) interaction in which the flow of
control alternates between the caller and callee. Conceptually, the client and server do not both execute at the same time.
Instead, the thread of execution jumps from the caller to the callee and then back again.