Operating System
Operating System
Semester: 1
Author
Credits
Centre for Distance and Online Education,
Parul University,
391760.
Website: https://paruluniversity.ac.in/
Disclaimer
This content is protected by CDOE, Parul University. It is sold under the stipulation that it cannot be
lent, resold, hired out, or otherwise circulated without obtaining prior written consent from the
publisher. The content should remain in the same binding or cover as it was initially published, and this
requirement should also extend to any subsequent purchaser. Furthermore, it is important to note that,
in compliance with the copyright protections outlined above, no part of this publication may be
reproduced, stored in a retrieval system, or transmitted through any means (including electronic,
Mechanical, photocopying, recording, or otherwise) without obtaining the prior
written permission from both the copyright owner and the publisher of this
content.
Note to Students
These course notes are intended for the exclusive use of students enrolled in
Online MCA. They are not to be shared or distributed without explicit permission
from the University. Any unauthorized sharing or distribution of these materials
may result in academic and legal consequences.
We have some examples here, First is Word Processor, Word processors are like Microsoft
word which is software used to make document files. Then we have spreadsheets,
Spreadsheets are used to make tabular Data or make some calculations on your tabular data.
And that are Compilers, the Software that we use to write computer code like C, C++, java, etc.
Next are Text Editors which are used to modify and Write text like your Notepad, WordPad, etc.
Last we have web Browsers, Web Browsers are the software that enables users to browse the
Web like google chrome, Mozilla Firefox, Internet Explorer, etc.
In the Computer system Operation we will be seen the structure of the Operating system. To
understand how an Operating system work we need to learn some basic about computer
system operation. A modern general-purpose computer system consists of one or more CPUs
and a number of device controllers connected through a common bus that provides access to
shared memory.
The CPU means it is the main part of the computer, the brain of the computer. It is the
Processing Unit all of the computation, processing, and calculation is done by the CPU.CPU is
not a box that you can see beside of Monitor, this box consists of a CPU, Motherboard, buses,
Memory, etc. the CPU is a small chip that is embedded in your motherboard. In modern
computers, we have one or more CPUs embedded in the motherboard. In this diagram, you can
see the number of devices connected to the CPU like Disks, Mouse, Keyboard, Printers, and
Monitor. All these devices are connected through Controller and the controllers are responsible
for how the devices work. For example, all Disks are connected to the Disk controller, mouse,
keyboard, and printer are connected to the USB Controller. CPU is connected along with this all
controllers to shared memory via a Common Bus.
1. Bootstrap Program: -The initial program that runs when a computer is powered up or
rebooted. -It is stored in the ROM (Stands for Read Only Memory is a kind of Main
Memory) -It must know how to load the OS and start executing that system(your
operating system is an interface between you and your hardware, it is one type of
software when you powered up your computer your operating system is load first in
your RAM (Random access memory)). -It must locate and load into memory the OS
Kernel (kernel is the main part of the operating system).
2. Interrupt: -The occurrence of an event is usually signaled by an Interrupt from Hardware
or software. (CPU is always working, and then the CPU is doing that work that time
hardware or software may interrupt the CPU. and tell the CPU to wait and execute this
task which is more important. so this activity is known as an interrupt) -Hardware may
trigger an interrupt at any time by sending a signal to the CPU, usually by the way of the
system bus.
3. System call(Monitor call): -Software may trigger an interrupt by executing a special
operation called System Call. Now let's see how the CPU behaves when it receives an
interrupt When the CPU is interrupted, it stops what it is doing and immediately
transfers execution to a new given task the new task executes. On Completion, the CPU
resumes the previous execution which it doing before interrupting.
STORAGE STRUCTURE
One of the most crucial components of a computer system is the operating system.
Communication between the user and the system must be established by the operating
system. The operating systems were not always as effective, quick, or organized as they are
today. An operating system has advanced thus far after many years of effort. Different
Between 1945 and 1955, the first generation of the operating system is covered. It was the
Second World War during this time. Even digital computers had not yet been created. People
utilizes calculating engines, a type of device made with mechanical relays, to perform
calculations. Because of how slowly these mechanical relays operate, vacuum tubes
The development of the shared system with multiprogramming and the start of
multiprocessing were challenges that the second generation of operating systems addressed.
A system that uses multiprogramming is one in which the processor switches between tasks
while a large number of user programs are simultaneously stored in the main storage. When a
machine is multi-programmed, its power is improved by utilizing multiple processors on a
The fourth generation of operating systems and systems is referred to be the period from 1980
to the present. As integrated circuits have become widely available, personal computers have
become common. To create personal computers, large-scale integrated circuits were combined.
These integrated circuits are made up of thousands of transistors packed onto a very small
silicon plate, sometimes only a few centimeters in size. The cost of microcomputers has
decreased relative to minicomputers due to the use of silicon. Microcomputers started to be
used widely by a lot of people due to their significantly lower price, which ultimately increased
the computer network around the world. Two different types of operating systems—the first
being network operating systems and the second being distributed operating systems—were
produced as a result of the expansion of networks. A data communication interface that
doubles as a server is employed in these systems. People may now access and log in to
machines that are located remotely thanks to the network operating system. Additionally, users
are now able to copy their files between several other PCs.
QUESTION BANK
Quiz 1.3
a) True
b) False
a) 1st
b) 2nd
c) 3rd
d) 4th
a) 1st
b) 2nd
c) 3rd
d) 4th
4. In this generation Time sharing, Real-time, Networks, and Distributed Operating System
was used.
a) 1st
b) 2nd
c) 3rd
d) 4th
b) generation
c) advancement
d) growth
1. ______ generation of computer started with using vacuum tubes as the basic
components.
3. In______ generation of the operating system, operating system designers develop the
concept of multi-programming in which several jobs are in the main memory at once.
4. ____ executes must frequently and makes the fine-grained decision of which process to
execute the next.
5. In the late __________, Herman Hollerith invented data storage on punched cards that
could then be read by the machine.
2. Multiprogramming System
3. Multitasking system
4. Multiprocessor System
Advantages
It is incredibly challenging to estimate or determine how long it will take to finish any task.
The batch system processors are aware of how long a work will take to complete when it is in
line.
Disadvantages
The difficulty of debugging batch operating systems is one of their main drawbacks.
MULTIPROGRAMMING SYSTEM
Advantages of Multiprogramming:
Disadvantages of Multiprogramming:
MULTITASKING SYSTEM
Advantages of Multitasking:
This operating system is more adapted to supporting several users concurrently, and numerous
apps can operate without slowing down the system.
● Virtual Memory
Operating systems that support multiple tasks have the best virtual memory system. Any
programme does not need to wait for a long period of time to finish its responsibilities thanks
to virtual memory; if this issue develops, those applications are shifted to virtual memory.
● Good Reliability
Users are happy as a result of the increased flexibility provided by multitasking operating
systems. It allows each user to run one or more programmes simultaneously.
● Secured Memory
Memory management is clearly specified in operating systems that support many tasks.
Because of this, the operating system forbids any permits for undesired apps to consume
memory.
● Time Shareable
So that no process has to wait for the CPU, each one is given a set length of time.
● Background Processing
An operating system that supports many tasks offers a better environment for background
operations. Most users are not aware of these background programmes, yet they are essential
to the smooth operation of other programmes like firewalls, antivirus software, and others.
An operating system that supports many tasks may control the I/O devices, RAM, hard drive,
CPU, and other hardware components.
Users have the option of running multiple programmes at once, including an internet browser,
games, Microsoft Excel, PowerPoint, and other utilities.
Disadvantages of Multitasking:
● Processor Boundation
Due to the processors' modest processing speeds, the system may run programmes slowly, and
processing numerous programmes may cause their reaction times to lengthen. More
computing power is necessary to address this issue.
● Memory Boundation
Multiple programmes running simultaneously can cause the computer to perform slowly since
the main memory becomes overburdened when many apps are loaded. Reaction time
lengthens because the CPU is unable to supply distinct times for each programme. Low-capacity
RAM usage is the main reason for this problem. As a result, a remedy can be offered by
increasing the RAM capacity.
● CPU Heat Up
In a multitasking environment, several processors are busier at once to perform any task, hence
the CPU produces more heat.
QUESTION BANK
Quiz 1.4
3. Multiprogramming systems
b) Multiprogramming
c) Time sharing
d) None of these
4. The environment in which programs of the system are executed is called _____?
5. OS stands for______________
2. Multiprogramming System
3. Multitasking system
4. Multiprocessor System
MULTIPROCESSOR SYSTEM
Advantages
Increased reliability: A multiprocessing system allows for the distribution of processing jobs
among a number of processors. This improves dependability since a job can be passed to
another processor for completion if one processor fails.
Increased throughout : More work can be done in less time as the number of processors rises.
The economy of Scale: Multiprocessor systems are less expensive than single-processor
systems because they share peripherals, secondary storage, and power supply.
Disadvantages
The various computers, nodes, and sites that make up this operating system are connected by
LAN/WAN links. It supports a wide range of real-time goods and different users, and it allows
For any task to be completed, a real-time operating system (RTOS), a special-purpose operating
system used in computers, must meet strict time requirements. It is primarily used in systems
where the output of computations is used to modify a process as it is running. A sensor used to
monitor the event is used to transmit information about external events to the computer
whenever they happen. The operating system interprets the signal from the sensor as an
interrupt. When an interrupt occurs, the operating system starts a particular process or group
of processes to handle it. Unless a higher priority interrupt happens while this process is
running, it continues without interruption. As a result, the interruptions must be prioritized in a
specific order. While lower priority interrupts should be retained in a buffer to be handled later,
the interrupt with the highest priority must be allowed to start the process. In a system like
this, interruption control is crucial.
All crucial actions in Hard RTOS must be finished by the deadline, or within the allotted time
frame. Missing the deadline would lead to serious failures, such as equipment damage or even
human life loss.
For Example,
Let's use the airbags and handle in the driver's seat that is provided by the automakers as an
example. The airbags expand when the driver applies the brakes at that precise moment,
preventing the driver's head from striking the handle. An accident would have happened if
there had been even a millisecond delay. Similarly to that, think of online stock trading
software. The system must make sure that an instruction to sell a certain share is executed
within a specified window of time. Otherwise, a sudden decline in the market could result in
the trader suffering a significant loss.
Through the use of the operating system, Soft RTOS accepts a few delays. It's possible under
this type of RTOS that a certain job has a deadline that must be met, but a brief delay is
allowed. Cutoff dates are therefore handled gently by this type of RTOS.
For Example,
This kind of system, for instance, is utilized in livestock price quote systems and online
transaction systems.
USER INTERFACE
By working as an interface, a user interface (UI) makes it easier for an application and its user to
communicate. For efficient communication, each application, including the operating system, is
given a unique user interface (UI). An application's user interface's two primary purposes are to
receive input from users and to deliver output to them. However, from one application to
another, the types of inputs and outputs that the UI accepts and produces may alter.
Any operating system's user interface can be categorized into one of the following types:
A form of GUI is a graphical user interface, which enables users to interact with the operating
system using point-and-click actions. A GUI has a number of icons that are visual
representations of various variables, like a file, directory, and device. Users can control the
graphical icons offered in the user interface (UI) using a mouse, trackball, touch screen, or light
pen, among other acceptable pointing devices. These graphical symbols can also be controlled
by other input devices like the keyboard. Because each object is represented by a
corresponding icon, GUIs are thought to be particularly user-friendly interfaces.
Using a command line interface, users can provide specific commands to the operating system
to communicate with it. The user must provide a command at the command line to carry out an
operation in this interface. The command line interpreter received a command when the user
entered the key. the computer program that is in charge of taking user commands and
processing them. Command line interpreters process commands and then display the
command prompt once more while also displaying the results of any earlier commands the user
has submitted. The CLI's drawback is that it requires a lot of user memory to interface with the
operating system.
The purpose of computer systems is to allow the user to execute programs. So the operating
systems provide an environment where the user can conveniently run programs. The user
does not have to worry about memory allocation or multitasking or anything. These things
are taken care of by the operating systems. Running a program involves allocating and
deallocating memory, and CPU scheduling in case of multiprocess. These functions cannot be
given to user-level programs. So, user-level programs cannot help the user to run programs
independently without help from operating systems.
Several steps must be completed in order for a program to be executed. The main memory
must be loaded with both the data and the instructions. Additionally, files and input-output
devices need to be initialized, and other resources need to be set up. These kinds of tasks are
carried out by the operating structures. The user no longer needs to be afraid of memory
allocation, multitasking, or anything else.
Each program requires an input and produces output. This involves the use of I/O. The
operating system hides the user the details of the underlying hardware for the I/O. All the
user sees is that the I/O has been performed without any details. So the operating systems by
providing I/O make it convenient for the users to run programs. For efficiency and protection
users cannot control I/O so this service cannot be provided by user-level programs.
Communication between the user and device drivers is established by the operating system,
which also controls input-output functions. Device drivers are programs that go along with
hardware that the operating system controls and are necessary for correct device
synchronization. Additionally, it gives the program access to input-output devices when
necessary.
The output of a program may need to be written into new files or input taken from some files.
The operating systems provide this service. The user does not have to worry about secondary
storage management. The user gives a command for reading or writing to a file and sees
his/her task accomplished. Thus operating systems make it easier for user programs to
accomplish their task. This service involves secondary storage management. The speed of I/O
that depends on secondary storage management is critical to the speed of many programs
and hence I think it is best relegated to the operating systems to manage it than giving
individual users control of it. It is not difficult for the user-level programs to provide these
services but for the above-mentioned reasons, it is best if these services are left with the
operating system.
Communication between the user and device drivers is established by the operating system,
which also controls input-output functions. Device drivers are programs that go along with
hardware that the operating system controls and are necessary for correct device
synchronization. Additionally, it gives the program access to input-output devices when
necessary. The operating system control how files are manipulated or how the files are
managed, sometimes you have to delete files, sometimes you have to update files or save
files, or search files all are controlled by the operating system. It also controls the permission
There are instances where processes need to communicate with each other to exchange
information. It may be between processes running on the same computer or running on
different computers. By providing this service the operating system relieves the user of the
worry of passing messages between processes. In case where the messages need to be passed
to processes on the other computers through a network, it can be done by the user
programs. The user program may be customized to the specifics of the hardware through
which the message transits and provides the service interface to the operating system.
The operating system controls how processes communicate with one another. Transferring
data among processes is a part of their communication. When processes are connected via a
ERROR DETECTION
An error is one part of the system that may cause malfunctioning of the complete system. To
avoid such a situation the operating system constantly monitors the system for detecting
errors. This relieves the user of the worry of errors propagating to various parts of the system
and causing malfunctioning. This service cannot allow being handled by user programs
because it involves monitoring and in some cases altering areas of memory or deallocation of
memory for a faulty process. Or maybe relinquishing the CPU of a process that goes into an
infinite loop. These tasks are too critical to be handed over to the user programs. A user
program if given these privileges can interfere with the correct (normal) operation of the
operating systems.
The operating system also manages errors that happen in input-output devices, the CPU, etc.
Additionally, it repairs the issues and makes sure they don't happen frequently. Additionally, it
keeps the process from reaching a deadlock. Additionally, it searches for any errors or defects
RESOURCE ALLOCATION
To improve overall performance, an operating system gathers usage data for a variety of
assets and keeps track of performance metrics and responsiveness. These private data are
useful for further upgrades and fine-tuning the device to improve overall performance.
Protection in an operating system is a method that restricts access to any computer system
resources by processes, programs, or users. All access to system resources must be monitored
and managed, thanks to the operating system. Additionally, it guarantees that peripherals or
external resources must be secured against unauthorized access. By using usernames and
passwords, it offers authentication.
SYSTEM CALLS
System calls to provide an interface between a running program and an operating system.
System calls are generally available as assembly language instructions. Several higher-level
languages such as C also allow making system calls directly.
In the UNIX operating system, the system called the interface layer contains an entry point in
kernel code. All system resources are managed by the kernel. Any request from a user or
application that involves access to any system resource must be handled by kernel code. The
user process must not be given open access to kernel code for security reasons. Many
opening into kernel code called system calls are provided to the user so that the user
processes can invoke the execution of kernel code. System calls allow processes and users to
manipulate system resources.
There are three general methods that are used to pass information (parameters) between a
running program and the operating system.
1. One method is to store parameters in registers.
2. Another is to store parameters in a table in memory and pass the address of the table.
3. The third method is to push parameters on the stack and allow the operating system to pop
the parameters off the stack.
One needs to first know the connection between a CPU's kernel mode and user mode in order
to understand system calls. These two modes are supported by all current operating systems.
USER MODE
● Programs can't directly access memory and hardware resources when the CPU is in user
mode.
● If a program breaks in user mode, only that specific program is stopped.
● This means that even if a user-mode program crashes, the system will still be in a secure
condition.
● So, the majority of OS programs operate in user mode.
This diagram shows how the processes run normally in user mode up until a system call
interrupts them. The system call is then carried out in kernel mode according to priority. The
control switches back to user mode after the system call has been completed, allowing user
processes to continue running.
Generally speaking, system calls are necessary for the following circumstances: −
● if a file system needs files to be created or deleted. A system call is also necessary for
reading and writing from files.
● development and administration of new procedures.
● System calls are also required for network connectivity. This also applies to packet
sending and receiving.
open()
A file in a file system can be accessed using the open() system call. In addition to allocating
resources for the file, this system call also gives the process a handle it can use to refer to the
read()
Data from a file that is kept in the file system can be accessed using the read() system call. The
file descriptor of the file to be read can be used to identify it, and it must first be opened using
open() before it can be read. The read() system function typically requires three arguments.
i.e., the file descriptor, the buffer that holds read data, and the number of bytes that need to
be read from the file.
write()
The write() system calls writes the data from a user buffer into a device such as a file. This
system call is one of the ways to output data from a program. In general, the write system
calls take three arguments i.e. file descriptor, a pointer to the buffer where data is stored, and
a number of bytes to write from the buffer.
close()
The close() system call is used to terminate access to a file system. Using this system call
means that the file is no longer required by the program and so the buffers are flushed, Both
the file resources and the file metadata are released.
STRUCTURE OF AN OS-LAYERED
The operating system structure is a container for a collection of structures for interacting with
the operating system’s file system, directory paths, processes, and I/O subsystem. The types
and functions provided by the operating system substructures are meant to present a model for
handling these resources that is largely independent of the operating system. There are
different types of structures as described in Figure
Users and computer hardware are connected by the operating system (OS). It offers users a
setting in which they may run programs quickly and effectively. Technically speaking, the
hardware is managed by software. An operating system manages how memory, processors,
devices, and information are distributed across various resources and services. In order to
conduct more functions and services, the operating system has many layers in its layer
structure. Each layer has a distinct task to complete that has been carefully established.
The operating system is segmented into several layers (say 0 to n). Each layer is in charge of
carrying out its specific duty. The following conventions are also available for the application of
these layers:
● In a layered operating system, the uppermost layer must be a UI (User Interface) layer.
● The hardware layer must be the innermost layer.
● Any layer has access to the layers that are present under it but not to the layer above it.
For instance, All layers from n-2 to 0 are accessible to layer n-1, but the nth layer is not.
Six Layers
1- Hardware: This layer controls how all peripheral devices, including a printer, mouse,
keyboard, scanner, and other devices, operate by interacting with the system hardware. Certain
categories of hardware devices must be managed by the hardware layer. In the layered
operating system architecture, the hardware layer is the lowest and most powerful layer. It has
a direct connection to the brain of the system.
2. CPU Planning: The scheduling of CPU processes is handled by this layer. Scheduling queues
are used in conjunction to manage processes. As soon as the methods are introduced to the
system, they are added to the job queue. The processes that are ready to run in the main
memory are listed in the ready queue.
Layering offers particular benefits in operating systems. Each layer can be separately designed
and can interact with other layers as necessary. In addition, it is simple to build, manage, and
update a system with layers. The other layers are unaffected by a change to the specification of
one layer. Only the layers above and below each one of the operating system's layers can
communicate with one another. The hardware is managed by the lowest layer, and user
applications are governed by the top layer.
MONOLITHIC SYSTEMS
A sort of kernel that enables operating system customization is the microkernel. It supports
low-level address space management and Inter-Process Communication while operating in
privileged mode (IPC). On top of the microkernel are OS services like the file system, virtual
memory management, and CPU scheduler. To ensure its security, each service has its own
address space. Additionally, each application has its own address space. As a result, kernels, OS
Services, and programs are all protected.
Another type of kernel is one that is monolithic. Each application in systems with a single,
monolithic kernel has its own address space. This one handles system resources between
software and hardware, much like the microkernel, except user services and kernel services are
implemented in the same address space. The operating system's size also grows as a result of
the kernel's size growth. Through system calls, this kernel offers CPU scheduling, a memory
management, file management, and other system operations. Operating system execution is
sped up by the fact that both services are implemented within the same address space.
Address space In a monolithic kernel, both user In microkernel user services and kernel,
services and kernel services are services are kept in separate address
kept in the same address space. spaces.
Security If a service crashes, then the whole If a service crashed, it does not affect
system crashes in a monolithic the working of the microkernel.
kernel.
WHAT IS PROCESS ?
1. Compiling
2. Running/Executing
That program only qualifies as a process after the second stage. You can turn any program
(application) into a process by double-clicking it on your computer or tapping it on your
Now, the operating system loads a process into memory once you start it (RAM). The process'
internal organization is shown as follows:
Global variables are contained in the data section, while the text section holds the program or
code. Because neither the code nor the program's variables will change, these two parts are
fixed in size. For dynamic memory allocation, the heap is used. When we are unable to
calculate the amount of memory needed, we now employ dynamic memory allocation. As a
result, the heap portion can expand in size as needed. The stack portion is utilized for
PROCESSES CREATION
The creation of a process requires the following steps. The order in which they are carried out is
not necessarily the same in all cases.
1. Name: The name of the program which is to run as the new process must be known.
2. Process ID and Process Control Block: The system creates a new process control block, or
locates an unused block in an array. This block is used to follow the execution of the program
through its course, keeping track of its resources and priority. Each process control block is
labeled by its PID or process identifier.
3. Locate the program to be executed on disk and allocate memory for the code segment in
RAM.
4. Load the program into the code segment and initialize the registers of the PCB with the start
address of the program and appropriate starting values for resources.
5. Priority: A priority must be computed for the process, using a default for the type of process
and any value which the user specified as a `nice’ value.
In a democratic system anyone can choose to start a new process, but it is never users which
create processes but other processes! That is because anyone using the system must already be
The processes are linked by a tree structure. If a parent is signaled or killed, usually all its
children receive the same signal or are destroyed with the parent. This doesn’t have to be the
case – it is possible to detach children from their parents – but in many cases, it is useful for
processes to be linked in this way.
The specific attributes of the child process that differ from the parent process are
2. The parent process ID of the child process is the process ID of its parent process.
3. The child process gets its own copies of the parent process’s open file descriptors.
Subsequently changing attributes of the file descriptors in the parent process won’t affect the
fi le descriptors in the child, and vice versa. However, the file position associated with each
descriptor is shared by both processes.
4. The elapsed processor times for the child process are set to zero.
7. The set of pending signals for the child process is cleared. (The child process inherits its mask
of blocked signals and signal actions from the parent process.)
PROCESS STATES
1. New
2. Ready
The ready state, where a process waits for the CPU to be assigned, is the first state it enters
after being formed. The operating system pulls new processes from secondary memory and
places them all in the main memory. The term "ready state processes" refers to processes that
are in the main memory and are prepared for execution. Numerous processes might be active
at the moment.
3. Running
The OS will select one of the processes from the ready state based on the scheduling
mechanism. As a result, if our system only has one CPU, there will only ever be one process
operating at any given time. We can run n processes concurrently in the system if there are n
processors.
4. Block or wait
Depending on the scheduling mechanism or the inherent behavior of the process, a process
can move from the Running state to the Block or Wait for states. The OS switches a process to
the block or waits for state and allows the CPU to the other processes while it waits for a
specific resource to be allocated or for user input.
5. Completion or termination
A process enters the termination state once it has completed its execution. The operating
system will end the process and erase the entire context of the process (Process Control
Block).
6. Suspend ready
When a process enters the suspend ready state, it is in the ready state after being transferred
from primary memory to secondary memory because primary memory was insufficient. When
a higher-priority process has to run but the main memory is already full, the operating system
must free up memory by moving the lower-priority process to the secondary memory. Until
7. Suspend wait
It is preferable to unblock the process that is blocking resources in the main memory rather
than remove it from the ready queue. It is better if it waits in the secondary memory to free
up space for the higher priority process as it is already waiting for a resource to become
available.
KEY TAKEAWAY
PCBs are stored in specially reserved memory for the operating system known as kernel space.
The task of allocating a CPU to a process falls to the operating system because a CPU is not
always required by a process. Let's use the input/output process as an example; the CPU only
makes use of it when it is triggered. The function of the process control block emerges as a
badge for every process. Before consulting each process' PCB, the operating system is unable
to identify which process is whose. For instance, the CPU is currently handling several
background processes, including those for MS Word, PDFs, and printing. Without knowing the
names of each process, how would OS manage and identify them? Therefore, PCB is used as a
data structure to record information about each operation in this situation. As a result,
anytime a user initiates a process (such as the command to print), the operating system
creates a process control block (PCB) for that process. The PCB is used by the operating
system to execute and manage the processes when the operating system is free.
1. Process ID:
When a user creates a new process, the operating system gives it a special ID or process ID. This
ID aids in separating the process from other ones already running within the system. The
operating system has a restriction on how many processes it can handle at once; let's say OS
can handle a maximum of N processes. Assuming that the older process at ID 0 has already
been terminated, the operating system will assign ID 0 to the new process if it is created after
the process(N-1). One of the methods for allocating process ids is this. The process IDs are not
assigned according to ascending order in another assignment technique. Let's assume that each
PCB uses X bytes of memory and that N processes can be active at once. Next, N*X bytes will be
set aside by the operating system for each PCB. From 0 to N-1, these PCBs are numbered.
2. Process State:
A process goes through various states from start to finish. A process may typically be in one of
the following 5 states while it is running:
New: The processes that are prepared to be loaded into the main memory by the operating
system are present in this state.
Ready: This state contains the process that is now stored in the system's main memory and is
prepared to be run. The operating system loads the processes into main memory from
secondary memory (hard disc) (RAM). The condition of these processes is known as the Ready
state since they are available in the main memory and are awaiting assignment to the CPU.
Running: The processes that are now being run by our system's CPU are listed in this state. Our
system can support a maximum of x processes operating at once if there are x total CPUs in use.
Termination: A process enters its termination state after completing its execution. The
operating system will also remove all of that process's data (Process Control Block).
3. Process Priority:
Each process' priority is indicated by a numeric value called "process priority." The process's
priority increases as the value decrease. This priority is determined when the PCB is created and
may vary depending on a number of variables, including the length of the process, the number
of resources used, and others. The process can also have an external priority assigned by the
user.
5. Program Counter:
The program counter is a pointer that directs program execution to the subsequent instruction.
The address of the subsequent instruction in the process is contained in this PCB characteristic.
6. CPU registers:
The CPU can access small, quickly sized locations in this manner. Virtual memory is where these
registers are kept (RAM).
7. Context Switching:
8. PCB pointer:
The address of the following PCB, which is in the ready state, is contained in this field. This
enables the operating system to maintain a simple control flow between parent processes and
child processes in a hierarchical manner.
KEY TAKEAWAY
➢ The Process Control Block (PCB) serves as a vital data structure within an operating
system, encapsulating essential information about a particular process.
➢ Created during the initialization of a process, the PCB acts as a dynamic runtime
descriptor, enabling the operating system to manage and coordinate the process
throughout its lifecycle.
➢ Information stored in the PCB encompasses diverse aspects, including the process's
identifier, priority, current state (such as ready, running, or suspended), hardware
status, scheduling details, memory management specifics, I/O status, file management
data, and accounting information.
➢ Upon termination of a process, its corresponding PCB is released, returning to the pool
of free cells available for new PCB creation.
CONTEXT SWITCHING
Typically there are several tasks to perform in a computer system. So if one task requires
some I/O operation, you want to initiate the I/O operation and go on to the next task. You
will come back to it later. This act of switching from one process to another is called a
“Context Switch” When you return back to a process, you should resume where you left off.
For all practical purposes, this process should never know there was a switch, and it should
look like this was the only process in the system. To implement this, on a context switch, you
have to 1. Save the context of the current process 2. Select the next process to run 3. Restore
the context of this new process.
Context Switching keeps track of the process's current state, which also contains information
about the data that was previously saved in registers, the program counter's value, and the
stack pointer. Context switching is required because, if we just transfer CPU control to the
new process without first storing the state of the old process, we won't be able to pick up
where the old process left off later since we won't know what was the last instruction the old
process performed. By preserving the process's state, context switching solves this issue.
Let's assume that the system has a process running on the CPU and that the ready queue is
empty. A new process has just been created and added to the ready queue. The CPU is allotted
by the system to the process with the greatest priority number. Therefore, the priority number
of the newly generated process will be compared to the priority number of the older process
when it enters the ready queue. In this case, the greater priority number demands preference
over the lower priority number. The older process's execution continues if the new process'
priority number is lower than the older process'. Context Switching will, however, be activated
if a new process has a higher priority number than an older process. The Process Control Block
stores the register data, program counter, and process state for the context switch. While the
KEY TAKEAWAY
SUBTOPIC 3.1
WHAT IS THREADS?
Take the human body as an example. A human body has various components with various
functionalities that operate in parallel ( E.g.: Eyes, ears, hands, etc.). Similar to this, a single
computer process may have several concurrently executing features, each of which can be
thought of as a separate thread. A thread is the orderly progression of tasks inside a process.
The types of threads in an OS can be the same or distinct. The performance of the programs is
improved through the use of threads. A program counter, stack, and set of registers are all
unique to each thread. However, a single process's threads may share the same code, data, or
file. Due to the fact that they share resources, threads are often referred to as lightweight
processes. Threads are a way for a program to fork (or split) itself into two or more
simultaneously (or pseudo-simultaneously) running tasks. A thread is a single sequence
stream within in a process. Because threads have some of the properties of processes, they
are sometimes called lightweight processes. In a process, threads allow multiple executions of
streams. In many respects, threads are a popular way to improve applications through
parallelism. The CPU switches rapidly back and forth among the threads giving the illusion that
the threads are running in parallel. Like a traditional process i.e., a process with one thread, a
thread can be in any of several states (Running, Blocked, Ready, or Terminated). Each thread
has its own stack. Since thread will generally call different procedures and thus a different
execution history. This is why the thread needs its own stack.
E.g.: While playing a movie on a device the audio and video are controlled by different threads
in the background.
THREAD STRUCTURE
Let us return to our example of the blocked file-server process in the single-process model. In
this scenario, no other server process can execute until the first process is unblocked. By
Differences
1. Unlike processes, threads are not independent of one another.
2. Unlike processes, all threads can access every address in the task.
3. Processes might or might not assist one another because processes may originate from
different users, but threads are designed to assist one another.
Process Thread
Creation and termination times of Creation and termination times of threads are
processes are slower. faster compared to processes.
Processes have their own code and Threads share code and data/file within a
data/file. process.
E.g. Opening two different browsers. E.g. Opening two tabs in the same browser.
TYPES OF THREAD
The user implements and manages user-level threads; the kernel is unaware of this.
● The OS is unaware of user-level threads because they are implemented using user-level
libraries.
● In comparison to kernel-level thread, the user-level thread can be created and managed
more quickly.
● It is quicker to switch contexts in user-level threads.
● The entire process becomes stalled if only one user-level thread does a blocking task.
Consider Java threads, POSIX threads, etc.
2. Kernel-level threads:
● System calls are used to implement kernel-level threads, and the OS is aware of them.
● Compared to user-level threads, kernel-level threads take longer to create and manage.
● In a kernel-level thread, context switching takes longer.
● The execution of a blocking action by one kernel-level thread has no impact on any
other threads. Consider Window Solaris.
MULTITHREADING MODELS
The application can divide its task into separate threads thanks to multithreading. The same
process or task can be completed by a number of threads in multi-threading, or we can say that
more than one thread is used to complete the task. Multitasking is possible by using
multithreading. One task can only be completed at a time in single threading systems; hence,
multithreading was invented to solve this problem. Multithreading allows numerous tasks to be
completed simultaneously.
Many user-level threads are mapped to one kernel thread in the many-to-one approach. A
context-switching environment that is effective and simple enough to be built on a kernel
without thread support is made possible by this kind of interaction.
The drawback of this architecture is that it is unable to benefit from the hardware acceleration
provided by multithreaded processes or multiprocessor systems because there is only one
kernel-level thread scheduling active at any given moment. All thread management in this is
carried out in user space. If a blockage occurs, the entire system is blocked by this model.
THREADING ISSUES
The threading issues are
BENEFITS OF THREADS
Following are some reasons why threads are used in designing operating systems:
1. A process with multiple threads makes a great server for example printer server.
2. Because threads can share common data, they do not need to use inter-process
communication.
4. Because of their very nature, threads can take advantage of multi-processors.
5. Threads need a stack and storage for registers therefore, threads are cheap to create.
6. Threads do not need new address space, global data, program code, or operating
system resources.
KEY TAKEAWAY
The objective of multiprogramming is to have some process running at all times to maximize
CPU utilization. The objective of a time-sharing system is to switch the CPU among processes
so frequently that users can interact with each program while it is running. For a uni-processor
system, there will never be more than one running process. If there are more processes, the
rest will have to wait until the CPU is free and can be rescheduled. As processes enter the
system, they are put into a job queue. This queue consists of all processes in the system. The
processes that are residing in the main memory and are ready and waiting to execute are kept
on a list called the ready queue. This queue is generally stored as a linked list. A ready-queue
header will contain pointers to the first and last PCBs in the list. Each PCB has a pointer field
that points to the next process in the ready queue. There are also other queues in the system.
When a process is allocated the CPU, it executes for a while and eventually quits, is
interrupted, or waits for the occurrence of a particular event, such as the completion of an I/O
request. In the case of an I/O request, such a request may be to a dedicated tape drive, or to a
shared device, such as a disk. Since there are many processes in the system, the disk may be
busy with the I/O request of some other process. The process therefore may have to wait for
the disk. The list of processes waiting for a particular I/O device is called a device queue. Each
device has its own device queue.
1. Utilization/Efficiency: keep the CPU busy 100% of the time with useful work
3. Turnaround time: from the time of submission to the time of completion, minimize the time
batch users must wait for output
5. Response Time: time from submission till the first response is produced, minimizing response
time for interactive users
6. Fairness: make sure each process gets a fair share of the CPU
You can create distinct queues for each process state and PCB using process scheduling queues.
The same queue is used to group processes that are in the same stage of execution. The PCB of
a process must therefore be unlinked from its previous queue and forwarded to the new state
queue whenever the status of the process is altered. Operating system queues come in three
varieties:
2. Ready queue - With this queue type, you may compile a list of all the active processes that
are waiting to be executed in the main memory.
3. Device queues: Device queues is a procedure that has been suspended because there is no
I/O device available.
Preemptive
Preemptive algorithms are driven by the notion of prioritized computation. The process with
the highest priority should always be the one currently using the processor. If a process is
currently using the processor and a new process with a higher priority enters, the ready list, the
process on the processor should be removed and returned to the ready list until it is once again
the highest-priority process in the system.
context_switch() is called even when the process is running usually done via a timer interrupt.
KEY TAKEAWAY
➢ CPU scheduling is a fundamental aspect of multiprogramming, allowing operating
systems to enhance computer productivity by switching the CPU among multiple
processes.
➢ The primary goal is to ensure a running process at all times, maximizing CPU utilization.
➢ Various scheduling strategies aim to meet criteria such as efficiency, throughput,
turnaround time, waiting time, response time, and fairness.
➢ Processes are organized into queues, including the job queue, ready queue, and device
queues, reflecting their different states and execution stages.
➢ Process schedulers, categorized into long-term, short-term, and medium-term
schedulers, play crucial roles in managing the degree of multiprogramming, selecting
PREEMPTIVE SCHEDULING:
Preemptive algorithms are driven by the notion of prioritized computation. The process with
the highest priority should always be the one currently using the processor. If a process is
currently using the processor and a new process with a higher priority enters, the ready list, the
process on the processor should be removed and returned to the ready list until it is once again
the highest-priority process in the system.
context_switch() is called even when the process is running usually done via a timer interrupt.
When a process transitions from the running state to the ready state or from the waiting state
to the ready state, preemptive scheduling is used. The resources, mostly CPU cycles, are given
to the process for a set period of time before being removed; if the process still has CPU burst
time left, it is then put back in the ready queue. Until it has its subsequent opportunity to run,
the process remains in the ready queue.
Round Robin (RR), Shortest Remaining Time First (SRTF), Priority (preemptive version), and
other preemptive scheduling algorithms are examples.
P1 4
P2 3
P3 5
Step 2) At time =2, P1 is added to the end of the Queue and P2 starts executing
Step 4) At time=6 , P3 is preempted and add at the end of the queue. P1 starts executing.
Step 5) At time=8 , P1 has a burst time of 4. It has completed execution. P2 starts execution
Step 7) Let’s calculate the average waiting time for above example.
Wait time
P1= 0+ 4= 4 ms
P2= 2+4= 6 ms
P3= 4+3= 7 ms
NON-PREEMPTIVE SCHEDULING
Non-preemptive When a process ends or shifts from the running state to the waiting state,
scheduling is employed. Once the resources (CPU cycles) are assigned to a process in this
scheduling, the process retains the CPU until it terminates or enters a waiting state. A process
operating on a CPU is not interrupted mid-execution in the case of non-preemptive scheduling.
Instead, it delays allocating the CPU to another process until the current one has finished its
CPU burst period.
Shortest Job First (SJF is essentially non-preemptive), Priority (non-preemptive version), and
other algorithms are based on non-preemptive scheduling.
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 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 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.
Wait time
P4= 0-0=0
P1= 3-2=1
P2= 9-5=4
P5= 11-4=7
P3= 15-1=14
1. Preemptive scheduling allots the CPU to the processes for a set period of time, whereas
non-preemptive scheduling allots the CPU to the process until it finishes or enters the
waiting state.
2. When a higher priority task arrives, the running process in preemptive scheduling is
halted in the middle of it; in contrast, the running process in non-preemptive scheduling
waits until it is finished running.
3. Preemptive scheduling involves the overhead of maintaining the ready queue as well as
transferring a process from the ready state to the running state and vice versa. In
Examples of preemptive
Examples of non-preemptive
scheduling are Round Robin
Examples scheduling are First Come First
and Shortest Remaining Time
Serve and Shortest Job First.
First.
KEY TAKEAWAY
The simplest CPU scheduling method arranges processes in agreement with their arrival times.
According to the first come, first served scheduling algorithm, the CPU is assigned to the
process that demands it first. The FIFO queue is used to implement it. A process's PCB is
connected to the tail of the queue when it enters the ready queue. The process at the front of
the queue receives the CPU when it is available. After then, the queue is cleared of the active
process. FCFS is a non-preemptive scheduling algorithm.
FCFS characteristics:
Purchasing a movie ticket at the box office is a real example of the FCFS approach. In this
scheduling system, a person is handled in a queue-style fashion. The person who enters the line
first purchases the ticket, followed by the next person. This will go on up until the very last
Here is an example of five processes arriving at different times. Each process has a different
burst time.
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
Using the FCFS scheduling algorithm, these processes are handled as follows.
Step 9) At time=21, P2 starts execution. It has a burst time of 2. It completes execution at time
interval 23
FCFS advantages
The benefits of FCFS include:
● simple to programme
● Come first, serve first
● simple algorithm for scheduling.
FCFS disadvantages
● The issue of starving occurs as a result of non-preemptive nature.
● More Typical wait time.
● FCFS is ineffective because of how easy it is to use.
● The best scheduling method for the time-sharing system is not FCFS.
● Since FCFS uses a non-preemptive scheduling mechanism, allocating the CPU to a
process never results in the CPU being released until the process has finished running.
● Resource usage in FCFS is poor because it is impossible to utilise the resources
concurrently, which results in the convoy effect.
KEY TAKEAWAY
The method known as "Shortest Job First" (SJF) selects the process with the shortest execution
time for the subsequent execution. Both preemptive and non-preemptive scheduling strategies
are possible. The average time that other processes are left waiting to be executed is greatly
decreased.
Completion Time: The time at which the process completes its execution.
Turn Around Time: Time Difference between completion time and arrival time.
Waiting Time(W.T): Time Difference between turn around time and burst time.
The SJF algorithm takes processes that use the shortest CPU time first. Mathematically seen,
and corresponding to the experience, this is the ideal scheduling algorithm. I won’t give details
in here about the performance. It’s all to do about overhead and response time, actually: the
system will be fast when the scheduler doesn’t take much of the CPU time itself, and when
interactive processes react quickly (get a fast response). This system would be very good. The
overhead caused by the algorithm itself is huge, however. The scheduler would have top
implement some way to time the CPU usage of processes and predict it, or the user should tell
the scheduler how long a job (this is really a word that comes from very early computer design
when Batch Job Operating Systems were used would take. So, it is impossible to implement this
algorithm without hurting performance very much.
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
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 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 the above example.
Turn Around Time = Completion Time – Arrival Time
P4= 3-0=3 ms
P1= 9-2=7 ms
P2= 11-5=6 ms
P5= 15-4=11 ms
P3= 23-1=22 ms
P4= 3-3=0 ms
P1= 7-6=1 ms
P2= 6-2=4 ms
P5= 11-4=7 ms
P3= 22-8=14 ms
● SJF has a shorter average waiting time than the First come first serve (FCFS) algorithm.
● Typically, SJF is used for long-term scheduling.
● It is appropriate for jobs that run in batches and whose run periods are known.
● In terms of turnaround time on average, SJF is probably ideal.
Disadvantages of SJF:
Shortest remaining time is a method of CPU scheduling that is a preemptive version of the
shortest job next scheduling. In this scheduling algorithm, the process with the smallest amount
of time remaining until completion is selected to execute. Since the currently executing process
is the one with the shortest amount of time remaining by definition, and since that time should
only reduce as execution progresses, processes will always run until they are complete or a new
process is added that requires a smaller amount of time. The shortest remaining time is
advantageous because short processes are handled very quickly. The system also requires very
little overhead since it only makes a decision when a process completes or a new process is
added, and when a new process is added the algorithm only needs to compare the currently
executing process with the new process, ignoring all other processes currently waiting to
execute. However, it has the potential for process starvation for processes which will require a
long time to complete if short processes are continually added, though this threat can be
Jobs are inserted into the ready queue as they are received in preemptive SJF scheduling. The
process with the least burst time starts running. The current process is terminated or prevented
from continuing if one with a shorter burst time enters the system, and the shorter job is given
a CPU cycle.
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
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 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.
P2 2 5
P3 8 1
P4 3 0
P5 3 out of 4 is remaining 4
P1 5 out of 6 is remaining 2
P2 2 5
P3 8 1
P4 3 0
P5 3 out of 4 is remaining 4
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 11) Let’s calculate the average waiting time for above example.
Turn Around Time = Completion Time – Arrival Time
P4= 3-0=3 ms
P1= 15-2=13 ms
P2=7-5 = 2 ms
P5= 10-4 = 6 ms
P3= 23-1 = 22 ms
P4= 3-3=0 ms
P1= 13-6 =7 ms
P2= 2-2 = 0 ms
P5= 6-4 =2
P3= 22-8 = 14
Average Waiting Time = 0+7+0+2+14/5 = 23/5 =4.6
KEY TAKEAWAY
➢ The Shortest Job First (SJF) scheduling algorithm, available in preemptive and non-
preemptive versions, prioritizes processes based on their execution times, aiming to
execute the shortest job first for optimal efficiency.
➢ In the non-preemptive variant, the CPU cycle is allocated to a process until it reaches a
waiting state or completes, while the preemptive version constantly selects the process
with the smallest remaining time.
➢ The non-preemptive SJF, though theoretically ideal, faces challenges in implementation
due to the need for accurate runtime predictions.
➢ An illustrative example showcases the non-preemptive SJF in action, considering
different processes with distinct burst times. The advantages of SJF include reduced
average waiting times, making it suitable for long-term scheduling and jobs with known
run periods.
➢ However, SJF is not without its drawbacks, such as the potential for process starvation
and the requirement for accurate runtime predictions.
➢ The preemptive SJF variant, also known as Shortest Remaining Time First, dynamically
selects the process with the smallest remaining time, efficiently handling short
processes but facing potential starvation for longer processes.
➢ The accompanying example demonstrates preemptive SJF scheduling, emphasizing its
advantages and potential pitfalls.
Example: Time limits, memory requirements, no. of open files, the ratio of average I/O burst
time to average CPU burst time, etc. external priorities are set by criteria that are external to
the OS, such as the importance of the process, the type and amount of funds being paid for
computer use, the department sponsoring work and other often political factors. Priority
scheduling can be preemptive or non-preemptive. A preemptive priority scheduling algorithm
will preempt the CPU if the priority of the newly arrived process is higher than the priority of
the currently running process. A non-preemptive priority scheduling algorithm will simply put
the new process at the head of the ready queue. A major problem with priority scheduling
algorithms is indefinite blocking or starvation. This can be solved by a technique called aging
wherein I gradually increase the priority of a long waiting process.
Determining the order in which processes and activities will access the processor and how
much processing time each will receive is a key responsibility of the operating system. Process
scheduling refers to this operating system feature.
The operating system stores each of these processes in a process table and assigns each one a
unique ID (PID) in order to keep track of them all. Additionally, the operating system utilises a
But did you ever wonder what the Operating System's criteria are for scheduling these
processes? What process needs to be given CPU resources first? The Operating System makes
these selections in a variety of ways, including by executing the process that requires the least
amount of time first or by executing the processes in the order in which they requested access
to the processor, among other options. The process's priority is one of these crucial factors.
Processes are carried out according to priority scheduling in OS. Higher-priority tasks or
processes are carried out first. Naturally, you might be curious about how procedures are
prioritized.
● Time limit
● Memory requirements of the process
● Ratio of average I/O to average CPU burst time
There may be more criteria used to determine the priority of a process or task. The scheduler
assigns the processes this priority.
● It is an algorithm for scheduling tasks that prioritizes the incoming processes before
scheduling them.
● The process has higher priority if the integer value of the priority number is lower. Low
numbers indicate high importance.
Completion Time: The time at which the process completes its execution.
Turn Around Time: Time Difference between completion time and arrival time.
Waiting Time(W.T): Time Difference between turn around time and burst time.
● The process that is now running won't be interrupted even if another process with a
higher priority is sent for execution while it is still in progress.
● Since it has a higher priority than the processes that are in the waiting state for
execution, the newly arrived high-priority process will be put in next for execution.
● All other processes will continue to wait in the processing queue. After the current
process has finished running, the CPU will be allocated to the high-priority task.
P1 1 4 0
P2 2 3 0
P3 1 7 6
P4 3 4 11
P5 2 2 12
Step 0) At time=0, Process P1 and P2 arrive. P1 has a higher priority than P2. The execution
begins with process P1, which has burst time 4.
Step 3) At time 3, no new process arrives so you can continue with P1. P2 process still in the
waiting queue.
Step 6) At time=6, P3 arrives. P3 is at higher priority (1) compared to P2 having priority (2). P2 is
preempted, and P3 begins its execution.
P1 1 4 0
P2 2 1 out of 3 pending 0
P3 1 7 6
P4 3 4 11
P5 2 2 12
Step 11) At time=11, P4 arrives with priority 4. P3 has higher priority, so it continues its
execution.
P1 1 4 0
P2 2 1 out of 3 pending 0
P3 1 2 out of 7 pending 6
P4 3 4 11
P5 2 2 12
Step 13) At time=13, P3 completes execution. We have P2,P4,P5 in ready queue. P2 and P5
have equal priority. Arrival time of P2 is before P5. So P2 starts execution.
P1 1 4 0
P2 2 1 out of 3 pending 0
P3 1 7 6
P4 3 4 11
P5 2 2 12
Step 16) At time= 16, P5 is finished with its execution. P4 is the only process left. It starts
execution.
Step 18) Let’s calculate the average waiting time for the above example.
Waiting Time = start time – arrival time + wait time for next burst
P1 = o - o = o ms
P2 =4 - o + 7 =11 ms
P3= 6-6=0 ms
P4= 16-11=5 ms
Average Waiting time = (0+11+0+5+2)/5 = 18/5= 3.6 ms
Advantages of Priority Scheduling Algorithm
● Due to the currently active process, high priority processes do not have to wait for their
turn to be executed.
● The relative relevance and priority of processes can be determined.
● Applications with variable resource and time requirements are beneficial.
KEY TAKEAWAY
➢ Priority Scheduling Algorithm in operating systems assigns a priority to each process and
allocates the CPU to the process with the highest priority. Priorities can be determined
by factors such as time limits, memory requirements, and the ratio of average I/O burst
time to average CPU burst time.
➢ The algorithm can be either preemptive, where the CPU is preempted if a higher-priority
process arrives, or non-preemptive, where the new process is placed at the head of the
ready queue.
➢ Priority scheduling aims to optimize resource usage based on task importance.
However, a major challenge is the potential for indefinite blocking or starvation of low-
priority processes, which can be addressed using aging techniques.
➢ The algorithm's characteristics, both preemptive and non-preemptive, are explored,
with an example illustrating their application.
➢ Priority scheduling offers advantages such as efficient execution of high-priority tasks
but also comes with drawbacks like the possibility of starvation for low-priority
processes.
➢ The average waiting time is calculated for an illustrative example, showcasing the
algorithm's practical implications in process execution.
The Round Robin CPU algorithm frequently emphasizes the Time Sharing method.
Time quantum refers to the amount of time that a process or job is permitted to operate when
using a preemptive technique.
Every process or job in the ready queue is given a CPU for the duration of that time quantum; if
the process is finished running during that time, the process ends; otherwise, the process
returns to the waiting list and waits for its turn to run again.
● The CPU is shifted to the next process after fixed interval time, which is called time
quantum/time slice.
● Time slice should be minimum, which is assigned for a specific task that needs to be
processed. However, it may differ OS to OS.
● It is a real time algorithm which responds to the event within a specific time limit.
Turn Around Time: Time Difference between completion time and arrival time.
Waiting Time(W.T): Time Difference between turn around time and burst time.
P1 4
P2 3
P3 5
Step 2) At time =2, P1 is added to the end of the Queue and P2 starts executing
Step 3) At time=4 , P2 is preempted and add at the end of the queue. P3 starts executing.
Step 5) At time=8 , P1 has a burst time of 4. It has completed execution. P2 starts execution
Step 7) Let’s calculate the average waiting time for above example.
Wait time
P1= 0+ 4= 4
P2= 2+4= 6
P3= 4+3= 7
KEY TAKEAWAY
Each algorithm supports a different process, but in a general system, some processes require
scheduling using a priority algorithm. While some processes want to stay in the system
(interactive processes), others are background processes whose execution can be delayed. The
number of ready queue algorithms between queues and within queues may differ between
systems. A round-robin method with various time quantum is typically utilized for such
maintenance. Several types of scheduling algorithms are designed for circumstances where the
processes can be readily separated into groups. There are two sorts of processes that require
different scheduling algorithms because they have varying response times and resource
requirements. The foreground (interactive) and background processes (batch process) are
distinguished. Background processes take priority over foreground processes. The ready queue
has been partitioned into seven different queues using the multilevel queue scheduling
technique. These processes are assigned to one queue based on their priority, such as memory
size, process priority, or type. The method for scheduling each queue is different. Some queues
are utilized for the foreground process, while others are used for the background process. The
foreground queue may be scheduled using a round-robin method, and the background queue
can be scheduled using an FCFS strategy.
EXAMPLE
Every queue would have absolute priority over the low-priority queues. No process may
execute until the high-priority queues are empty. In the above instance, no other process may
execute until and unless the queues for system, interactive, and editing processes are empty. If
an interactive editing process enters the ready queue while a batch process is underway, the
batch process will be preempted.
There are descriptions of the processes that are used in the above example:
System Process
The OS has its process to execute, which is referred to as the System Process.
Interactive Process
Batch processing is an operating system feature that collects programs and data into a batch
before processing starts.
Student Process
The system process is always given the highest priority, whereas the student processes are
always given the lowest.
EXAMPLE PROBLEM
Let's take an example of a multilevel queue-scheduling (MQS) algorithm that shows how
multilevel queue scheduling work. Consider the four processes listed in the table below under
multilevel queue scheduling. The queue number denotes the process's queue.
P1 0 4 1
P2 0 3 1
P3 0 8 2
P4 10 5 4
Queue 1 has a higher priority than queue 2. Round Robin is used in queue 1 (Time Quantum =
2), while FCFS is used in queue 2.
1. Both queues have been processed at the start. Therefore, queue 1 (P1, P2) runs first
(due to greater priority) in a round-robin way and finishes after 7 units.
2. The process in queue 2 (Process P3) starts running (since there is no process in queue
1), but while it is executing, P4 enters queue 1 and interrupts P3, and then P3 takes the
CPU and finishes its execution.
Advantages
1. You can use multilevel queue scheduling to apply different scheduling methods to
distinct processes.
2. It will have low overhead in terms of scheduling.
Disadvantages
Multilevel feedback queue scheduling, however, allows a process to move between queues.
The idea is to separate processes with different CPU-burst characteristics. If a process uses too
much CPU time, it will be moved to a lower-priority queue. Similarly, a process that waits too
long in a lower-priority queue may be moved to a higher-priority queue. This form of aging
prevents starvation.
● The method used to determine which queue a process will enter when that process
needs service.
The definition of a multilevel feedback queue scheduler makes it the most general CPU-
scheduling algorithm. It can be configured to match a specific system under design.
Unfortunately, it also requires some means of selecting values for all the parameters to define
the best scheduler. Although a multilevel feedback queue is the most general scheme, it is also
the most complex.
Explanation:
First of all, Suppose that queues 1 and 2 follow round robin with time quantum 8 and 16
respectively and queue 3 follows FCFS. One of the implementations of Multilevel Feedback
Queue Scheduling is as follows:
Also, the above implementation may differ for the example in which the last queue will follow
Round-robin Scheduling.
In the above Implementation, there is a problem and that is; Any process that is in the lower
priority queue has to suffer starvation due to some short processes that are taking all the CPU
time.
And the solution to this problem is: There is a solution that is to boost the priority of all the
processes after regular intervals and then place all the processes in the highest priority queue.
Following are some points to understand the need for such complex scheduling:
● In order to optimize the turnaround time, the SJF algorithm is needed which basically
requires the running time of processes in order to schedule them. As we know that the
running time of processes is not known in advance. Also, this scheduling mainly runs a
process for a time quantum and after that, it can change the priority of the process if
the process is long. Thus this scheduling algorithm mainly learns from the past behavior
of the processes and then it can predict the future behavior of the processes. In this
way, MFQS tries to run a shorter process first which in return leads to optimizing the
turnaround time.
ADVANTAGES OF MFQS
● This scheduling algorithm allows different processes to move between different queues.
● In this algorithm, A process that waits too long in a lower priority queue may be moved
to a higher priority queue which helps in preventing starvation.
DISADVANTAGES OF MFQS
● As processes are moving around different queues it leads to the production of more
CPU overheads.
● In order to select the best scheduler this algorithm requires some other means to select
the values
KEY TAKEAWAY
➢ Multilevel Queue (MLQ) and Multilevel Feedback Queue (MFQ) scheduling algorithms
offer flexible solutions to the diverse needs of various processes within an operating
system.
➢ In MLQ, different processes are assigned to specific queues based on priorities, allowing
for the separation of foreground and background tasks.
➢ Each queue is scheduled independently, accommodating processes with varying
response times and resource requirements.
➢ While MLQ has low scheduling overhead, it may pose a risk of starvation for lower-
priority processes and is inherently rigid.
➢ On the other hand, MFQ introduces flexibility by allowing processes to move between
queues based on their CPU usage and waiting times. This dynamic approach prevents
starvation, but the algorithm is more complex and introduces additional CPU overhead.
➢ MFQ optimizes turnaround time by learning from past process behavior, making it a
valuable scheduling algorithm despite its intricacies.
➢ The need for such complexity arises from the flexibility required to handle diverse
processes efficiently.
PROCESS SYNCHRONIZATION
Modern operating systems, such as Unix, execute processes concurrently. Although a single
Central Processor (CPU) executes the instructions of only one program at a time, the operating
system rapidly switches the processor between different processes (usually allowing a single
process a few hundred microseconds of CPU time before replacing it with another process.)
Some of these resources (such as memory) are simultaneously shared by all processes. Such
resources are being used in parallel between all running processes on the system. Other
resources must be used by one process at a time, so must be carefully managed so that all
processes get access to the resource. Such resources are being used concurrently between all
running processes on the system. The most important example of a shared resource is the CPU,
although most of the I/O devices are also shared. For many of these shared resources, the
operating system distributes the time a process requires of the resource to ensure reasonable
access for all processes. Consider the CPU, the operating system has a clock that sets an alarm
every few hundred microseconds. At this time the operating system stops the CPU and saves all
the relevant information that is needed to re-start the CPU exactly where it last left off (this will
include saving the current instruction being executed, the state of the memory in the CPUs
registers, and other data), and removes the process from the use of the CPU. The operating
system then selects another process to run, returns the state of the CPU to what it was when it
last ran this new process and starts the CPU again. Let’s take a moment to see how the
operating system manages this. In this unit, we shall discuss the deadlock. A deadlock is a
situation wherein two or more competing actions are waiting for the other to finish, and thus
neither ever does. It is often seen in a paradox like ‘the chicken or the egg’. This situation may
be likened to two people who are drawing diagrams, with only one pencil and one ruler
This is a new chapter on process synchronization. in this chapter, we learn what process
synchronization is, and how it is a useful and very important part of the operating system.
Process synchronization means that we need some kind of synchronization between processes.
To understand this we need to understand what is cooperating processes. so basically we have
two types of processes here.
1. Independent processes
2. Cooperating processes
While a cooperative process can be impacted by other processes that are executing, an
independent process is unaffected by the execution of other processes. Although it is possible
to assume that processes operating independently will function extremely effectively, there are
really many circumstances in which cooperative nature can be used to boost computational
speed, ease, and modularity. Processes can communicate with one another and coordinate
their actions through a technique called inter-process communication (IPC). These processes'
communication with one another might be thought of as a means of cooperation. Processes
can speak with one another using both:
1. Cooperation by sharing
The processes may cooperate by sharing data, including variables, memory, databases, etc. The
critical section provides data integrity, and writing is mutually exclusive to avoid inconsistent
data.
2. COOPERATION BY COMMUNICATION
The cooperating processes may cooperate by using messages. If every process waits for a
message from another process to execute a task, it may cause a deadlock. If a process does not
receive any messages, it may cause starvation.
There are various advantages of cooperating processes in the operating system. Some
advantages of the cooperating system are as follows:
1. Information Sharing: Cooperating processes can be used to share data between processes.
That could involve having access to the same files. A technique is required to allow the
processes to access the files concurrently.
2. Modularity: The division of large tasks into smaller subtasks is referred to as modularity.
These smaller subtasks can be completed by multiple collaborating processes. As a result, the
necessary tasks are finished quicker and more effectively.
Systems that allow processes to communicate and synchronize their actions are necessary for
the concurrent execution of cooperating processes.
The key to preventing trouble involving shared storage is to find some way to prohibit more
than one process from reading and writing the shared data simultaneously. That part of the
program where the shared memory is accessed is called the Critical Section. To avoid race
conditions and flawed results, one must identify codes in Critical Sections in each thread.
Here, the important point is that when one process is executing shared modifiable data in its
critical section, no other process is to be allowed to execute in its critical section. Thus, the
execution of critical sections by the processes is mutually exclusive in time.
A way of making sure that if one process is using shared modifiable data, the other processes
will be excluded from doing the same thing. Formally, while one process executes the shared
variable, all other processes desiring to do so at the same time moment should be kept waiting;
when that process has finished executing the shared variable, one the processes waiting; while
that process has finished executing the shared variable, one of the processes waiting to do so
should be allowed to proceed. In this fashion, each process executing the shared data
(variables) excludes all others from doing so simultaneously. This is called Mutual Exclusion.
The critical section is a code segment that allows access to the shared variables. In a critical
section, an atomic action is required, which means that only one process can execute its
critical part at a time. All the other processes have to wait to execute in their essential areas.
Example:
In the clothes section of a supermarket, two people are shopping for clothes.
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.
The critical section problem needs a solution to synchronize the different processes. The
solution to the critical section problem must satisfy the following conditions −
● Mutual Exclusion: 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.
● 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.
● Bounded Waiting: Bounded waiting means that each process must have a limited
waiting time. It should not wait endlessly to access the critical section.
RACE CONDITION
A race condition is an undesirable scenario that arises when a device or system attempts to do
two or more actions at the same time, but because of the nature of the device or system, the
activities must be done in the proper sequence to be done correctly. Race conditions are most
typically connected with computer science and programming. They arise when two computer
programme processes, or threads, try to access the same resource at the same time, causing
system issues. Race conditions are a prevalent problem in multithreaded programmes.
A light switch is a basic example of a race situation. In certain homes, many light switches are
linked to a single ceiling light. When these circuits are used, the switch position is rendered
meaningless. If the light is turned on, moving either switch away from its current position turns
it off. Similarly, if the light is off, then moving either switch from its current position turns the
light on. Consider what would happen if two people tried to turn on the light using two
different switches at the same time. One instruction may negate the other, or the two acts may
cause the circuit breaker to trip. A race condition may arise in computer memory or storage if
requests to read and write a large amount of data are received almost simultaneously, and the
machine attempts to overwrite some or all of the old data while that old data is still being read.
Consider a case in which a process requires a resource for a particular software. Nevertheless,
the resource is now in use and unavailable, thus the procedure must wait for resource
availability before proceeding. This is referred to as busy waiting, as depicted below:
DISABLING INTERRUPTS
Each process disables all interrupts just after entering in its critical section and re-enable all
interrupts just before leaving critical section. With interrupts turned off the CPU could not be
switched to other process. Hence, no other process will enter its critical and mutual exclusion
achieved.
The easiest solution is for each process to disable all interrupts as soon as it enters its critical
region and re-enable them as soon as it exits it. No clock interrupts can occur while interrupts
are disabled. After all, the CPU is only shifted from process to process as a result of a clock or
other interruptions, and if interrupts are disabled, the CPU will not be switched to another
process. As a result, once a process has deactivated interrupts, it can examine and update
shared memory without risk of interference from other processes.
Yet, it is frequently advantageous for the kernel to suppress interrupts for a few instructions
while updating variables or lists. Race situations may develop if an interrupt happened while
the list of ready processes, for example, was in an inconsistent state. The conclusion is that
while deactivating interrupts is frequently useful within the operating system, it is not suited as
a broad mutual exclusion mechanism for user programs.
while (true)
Conclusion
Disabling interrupts is sometimes a useful interrupts is sometimes a useful technique within the
kernel of an operating system, but it is not appropriate as a general mutual exclusion
mechanism for users process. The reason is that it is unwise to give user process the power to
turn off interrupts.
In this solution, you consider a single, shared, (lock) variable, initially 0. When a process wants
to enter in its critical section, it fi rst test the lock. If lock is 0, the process fi rst sets it to 1 and
then enters the critical section. If the lock is already 1, the process just waits until (lock) variable
becomes 0. Thus, a 0 means that no process in its critical section, and 1 means hold your horses
- some process is in its critical section.
Let us look for a software solution as a second effort. Consider having a single shared (lock)
variable that starts at 0. When a process wishes to reach its crucial region, it first attempts to
unlock the lock. If the lock is set to 0, the process enters the critical zone. If the lock is already a
1, the process simply waits for it to become a 0. As a result, a 0 indicates that no process is in its
critical region, whereas a 1 indicates that some process is in its critical region.
Sadly, this concept suffers the same fatal fault that we discovered in the spooler directory.
Assume that one process reads the lock and discovers that it is set to 0. Another process is
scheduled, executes, and sets the lock to 1 before it can set the lock to 1. When the first
You may think that we might avoid this problem by first reading out the lock value and then
checking it again right before saving it, however, that is not the case. The race now happens if
the second process updates the lock immediately after the first has completed its second check.
while (true)
Problem:
• If process-A sees the value of lock variable 0 and before it can set it to 1 context switch
occurs.
• Now process-B runs and finds the value of lock variable 0, so it sets the value to 1, and
enters the critical region.
• At some point of time process-A resumes, sets the value of the lock variable to 1, and
enters the critical region.
• Now two processes are in their critical regions accessing the same shared memory, which
violates the mutual exclusion condition.
KEY TAKEAWAY
➢ The concept of busy waiting, specifically the approach of disabling interrupts and using a
shared lock variable, is explored in interprocess communication for achieving mutual
exclusion.
➢ Disabling interrupts involves preventing the CPU from switching to other processes by
turning off interrupts while a process is in its critical section.
➢ While this ensures mutual exclusion, it poses risks and is not a suitable general solution
for user programs. On the other hand, the shared lock variable approach utilizes a single
variable that is initially set to 0.
➢ Processes attempt to set it to 1 before entering the critical section, but this method
suffers from a race condition where multiple processes may enter the critical section
simultaneously, violating mutual exclusion.
➢ These approaches demonstrate the challenges of busy waiting and the need for more
robust synchronization mechanisms in interprocess communication systems.
TEST-AND-SET INSTRUCTION
● In a single atomic operation, it returns the old value of a memory location and sets the
memory location value to 1.
● If one process is currently running a test-and-set, no other process may start a new test-
and-set until the first process's test-and-set is completed
It is implemented as-
●Lock value = 0 means the critical section is currently vacant and no process is present
inside it.
●Lock value = 1 means the critical section is currently occupied and a process is present
inside it.
SCENE-01:
●Process P0 arrives.
●It executes the test-and-set(Lock) instruction.
●Since the lock value is set to 0, so it returns the value 0 to the while loop and sets the
lock value to 1.
●The returned value 0 breaks the while loop condition.
●Process P0 enters the critical section and executes.
●Now, even if process P0 gets preempted in the middle, no other process can enter the
critical section.
●Any other process can enter only after process P0 completes and sets the lock value to 0.
SCENE-02:
SCENE-03:
●Process P0 comes out of the critical section and sets the lock value to 0.
CHARACTERISTICS-
EXPLANATIONS-
POINT-01:
EXPLANATION-
● The test-and-set instruction is crucial to the mechanism's success in establishing
mutual exclusion.
● The test-and-set instruction returns the previous value of a memory location
(lock) while also updating its value to 1.
● Because these two processes are conducted as a single atomic operation, they
are mutually exclusive.
● The failure of the lock variable synchronization technique was largely due to
preemption after reading the lock value.
● Preemption can no longer occur immediately after reading the lock value.
POINT-02:
EXPLANATION-
● When the process arrives, it runs the test-and-set instruction, which returns 0 to
the while loop and sets the lock value to 1.
● No other process can now enter the crucial portion until the process that started
the test-and-set completes the critical section.
● Additional processes can only enter when the process that started the test-and-
test has been completed and set the lock value to 0.
● This avoids a stalemate from occurring.
POINT-03:
POINT-04:
This synchronization mechanism suffers from spin lock where the execution of processes
is blocked.
EXPLANATION-
It is implemented as-
●Turn value = 0 means it is the turn of process P0 to enter the critical section.
●Turn value = 1 means it is the turn of process P1 to enter the critical section.
SCENE-01:
SCENE-02:
● Process P1 has arrived.
● It carries out the turn!=1 instruction.
● Because the turn value is set to 0, the while loop receives value 1.
● The while loop condition is not broken by the returned value 1.
● P1 is entrapped inside an unending while loop.
● The while loop keeps process P1 busy until the turn value is 1, at which point the
condition breaks.
SCENE-03:
● Process P0 exits the critical section and changes the turn value to 1.
● Process P1's while loop condition fails.
● Now, the process P1 that was waiting for the critical section enters and executes the
critical section.
● Process P0 can no longer enter the critical part if process P1 is preempted in the middle.
● Process P0 cannot enter until Process P1 is finished and the turn value is set to 0.
CHARACTERISTICS-
●Processes have to compulsorily enter the critical section alternately whether they
want it or not.
●This is because if one process does not enter the critical section, then other
processes will never get a chance to execute again.
KEY TAKEAWAY
➢ The Test and Set Lock (TSL) instruction, as a hardware approach to synchronization,
utilizes a test-and-set instruction to manage concurrent processes.
➢ This mechanism ensures mutual exclusion by employing a lock variable that is set to 0
when the critical section is vacant and 1 when occupied.
➢ While effective in guaranteeing mutual exclusion and preventing deadlocks, TSL lacks
bounded waiting, may result in hunger, and introduces a spin lock problem, keeping the
CPU busy during the waiting process.
➢ On the other hand, the Strict Alternation, a software-based approach, employs a turn
variable to synchronize two processes.
PETERSON’S SOLUTION
PETERSON’S SOLUTION
Peterson's solution is elegant, making it an interesting approach that provides these three
crucial features. This strategy, however, is rarely employed in modern systems. Peterson's
solution is based on the premise that instructions be executed in a specific order and that
memory accesses can be accomplished atomically. With current hardware, both of these
assumptions can fail. The instructions may be executed in a different order due to the
complications in the design of pipelined CPUs. Furthermore, if the threads are running on
different cores that do not ensure immediate cache coherency, the threads' memory values
may diverge.
Although hardware solutions exist to address these challenges, Peterson's solution suffers from
a lack of abstraction. It is preferable for a systems programmer to use abstractions such as
locking and unlocking a vital region rather than manipulating memory bits. As a result, the
remainder of this chapter will concentrate on higher-level abstractions and approaches that
enable more robust and adaptable solutions to these types of challenges, known as
synchronization primitives. In general, these primitives fail to provide fairness and, as a result,
cannot give the same theoretical features as Peterson's solution. Their higher level of
abstraction, on the other hand, provides better clarity in their practical use.
Peterson's solution is a traditional critical section problem solution. The critical section problem
assures that no two processes update or modify the value of a resource at the same time.
The critical section is the section in which the values are changed. Except for the important
areas, there are three sections: the entry section, the exit section, and the reminder section.
● The process that enters the crucial region must first travel via the entry region where they
request access to the critical section.
● The process that exits the vital part must travel via the exit region.
● The remainder portion contains the code that was left over after execution.
Now we will first see Peterson's solution algorithm and then see how any two processes P and
Q get mutual exclusion using Peterson's solution.
#define N 2
#define TRUE 1
#define FALSE 0
int interested[N]=False
int turn;
int other;
other=1-process
interested[process]= TRUE ;
turn = process;
interested[process]=FALSE;
EXPLANATION
There will be two processes, with the first process having a process number of 0 and the
second process having a process number of 1.
So, because the process is named the entry section, it signifies that the process wants to run a
critical section, thus it will set interest.
[process]=TRUE
It will set its turn after announcing that the process is interesting. Thus, if process 1 is invoked,
turn =1.
In this line, the process determines whether or not other processes are interested. If that
process is interested, then interested[other]==TRUE is true, and the process believes that
another process is executing the key part.
It will then enter a loop until another process is no longer interesting. Now, if another process
is interested, interested[other]==TRUE.
Mutual Exclusion: For certain, the approach gives reciprocal exclusion. Because the while
condition in the entry section includes the criterion for two variables, a process cannot enter
the crucial part until the other process is interested and the process is the last to update the
turn variable.
Process: A process that is uninterested will never prevent another interested process from
entering the critical section. If the other process is likewise fascinating, the process will be put
on hold.
Bounded waiting: Because it did not provide bounded waiting, the interested variable
mechanism failed. In Peterson's technique, however, a stalemate can never occur because the
process that initially sets the turn variable will always enter the critical area. As a result, if a
process is preempted after executing line 4 of the entry section, it will almost certainly enter
the critical section on its second chance.
Portability: Because this is a complete software solution, it can run on any hardware.
KEY TAKEAWAY
SEMAPHORES
semaphore is a hardware-based solution to the critical section problem. A Semaphore S is a
integer variable that, apart from initialization is accessed only through two standard atomic
operations: wait() and signal().
The wait () operation is termed as P and signal () was termed as V
Definition of wait () is
Wait (S) {
While S <= 0
; S--;
}
Definition of signal () is
Signal (S) {
S++;
}
All modifications to the integer value of the semaphore in the wait () and signal() operations
must be executed indivisibly, that is when one process modifies the semaphore value, no other
process can simultaneously modify that same semaphore value
Semaphore is basically a non-negative variable that is shared between threads. A semaphore is
a signaling mechanism, and another thread can signal a thread that is waiting on a semaphore.
For process synchronization, it employs two atomic operations: 1) Wait and 2) Signal.
Depending on how it is configured, a semaphore either enables or denies access to the
resource.
TYPES OF SEMAPHORES
This type of Semaphore uses a count that helps tasks to be acquired or released numerous
times. If the initial count = 0, the counting semaphore should be created in the unavailable
state.
However, If the count is > 0, the semaphore is created in the available state, and the number of
tokens it has equals to its count.
The binary semaphores are quite similar to counting semaphores, but their value is restricted to
0 and 1. In this type of semaphore, the wait operation works only if semaphore = 1, and the
signal operation succeeds when semaphore= 0. It is easy to implement than counting
semaphores.
EXAMPLE OF SEMAPHORE
Process i
begin
P(mutex);
execute CS;
V(mutex);
End;
Both of these operations are used to implement process synchronization. The goal of this
semaphore operation is to get mutual exclusion.
WAIT FOR OPERATION
You can use this form of semaphore action to control the entry of a job into the critical section.
If, on the other hand, the value of wait is positive, the value of the wait argument X is
decremented. If the value is negative or 0, no operation is performed. It is also known as the
P(S) operation.
The command is held up until the required conditions are met when the semaphore value is
reduced to a negative value.
Copy CodeP(S)
while (S<=0);
S--;
SIGNAL OPERATION
This type of Semaphore operation is used to control the exit of a task from a critical section. It
helps to increase the value of the argument by 1, which is denoted as V(S).
Copy CodeP(S)
S++;
Here, are some major differences between counting and binary semaphore:
Modification The wait and signal operations can It is modified only by the process
modify a semaphore. that may request or release a
resource.
Thread You can have multiple program You can have multiple program
threads. threads in mutex but not
simultaneously.
Ownership Value can be changed by any process Object lock is released only by the
of releasing or obtaining the process, which has obtained the
resource. lock on it.
ADVANTAGES OF SEMAPHORES
A DISADVANTAGE OF SEMAPHORES
MONITORS
Monitors are a programming language component that helps to control shared data access. The
Monitor is a set of shared data structures, operations, and synchronization between concurrent
procedure calls. As a result, a monitor is frequently referred to as a synchronization tool.
Among the languages that support monitors are Java, C#, Visual Basic, Ada, and concurrent
Euclid. Outside processes cannot access the monitor's internal variables, but they can invoke
the monitor's operations.
In the Java programming language, for example, synchronization techniques such as wait() and
notify() are provided.
SYNTAX OF A MONITOR IN OS
Monitor monitorName{
variables_declaration;
condition_variables;
...
initializing_code;
CHARACTERISTICS OF MONITORS IN OS
1. Initialization: The initialization code is supplied in the package, and it is only required
once when generating the monitors.
2. Private Data: A feature of an operating system's monitor that makes data private. It
contains all of the monitor's secret data, including private features that may be used
only within the monitor. As a result, confidential fields and functionalities are hidden
behind the monitor.
3. Process for Monitoring: Monitor procedures are procedures or functions that can be
invoked from outside the monitor.
4. Keep an eye on the entry queue: The Monitor Entry Queue is another critical
component of the monitor. It only contains the threads that are typically referred to be
procedures.
CONDITION VARIABLES
There are two sorts of operations we can perform on the monitor's condition variables:
1. Wait
2. Signal
y.signal(): If an activity/process applies the signal action on the condition variable, then one of
the blocked activity/processes in the monitor is given a chance to execute.
ADVANTAGES OF MONITOR IN OS
● Monitors make concurrent or parallel programming simpler and less error-prone than
semaphore-based methods.
● It aids in the synchronization of processes in the operating system.
● Mutual exclusion is integrated into monitors.
● Monitors are simpler to configure than semaphores.
● Monitors may be able to compensate for the timing errors caused by semaphores.
DISADVANTAGES OF MONITOR IN OS
KEY TAKEAWAY
➢ In the realm of interprocess communication, semaphores and monitors play crucial roles
in managing shared resources and ensuring synchronization among concurrent
processes.
➢ Semaphores, as hardware-based solutions to the critical section problem, utilize atomic
operations such as wait() and signal() to control access to resources.
➢ They can be categorized as counting or binary semaphores, each serving specific
synchronization needs. Despite their advantages, semaphores come with challenges,
including priority inversion and the risk of deadlocks.
➢ On the other hand, monitors offer a higher-level abstraction for synchronizing
concurrent processes. Monitors encapsulate shared data, operations, and
Because the buffer pool has a maximum size, this problem is often called the Bounded buffer
problem. This problem is generalized in terms of the Producer-Consumer problem, where a
finite buffer pool is used to exchange messages between producer and consumer processes.
The solution to this problem is, creating two counting semaphores "full" and "empty" to keep
track of the current number of full and empty buffers respectively. In this Producers mainly
produces a product and consumers consume the product, but both can use one of the
containers each time. The main complexity of this problem is that we must have to maintain
the count for both empty and full containers that are available.
In the producer-consumer problem, there is one Producer who produces some products and
one Consumer who consumes the items generated by the Producer. Both producers and
consumers share the same memory buffer, which has a fixed size.
The Producer's job is to make the item, store it in the memory buffer, and then start making
more items. The Consumer's task is to consume the item from the memory buffer.
Below are a few points that are considered as the problems that occur in Producer-Consumer:
● When the buffer is not full, the producer should produce data. If the buffer is found to
be full, the producer is not permitted to put any data in the memory buffer.
PRODUCER CODE
CONSUMER CODE
Semaphores can help solve Producer and Consumer problems caused by context switches and
delivering inconsistent results.
To overcome the above-mentioned race situation problem, we will employ Binary Semaphores
and Counting Semaphores.
Semaphore Binary: Just two processes can compete to enter its CRITICAL SECTION at any
moment in time in a Binary Semaphore, and the criterion of mutual exclusion is also preserved.
Counting Semaphore: In counting semaphores, many processes can compete to enter its
CRITICAL SECTION at any time, while the condition of mutual exclusion is also maintained.
1. wait( S )
while( S <= 0) ;
S--;
S++;
From the above definitions of wait, it is clear that if the value of S <= 0 then it will enter into an
infinite loop (because of the semicolon; after while loop). Whereas the job of the signal is to
increment the value of S.
Let's see the code as a solution to the producer and consumer problem using semaphore ( Both
Binary and Counting Semaphore):
1. do {
2. wait(empty); // wait until empty>0
3. wait(s); //acquire lock
1. do {
2. wait(full); // wait until full>0
3. wait(s); //acquire lock
Before Starting an explanation of code, first, understand the few terms used in the above code:
1. m(s): a binary semaphore which is used to acquire and release the lock
2. Empty: a counting semapho0re whose initial value is the number of slots in the buffer,
since initially, all slots are empty.
3. Full: a counting semaphore whose initial value is 0.
1. wait(empty) decreases the value of the counting semaphore variable empty by one;
that is, when the producer provides some element, the value of the space in the buffer
is automatically decreased by one. If the buffer is filled, that is, the value of the counting
semaphore variable "empty" is 0, then wait(empty); will trap the process (as defined by
wait) and prevent it from proceeding.
2. wait(S) sets the binary semaphore variable S to 0, preventing any other process from
entering its critical area.
3. Signal(s) sets the binary semaphore variable S to 1, allowing other processes wanting to
reach its critical area to do so.
1. wait(full) decreases the value of the counting semaphore variable full by one; that is,
when the consumer eats some element, the value of the full space in the buffer is
automatically decreased by one. If the buffer is empty, that is, the value of the counting
semaphore variable full is 0, then wait(full); will trap the process (as defined by wait)
and prevent it from proceeding.
Conclusion:
This approach also leads to the same race conditions you have seen in earlier approaches. Race
conditions can occur due to the fact that access to ‘count’ is unconstrained. The essence of the
problem is that a wake-up call, sent to a process that is not sleeping, is lost.
KEY TAKEAWAY
There is a common resource that numerous processes should be able to access. In this context,
there are two sorts of processes. They are readers as well as authors. Any number of readers
can read from the shared resource at the same time, but only one writer can write to it. No
other process can access the resource while a writer is writing data to it. If there are non-zero
readers accessing the resource at the time, a writer cannot write to it.
Allowed/Not
Case Process 1 Process 2
Allowed
Here priority means, no reader should wait if the share is currently opened for reading.
It is clear from the problem description that readers take precedence over writers. If a writer
wants to contribute to the resource, he or she must wait until no readers are currently using it.
We utilize one mutex m and one semaphore w in this case. The number of readers currently
accessing the resource is kept track of using the integer variable read count. The variable read
count is set to 0. m and w are originally assigned a value of 1.
Instead of requiring the process to acquire a lock on the shared resource, we use the mutex m
to require the process to acquire and release a lock whenever the read count variable is
updated.
while(TRUE)
wait(w);
signal(w);
And, the code for the reader process looks like this:
while(TRUE)
wait(m);
read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
● As seen in the code for the writer above, the writer just waits on the w semaphore until it
is given the opportunity to write to the resource.
● It increments w after performing the write operation so that the following writer can
access the resource.
● With the reader's code, on the other hand, the lock is obtained whenever the read count
is updated by a process.
● When a reader wishes to access a resource, it first increases the read count value, then
accesses the resource, and finally decrements the read count value.
● The first reader who enters the critical section and the last reader who exits the critical
portion utilizes the semaphore w.
● This is because when the first reader reaches the critical portion, the writer is stopped
from accessing the resource. The resource is currently only available to new readers.
● Similarly, after the final reader exits the critical region, it signals the writer with the w
semaphore since there are no more readers and the resource is now available to the
writer.
CASE 1: WRITING - WRITING → NOT ALLOWED. That is when two or more two processes are
willing to write, then it is not allowed. Let us see that our code is working accordingly or not?
Explanation :
Suppose two processes P0 and P1 wants to write, let P0 enter first the writer code, The
moment P0 enters
Now suppose P1 wants to write at the same time (will it be allowed?) let's see.
P1 does Wait( write ), since the write value is already 0, therefore from the definition of wait, it
will go into an infinite loop (i.e. Trap), hence P1 can never write anything, till P0 is writing.
This proofs that, if one process is writing, no other process is allowed to write.
CASE 2: READING - WRITING → NOT ALLOWED. That is when one or more than one process is
reading the file, then writing by another process is not allowed. Let us see whether our code is
working accordingly or not.
Explanation:
Suppose two processes P0 and P1 are in a system, P0 wants to read while P1 wants to write, P0
enters first into the reader code, the moment P0 enters
signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are
allowed to enter.
Suppose now any writer wants to enter into its code then:
As the first reader has executed wait (write); because of which write value is 0, therefore
wait(writer); of the writer, code will go into an infinite loop and no writer will be allowed.
This proves that, if one process is reading, no other process is allowed to write.
Now suppose P0 wants to stop the reading and wanted to exit then
Now if again any writer wants to write, it can do it now, since write > 0
Explanation:
Suppose two processes P0 and P1 are in a system, P0 wants to write while P1 wants to read, P0
enter first into the writer code, The moment P0 enters
Now suppose P1 wants to read the same time (will it be allowed?) let's see.
This proves that, if one process is writing, no other process is allowed to read.
The moment writer stops writing and is willing to exit then it will execute:
CASE 4: READING - READING → ALLOWED. That is when one process is reading the file, and
another process or process is willing to read, then they all are allowed i.e. reading - reading is
not mutually exclusive. Let us see if our code is working accordingly or not.
Explanation:
Suppose three processes P0, P1 and P2 are in a system, all the three processes P0, P1, and P2
want to read, let P0 enter first into the reader code, the moment P0 enters
allowed.
signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are
allowed to enter.
signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are
allowed to enter.
Suppose now any writer wants to enter into its code then:
signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are
allowed to exit
signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are
allowed to exit
→Now suppose P2 (last process) wants to come out of system (stop reading) then
to write now.
The above explanation proves that if one or more than one processes are willing to read
simultaneously.
KEY TAKEAWAY
The dining philosopher's issue is a classic synchronization problem in which five philosophers sit
around a circular table and alternate between thinking and eating. A bowl of noodles, as well as
five chopsticks for each of the philosophers, is placed in the center of the table. A philosopher
requires both a right and a left chopstick to dine. A philosopher can only eat if both his or her
immediate left and right chopsticks are available. If the philosopher's immediate left and right
chopsticks are not available, the philosopher sets down their (either left or right) chopstick and
resumes pondering. When a resource is shared by multiple processes at the same time, data
inconsistency might occur.
The atomic operations of a semaphore are wait() and signal (). If the value of its input S is
positive, the wait() action decrements; otherwise, it is used to acquire resource while entry. If S
is negative or zero, no operation is performed. When the critical section is executed at exit, the
value of the signal() operation's argument S is incremented.
void Philosopher
while(1)
wait(use_resource[x]);
wait(use_resource[(x + 1) % 5]);
signal(free_resource[x]);
Explanation:
● When the philosopher is using the resources while the others are considering, the wait()
operation is used. The threads use resource[x] and use resource[(x + 1)% 5] are
executed here.
● Following the use of the resource, the signal() procedure represents the philosopher
using no resources and thinking. Threads free resource[x] and free resource[(x + 1)% 5]
are running here.
Using the prior solution to the dining philosopher problem, we demonstrated that no two
neighbouring philosophers can eat at the same time. The above strategy has the disadvantage
of potentially resulting in a deadlock situation. This happens when all of the philosophers
choose their left chopstick at the same time, resulting in a stalemate situation in which none of
the philosophers can eat and so deadlock.
We can also avoid deadlock through the following methods in this scenario -
1. The maximum number of philosophers at the table should not be more than four. Let's
look at why these four processes are important:
○ Philosopher P3 will be able to use chopstick C4, thus P3 will begin eating and
then set down both chopsticks C3 and C4, indicating that semaphore C3 and C4
will now be reduced to one.
4. A philosopher should be allowed to choose his or her chopsticks only if both chopsticks
(left and right) are available at the same moment.
KEY TAKEAWAY
DEADLOCKS
Deadlock occurs when you have a set of processes [not necessarily all the processes in the
system], each holding some resources, each requesting some resources, and none of them is
able to obtain what it needs, i.e. to make progress. Those processes are deadlocked because all
the processes are waiting. None of them will ever cause any of the events that could wake up
any of the other members of the set, and all the processes continue to wait forever. For this
model, I assume that processes have only a single thread and that there are no interrupts
possible to wake up a blocked process. The no-interrupts condition is needed to prevent an
otherwise deadlocked process from being awakened by, say, an alarm, and then causing events
that release other processes in the set. In most cases, the event that each process is waiting for
is the release of some resource currently possessed by another member of the set. In other
words, each member of the set of deadlocked processes is waiting for a resource that is owned
by another deadlocked process. None of the processes can run, none of them can release any
resources, and none of them can be awakened. The number of processes and the number and
kind of resources possessed and requested are unimportant. This result holds for any kind of
resource, including both hardware and software.
A deadlock occurs when a collection of processes is stalled because each process is holding a
resource and waiting for another resource to be acquired by another process.
Consider this: if two trains are approaching each other on the same track and there is only one
track, neither train can move once they are in front of the other. In operating systems, a similar
situation happens when two or more processes hold some resources and wait on resources
held by other processes (s). For example, in the picture below, Process 1 is holding Resource 1
and waiting for Process 2 to acquire Resource 2, and Process 2 is waiting for Resource 1.
Deadlock situation can arise if the following four conditions hold simultaneously in a system: 1.
Resources are used in mutual exclusion. 2. Resources are acquired piecemeal (i.e. not all the
resources that are needed to complete an activity are obtained at the same time in a single
indivisible action). 3. Resources are not preempted (i.e. a process does not take away resources
being held by another process). 4. Resources are not spontaneously given up by a process until
it has satisfied all its outstanding requests for resources (i.e. a process, being that it cannot
obtain some needed resource it does not kindly give up the resources that it is currently
holding).
EXAMPLE OF DEADLOCK
2. Hold and Wait: A process waits for some resources while holding another resource at
the same time.
3. No preemption: The process which once scheduled will be executed till the completion.
No other process can be scheduled by the scheduler meanwhile.
4. Circular Wait: One process is waiting for a resource that is being held by the second
process, which is also waiting for a resource that is being held by the third process, and
so on. This will continue until the last process needs a resource that the first process
has. This results in a circular chain.
Process A, for example, is assigned Resource B since it requests Resource A. Similarly, Process B
is assigned Resource A and requests Resource B. This results in a circular wait loop.
A computer, for example, contains three USB drives and three processes. One of the USB discs
can be held by each of the three processes. As a result, when each process demands another
disc, the three processes will be stuck in a deadlock since each is waiting for the USB drive that
is presently in use to be released. This will produce a circular
chain.
ADVANTAGES OF DEADLOCK
DISADVANTAGES OF DEADLOCK
The resource allocation graph is a graphical representation of a system's status. The resource
allocation graph, as the name implies, contains all of the information about all of the activities
that are holding or waiting for resources.
It also includes information on all instances of all resources, regardless of whether they are
available or being used by the processes.
The process is represented by a circle in the Resource allocation graph, whereas the resource is
represented by a rectangle. Let's take a closer look at the various types of vertices and edges.
A resource can have more than one instance. Each instance will be represented by a dot inside
the rectangle.
If the tail of the arrow is attached to an instance of the resource and the head is attached to a
process, the resource is shown as assigned to that process.
If the tail of an arrow is attached to the process while the head is pointing toward the resource,
the process is shown to be waiting for a resource.
EXAMPLE
Consider three processes P1, P2, and P3, as well as two types of resources R1 and R2. Each
resource has a single instance.
According to the graph, P1 is using R1, P2 is holding R2 while waiting for R1, and P3 is waiting
for both R1 and R2.
➢ Deadlocks in operating systems occur when a set of processes, each holding certain
resources and requesting others, cannot make progress. This situation arises from four
necessary conditions: mutual exclusion, hold and wait, no preemption, and circular wait.
➢ A deadlock example is likened to two trains approaching each other on a single track,
unable to move forward. These conditions include mutual exclusion, hold and wait, no
preemption, and circular wait.
➢ To prevent deadlocks, operating systems employ strategies such as violating mutual
exclusion, addressing hold and wait situations, and implementing preemption. However,
the feasibility of these techniques varies due to resource characteristics and potential
inconsistencies.
➢ Deadlock prevention aims to design systems that minimize the risk of deadlocks,
ensuring safety and efficient resource allocation.
➢ In terms of disadvantages, deadlock methods may delay process starts, require
processes to anticipate future resource needs, and lead to more frequent preemption.
➢ The resource allocation graph is a useful tool for representing the status of resources
and processes, aiding in the analysis and prevention of deadlocks.
For Example, Several processes can perform read operations on a file at the same time, but not
write operations. Because write operations need sequential access, some processes must wait
while another process performs a write action. Mutual exclusion cannot be eliminated since
some resources are intrinsically non-shareable.
A hold-and-wait condition occurs when a process holds one resource while simultaneously
waiting for another process to hold another resource. The process cannot proceed until all of
the necessary resources are obtained. As shown in the diagram below:
1. By eliminating wait: The process specifies the resources it requires in advance so that it
does not have to wait for allocation after execution starts.
For Example, Process1 declares in advance that it requires both Resource1 and
Resource2
2. By eliminating hold: The process has to release all resources it is currently holding
before making a new request.
For Example, Process1 has to release Resource2 and Resource3 before making a request
for Resource1
Challenges:
● Because a process executes instructions one by one, it cannot be aware of all necessary
resources prior to execution.
● Releasing all of the resources that a process is now holding is also troublesome because
they may be inaccessible to other processes and are thus released unnecessarily.
● For example, if Process1 releases both Resource2 and Resource3, Resource3 is released
unnecessarily because Process2 does not require it.
NO PREEMPTION
Preemption is temporarily interrupting an executing task and later resuming it. For example, if
process P1 is using a resource and a high-priority process P2 requests for the resource, process
P1 is stopped and the resources are allocated to P2.
1. If a process is holding some resources while waiting for others, it should release all
previously held resources and make a fresh request for the necessary resources. When
all of the necessary resources are available, the procedure can be restarted.
As an example: If a process has resources R1, R2, and R3 and is waiting for resource R4,
it must relinquish resources R1, R2, and R3 and make a new request for all resources.
2. If a process P1 is waiting for some resource, and there is another process P2 that is
holding that resource and is blocked waiting for some other resource. Then the resource
is taken from P2 and allocated to P1. This way process P2 is preempted and it requests
again for its required resources to resume the task. The above approaches are possible
for resources whose states are easily restored and saved, such as memory and registers.
Challenges:
● These approaches are troublesome since the process may be actively utilizing these
resources, and stopping it via preempting can result in inconsistencies. For example, if a
process starts writing to a file and its access is denied before it completes the update,
the file will remain unusable and inconsistent.
● It is inefficient and time-consuming to submit requests for all resources again.
CIRCULAR WAIT
In a circular wait, two or more processes wait for resources in a circular order. We can
understand this better by the diagram given below:
In the example above, process P3 is requesting resource R1, which has a number lower than
resource R3 which is already allocated to process P3. So this request is invalid and cannot be
made, as R1 is already allocated to process P1.
Challenges:
● Mutual exclusion cannot be totally eradicated since some resources are intrinsically
unshareable.
● The only feasible approach to avoid stalemate is to eliminate the cyclic wait.
KEY TAKEAWAY
DEADLOCK AVOIDANCE
Deadlock Avoidance, assuming that you are in a safe state (i.e. a state from which there is a
sequence of allocations and releases of resources that allows all processes to terminate) and
you are requested certain resources, simulates the allocation of those resources and
determines if the resultant state is safe. If it is safe the request is satisfied, otherwise, it is
delayed until it becomes safe. The Banker’s Algorithm is used to determine if a request can be
satisfied. It uses requires knowledge of who are the competing transactions and what are their
resource needs. Deadlock avoidance is essentially not used in distributed systems.
The Operating System employs Deadlock Avoidance to avoid deadlock. The operating system
avoids deadlock by originally knowing the maximal resource requirements of the processes, as
well as the free resources available at the time. The operating system attempts to allocate
resources in accordance with the process requirements and determines if the allocation can
lead to a safe or unsafe condition. If the allocation of resources results in an unsafe state, the
operating system does not continue with the allocation process.
Let's understand the working of Deadlock Avoidance with the help of a reflexive example.
P1 9 5 4
P2 5 2 3
P3 3 1 2
Let's consider three processes P1, P2, P3. Some more information on which the processes tell
the Operating System are:
● To finish its execution, the P1 process requires a maximum of 9 resources (which can be
any software or hardware resource such as a tape drive or printer). P1 presently has 5
Resources and needs 4 more to complete its execution.
● The P2 process requires a maximum of 5 resources and is presently allocated two. As a
result, it requires three extra resources to finish its execution.
● The P3 process requires a maximum of three resources and is presently given one. As a
result, it requires two additional resources to finish its execution.
● The operating system is aware that just two of the total available resources are now free.
Because just two resources are currently available, only P3 can meet its requirement for two
resources. P3 can release its three (1+2) resources if it takes two resources and completes its
execution. Now, the three free resources given by P3 can meet P2's needs. P2 can now finish its
execution and release 5 (2+3) resources after taking the three free resources. Five resources are
now available for free. P1 can now use four of the five available resources to complete its
execution. Hence, with only two free resources available at the start, all processes were able to
finish their execution, resulting in the Safe State. The processes were carried out in the
following order.
What if initially there was only 1 free resource available? None of the processes would be able
to complete its execution. Thus leading to an unsafe state.
We use two words, safe and unsafe states. What are those states? Let's understand these
concepts.
Safe State - In the preceding example, we observed that the Operating System was able to meet
the resource requirements of all three processes, P1, P2, and P3. As a result, all of the processes
were able to complete their execution in the order P3->P2->P1. As a result, if the operating
system can allocate or satisfy the maximum resource requirements of all processes in any
sequence, the system is considered to be in a safe state. As a result, the safe state does not
result in a deadlock.
Unsafe State - If the operating system is unable to prohibit processes from seeking resources,
resulting in Deadlock, the system is said to be in an Unsafe Condition. An unsafe State does not
always result in deadlock; it may or may not result in deadlock.
Let's take an example that has multiple resources requirement for every Process. Let there be
three Processes P1, P2, P3, and 4 resources R1, R2, R3, R4. The maximum resource
requirements of the Processes are shown in the below table.
Process R1 R2 R3 R4
P1 3 2 3 2
P2 2 3 1 4
P3 3 1 5 0
Process R1 R2 R3 R4
P1 1 2 3 1
P2 2 1 0 2
P3 2 0 1 0
R1 R2 R3 R4
7 4 5 4
We can find out the no of available resources for each of P1, P2, P3 by subtracting the currently
allocated resources from the total resources.
R1 R2 R3 R4
2 1 1 1
Process R1 R2 R3 R4
P1 2 1 0 1
P2 0 2 1 2
P3 1 1 4 0
The available free resources are 2,1,1,1> of R1, R2, R3, and R4 resources, which can initially be
used to satisfy just the requirements of process P1 because process P2 requires two R2
resources, which are not accessible. The same is true for Process P3, which requires four R3
resources that are not originally available.
1. Initially, Process P1 will use the available resources to meet its resource requirements,
then complete its execution and release all of its allotted resources. Process P1 is
originally assigned <1,2,3,1> resources of R1, R2, R3, and R4. Process P1 requires
<2,1,0,1> resources from R1, R2, R3, and R4 to finish its execution. Therefore, process
P1 takes the available free resources 2,1,1,1> of R1, R2, R3, R4, and completes its
execution, releasing its existing allocated resources as well as the free resources it
utilized to complete its execution. Hence, P1 releases <1+2,2+1,3+1,1+1> = <3,3,4,2>
resources from R1, R2, R3, and R4.
Hence, in the preceding example, the process execution sequence was <P1, P2, P3>. However,
it could have been <P1, P3, P2> if process P3 had been executed before process P2, which was
conceivable because there were enough resources available to meet the needs of both Process
P2 and P3 after step 1 above.
BANKER'S ALGORITHM
This is modeled on the way a small town banker might deal with customers’ lines of credit. In
the course of conducting business, our banker would naturally observe that customers rarely
draw their credit lines to their limits. This, of course, suggests the idea of extending more credit
than the amount the banker actually has in her coffers
Banker's method does the same thing as we have shown with an example of deadlock
avoidance. The algorithm predicts whether or not the System will be in a safe condition by
simulating the allocation of resources to processes based on the maximum available resources.
When there are a large number of processes and resources, the Banker's Algorithm comes in
handy.
P1 212 433
P2 301 913
P3 020 864
P4 112 223
Considering the above processing table, we need to calculate the following two things:
Q.1 Calculate the need matrix? Q.2 Is the system in a safe state?
Process Need
ABC
P0 432
P1 221
P2 612
P3 844
P4 111
1. For process P0, Need = (4, 3, 2) and Available = (3, 2, 1) Clearly, the resources needed are
more in number than the available ones. So, now the system will move to process the next
request.
2. For Process P1, Need = (2, 2, 1) and Available = (3, 2, 1) Clearly, the resources needed are less
than equal to the available resources within the system. Hence, the request of P1 is granted.
4. For Process P3, Need = (8, 4, 4) and Available = (5, 3, 3) Clearly, the resources needed are
more in number than the available ones. So, now the system will move to process the next
request.
5. For Process P4, Need = (1, 1, 1) and Available = (5, 3, 3) Clearly, the resources needed are less
than equal to the available resources within the system. Hence, the request for P4 is granted.
6. Now again check for Process P2, Need = (6, 1, 2) and Available = (6, 4, 5) Clearly, resources
needed are less than equal to the available resources within the system. Hence, the request of
P2 is granted.
7. Now again check for Process P3, Need = (8, 4, 4) and Available = (9, 4, 6) Clearly, resources
needed are less than equal to the available resources within the system. Hence, the request of
P3 is granted.
Available=Available+Allocation =(9,4,6)+(0,2,0)=(9,6,6)(NewAvailable)
8. Now again check for Process P0, Need = (4, 3, 2), and Available (9, 6, 6) Clearly, the request
for P0 is also granted.
The single constraint in Banker's algorithm is that Available must always satisfy at least one of
the process resource needs so that the system does not become unsafe and the program must
roll back to the original allocated state.
● The [MAX] array attribute in the Bankers algorithm in OS indicates the maximum
number of resources of each category that a process can store. We can always identify
the need for resources for a certain process by using the [MAX] array.
● [Need]=[MAX]−[Allocated]
● This algorithm aids in the detection and avoidance of deadlock, as well as the
management and control of process requests for each type of resource inside the
system.
● Each process should inform the operating system about impending resource demands,
the number of resources available, any delays, and how long the resources will be held
by the process before being released. This is also one of the key features of the Bankers
algorithm.
● Various types of resources are maintained by the system while using this algorithm,
which can fulfill the needs of at least one process type.
● This algorithm also consists of two other advanced algorithms for maximum resource
allocation.
● The Bankers algorithm in the operating system does not let a process can adjust its
maximum resource requirement while processing.
● Another downside of this approach is that all processes within the system must be
aware of the system's maximal resource requirements in advance.
● This technique permits all process resource requests to be granted in a specified limited
amount of time, although the maximum time period for assigning resources is one year.
KEY TAKEAWAY
DEADLOCK DETECTION
Often neither deadlock avoidance nor deadlock prevention may be used. Instead, deadlock
detection and recovery are used by employing an algorithm that tracks resource allocation and
process states, and rolls back and restarts one or more of the processes in order to remove the
deadlock. Detecting a deadlock that has already occurred is easily possible since the resources
that each process has locked and/or currently requested are known to the resource scheduler
or OS. Detecting the possibility of a deadlock before it occurs is much more difficult and is, in
fact, generally undecidable, because the halting problem can be rephrased as a deadlock
scenario. However, in specific environments, using specific means of locking resources,
deadlock detection may be decidable. In the general case, it is not possible to distinguish
between algorithms that are merely waiting for a very unlikely set of circumstances to occur
and algorithms that will never finish because of deadlock.
Deadlocks must be detected and resolved by the operating system if they occur. Deadlock
detection techniques like the Wait-For Graph are used to detect deadlocks, and recovery
algorithms like the Rollback and Abort algorithm are used to resolve them. The recovery
algorithm frees resources held by one or more processes, allowing the system to keep working.
When there is a single instance of each resource the system can use wait-for-graph for
deadlock detection. The key points about the wait-for-graph are
WORKING OF WAIT-FOR-GRAPH
In the example above, P2 is seeking R3, which is owned by P5. As a result, we eliminate R3 and
collapse the edge between P2 and P5. This is reflected in the wait-for-graph. Similarly, the
edges between P4 and P1 have collapsed, as have all other processes.
Detection of the cycle is necessary but not sufficient condition for deadlock detection, in this
case, the system may or may not be in deadlock varies according to different situations.
DEADLOCK RECOVERY
ADVANTAGES OR DISADVANTAGES:
Generally, the approach used to identify and recover from deadlocks is determined by the
system's specific requirements, the trade-offs between performance, complexity, and accuracy,
and the system's risk tolerance. To efficiently detect and resolve deadlocks, the operating
system must balance these aspects.
The practice of shifting processes from primary memory to secondary memory and vice versa is
known as memory management. It also keeps track of available memory, memory allocation,
and free memory.
Logical Address space: A "Logical Address" is an address generated by the CPU. It is often
referred to as a virtual address. The size of the process can be described as the logical address
space. It is possible to alter a logical address.
Physical Address space: A "Physical Address" is an address viewed by the memory unit (i.e. one
loaded into the memory's memory address register). A Real address is another term for a
physical address. Physical address space refers to the collection of all physical addresses that
correspond to these logical addresses. MMU generates a physical address. A hardware device,
the Memory Management Unit, performs the run-time mapping from virtual to physical
addresses (MMU). The physical address is always the same.
Loading a process into the main memory is done by a loader. There are two different types of
loading :
Static loading:- loading the entire program into a fixed address. It requires more memory
space.
Dynamic loading:- To execute a process, the complete program and its data must be in physical
memory. As a result, the size of a process is restricted by the amount of physical memory
available. Dynamic loading is used to maximize memory utilization. A routine is not loaded in
dynamic loading until it is called. All of the routines are stored on a disc in a relocatable load
format. One benefit of dynamic loading is that unused routines are never loaded. When a
significant amount of code is required to handle it efficiently, this loading is useful.
Static linking: The linker in static linking integrates all required program modules into a single
executable program. As a result, there is no runtime reliance. Certain operating systems only
enable static linking, which treats system language libraries like any other object module.
Dynamic linking: The fundamental principle of dynamic linking is similar to that of dynamic
loading. "Stub" is included in dynamic linking for each eligible library procedure reference. A
stub is a short section of code. When the stub is executed, it checks to see if the required
routine is already in memory. If not available then the program loads the routine into memory.
SWAPPING:
Any operating system has a fixed amount of physical memory available. Usually, applications
need more than the physical memory installed on your system, for that purpose the operating
system uses a swap mechanism: instead of storing data in physical memory, it uses a disk file.
Swapping is the act of moving processes between memory and a backing store. This is done to
free up available memory. Swapping is necessary when there are more processes than available
memory. At the coarsest level, swapping is done a process at a time. To move a program from
fast-access memory to slow-access memory is known as “swap out”, and the reverse operation
is known as “swap in”. The term often refers specifically to the use of a hard disk (or a swap file)
as virtual memory or “swap space”. When a program is to be executed, possibly as determined
by a scheduler, it is swapped into core for processing; when it can no longer continue executing
for some reason, or the scheduler decides its time slice has expired, it is swapped out again.
KEY TAKEAWAY
➢ Memory management in an operating system is a crucial aspect responsible for
controlling and optimizing the usage of random access memory (RAM).
The memory management techniques can be classified into the following main categories:
The real challenge of efficiently managing memory is seen in the case of a system which has
multiple processes running at the same time. Since primary memory can be space-multiplexed,
The simplest memory management strategy employed in the first generation of computer
systems were single contiguous memory management. The main memory is separated into two
contiguous sections or partitions in this approach. The operating systems are permanently
installed in one partition, usually in the lower RAM, and the user processes are put into the
other.
● Simple to implement.
● Easy to manage and design.
● In a Single contiguous memory management scheme, once a process is loaded, it is
given full processor time, and no other processor will interrupt it.
● Memory waste owing to unused memory since the process is unlikely to utilise all of the
allocated memory space.
● The CPU remains idle while the disc loads the binary image into main memory.
● It cannot be performed if the software occupies more than the allotted main memory
space.
MULTIPLE PARTITIONING:
● Fixed Partitioning
● Dynamic Partitioning
FIXED PARTITIONING
In a fixed partition memory management technique or static partitioning, the main memory is
divided into many fixed-sized divisions. These dividers might be of the same or varying sizes.
Each partition can only hold one process. The number of partitions defines the degree of
multiprogramming or the maximum number of processes that can be stored in memory. These
partitions are created during system formation and stay fixed afterward.
● Simple to implement.
● Easy to manage and design.
1. Internal Fragmentation
As illustrated in the screenshot below, the 4 MB partition is only used to load three MB
processes, with the remaining one MB being wasted.
2. External Fragmentation
Even when there is space available but not in a contiguous manner, the entire unused space of
multiple partitions cannot be used to load the processes.
The remaining 1 MB space of each partition, as illustrated in the graphic below, cannot be used
as a unit to contain a 4 MB process. Despite the fact that there is enough space to load the
process, it will not be loaded.
If the process size is more than the maximum partition size, the process cannot be loaded into
memory. As a result, the process size can be restricted to no greater than the size of the largest
partition.
The greatest number of processes that can be loaded into memory at the same time is referred
to as the degree of multiprogramming. Because the size of the partition cannot be modified
according to the size of the processes, the degree of multiprogramming in fixed partitioning is
fixed and very low.
DYNAMIC PARTITIONING
Dynamic partitioning was created to address the shortcomings of a fixed partitioning system.
With a dynamic partitioning strategy, each process only takes up as much RAM as it needs
when it is loaded for processing. Requested processes are given memory until the physical
1. NO INTERNAL FRAGMENTATION
Due to a lack of adequate contiguous memory in Fixed partitioning, processes with sizes bigger
than the largest partition could not be performed. The process size cannot be limited in
dynamic partitioning since the partition size is determined by the process size.
Because there would be no unused space in the partition due to the lack of internal
fragmentation, more processes can be loaded into memory at the same time.
1. EXTERNAL FRAGMENTATION
The absence of internal fragmentation does not rule out external fragmentation.
P1 and P3 were completed after some time, and their assigned space was freed. There are now
two unused partitions (1 MB and 1 MB) accessible in main memory, however they cannot be
used to load a 2 MB process because they are not contiguous.
The rule states that the process must be present in main memory at all times in order to be
performed. To avoid external fragmentation, we must amend this regulation.
The list of partitions is created once and never changes in fixed partitioning, but in dynamic
partitioning, the allocation and deallocation is quite complex since the partition size varies
every time it is assigned to a new process. The operating system must keep track of all
partitions.
PARTITION ALLOCATION
Memory is partitioned or divided into blocks. Each process is assigned based on the
requirements. Partitioning is an excellent strategy for avoiding internal fragmentation.
Below are the various partition allocation schemes:
● First Fit: With this sort of fit, the partition, which is the first sufficient block from the
beginning of the main memory, is allocated.
● Best Fit: It assigns the process to the partition with the least size among the free
partitions.
● Worst Fit: It assigns the process to the partition that has the biggest sufficient free
space in the main memory.
● Next Fit: This Fit is similar to the first Fit in many ways, except it looks for the first
sufficient partition from the last allocation point.
KEY TAKEAWAY
PARTITION ALLOCATION
Memory is partitioned or divided into blocks. Each process is assigned based on the
requirements. Partitioning is an excellent strategy for avoiding internal fragmentation.
Below are the various partition allocation schemes:
● First Fit: which is the first sufficient block from the beginning of the main memory, is
allocated in this sort of fit.
● Next Fit: This Fit is similar to the first Fit in many ways, except it looks for the first
sufficient partition from the last allocation point.
● Best Fit: It assigns the process to the partition with the least size among the free
partitions.
● Worst Fit: It assigns the process to the partition that has the biggest sufficient free space
in the main memory.
Whenever a new process P2 comes, it does the same thing. Search from the first index again.
ADVANTAGES
● Easy to implement
● OS can allocate processes quickly as an algorithm to allocate processes will be quick as
compared to other methods (Best Fit, Worst Fit, Next Fit, etc.)
DISADVANTAGES
Next fit is a variant of First Fit in which memory is searched for empty places in a manner
similar to the first fit memory allocation scheme. The only difference between the next fit
and the first fit memory allocation is that if the search is paused in between, the fresh
search starts from the last position.
The next fit can alternatively be described as a modified version of the first fit because it
begins searching for free memory in accordance with the first-fit memory allocation
strategy. This memory allocation approach employs a moving pointer to seek memory for
the next fit by traveling along the empty memory slots. The next fit memory allocation
approach always avoids memory allocation from the start of the memory space. For this
reason, the operating system employs a memory allocation method, commonly known as
the scheduling algorithm.
Example –
If you see the image below/right you will see that the process size is 40.
While blocks 1, 2 and 4 can accommodate the process. Block 2 is chosen as it leaves the lowest
memory wastage
It is allocated to the process. This scheme is considered the best approach as it results in the
most optimized memory allocation.
DISADVANTAGE:
The algorithms searches sequentially starting from the first memory block and searches for the
memory block that fulfills the following condition –
➢ Memory partition allocation plays a crucial role in optimizing system performance, and
various schemes are employed to efficiently assign memory blocks to processes.
➢ First Fit allocates the first available memory block that can accommodate a process, but
it leads to significant internal fragmentation.
➢ Next Fit, a variant of First Fit, resumes its search from the last position, aiming to reduce
allocation overhead.
➢ Best Fit allocates memory blocks that not only fit the process but also minimize memory
wastage, resulting in optimized memory allocation and reduced internal fragmentation.
On the other hand,
➢ Worst Fit selects a memory block that accommodates the process while leaving the
largest wasted space.
➢ Although Best Fit minimizes fragmentation, it can be time-consuming, while Worst Fit
emphasizes efficient space utilization over allocation speed.
➢ Each allocation scheme has its advantages and disadvantages, influencing the overall
performance of memory management in an operating system.
A process can acquire many memory blocks at different locations in memory using this sort of
memory allocation. Unlike contiguous memory allocation, which allocates all available free
space in one area, available free space is scattered throughout the system.
Non-contiguous memory allocation is slower than contiguous memory allocation. It consists of
segmentation and paging. This allocation results in no memory loss. The processes that follow
the switch might take place in any location.
For example, a process with three segments, say P1, P2, and P3, will be allocated in RAM in
contiguous memory blocks such as block 1 (-> P1), block 3 (-> P2), and block 5 (-> P3).
Example:
One of the simplest paging strategies is to build the page table as a collection of registers.
Because register capacity is limited, the page table is kept in main memory, despite the fact
that the size of the page table is often enormous.
This strategy results in no external fragmentation because every free frame is allocated to a
process that demands it. Internal fragmentation, on the other hand, endures.
● If a procedure requires 'n' pages, 'n' frames must be employed.
● The first frame on the free-frame list is loaded with the process's initial page, and
the frame number is then inserted into the page table.
The operating system preserves a copy of the instruction counter and registers contents for
each process in the same way that it keeps a copy of the page table for each process. This copy
Advantages of paging:
SEGMENTATION
It is very common for the size of program modules to change dynamically. For instance, the
programmer may have no knowledge of the size of a growing data structure. If a single address
space is used, as in the paging form of virtual memory, once the memory is allocated for
modules they cannot vary in size. This restriction results in either wastage or shortage of
memory. To avoid the above problem, some computer systems are provided with many
independent address spaces. Each of these address spaces is called a segment. The address of
each segment begins with 0 and segments may be compiled separately. In addition, segments
may be protected individually or shared between processes. However, segmentation is not
transparent to the programmer like paging. The programmer is involved in establishing and
maintaining the segments. Segmentation is one of the most common ways to achieve memory
protection like paging. An instruction operand that refers to a memory location includes a value
that identifies a segment and an offset within that segment. A segment has a set of
permissions, and a length, associated with it. If the currently running process is allowed by the
permissions to make the type of reference to memory that it is attempting to make, and the
offset within the segment is within the range specified by the length of the segment, the
reference is permitted; otherwise, a hardware exception is delivered. In addition to the set of
Example:
The segmentation example below shows five parts numbered 0 to 4. These sections, as shown,
will be kept in physical memory. Each segment has its own entry in the segment table, which
contains both the segment's initial entry address in physical memory (referred to as the base)
and its length (denoted as limit).
It takes you to the relevant Segment Number based on the number of segments.
It takes you to the specific page inside the segment based on the number of pages.
Page Offset: This is a value that is used to offset the page's position within the frame.
Each Page table contains a variety of information about each segment page. The Segment Table
contains information for each segment. Each page table entry corresponds to one of the pages
contained inside a segment, and each segment table entry refers to a page table item.
KEY TAKEAWAY
➢ Memory management in operating systems involves various strategies for efficient
allocation and use of system memory.
➢ In non-contiguous memory management schemes, processes can acquire memory
blocks at different locations, unlike contiguous allocation where all available space is in
one area.
➢ Two main non-contiguous memory management techniques are segmentation and
paging. Segmentation involves dividing memory into varying-sized segments, each
associated with a process, offering flexibility but requiring manual management.
➢ Paging, on the other hand, divides processes into fixed-size pages, simplifying allocation
but potentially leading to internal fragmentation.
➢ Paging allows the efficient use of main memory by swapping infrequently-used parts of
a program between RAM and secondary storage.
VIRTUAL MEMORY
VIRTUAL MEMORY
Many of us use computers on a daily basis. Although you use them for many different purposes
in many different ways, you share one common reason for using them; to make our job more
efficient and easier. However, there are times when computers cannot run as fast as you want
them to or just cannot handle certain processes effectively, due to the shortage of system
resources. When the limitations of system resources become a major barrier to achieving your
maximum productivity, you often consider the apparent ways of upgrading the system, such as
switching to a faster CPU, adding more physical memory (RAM), installing utility programs, and
so on. As a computer user, you want to make the most of the resources available; the process
of preparing plans to coordinate the total system to operate in the most efficient manner. This
is called system optimization. When it comes to system optimization, there is one great
invention of modern computing called virtual memory. It is an imaginary memory area
supported by some operating system (for example, Windows but not DOS) in conjunction with
the hardware. You can think of virtual memory as an alternate set of memory addresses.
Programs use these virtual addresses rather than real addresses to store instructions and data.
When the program is actually executed, the virtual addresses are converted into real memory
addresses. The purpose of virtual memory is to enlarge the address space, the set of addresses
a program can utilize. To facilitate copying virtual memory into real memory, the operating
system divides virtual memory into pages, each of which contains a fixed number of addresses.
Each page is stored on a disk until it is needed. When the page is needed, the operating system
copies it from the disk to the main memory, translating the virtual addresses into real
addresses. The process of translating virtual addresses into real addresses is called mapping.
The copying of virtual pages from the disk to the main memory is known as paging or swapping.
Some physical memory is used to keep a list of references to the most recently accessed
information on an I/O (input/output) device, such as the hard disk. The optimization it provides
Virtual memory is a memory management solution that employs both hardware and software.
It is a component of secondary storage that provides the user with the impression that it is a
component of the main memory. It facilitates the execution of several applications with limited
main memory and enhances the degree of multiprogramming in systems. Demand paging is
typically used to implement it.
Virtual memory is a component of the system's secondary memory that acts and seems to be a
component of the main memory. Virtual memory enables a system to run heavy applications or
several applications concurrently without running out of RAM (Random Access Memory). The
system can, for example, act as if its total RAM resources were equal to the total amount of
physical RAM plus the total amount of virtual Memory.
A system can function normally with far less physical RAM than it would ordinarily require. This
can be accomplished by transferring data between RAM and virtual memory as needed.
Primary and secondary storage were available on early computers. Storage was extremely
expensive back then, so expanding it would be problematic. The system ran out of memory
when programmes grew and requested more memory than was available. They were
compelled to use the hard drive's memory when this occurred. But, because access to this was
sluggish, they reasoned that it would be beneficial to have some form of rapid cheap memory
to aid in the resolution of this dilemma.
Yet, it was not until 1985, when Intel developed virtual memory, that virtual memory in desktop
computers became a reality. That is how virtual memory entered our daily lives.
Let's understand the working of virtual memory using the example shown below.
Imagine that an operating system consumes 500 MB of Memory to keep all processes
functioning. Unfortunately, there is now just 10 MB of actual RAM capacity available. The
operating system will then allocate 490 MB of virtual memory and manage it using the Virtual
Memory Management tool (VMM). As a result, the VMM will create a 490 MB file on the hard
disc to hold the additional RAM required. Because it considers 500 MB of actual memory
preserved in RAM, the System will now proceed to access memory even if only 10 MB of space
PAGE FRAMES
A page frame is used to structure physical memory. A page frame's size is a power of two bytes
and varies between platforms.
The CPU accesses processes using their logical addresses, whereas the main memory only
recognizes the physical address. This is made easier by the Memory Management Unit, which
converts page numbers (logical addresses) to frame numbers (physical address). Both have the
same offset.
The page table associates the page number with the frame number. The graphic above shows
how the frame number and frame offset work together to help us find the needed word.
A page table is a logical data structure that virtual memory uses to store the mapping between
virtual and physical locations. The accessing process's programs use virtual addresses, but the
hardware, particularly the random-access memory (RAM) subsystem, employs physical
addresses. The page table is an essential component of virtual address translation, which is
necessary to access memory data.
Let us suppose, as shown in the picture above, that the physical address space, logical address
space, and page size are M, L, and P words, respectively. The physical address, logical address,
and page offset can then be specified as follows:
Physical Address = log2 M m bits L page offset = p bits = log2 P Logical Address = l bits = log2 P
By removing the page offset from the address, we get the frame numbers, which are m-p and l-
p for physical and logical addresses, respectively.
Consider the wardrobe analogy that we discussed before. Perhaps you're browsing an e-
commerce site and fall in love with certain clothes. But, because you don't have enough space
in your wardrobe, you put it on your wishlist and intend to buy it only when you need it.
Demand paging, in contrast, is a method that saves pages of a process that are infrequently
utilized in secondary memory and draws them only when needed to satisfy the demand.
As a result, when a context transition occurs, the System begins executing the new program
after loading the first page and only fetches the referred pages of the application. If the
software attempts to access a page that is no longer available in main memory due to
swapping, If the software targets a page that is no longer present in main memory due to
swapping, the CPU considers it an invalid memory reference.
When primary memory (RAM) is insufficient to store data required by several applications, we
use it to swap out to transfer certain programs from RAM to the hard drive.
Similarly, as RAM becomes available, we move apps from hard disc to RAM. With swaps, we
may manage many processes inside the same Memory. Swapping is a low-cost method of
creating virtual memory.
● Improves the degree of multiprogramming: You can run multiple apps at the same time
without purchasing additional memory RAMs.
● Data Sharing: Data that is shared across memory can be shared.
● Avoids Relocation: The code in physical memory can be accessed whenever it is needed
without the need for relocation.
1. Slows down the system: Switching takes time, which reduces the system's speed. This
occurs because moving between applications takes longer.
2. Fewer Hard Disk Space: Virtual Memory consumes storage space that could otherwise
be used to store long-term data.
KEY TAKEAWAY
➢ Virtual memory is a crucial aspect of modern computing that plays a significant role in
optimizing system performance.
➢ It acts as an imaginary memory area supported by the operating system and hardware,
providing an extended address space for programs to utilize.
➢ In situations where physical memory (RAM) is limited, virtual memory enables the
execution of multiple applications simultaneously by temporarily storing unused data on
the hard disk.
➢ The process involves mapping virtual addresses to real memory addresses, known as
mapping, and transferring virtual pages between disk and main memory, known as
paging or swapping.
➢ This dynamic allocation of memory allows systems to function efficiently with less
physical RAM, enhancing the degree of multiprogramming.
➢ Virtual memory has evolved since its inception in 1959, becoming a standard feature in
contemporary operating systems.
A page replacement situation in an operating system is one in which a page from the main
memory is replaced by a page from the secondary memory. Page defects cause page
replacement. Several page replacement algorithms, such as
● FIFO
● Optimal page replacement
● LRU
● LIFO
In operating systems that use virtual memory with Demand Paging, page replacement is
required. As we know, demand paging loads only a subset of a process's pages into memory.
This is done so that multiple processes can run concurrently in memory.
When a process requests a page from virtual memory for execution, the Operating System must
determine which page will be replaced by this requested page. This is known as page
replacement, and it is an important part of virtual memory management.
To understand why we require page replacement techniques, we must first understand page
faults. Let's look at what a page fault is.
Page Fault: A Page Fault happens when a program executing in the CPU attempts to access a
page that is in that program's address space, but the requested page is currently not loaded
into the system's main physical memory, the RAM.
Page errors occur because the actual RAM is much less than the virtual memory. As a result, if a
page fault occurs, the operating system must replace a previously requested page in RAM with
the newly requested page. Page replacement algorithms aid the Operating System in
determining which page to replace in this case. The primary objective of all the page
replacement algorithms is to minimize the number of page faults.
The FIFO method is the most basic of the page replacement algorithms. In this case, we keep a
queue of all the pages that are currently in memory. The oldest page in memory is at the head
of the queue, and the most recent page is at the tail end.
When a page fault occurs, the operating system examines the front end of the queue to
determine which page should be replaced by the newly requested page. It also adds the newly
requested page to the back end of the queue and removes the oldest page from the front end.
● Initially, all of the slots are empty so page faults occur at 3,1,2.
Page faults = 3
Page faults = 3
● When page 6 comes, it is not present and a page fault occurs. Since there are no empty
slots, we remove the front of the queue, i.e 3.
Page faults = 4
● When page 5 comes, it is also not present and hence a page fault occurs. The front of
the queue i.e 1 is removed.
● When page 1 comes, it is not found in memory and again a page fault occurs. The front
of the queue i.e 2 is removed.
Page faults = 6
● When page 3 comes, it is again not found in memory, a page fault occurs, and page 6 is
removed being on top of the queue
Belady's anomaly: Generally if we increase the number of frames in the memory, the number of
page faults should decrease due to obvious reasons. Belady’s anomaly refers to the phenomena
where increasing the number of frames in memory, increases the page faults as well.
Advantages
Disadvantages
● Poor performance
● Doesn’t use the frequency of the last used time and just simply replaces the oldest page.
● Suffers from Belady’s anomaly.
The best page replacement algorithm is optimal page replacement since it produces the fewest
number of page defects. The pages that will not be used for the greatest period of time in the
future are replaced by this algorithm. In basic terms, this algorithm replaces the sites that will
be referred to the most in the future.
Take the same page reference string as in FIFO: 3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames. This also
assists you in understanding how Optimal Page Replacement works best.
● Initially, since all the slots are empty, pages 3, 1, 2 cause a page fault and take the empty
slots.
Page faults = 3
Page faults = 3
● When page 6 comes, it is not in the memory, so a page fault occurs and 2 is removed as
it is not going to be used again.
Page faults = 4
● When page 5 comes, it is also not in the memory and causes a page fault. Similar to
above 6 is removed as it is not going to be used again.
page faults = 5
● When page 1 and page 3 come, they are in the memory so no page fault occurs.
Advantages
● Excellent efficiency
● Less complexity
● Easy to use and understand
● Simple data structures can be used to implement
● Used as the benchmark for other algorithms
Disadvantages
The least recently used page replacement method maintains track of page usage over time. This
algorithm is based on the locality of a reference principle, which claims that a programme has a
tendency to access the same set of memory locations repeatedly during a short period of time.
Pages that have been widely utilised in the past are likely to be heavily used in the future as
well.
When a page failure occurs in this algorithm, the page that has not been utilised for the longest
period of time is replaced by the newly requested page.
Example: Let’s see the performance of the LRU on the same reference string of 3, 1, 2, 1, 6, 5, 1,
3 with 3-page frames:
Page faults = 3
Page faults = 3
● When page 6 comes, it is not in the memory, so a page fault occurs and the least
recently used page 3 is removed.
Page faults = 4
● When page 5 comes, it again causes a page fault and page 1 is removed as it is now the
least recently used page.
Page faults = 5
● When page 1 comes again, it is not in the memory and hence page 2 is removed
according to the LRU.
Page faults = 6
In the preceding example, the LRU creates the same page faults as the FIFO, but this is not
always the case because it depends on the series, the amount of frames available in memory,
and so on. In reality, in most circumstances, LRU is preferable to FIFO.
Advantages
Disadvantages
This method is based on the Last in First Out (LIFO) concept. The requested page replaces the
newest page in this algorithm. Typically, this is accomplished using a stack, in which we retain a
Example: Let’s see how the LIFO performs for our example string of 3, 1, 2, 1, 6, 5, 1, 3 with 3-
page frames:
● Initially, since all the slots are empty, page 3,1,2 causes a page fault and takes the empty
slots.
Page faults = 3
Page faults = 3
● When page 6 comes, the page fault occurs and page 2 is removed as it is on the top of
the stack and is the newest page.
Page faults = 4
Page faults = 5
● When page 1 and page 3 come, they are in memory already, hence no page fault occurs.
This is, as you may have seen, the same number of page faults as the Optimal page
replacement algorithm. Hence, for this series of pages, we can state that this is the best
method that can be implemented without previous knowledge of future references.
Advantages
● Simple to understand
● Easy to implement
● No overhead
Disadvantages
● Does not consider Locality principle, hence may produce worst performance
● The old pages may reside in memory forever even if they are not used
As the name implies, this technique selects any random page in memory to be replaced by the
desired page. Based on the random page picked to be replaced, this method can act like any of
the approaches.
● Initially, since all the slots are empty, page 3,1,2 causes a page fault and takes the empty
slots
Page faults = 3
Page faults = 3
● When page 6 comes, the page fault occurs, we replace the middle element i.e 1 is
removed.
Page faults = 4
● When page 5 comes, the page fault occurs again and middle element 6 is removed
● When page 1 comes, there is again a page fault and again the middle element 5 is
removed
Page faults = 6
As we can see, the performance is not the best, but it's also not the worst. The performance in
the random replacement algorithm depends on the choice of the page chosen at random.
Advantages
Disadvantages
● Can not be analyzed, may produce different performances for the same series
● Can suffer from Belady’s anomaly
KEY TAKEAWAY
➢ Page replacement algorithms are crucial for optimizing memory management in
operating systems, particularly in scenarios where available real memory frames
become limited.
I/O HARDWARE
An Operating System is responsible for managing numerous I/O devices such as the mouse,
keyboards, touchpad, disc drives, display adapters, USB devices, Bit-mapped screens, LEDs,
Analog-to-digital converters, On/off switches, network connections, audio I/O, printers, and so
on.
An I/O system must receive an application I/O request and send it to the physical device, then
receive the device's response and transmit it back to the application. I/O devices are classified
into two types:
● Block devices − The driver connects with a block device by transmitting whole blocks of
data. Hard drives, USB cameras, Disk-On-Key, and other similar devices are examples.
● Character devices − The driver communicates with a character device by transmitting
and receiving single characters (bytes, octets). For instance, serial ports, parallel ports,
sound cards, and so on.
DEVICE CONTROLLERS
Device drivers are software modules that can be put into an operating system to manage a
specific device. The operating system relies on device drivers to manage all I/O devices.
The Device Controller acts as a bridge between a device and its driver. I/O units (keyboard,
mouse, printer, etc.) are normally made up of a mechanical component and an electrical
component, the latter of which is known as the device controller.
Each device has a device controller and a device driver to connect with the operating systems. A
device controller may be capable of controlling many devices. Its primary function as an
interface is to transform a serial bit stream to a block of bytes and repair errors as needed.
● Synchronous I/O − In this scheme CPU execution waits while I/O proceeds
● Asynchronous I/O − I/O proceeds concurrently with CPU execution
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.
This makes advantage of CPU instructions designed expressly for manipulating I/O devices.
These instructions typically permit data to be delivered to or read from an I/O device.
Memory and I/O devices share the same address space when utilizing memory-mapped I/O.
The device is directly attached to certain primary memory locations, allowing the I/O device to
transmit data blocks to and from memory without passing via the CPU.
By employing memory-mapped IO, the operating system allocates a buffer in memory and
instructs the I/O device to use that buffer to transfer data to the CPU. I/O device operates
asynchronously with the CPU and interrupts the CPU when completed.
This technique has the advantage of allowing any instruction that can access memory to be
used to operate an I/O device. Most high-speed I/O devices, such as discs and communication
interfaces, use memory-mapped IO.
For Every byte transferred, slow devices such as keyboards will generate an interrupt to the
main CPU. If a fast device, such as a disc, generates an interrupt for every byte, the operating
system would spend the majority of its time dealing with these interruptions. To eliminate this
overhead, a typical computer employs direct memory access (DMA) technology.
Direct Memory Access necessitates the use of specialized hardware known as a DMA controller
(DMAC), which handles data transfers and arbitrates access to the system bus. The controllers
are pre-programmed with source and destination pointers (where to read/write data), counters
to track the number of transmitted bytes, and parameters such as I/O and memory types,
interrupts, and CPU cycle states.
A computer must be able to detect the presence of any sort of input. There are two ways for
this to happen: polling and interruptions. Both of these strategies enable the processor to deal
with events that can occur at any time and are unrelated to the present operation.
● POLLING I/O
Polling is the most basic method for I/O devices to communicate with the processor. Polling is
the process of periodically monitoring the device's state to see if it is time for the next I/O
operation. The I/O device merely stores the information in a Status register, and the processor
must retrieve it.
Most of the time, gadgets do not require attention, and when they do, they must wait until the
polling program interrogates them again. This is an inefficient strategy that wastes a significant
amount of processor time on useless polls.
● INTERRUPTS I/O
The interrupt-driven method is an alternate method for dealing with I/O. An interrupt is a signal
from a device that requires attention to the microprocessor.
When a device controller needs the CPU's attention, it sends an interrupt signal to the bus.
When the CPU gets an interrupt, it saves its current state and runs the appropriate interrupt
handler using the interrupt vector (addresses of OS routines to handle various events). After
dealing with the interrupting device, the CPU returns to its original task as if it had never been
stopped.
I/O SOFTWARE
● User Level Libraries − This provides a simple interface to the user program to perform
input and output. For example, stdio is a library provided by C and C++ programming
languages.
● Kernel Level Modules − This provides the device driver to interact with the device
controller and device-independent I/O modules used by the device drivers.
● Hardware − This layer includes actual hardware and hardware controller which interact
with the device drivers and makes hardware alive.
A crucial idea in the design of I/O software is device independence, which means that programs
should be able to access any I/O device without having to identify the device in advance. A
program that reads a file as input, for example, should be able to read a file on a floppy disc, a
hard drive, or a CD-ROM without having to alter the program for each device.
Device drivers are software modules that can be put into an operating system to manage a
specific device. The operating system relies on device drivers to manage all I/O devices. Device
drivers encapsulate device-specific code and implement a standard interface in such a way that
code containing device-specific register reads/writes is included. Device drivers are typically
created by the device's manufacturer and distributed on a CD-ROM with the device.
The following is how a device driver handles a request: Assume a request is received to read
block N. If the driver is idle when a request arrives, it begins processing the request
immediately. If the driver is already occupied with another request, the new request is added
to the queue of pending requests.
INTERRUPT HANDLERS
An interrupt handler, also known as an interrupt service routine or ISR, is a piece of software or,
more precisely, a callback function in an operating system or, more precisely, in a device driver,
whose execution is initiated by the receipt of an interrupt.
The following is how a device driver handles a request: Assume a request is received to read
block N. If the driver is idle when a request arrives, it begins processing the request
immediately. If the driver is already occupied with another request, the new request is added
to the queue of pending requests.
An interrupt handler, also known as an interrupt service routine or ISR, is a piece of software or,
more precisely, a callback function in an operating system or, more precisely, in a device driver,
whose execution is initiated by the receipt of an interrupt.
The device-independent software's primary job is to perform I/O functions shared by all devices
and to provide a consistent interface to the user-level program. Despite writing totally device-
independent software is difficult, we may develop some modules that are shared by all devices.
The following is a list of device-independent I/O Software functions.
These are the libraries that give a deeper and more straightforward interface for accessing
kernel functionality or, ultimately, interacting with device drivers. With the exception of the
spooling system, which is a method of dealing with dedicated I/O devices in a
multiprogramming system, most user-level I/O software consists of library functions.
I/O Libraries (for example, stdio ) exist in user space and serve as an interface to the OS's
device-independent I/O SW. Putchar(), getchar(), printf(), and scanf() are some examples of
user-level I/O library stdio functions available in C programming.
The Kernel I/O Subsystem is in charge of providing several I/O-related services. The following
are some of the services offered.
● Scheduling − A series of I/O requests are scheduled by the kernel to identify the best
order in which to execute them. When an application makes a blocked I/O system call,
the request is routed to the device's queue. The Kernel I/O scheduler reorders the
queue in order to optimize overall system efficiency and the average response time
observed by applications.
● Buffering − The Kernel I/O Subsystem maintains a buffer memory space that retains
data while it is transported between two devices or between a device and an
application action. Buffering is used to compensate for a speed difference between the
producer and consumer of a data stream, as well as to adjust across devices with varied
data transmission sizes.
KEY TAKEAWAY
➢ In the realm of I/O hardware management, an operating system plays a critical role in
overseeing a diverse array of devices, ranging from keyboards and display adapters to
disk drives and network connections.
➢ Device controllers act as intermediaries between devices and their respective drivers,
ensuring seamless communication.
➢ Two broad categories of I/O devices, block devices and character devices, necessitate
different handling approaches. Synchronous and asynchronous I/O strategies determine
whether CPU execution waits for I/O completion or proceeds concurrently.
➢ Various communication methods include special instruction I/O, memory-mapped I/O,
and Direct Memory Access (DMA), each with its advantages and use cases.
➢ The crucial choice between polling and interrupts I/O methods defines how a computer
detects input, with interrupts offering more efficient event handling by signaling the
CPU to address specific device needs.
➢ I/O software operates at different layers, from user-level libraries providing a simple
interface to kernel-level modules managing device drivers and hardware interactions.
➢ Device drivers play a pivotal role in interacting with specific devices, encapsulating
device-specific code and implementing standard interfaces.
➢ Interrupt handlers respond to interrupts, initiating specific actions upon receipt. Device-
independent I/O software facilitates uniform interfacing for device drivers, device
naming, protection, block size determination, buffering, storage allocation, error
reporting, and more.
OS DESIGN ISSUES
Following are the design issues in a distributed operating system:
Transparency
A transparent distributed system's user interface must be consistent across local and remote
resources. That is, users should be able to access remote resources as if they were local, and
the distributed system should be in charge of locating the resources and coordinating their
precise interaction. Another facet of transparency is user mobility.
Users should not be forced to utilize a specific machine, but the system should allow them to
log into any machine and access its services. A transparent distributed system enables user
mobility by copying the user's environment (for example, the home directory) to wherever the
user checks in.
The concept of transparency can be applied to many facets of a distributed system. These are
given below.
b) Migration Transparency: Resources can be moved from one location to another without
losing their identities, such as names.
b) Transparency in Replication: Users have no idea how many copies of the same file or
directory exist on various systems.
d) Concurrency Transparency: Several users can automatically share resources at the same
time.
f) Parallelism Transparency: All tasks can run in parallel without bothering the users.
Take, for example, a file system; saturation occurs when a server's CPU is overburdened or
when discs are nearly full. Scalability is a relative trait, yet it may be precisely measured. A
scalable system should be able to handle more load while maintaining the same performance
as a non-scalable system.
In fact, however, its performance first declines moderately, followed by saturation of its
resources. Even the most ideal design cannot manage this issue. Scalability and fault tolerance
are linked. A strongly loaded component can become paralyzed and perform as if it were
malfunctioning.
When we shift the load from a faulty component to the backup component, the latter can
become saturated. In general, having extra resources is critical for assuring reliability and
readily handling peak loads. Because of the diversity of resources, a distributed system has the
potential for fault tolerance and scalability.
Flexibility
Another critical aspect is adaptability. Because we are only beginning to discover how to create
a distributed system, it is critical that the system be flexible.
Reliability
One of the primary goals of distributed operating systems was to make them more reliable than
single-processor systems. If some machines fail, other machines will continue to function
normally. A highly reliable system must also be highly available, but this is insufficient. Trusted
data to the system must not be lost, and if files are stored on several computers, consistency
must be maintained. The more copies retained, the greater the availability.
Performance
Fault Tolerance
Computer systems occasionally fail. When hardware or software fails, programs may provide
unexpected or inaccurate results. It may occasionally cease before they have completed the
relevant calculation. Yet, failures in a distributed system are partial, meaning that although
some parts of the system fail, others continue to function. As a result, dealing with failures can
be challenging. Because it uses replicated servers, the distributed system has a high level of
availability, and data may be retrieved quickly.
I/O BUFFERING
WHAT IS A BUFFER
A data buffer (or simply buffer) is a physical memory storage region used to temporarily hold
data while it is being transported from one location to another. Data is typically kept in a buffer
as it is retrieved from an input device (such as a microphone) or immediately before it is
transferred to an output device (such as speakers). When transporting data between processes
on a computer, a buffer may be used. This is analogous to buffers in telecommunications.
Buffers can be implemented in hardware as a fixed memory region, or in software as a virtual
data buffer pointing to a physical memory location. The data stored in a data buffer is always
stored on a physical storage device. The bulk of buffers is implemented in software, which often
stores temporary data in quicker RAM due to the significantly faster access time compared to
hard disc drives. Buffers are often employed when there is a gap between the rate at which
data is received and the pace at which it can be processed, or when these rates are changing, as
USES OF BUFFERS
Buffers are frequently used in conjunction with I/O to hardware, such as disc drives, to send or
receive data to or from a network, or to play sound on a speaker. A line to a rollercoaster at an
amusement park is very similar. Riders on the roller coaster arrive at an unknown and
frequently unpredictable rate, however, the roller coaster will be able to load riders in bursts
(as a coaster arrives and is loaded). The queue area serves as a buffer—a temporary space
where individuals who want to ride can wait until the ride is ready. Buffers are typically
employed in a FIFO (first in, first out) technique, which outputs data in the order in which it
arrived.
Buffers can boost application performance by allowing synchronous operations like file reads
and writes to complete quickly rather than blocking while waiting for hardware interrupts to
access a physical disc subsystem; instead, an operating system can immediately return a
successful result from an API call, allowing an application to continue processing while the
kernel completes the disc operation in the background. If the application is reading or writing
small blocks of data that do not correspond to the disc subsystem's block size, a buffer can be
utilized to aggregate multiple smaller read or write operations into block sizes that are more
efficient for the disc subsystem, or in the case of a read, sometimes to completely avoid having
to physically access a disk.
TYPES OF BUFFERS
There are several common types of buffering used. We will talk about just a couple.
A single buffer implementation is the most basic form of buffering. As a user process runs, it
sends out an I/O request, which causes the operating system to allocate a buffer in the system
This procedure can also work in the opposite direction. The process may involve the generation
of data that must be saved on a disc drive or transferred to another output device. As the
process generates data, it stores it in the buffer until the device is ready to accept it, at which
point the OS transfers the buffer's contents to the output device.
This technique essentially adds a second buffer to improve efficiency. When the first buffer is
full, the operating system automatically switches to the second buffer. Once the second buffer
fills, the OS moves data out of the first buffer. As a result, by the time the first buffer is needed
again, it is empty, and the OS begins filling it again while emptying the other buffer.
CIRCULAR BUFFER
A circular buffer is initially empty and has a fixed length. A 7-element buffer is depicted in the
diagram below:
Assuming that 1 is written in the center of a Circular Buffer (the exact starting point in a Circular
Buffer is unimportant):
Assume two more pieces are added to the Circular Buffer — 2 and 3 — and are placed after 1:
If two elements are removed, the two oldest values in the Circular Buffer are removed as well.
FIFO (First In, First Out) logic is used by circular buffers. Because 1 and 2 were the first to enter
the Circular Buffer, they are the first to be eliminated, leaving three inside the Buffer.
When the circular buffer becomes full and a subsequent write is made, it begins overwriting the
oldest data. In the current example, two new elements — A and B — are added and replace
elements 3 and 4:
Alternatively, the buffer management procedures could prevent data overwriting and return an
error or raise an exception. The semantics of the buffer procedures or the application using the
circular buffer determine whether or not data is overwritten.
Finally, if two components are removed, what is returned is not 3 and 4, but 5 and 6, because A
and B overwrote the 3 and 4.
DISK ORGANISATION
Disks come in a variety of shapes and sizes. The most visible similarity between floppy discs,
diskettes, and hard discs is that floppy discs and diskettes are made up of a single magnetic
disc, but hard drives are usually made up of several stacked on top of one another. Hard drives
are completely enclosed electronics that are significantly more finely built and so require dust
protection. A hard disc spins at a regular speed, whereas floppy drives rotate on and off. Floppy
drives on the Macintosh system operate at various speeds, whereas other floppy drives only
rotate at one speed. As hard drives and tape units become more efficient and less expensive to
manufacture, the floppy disk's importance is waning.
A hard drive is made up of numerous physical discs stacked on top of each other, as shown in
Figure. The disc seen in the figure has three platters and six recording surfaces (two on each
On other systems (often microcomputers), it may be selected in the software when the disc is
ready for usage (formatting). We can improve read-write efficiency by assigning blocks in
parallel across all surfaces because the heads of the disc move together on all surfaces. As a
result, if a file is stored in sequential blocks on a disc with n surfaces and n heads, it can read n
sectors per track without moving the head. When a manufacturer ships a disc, the physical
properties of the disc (number of tracks, number of heads, sectors per track, speed of
revolution) are included. An operating system must be able to adapt to various disc types.
Clearly, the number of sectors per track and the number of tracks are not constants. The
numbers mentioned are only a convention used to generate a consistent set of addresses on a
disc and may have nothing to do with the disk's hard and fast physical limits. To address any
part of a disc, we require a three-component address made up of (surface, track, and sector).
The seek time is the time required for the disk arm to move the head to the cylinder with the
desired sector. The rotational latency is the time required for the disk to rotate the desired
sector until it is under the read-write head.
A hard drive is a device, and as such, an operating system must communicate with it via a
device controller. Some device controllers are basic microprocessors that convert numerical
Hard drives are not perfect: they develop flaws as a result of magnetic dropout and poor
manufacturing. This is verified when the disc is formatted on older discs, and damaged sectors
are avoided. If a sector becomes damaged while in use, the disc structure must be repaired
using a repair program. Typically, the data is lost. On more intelligent drives, such as SCSI drives,
the disc itself maintains a defect list that includes a record of all defective sectors. A new disc
from the manufacturer has a starting list, which is updated as more problems occur. Formatting
is the process through which the disk's sectors are:
Formatting is done manually on basic discs used by microcomputers. Some varieties, such as
SCSI drives, have low-level formatting already on the disc when it is shipped from the
manufacturer. In certain ways, this is part of the SCSI protocol. High-level formatting is not
required because an advanced enough filesystem will be able to control the hardware sectors.
KEY TAKEAWAY
A process, as we all know, requires two types of time: CPU time and IO time. It asks the
operating system to access the disc for I/O.
Yet, the operating system must be far enough to satisfy each request while also maintaining the
efficiency and speed of process execution.
Disk scheduling refers to the technique used by the operating system to select which request
should be fulfilled next.
● SEEK TIME
Seek time is the amount of time it takes to locate the disc arm to a specific track where the
read/write request will be fulfilled.
● ROTATIONAL LATENCY
It is the amount of time it takes the targeted sector to rotate itself to a position where it can
access the R/W heads.
● TRANSFER TIME
The disc scheduling algorithm's main goal is to select a disc request from a queue of IO requests
and determine when this request will be handled.
○ Fairness
○ High throughout
○ Minimal traveling head time
The following is a list of various disc scheduling algorithms. Each algorithm has both advantages
and cons. Each algorithm's limitation leads to the evolution of a new algorithm.
It is the most fundamental Disk Scheduling algorithm. It processes IO requests in the order in
which they arrive. This algorithm has no hunger; every request is met.
EXAMPLE
Consider the following disc request sequence for a disc with 100 tracks 45, 21, 67, 90, 4, 50, 89,
52, 61, 87, 25
Head pointer begins at 50 and travels in the left direction. Find the number of head movements
in cylinders using FCFS scheduling.
= (50-45)+(45-21)+(67-21)+(90-67)+(90-4)+(50-4)+(89-50)+(61-52)+(87-61)+(87-25)
= 5 + 24 + 46 + 23 + 86 + 46 + 49 + 9 + 26 + 62
The algorithm that selects the disc I/O request that requires the least amount of disc arm
movement from its current position, independent of direction, is known as the shortest seek
time first (SSTF). It reduces the total seek time as compared to FCFS.
It allows the head to move to the closest track in the service queue.
DISADVANTAGES
EXAMPLE
Consider the following disk request sequence for a disk with 100 tracks
Head pointer starting at 50. Find the number of head movements in cylinders using SSTF
scheduling.
SCAN ALGORITHM
It is also referred to as the Elevator Algorithm. In this algorithm, the disc arm moves in a
particular direction till the end, satisfying all the requests coming in its path, and then it turns
back and moves in the reverse direction satisfying requests coming in its path.
It operates similarly to an elevator in that it moves in one direction until it reaches the last floor
in that direction and then reverses direction.
EXAMPLE
Starting at 54, the head pointer moves to the left. Using SCAN scheduling, determine the
number of head movements in cylinders.
C-SCAN ALGORITHM
The arm of the disc moves in a specific way, serving requests until it reaches the last cylinder,
then it jumps to the final cylinder in the opposite direction, without processing any requests,
then it turns back and starts moving in that direction, servicing the remaining demands.
EXAMPLE
Starting at 54, the head pointer moves to the left. Find the number of head movements in
cylinders using C-SCAN scheduling.
LOOK SCHEDULING
It is similar to the SCAN scheduling algorithm in some ways, except that in this scheduling
algorithm, the arm of the disc stops moving inwards (or outwards) when there are no more
requests in that direction. This technique tries to overcome the overhead of the SCAN
algorithm which compels the disc arm to proceed in one way till the end regardless of knowing
if any request exists in the direction or not.
EXAMPLE
Starting at 54, the head pointer moves to the left. Using LOOK scheduling, determine the
number of head movements in cylinders.
C LOOK SCHEDULING
C Look Algorithm is comparable to the C-SCAN algorithm to some extent. In this algorithm, the
arm of the disc moves outwards servicing requests until it reaches the highest request cylinder,
then it jumps to the lowest request cylinder without servicing any request then it again starts
moving outwards servicing the remaining requests.
It differs from the C SCAN algorithm in that C SCAN forces the disc arm to travel to the last
cylinder regardless of whether or not any requests are to be handled on that cylinder.
Starting at 54, the head pointer moves to the left. Find the number of head movements in
cylinders using C LOOK scheduling.
KEY TAKEAWAY
➢ Disk scheduling is a crucial aspect of operating system design, optimizing the order in
which I/O requests are fulfilled to enhance efficiency.
➢ Several terms are central to disk scheduling, including seek time, rotational latency,
transfer time, disk access time, and disk response time.
Disks have high failure rates and hence there is the risk of data loss and lots of downtime for
restoring and disk replacement. To improve disk usage many techniques have been
implemented. One such technology is RAID (Redundant Array of Inexpensive Disks). Its
organization is based on disk striping (or interleaving), which uses a group of disks as one
storage unit. Disk striping is a way of increasing the disk transfer rate up to a factor of N, by
splitting files across N different disks. Instead of saving all the data from a given file on one disk,
it is split across many. Since the N heads can now search independently, the speed of transfer
is, in principle, increased manifold. Logical disk data/blocks can be written on two or more
separate physical disks which can further transfer their sub-blocks in parallel. The total transfer
rate system is directly proportional to the number of disks. The larger the number of physical
disks striped together, the larger the total transfer rate of the system. Hence, the overall
performance and disk accessing speed are also enhanced. The enhanced version of this scheme
is mirroring or shadowing. In this RAID organization, a duplicate copy of each disk is kept. It is
costly but a much faster and more reliable approach. The disadvantage of disk striping is that, if
one of the N disks becomes damaged, then the data on all N disks is lost. Thus striping needs to
be combined with a reliable form of backup in order to be successful. Another RAID scheme
uses some disk space for holding parity blocks. Suppose, three or more disks are used, then one
of the disks will act as a parity block, which contains corresponding bit positions in all blocks. In
case some error occurs or the disk develops problems all its data bits can be reconstructed. This
technique is known as disk striping with parity or block interleaved parity, which increases
The operating system (OS) sees RAID arrays as a single logical drive.
RAID involves disc mirroring or disc striping techniques. Mirroring copies identical data to many
drives. Striping partitions aid in the distribution of data over many disc drives. The storage
space on each disc is divided into chunks ranging from 512-byte sectors to several megabytes.
All of the discs' stripes are interleaved and addressed sequentially. Disk mirroring and disc
striping can also be coupled in a RAID array.
On a single-user system where huge records are kept, stripes are often configured to be small
(512 bytes, for example) such that a single record spans all discs and may be accessed rapidly by
reading all discs at the same time.
Better performance in a multiuser system necessitates a stripe broad enough to carry the
typical or maximum size record, allowing for overlapping disc I/O across drives.
RAID CONTROLLER
A RAID controller is a piece of hardware that manages the hard disc drives in a storage array. It
can be used as a layer of abstraction between the operating system and the actual discs,
presenting disc groups as logical units. Utilizing a RAID controller can boost performance while
also protecting data in the event of a crash.
RAID controllers can be either hardware or software-based. A physical controller oversees the
entire array in a hardware-based RAID device. In addition, the controller can be configured to
accommodate drive formats such as Serial Advanced Technology Attachment and Tiny
The controller in software-based RAID makes use of the physical system's resources, such as the
central CPU and memory. While it performs the same responsibilities as a hardware-based RAID
controller, software-based RAID controllers may not provide as much of a speed gain and may
interfere with the performance of other server applications.
Similar to software-based RAID, firmware-based RAID controller chips are situated on the
motherboard and all operations are executed by the central processor unit (CPU). With
firmware, however, the RAID system is only activated at the start of the boot procedure. After
the operating system has been loaded, the controller driver takes over the RAID operation. A
firmware RAID controller is less expensive than a hardware RAID controller, but it places more
burden on the computer's CPU. Firmware-based RAID is also known as hardware-assisted
software RAID, hybrid model RAID, and fake RAID.
RAID LEVELS
RAID devices employ various versions known as levels. The original study that originated the
name and introduced the RAID configuration idea defined six RAID levels ranging from 0 to 5.
This numbering system allowed IT personnel to distinguish between RAID versions. Since then,
the number of levels has grown and been divided into three categories: standard, nested, and
nonstandard RAID levels.
DISK CACHE
Disk caching is an extension of buffering. The cache is derived from the French word cacher,
meaning to hide. In this context, a cache is a collection of blocks that logically belong on the
disk, but are kept in memory for performance reasons. It is used in multiprogramming
A disc cache (cache memory) is a temporary holding space in a hard drive or random access
memory (RAM) where the computer saves frequently needed information. The computer can
use it to speed up the process of storing and accessing information from the disc cache
considerably faster than if the information was stored in the usual area (which could be on a
disc or in a slower-accessing part of the computer's memory). A disc cache may also refer to a
disc buffer or a cache buffer.
Working with information stored in memory (RAM) is significantly faster than working with
information saved on a disc, which is the underlying notion behind a disc cache. A disc cache is
a software program that operates by reserving a region of memory in which it stores a copy of
previously read information from your drives. When your computer needs that information
again, it can get it directly from the cache rather than the slower disc. But, if the CPU (central
processing unit, the chip that runs the computer) requires data that is not in the disc cache, it
must access the disc, Then it has to go to the disc and get it, therefore having a portion of your
memory set aside for the disc cache is pointless.
Because related data is frequently physically contiguous on a disc, the cache may also make a
copy of information near previously utilized data in the hope that extra data may be needed in
the future. Nevertheless, because cache space is limited, only a portion of the information on
Some disc caching tools also cache files that you want to save as well as other data that your
computer is attempting to save to a disc. Because the cache delivers this data to the disc in
little drips and drabs rather than all at once, you don't have to wait for the saving procedure to
complete before returning to work. Although advertisements claim that a disc cache "speeds
up your hard disc," the disc does not operate any faster—it simply isn't used as frequently.
The Memory Control Panel on a Mac allows you to specify how much memory to set aside as a
disc cache. In System 6, you can disable the cache entirely; in System 7, you can reduce it to 16
K. Bear in mind that whatever amount selected as disc cache depletes your main RAM. If you
only have 2 MB in your computer, turn the cache off or down to 16K, because you won't notice
a difference with less than a 256 K cache, and you really can't spare any if you only have 2 MB.
Try setting your cache between 256 K and 1,024 K if you have 4 MB or more of memory and
see if you observe a performance improvement. If you don't see an improvement, reduce it as
much as possible because there's no use in saving that RAM space if it's not helping you.
You must run your disc caching utility as a device driver (through your CONFIG.SYS file) or as a
memory resident program on IBM-compatible PCs (usually by including it in your
AUTOEXEC.BAT file). With Windows and MS-DOS versions 5.0 and higher, Microsoft included a
good caching application called SMART Drive. If you're prepared to spend more, you can
purchase faster-caching tools from various vendors, the most well-known of which is Super PC-
Kwik.
Let us first review operating systems and files before diving into the file system in the operating
system.
Files are collections of related information that are kept on various storage devices such as flash
drives, hard disc drives (HDD), magnetic tapes, optical discs, cassettes, and so on. Files can be
read-only or write-only. Files are simply utilized to provide input(s) and get output (s).
Currently, an operating system is simply a software program that serves as a bridge between
hardware, application software, and users. An operating system's primary goal is to manage all
computer resources. So, simply put, the operating system provides a platform for application
software and other system software to perform their functions.
Refer to the diagram below to understand the value and working of the operating system.
As a result, file management is one of the operating system's fundamental but vital
functionalities. File management in an operating system is simply software that handles or
manages the files (binary, text, pdf, documents, audio, video, and so on) that are present in a
computer program. The file system in the operating system can manage both individual files
and groups of files on the computer. File management in the operating system manages all of
the files with various extensions that are present in the computer system (such as .exe, .pdf,
.txt, .docx, etc.)
We may also use the operating system's file system to retrieve information about any file(s) on
our system. Details can include:
● location of the file (the logical location where the file is stored in the computer system)
● the owner of the file (who can read or write on the particular file)
● when was the file created (time of file creation and modification time)
● a type of file (format of the file for example text, pdfs, docs, etc.)
● state of completion of the file, etc.
The file must have a predefined structure or format in order to be managed by the operating
system or to be understood by the operating system. In most operating systems, there are
three types of file structures:
1. text file: Text files are non-executable files that include a series of numbers, symbols,
and letters structured in the form of lines.
2. source file: A source file is an executable file that comprises a series of routines and
processes. In layman's terms, a source file is a file that contains the program's
instructions.
● The operating system's file management allows users to create new files, as well as
change and delete existing files located across the computer system.
● The file management program in the operating system handles the locations of the file
shop so that files can be quickly extracted.
● Because processes share files, making files shareable between processes is one of the
most significant functions of file management in operating systems. It enables several
processes to securely access the information they require from a file.
● The operating system file management software also handles the files, reducing the
possibility of data loss or destruction.
● The file management in the operating system gives input-output operation support to
the files so that the data can be written, read, or extracted from the file(s) (s).
● It also serves as a standard input-output interface for both the user and the system
operations. The intuitive interface enables quick and easy data editing.
● The operating system's file management system also manages the various user
permissions that are present on a file. The operating system provides three types of user
permissions: read, write, and execute.
● The operating system's file management supports many types of storage devices such as
flash drives, hard disc drives (HDD), magnetic tapes, optical discs, tapes, and so on, and
it also allows the user(s) to quickly store and remove them.
1. The files are organized or grouped into a more complicated structure, such as a tree, to
reflect the relationship between the various files. File systems function similarly to how
2. Every file has a name and access rights that indicate who can access the files in which
mode (read or write).
● Users can use this program to create, change, and remove files on the computer system.
● Manages the locations of files stored in secondary or primary memory.
KEY TAKEAWAY
➢ File management is a crucial feature of operating systems, serving as the software that
oversees various types of files within a computer program.
➢ It handles individual and grouped files, providing information about their location,
ownership, creation and modification timestamps, types, and states.
➢ Operating systems act as a bridge between hardware, applications, and users, with file
management being one of its fundamental functionalities.
➢ Files, including binary, text, pdfs, and others, are stored on various devices, and file
management ensures efficient organization and retrieval.
➢ The objectives of file management in operating systems encompass creating, modifying,
and deleting files, facilitating file sharing among processes, reducing the risk of data loss,
supporting input-output operations, and managing user permissions.
➢ The properties of file management involve organizing files hierarchically and assigning
names and access rights to each file.
➢ The functions of file management include creating, changing, and removing files,
managing file locations, handling permissions, organizing files into a tree structure,
interfacing with I/O operations, and safeguarding files from unauthorized access.
➢ Overall, effective file management is essential for streamlined and secure computing
operations.
FILE DIRECTORIES
A file directory is a collection of files that have been organized. A directory entry relates to a file
or another directory. As a result, a tree structure/hierarchy can be created. Directories are used
to organize files belonging to various applications/users. Large-scale time-sharing and
distributed systems hold thousands of files and large amounts of data. A file system must be
correctly organized for this type of setting. A file system can be partitioned or divided into
volumes. They provide different sections within a single disc that are handled as separate
storage devices for files and directories. Hence, directories allow files to be divided based on
user and user application, simplifying system administration issues such as backups, recovery,
security, integrity, name-collision (file name clashes), file cleaning, and so on.
The device directory stores information about all the files on the partition, such as their name,
location, size, and type. A root is the area of the disc from which the root directory points to the
user directories. The root directory differs from subdirectories in that it has a defined position
and size. As a result, the directory functions similarly to a symbol table, converting file names
into their associated directory entries.
3) Mechanisms for sharing files should provide controlled access like read, write, execute or
various combinations.
4) List the files in a directory and also the contents of the directory entry.
5) Renaming a file when its contents or uses change or file position needs to be changed.
The most common schemes for describing logical directory structure are
I. Single-level directory
All of the files are in the same directory, which is basic and easy to grasp; nevertheless, all files
must have unique names. Furthermore, even with a single user, as the number of files grows, it
becomes impossible to remember and track all of the file names. The figure depicts this
hierarchy.
We can overcome the limitations of the previous scheme by creating a separate directory called
User File Directory for each user (UFD). When a user first logs in, the system's Master File
Directory (MFD) is searched, which is indexed with respect to the user's username/account and
UFD reference. Hence, various users may have the same file names, but they must be unique
within each UFD. To some extent, this fixes the name-collision problem, but this directory
structure isolates one user from another, which is not always desirable when users need to
communicate or collaborate on a job. The figure clearly depicts this scheme.
The two-level directory structure is like a 2-level tree. To generalize, we can extend the
directory structure to any height tree. As a result, the user can construct his or her own
directory and subdirectories, as well as organize files. One bit in each directory entry specifies if
the entry is a file (0) or a subfolder (1).
The tree contains a root directory, and each file within it has a distinct path name (path from
root, through all subdirectories, to a specified file). The pathname precedes the filename and
aids in navigating to the needed file from a base directory. Pathnames can be of two types,
depending on the base directory: absolute path names or relative path names. Depending on
the base directory, pathnames might be of two types: absolute path names or relative path
names. An absolute path name starts at the root and proceeds to a specific file location. It is a
complete pathname that refers to the root directory. The path relative to the current directory
is defined by relative.
This scheme, as the name implies, features a graph with no cycles. This approach introduced
the concept of a shared common subdirectory/file, which existed in the file system in two (or
more) locations at the same time. Having two copies of a file does not result in changes in one
copy reflecting changes in the other copy. Nevertheless, in a shared file, only one actual file is
utilized, so modifications are apparent. As a result, an acyclic graph is a broadening of a tree-
structured directory system. This is important when several people are working as a team and
need access to shared files housed in a single directory. This strategy can be carried out by
adding a new directory item known as a link that points to another file or subdirectory. This
arrangement for directories is depicted in Figure.
Cycles are not permitted in acyclic graphs. When there are cycles, the reference count may be
non-zero even if the directory or file is no longer referenced. In such cases, trash pickup is
beneficial. This technique necessitates traversing the entire file system and marking only
accessible entries. The second pass collects anything that is not designated on a free-space list.
The figure illustrates this.
The accessing or sharing of files by one or more users is known as file sharing, sometimes
known as file swapping. It is used on computer networks to transmit data quickly. A file-sharing
system typically has more than one administrator, with users having the same or different
access privileges. It also involves having a set number of personal files in common storage.
For many years, file sharing has been utilized in mainframe and multi-user computer systems,
and now that the internet is widely available, a file transmission method known as the File-
Transfer Protocol, or FTP, is extensively used.
The internet can be thought of as a large-scale file-sharing system in which files are constantly
downloaded or viewed by online users.
But, with a file-sharing system, the user can read and write in the files, and if the entire system
is unavailable for access, the owner's common file can be read and written into.
KEY TAKEAWAY
➢ File directories play a crucial role in organizing files within an operating system, forming
a tree structure or hierarchy.
➢ They help manage large amounts of data and prevent issues such as name collisions,
providing a structured approach to system administration tasks like backups, recovery,
security, and integrity.
➢ File systems can be partitioned into volumes, allowing for efficient storage device
management.
➢ Operations performed on directories and file systems include creating, deleting,
modifying, searching, sharing, listing, renaming, backing up, recovering, and traversing.
➢ Different logical directory structures, such as single-level, two-level, tree-structured,
acyclic-graph, and general graph, offer varying degrees of organization and user
isolation.
➢ File sharing, an essential aspect of file management, involves the access and
transmission of files among users, typically facilitated by systems like FTP.
➢ However, managing file access in shared environments requires careful consideration of
control and security aspects.
RECORD BLOCKING
Records are the logical units of a file whereas blocks are units of I/O with secondary storage. It
performs for I/O and the records must organize as blocks. Blocking is a process of grouping
similar records into blocks that the operating system component explores exhaustively. Block is
the unit of I/O for secondary storage. Generally, the larger blocks reduce the I/O transfer time.
Larger blocks require larger I/O buffers. In record blocking, the records are grouped into blocks
by shared properties that are indicators of duplication.
1. Fixed blocking
2. Variable-length spanned blocking
3. Variable-length unspanned blocking
FIXED BLOCKING:
In this method, record lengths are fixed. The prescribed number of records stored in a block.
Internal fragmentation is stored in a block. Fixed blocking is common for sequential files with
fixed-length records length records
In this method, record sizes aren’t the same, Variable-length records packing into blocks with
no unused space. So, some records may divide into two blocks. In this type of situation, a
pointer passes from one block to another block. So, the Pointers used to span block unused
space. It is efficient in length and efficiency of storage. It doesn’t limit record size, but it is more
complicated to implement. So, the files are more difficult to update
DIRECT ACCESS TO DISKS AND KEEPING FILES IN ADJACENT AREAS OF THE DISK IS
HIGHLY DESIRABLE. BUT THE PROBLEM IS HOW TO ALLOCATE SPACE TO FILES FOR
EFFECTIVE DISK SPACE UTILIZATION AND QUICK ACCESS. ALSO, AS FILES ARE
ALLOCATED AND FREED, THE SPACE ON A DISK BECOME FRAGMENTED. THE MAJOR
METHODS OF ALLOCATING DISK SPACE ARE:
I) CONTINUOUS
I) CONTINUOUS
This technique suffers from fragmentation issues related to variable memory partitioning. This
is due to the possibility that allocation and deal locating will result in regions of free disc space
This plan has taken the place of the previous ones. The following are the most common non-
contiguous storage allocation schemes:
• Linked/Chained allocation
• Indexed Allocation.
Linked/Chained allocation:
All files are stored in fixed-size blocks that are linked together in the same way that a linked list
is. Disk blocks can be found anywhere on the disc. The directory contains a pointer to the file's
first and last blocks. In addition, each block contains pointers to the following block that are not
visible to the user. This has no external fragmentation because any free block can be used for
storage. Compaction and repositioning are thus unnecessary. However, because blocks are split
over the disc and must follow pointers from one disc block to the next, it is potentially
inefficient for direct-accessible files. It is only effective for sequential access, but it may create
long seeks between blocks. Another consideration is the additional storage space required for
pointers. But, there is a dependability issue owing to the loss/damage of any pointer. The usage
of doubly linked lists could be a solution to this problem, but it would introduce additional
overheads for each file. A doubly linked list also makes searching easier because blocks are
FAT is another version on the linked list used by MS-DOS and OS/2 (File Allocation Table). Each
partition starts with a table that has one item for each disc block and is indexed by the block
number. The directory entry provides the file's initial block's block number. The block number
of the next block in the file is contained in the table entry indexed by the block number. The last
block in the file's Table pointer has an EOF pointer value. This chain will be repeated until an
EOF (end of file) table entry is met. We still have to visit the next pointers sequentially, but we
don't have to go to the disc for each one. A table value of 0 (Zero) indicates an unused block.
Hence, allocating free blocks with the FAT scheme is simple; simply look for the first block with
a 0 table pointer. This method is used by MS-DOS and OS/2. The figure depicts this system.
Each file has its own index block in this. The index keeps an array of block pointers for each file,
with each entry pointing to the disc blocks containing actual file data. As a result, an index block
is an array of disc block addresses. The index block's i th entry points to the file's i th block. The
main directory also provides the address of the index block on the disc. All of the pointers in the
index block are initially set to NIL. This approach has the advantage of supporting both
sequential and random access. Searching can take conducted within index blocks. To reduce
seek time, index blocks in secondary storage might be stored near together. Additionally, just a
little amount of space is wasted on the index, and there is no external fragmentation. However,
several constraints of the old scheme remain, such as the necessity to establish a maximum file
length and the ability to have an overflow scheme for files greater than the anticipated value.
Insertions may necessitate the complete reconstruction of index blocks. The figure depicts the
indexed allocation scheme diagrammatically.
➢ Record blocking and disk space management are essential components of file
management in operating systems.
➢ Record blocking involves organizing records into blocks for efficient I/O operations with
secondary storage.
➢ Three types of record-blocking methods include fixed blocking, variable-length spanned
blocking, and variable-length unspanned blocking, each with its own advantages and
complexities.
➢ Disk space management is crucial for effective utilization and quick access to disk space.
➢ Two major methods for allocating disk space are continuous and non-continuous.
Continuous allocation, also known as contiguous allocation, assigns a set of contiguous
blocks to each file, minimizing fragmentation and reducing disc seek time.
➢ However, it suffers from issues like fragmentation and requires size declaration in
advance. Non-continuous allocation includes linked/chained allocation and indexed
allocation.
➢ Linked/chained allocation uses linked blocks like a linked list, suitable for sequential
access but potentially inefficient for direct access. Indexed allocation assigns an index
An open-source operating system called Linux has completely changed the computing industry.
Linux has established itself as the foundation of many devices, including personal computers,
servers, and even embedded systems, because of its stability, security, and adaptability. In
addition to Linux, shell programming is essential for automating processes and maximizing the
operating system's functionality.
Linux was created in 1991 by Linus Torvalds and is based on the ideas of open-source
development. This indicates that the public can see, alter, and share its source code without
paying any fees. An operating system that can compete with commercial alternatives is the
result of the international cooperation of thousands of developers.
There are several benefits to using Linux. First off, Linux is known for being incredibly stable. It
is a great option for crucial systems and servers because it is known to operate for extended
periods of time without needing to reboot. The effective memory management, process
separation, and error handling methods built into the Linux kernel are responsible for its
stability.
Second, security is an area where Linux shines. Because Linux is open-source, extensive code
review by security professionals is made possible, guaranteeing that vulnerabilities are found
and fixed right away. Furthermore, Linux offers fine-grained access restrictions that let
administrators give users particular permissions in order to maintain a secure computer
environment.
Third, Linux offers much customizability. Users are free to customize and enhance the operating
system to meet their own requirements. Linux distributions like Ubuntu, Fedora, and Debian
Additionally, Linux has a huge software ecosystem. The Linux community has created sizable
repositories of software packages that include a variety of tools, libraries, and applications.
Users may easily identify and install software thanks to package management systems like apt,
yum, and pacman, including web servers and scientific computing frameworks.
A basic feature of Linux is shell programming, which enables users to communicate with the
operating system via a command-line interface. A programme called the shell interprets user
commands and carries them out appropriately. The Bash shell, which stands for "Bourne Again
SHell," is among the most well-liked types of shells. It offers a robust and feature-rich scripting
environment and is the default shell for many Linux distributions.
Shell programming helps users to effectively manage system setups, automate jobs, and carry
out a variety of procedures. Shell scripts are collections of commands that the shell interprets
from scripts written in a scripting language. By combining several commands, loops,
conditionals, and variables, these scripts can be utilized to carry out complex processes.
Shell scripting is an excellent resource for both inexperienced and seasoned users due to its
simplicity and flexibility. Users can use it to automate repetitive tasks like file manipulation,
backup procedures, and system monitoring by writing scripts. Users can save time and effort
while assuring consistency and accuracy in task execution by encapsulating a set of commands
within a script.
Shell programming also makes it easier to create interactive programmes and utilities. Shell
scripts can prompt users for information, validate inputs, and perform suitable actions based on
the provided conditions with the use of user input and conditional statements. Shell
programming is a crucial ability for system administrators, developers, and power users
because of its versatility.
The shell takes textual commands and translates them into instructions that the operating
system can understand and execute. It provides a command-line prompt where users can type
commands and receive responses or perform actions based on those commands.
In addition to accepting and executing commands, the shell also provides various features and
functionalities to enhance the user experience. These include:
1. Command History: The shell normally keeps track of all commands that have already
been run, making it simple for users to recall and reuse them.
2. Shell programmes frequently have tab completion, which allows users to start typing a
command or file name and then hit the Tab key to have the rest of the text
automatically complete or be suggested. This function speeds up the process and
reduces errors.
3. Redirection of Command Input and Output: The shell lets users reroute the input or
output of commands. For instance, they can direct input from a file rather than
manually putting it in or the output of a command to a file rather than displaying it on
the screen..
4. Pipelines: Shell programmes give users the ability to combine numerous instructions.
Pipes enable the creation of strong and complicated command sequences by allowing
the output of one command to be transferred as input to another.
5. Environment management and variables: The shell has the ability to define and work
with variables, which can be used in commands and scripts to store data. Additionally, it
controls the shell session's environment, including environment variables that dictate
how scripts and programmes operate.
Unix-like operating systems are the Bash (Bourne Again SHell). It comes with a wealth of
functionality and is compatible with existing shell scripts, making it the default shell for many
Linux distributions.
The Z shell (zsh), the Korn shell (ksh), and other frequently used shells each have their own
syntax, features, and capabilities.
KEY TAKEAWAY
The open-source operating system Linux has become quite popular and is used extensively in
many different fields, including personal computers, servers, embedded systems, and even
supercomputers. Linux, an operating system designed to resemble Unix, is recognised for its
reliability, security, adaptability, and scalability. This in-depth overview of Linux will cover its
history, architecture, features, distributions, and community, demonstrating why it has become
a popular operating system for many people.
1. History of Linux:
The origins of Linux may be traced to a Finnish computer science student named Linus Torvalds
who began developing a Unix-like operating system kernel in the early 1990s. It was created by
Torvalds as a side project at first, but he eventually made it available to the online community
for participation and enhancement. The Linux kernel, the central element of the Linux
operating system, was created as a result of this cooperative effort.
2. Linux Architecture:
The kernel and user space are the two fundamental parts that make up Linux's architecture.
Low-level features including process control, memory management, device drivers, and security
are offered by the Linux kernel. By controlling the system's resources and providing
communication between the hardware and applications, it serves as a bridge between the
hardware and the software.
A complete operating system environment is provided by the user space, which sits on top of
the kernel and is made up of a variety of software components and libraries that communicate
with the kernel. Utilities, libraries, shells, and graphical user interfaces (GUIs) are all
components of the user space, which enables efficient user interaction with the system.
3.1. Open Source Nature: Since Linux is an open-source operating system, anyone can access its
source code without charge. This transparency enables developers to examine, edit, and share
the code, leading to ongoing development, security upgrades, and quick bug patches.
3.2. Stability and Reliability: The dependability and stability of Linux are well known. Because
of its strong architecture and developed design, it can function for long stretches of time
without needing to be rebooted and manage heavy workloads. When stability is crucial, Linux
systems are frequently utilized in critical infrastructure like servers and embedded systems.
3.3. Security: In addition to process isolation, user-based file permissions, and the ability to run
on secure hardware designs, Linux offers increased security capabilities. Linux is a popular
choice for people and organizations who are concerned about security because of its open-
source nature, which enables speedy identification and patching of flaws.
3.4. Flexibility and Customizability: Linux offers consumers a great deal of flexibility and
customization. Users can select and install only the essential components thanks to the
modular design, creating systems that are light and efficient. In order to create a customized
computing experience, users can choose from a variety of desktop environments, window
managers, and software applications.
3.5. Scalability: Linux is highly scalable and can be configured to run on a variety of hardware
platforms, including powerful servers, supercomputers, embedded systems, and mobile
devices. Due to its scalability, it may be used with a variety of hardware and software.
3.6. Multitasking and Multithreading: Linux enables multitasking, enabling the execution of
many tasks simultaneously. Additionally, it has strong multithreading capabilities that enable
effective use of multi-core processors and enhance overall system performance.
4. Linux Distributions:
ARCHITECTURE OF LINUX
Linux architecture has the following components:
Figure 10.2.1 Linux architecture (This image needs to be shown in the background of video))
1. Kernel: The kernel is the foundation of an operating system built on Linux. It virtualizes
the computer's shared hardware resources to provide each process access to its own
virtual resources. This gives the impression that the process is the only one active on the
computer. Additionally, the kernel is in charge of avoiding and resolving conflicts
between various programmes.
Monolithic Kernel
Hybrid kernels
Exo kernels
Micro kernels
2. System Library: of functions that are used to implement the operating system's
capabilities.
3. Shell: The intricacy of the kernel's functions are concealed from users through this
interface to the kernel. It accepts user commands and carries out kernel operations.
4. Hardware Layer: This layer consists all peripheral devices like RAM/ HDD/ CPU etc.
Advantages of Linux
● Linux's primary benefit is that it is an open-source operating system. This indicates that
the source code is easily accessible to everyone and that you are free to contribute,
change, and share the code with anyone without obtaining any special authorizations.
● Linux is the safest operating system out there in terms of security. Although there is
malware for Linux, it is less vulnerable than any other operating system. This does not
imply that Linux is completely secure. Therefore, no anti-virus software is needed.
● Linux offers simple and regular software updates.
● There are numerous Linux distributions available, and you can use any of them
depending on your needs or preferences.
● On the internet, Linux can be used without charge.
● The community as a whole supports it.
Disadvantages of Linux
To that, I say YES. Ubuntu is a free open-source operating system and a Linux distribution,
whereas Linux is a family of open-source operating systems built on the Debian kernel. This is
the main distinction between the two operating systems. Or, to put it another way, Ubuntu is a
distribution of Linux, the operating system as a whole. Ubuntu was created by Canonical Ltd.
and released in 2004 whereas Linux was created by Linus Torvalds and released in 1991.
KEY TAKEAWAY
1. Kernel: The fundamental building block of the operating system is the Linux kernel. It serves as a
bridge between hardware and software, controlling system resources, offering fundamental
functions, and promoting interaction between various parts. It manages networking, device
drivers, filesystems, memory management, and scheduling of processes.
2. Shell: The shell is an operating system command-line interface that enables users to
communicate with it. It provides access to many system functions and programmes by
interpreting user commands and carrying them out. Linux users frequently use the Bourne Again
SHell (Bash), Zsh (Z shell), and Tcsh (TENEX C shell) shells.
3. Filesystem: Files and folders are organized and managed by the Linux filesystem. It offers a
hierarchical structure for the storage and access of files. Ext4, XFS, Btrfs, and other file system
formats are supported by Linux. The filesystem also includes components like access control,
symbolic links, and file permissions.
4. Process Management: Linux supports running several processes at once. The conception,
planning, execution, and termination of processes are all handled by the process management
component. It allocates memory, controls system resources, and enforces process priorities.
Process management is done using the system commands fork(), exec(), and kill().
5. Device Drivers: Device drivers allow the Linux kernel to connect with hardware components
such input/output devices, graphics cards, storage devices, and network adapters. They offer an
abstraction layer that enables interaction between the kernel and devices via common
interfaces and protocols.
6. Networking: Linux's networking module enables system to system communication over
networks. It features network stack functions and protocols like TCP/IP, UDP, and ICMP.
These are yet a few of the main Linux system components. Due to its modular structure, which enables
flexibility and customization, Linux can be used in a variety of applications and contexts. Because Linux is
an open-source operating system, community contributions and ongoing development are encouraged,
creating a powerful and feature-rich operating system.
1. Process Management: The kernel controls processes, which are active instances of executing
programmes. It assures fair and effective execution by scheduling and allocating system
resources (such CPU time, memory, and I/O devices) to processes according to their priority. The
kernel is also in charge of starting, stopping, and switching contexts for processes.
2. Memory Management: The kernel is responsible for allocating and releasing memory to
processes as needed. Because of the virtual memory features it offers, each process can have its
own, isolated address space. To effectively employ the physical memory resources available, the
kernel manages memory protection, paging, swapping, and demand paging.
3. Device Management: The kernel coordinates the communication of hardware and software
devices as well as the management of device drivers. By removing the focus from the physical
details, it offers a consistent interface for accessing devices. The startup, setup, and control of
devices are handled by the kernel, ensuring correct data transit between devices and processes.
4. Filesystem Management: The filesystem layer offered by the kernel enables the arranging,
establishing, modifying, and erasing of files and directories. It manages access control,
permissions, and file metadata. To enhance performance and offer dependable storage, the
kernel controls file I/O operations, caching, and buffering.
5. Networking: The kernel offers networking features like sockets, device drivers, and network
protocols. It takes care of packet forwarding, network stack implementation, routing, and
network setup. The kernel controls network interfaces and handles data transmission to make it
possible for processes to communicate with one another over networks.
6. Interrupt Handling: Timer interrupts, I/O interrupts, and hardware exceptions are among the
interrupts handled by the kernel. It prioritises and handles interruptions, providing prompt
action and appropriate event management. The kernel's interrupt handlers control interrupt-
driven operations and coordinate with other parts.
7. Security and Access Control: To safeguard the system and its resources, the kernel imposes
security regulations and implements access control systems. It controls user identification,
permissions, and authorization. To maintain system integrity and guard against unauthorized
access, the kernel manages user and group administration, file permissions, and security
modules.
These are a few of the fundamental tasks that the kernel carries out. The operating system's main
component, the kernel, manages resources, offers services, and ensures the smooth operation of the
system as a whole.
KEY TAKEAWAY
➢ The Linux operating system consists of several essential components that collectively
provide a robust and versatile computing environment.
➢ The kernel acts as the foundation, managing system resources, process control,
memory, and device drivers.
➢ The shell, a command-line interface, facilitates user interaction by interpreting
commands.
➢ Other crucial components include the filesystem for organizing files, process
management for multitasking, device drivers for hardware communication, networking
for system communication, system libraries for common features, command-line
utilities, and graphical user interfaces.
➢ Linux also incorporates package management, security mechanisms, virtualization
technologies, system logs, printing subsystems, and various build systems.
➢ The modular structure of Linux enables flexibility, customization, and application in
diverse contexts.
➢ The kernel, as the core component, plays key roles in process management, memory
allocation, device coordination, filesystem management, networking, interrupt handling,
security, access control, and system calls, ensuring the smooth operation of the entire
system.
BASIC COMMANDS
BASIC COMMANDS
The cd command is used to change the current directory (i.e., the directory in which the user is currently
working)
Syntax :
ls [Options] [file/dir]
Example:
Syntex :
Example
Syntax :
Example :
Syntax :
Example :
Syntax :
Example :
If you take a detailed look after running the clear command, you will find that it doesn’t really clear the
terminal. The tool just shifts the text upwards, out of the viewable area.
Syntax :
clear
8. cat Command
Syntax :
Example:
After inserting content you can use ctrl+c to exit the file.
Read the contents of file.txt and write them to newfile.txt, overwriting anything newfile.txt previously
contained. If newfile.txt does not exist, it will be created.
it will read the contents of file1.txt and file2.txt and display the result in the terminal.
it will concatenate the contents of file1.txt and file2.txt and write them to a new file combinedfile.txt
using the (>) operator.
If the combinedfile.txt file doesn’t exist the command will create it. Otherwise it will overwrite the file.
It prints the current working directory name with the complete path starting from root (/).
Syntax :
pwd [- option ]
Example :
It display the users that are currently logged into your Unix computer system.
Syntax :
Example :
This command prints the username associated with the current effective user ID.
Syntax :
whoami [-option ]
Example :
Syntax :
Example :
Syntax :
Example :
Syntax :
Example :
Syntax :
cp [option ] source destination/directory
Example:
Syntax :
mv [options] source dest
Example :
Syntax:
rm [option] filename
Example:
The cut command extracts a given number of characters or columns from a file.
Example:
The paste command displays the corresponding lines of multiple file side by side.
Syntax:
Example :
20. wc Command
It is used to find out the number of newline count, word count,byte and characters count in a files
specified by the file arguments.
Syntax:
wc [options] filenames
KEY TAKEAWAY
OPERATORS
1. Arithmetic Operators
Certainly! Here's a table depicting the arithmetic operators commonly used in shell
programming, particularly in Unix-like shells like Bash:
In shell programming, arithmetic operations are often performed within double parentheses
`$((...))` or using the `expr` command. However, please note that the `**` operator for
exponentiation operator isn't directly supported in basic shell arithmetic. You might need to
use external tools or functions to perform exponentiation.
Keep in mind that shell arithmetic is typically integer-based, so the division operator `/`
performs integer division, and the result is truncated to an integer. If you need floating-point
division or more advanced mathematical operations, you might need to use external tools like
`bc` or perform calculations in higher-level programming languages.
These operators are used to compare numerical values within conditional statements like `if`,
`while`, and other control structures in shell scripts. Make sure to enclose the expressions
involving these operators within square brackets `[ ]` and provide appropriate spacing.
In these operators, make sure to properly quote the strings within square brackets `[ ]` to
ensure that they are treated as single entities and to prevent word splitting and globbing. Also,
note that the `<` and `>` operators might need to be escaped or enclosed in double quotes to
avoid misinterpretation by the shell.
Remember, the comparison using these operators is based on lexicographical (ASCII) order for
strings.
4. Boolean operators
These operators are often used within conditional statements like `if`, `while`, and other control
structures to combine multiple conditions and create more complex logical expressions. The `-
a` and `-o` operators can also be replaced by the `&&` and `||` operators, respectively.
Remember to properly enclose conditions in parentheses or square brackets as required and to
use correct spacing for these operators.
These operators are used to evaluate conditions involving string values within shell scripting.
The `-n` and `-z` operators are specifically used to check if a string is not empty and if it's empty,
respectively. The `=` and `!=` operators are used for string equality and inequality comparisons.
Remember to properly quote strings within square brackets `[ ]` to ensure accurate evaluation
and to prevent issues with word splitting and globbing.
“EXPR” COMMAND
The `expr` command in shell programming is used for evaluating expressions, performing
arithmetic operations, and performing string comparisons. It's a command-line utility that helps
**Arithmetic Operations:**
**String Length:**
**String Comparison:**
Keep in mind that while the `expr` command can be useful for basic operations, for more
complex expressions and calculations, you might consider using shell arithmetic (`$((...))`) or
even external tools like `bc` for precision arithmetic or languages like Python for more
advanced operations.
IF STATEMENTS
In shell programming, `if` statements are used to make decisions based on the evaluation of
conditions. They allow you to execute specific code blocks depending on whether a certain
condition is true or false. Here's the basic syntax of an `if` statement in shell scripting:
- The condition is enclosed in square brackets `[ ]`. You can use various operators for
comparison, and it's a good practice to enclose variables in double quotes within the condition
to handle cases with spaces and special characters.
- The `then` keyword indicates the start of the code block to execute if the condition is true.
- The code within the `if` block is executed if the condition is true.
- The `else` keyword indicates the start of the code block to execute if the condition is false.
- The code within the `else` block is executed if the condition is false.
Remember to properly format your script with correct spacing and indentation for clarity. You
can also use nested `if` statements and combine them with other control structures like loops
for more complex logic in your shell scripts.
IF ELSE STATEMENTS
You can also add more conditions and nesting to handle different cases:
In this extended example, the script first checks if `x` is equal to 10. If not, it checks if `x` is
greater than 10. If neither condition is true, it defaults to the `else` block and indicates that `x`
is less than 10.
In this script:
- If true, it enters the nested `if` statement to check if `y` is equal to 20.
- If both conditions are true, it prints "Both x and y are 10 and 20".
This demonstrates how you can use nested `if`-`else` statements to handle more intricate
conditions step by step. However, it's important to keep your code organized and maintain
readability, especially when dealing with deep levels of nesting.
IF-ELSE-IF LADDER
In this script:
1. The first `if` statement checks if the `grade` is greater than or equal to 90.
2. The second `elif` statement checks if the `grade` is greater than or equal to 80.
3. The third `elif` statement checks if the `grade` is greater than or equal to 70.
4. The fourth `elif` statement checks if the `grade` is greater than or equal to 60.
5. The `else` block is reached if none of the conditions in the `if` and `elif` statements are true. It
prints "Grade: F".
An if-else-if ladder allows you to handle various cases in a structured manner. However, keep in
mind that the conditions are evaluated sequentially, so once a condition is satisfied, the
subsequent conditions are not checked.
CASE STRUCTURE
Certainly! The `case` statement in shell programming is used to perform multiway branching
based on the value of a variable. It's an alternative to using a long sequence of `if`-`elif`
statements when you want to match a variable's value against multiple possible values. Here's
an example with explanations:
1. The `case` statement starts with the value of the variable `$fruit`.
2. Each possible value is followed by `)` and a block of code delimited by `;;`. This block is
executed if the variable's value matches the given value.
3. The last `)` before `*)` is used to match any value that didn't match the previous cases.
4. `*)` represents the default case. If none of the cases match, this block of code is executed.
It's an apple.
It's a banana.
The `case` statement makes code more readable when you have multiple possible values to
match against. It's especially useful when you need to compare a variable to different values
and execute different actions based on the match.
REPETITION CONSTRUCTS
Repetition constructs, also known as loops, are essential in shell programming to execute a
certain block of code repeatedly. Shell scripts use loops to iterate through lists of items,
perform actions a specified number of times, or until a certain condition is met.
1. While Loop
Certainly! A `while` loop is a fundamental construct in shell programming that allows you to
repeatedly execute a block of code as long as a certain condition remains true. Here's how a
`while` loop works, along with an example for explanation:
3. `[ $count -le 5 ]` is the condition being evaluated. It checks whether the value of `count` is
less than or equal to 5.
4. The `do` keyword indicates the beginning of the loop's code block.
The loop continues to run as long as the condition `[ $count -le 5 ]` remains true. In each
iteration, it prints the value of `count` and then increments it. The loop stops when `count`
reaches 6, as 6 is not less than or equal to 5.
Once the condition becomes false, the loop terminates, and the script execution continues with
the next command after the loop. This demonstrates the basic structure and operation of a
`while` loop in shell programming.
2. Until Loop
Certainly! An `until` loop is similar to a `while` loop in shell programming, but it repeats a block
of code as long as a condition remains false. This is in contrast to a `while` loop, which repeats
as long as a condition remains true. Here's how an `until` loop works, along with an example for
explanation:
3. `[ $count -gt 5 ]` is the condition being evaluated. It checks whether the value of `count` is
greater than 5.
4. The `do` keyword indicates the beginning of the loop's code block.
The loop continues to run as long as the condition `[ $count -gt 5 ]` remains false. In each
iteration, it prints the value of `count` and then increments it. The loop stops when `count`
becomes greater than 5.
3. For … in Loop
Certainly! The `for` loop in shell programming is used to iterate over a list of items, performing
a set of actions for each item in the list. Here's how a `for` loop works, along with an example
for explanation:
In this example:
1. `fruits=("apple" "banana" "orange" "grape")` initializes an array called `fruits` containing four
different fruit names.
3. `fruit` is a variable that will hold the current item from the list in each iteration.
4. `in "${fruits[@]}"` specifies the list of items to iterate over. The `"${fruits[@]}"` syntax
expands the array elements as separate items.
5. The `do` keyword indicates the beginning of the loop's code block.
6. `echo "Fruit: $fruit"` prints the current value of the `fruit` variable.
The loop iterates over each element in the `fruits` array and prints the name of each fruit.
4. Select Loop
The `select` loop is a special construct in shell programming, specifically used to create simple
menus in interactive shell scripts. It prompts the user to choose an option from a list, allowing
them to make a selection by entering a number corresponding to the options. Here's how a
`select` loop works, along with an example for explanation:
1. `options=("Option 1" "Option 2" "Option 3" "Quit")` initializes an array called `options` with
four menu options.
2. `PS3="Select an option: "` sets the prompt string that will be displayed before each menu
iteration.
6. The `do` keyword indicates the beginning of the loop's code block.
7. The `case` statement matches the value of `$REPLY`, which corresponds to the user's input.
8. `1)`, `2)`, and `3)` represent the options numbered 1, 2, and 3 respectively.
10. Inside each case, the script provides the appropriate response.
11. `break` is used to exit the loop when the "Quit" option is selected.
When the user selects an option by entering a number, the corresponding code block is
executed. For instance, if the user selects option 2, the output will be:
The `select` loop continues to prompt the user until they select the "Quit" option. It's a great
way to create interactive menus in shell scripts for tasks that require user input.