OS Book 2025
OS Book 2025
CHAPTER 1
BASICS CONCEPT
INTRODUCTION
The operating system is a program that can play a middle role between
hardware and software. An operating system (OS) exploits the hardware resources
of one or more processors to provide a set of services to system users. An operating
system serves as an interface between the program and various computer hardware
or software components. The operating system is made to control all of the
computer's resources and activities. It is an entirely integrated collection of
specialized applications that manage all of the computer's functions.
Page 1 of 159
Operating System
• Input/Output: This layer manages all physical interactions with other devices,
including the keyboard, printer, display, and disc drive. The I/O layer receives a
request if a higher layer needs access to a device.
• File Management System: It also goes by the name "file system." It is in
charge of planning and overseeing the data storage on long-term storage devices
including hard drives, and floppy disc drives.
• User Interface: It is referred to as the area where human and machine
interaction takes place. There are two different types of user interfaces: the icon-
based Graphical User Interface (GUI), which is used in Windows and Apple Mac
OS, and the text-based Command Line Interface (CLI), which is used in MS-DOS
and LINUX.
Page 2 of 159
Operating System
Page 3 of 159
Operating System
Page 4 of 159
Operating System
Page 5 of 159
Operating System
• Layers are independent of each other. This means that a change to one layer
should not affect the other layers.
Below is the Image illustrating the Layered structure in OS:
Page 6 of 159
Operating System
Advantages
• It allows the system to run multiple programs simultaneously.
• Beneficial for tasks that need to use all of the processor’s resources, such as
games, scientific calculations, and financial simulations.
Disadvantages: They require additional hardware, such as processors and
memory, making a system more expensive.
Page 7 of 159
Operating System
Page 8 of 159
Operating System
Advantages
• Allows companies to scale their computing resources to handle increased
demand without having to buy new hardware.
• Client-server systems can be quickly reconfigured to meet the changing
needs of an organization.
• They are also more reliable and easier to maintain than dedicated server
systems.
• Lower operating cost.
• More reliable and easier to maintain than dedicated server systems
Disadvantages
• These OS need more sophisticated management and networking
technologies, longer startup times, and increased vulnerability to attack.
• Less secure than dedicated server systems.
• More challenging to scale than dedicated server systems.
Batch OS: There are different users, and each user prepares their work in a
standalone device, such as punch cards in batch operating systems and sends
them to a computer operator. The various systems split and distribute similar tasks
in batches to facilitate computing and faster responses. A single operator takes
similar jobs with similar needs and requirements and then groups them into
various batches. Similar kinds of jobs that share similar needs and requirements.
These types of operating systems are not used nowadays.
Page 9 of 159
Operating System
Advantages
• The overall time the system takes to execute all the programs will be
reduced.
• Less time to execute all programs.
• These operating systems are shared between multiple users.
• Suitable for small-scale businesses.
• It can work in offline mode also.
• It can give specific time to the computer, and when a computer is idle can
process the jobs.
Disadvantages
• Sometimes, manual interventions are required between two batches.
• The CPU utilization is low because the time taken in loading and unloading
batches is very high compared to execution time.
• Sometimes, jobs enter into an infinite loop due to some mistake.
• Meanwhile, if one job takes too much time, other jobs must wait.
Thus, the diverse types of operating systems (OS) serve as the backbone of
computer technology, each catering to different requirements and user
preferences.
Page 10 of 159
Operating System
OS helps in:
• Creating a file: The operating system provides a graphical user interface or
command-line interface that allows users to create new files. In a graphical
user interface-
• You can right-click on a folder or desktop and select “New”
• Choose the type of file you want to create, such as a text file or a Microsoft
Word document. Alternatively, you can use a command-line interface and
type commands to create files.
• Editing a file: Once a file has been created, you can use various tools, such as
a word processor and applications, provided by the operating system, to edit it.
• Updating a file: The operating system provides the facility to edit the file and
also tracks changes made to the file and updates the file metadata accordingly.
• Deleting a file: The operating system allows you to delete the file you no longer
need. The OS moves the file to the recycle bin or trash folder, which can be
restored if necessary, or permanently deletes it from the storage device.
Page 11 of 159
Operating System
• Allocates resources such that the system doesn’t run out of resources.
• Offering mechanisms for process synchronization.
• Helps in process communication(inter-communication).
Page 12 of 159
Operating System
CHARACTERISTICS OF MODERN OS
An operating system is a fundamental piece of software that manages
computer hardware resources and provides services for running applications.
Modern operating systems have evolved significantly over the years and have a
plethora of features that enhance their functionality, security, and usability.
The characteristics of modern operating systems that make them reliable,
efficient, and user-friendly are given below.
Object-Oriented Design: An object-oriented operating system (OOOS) is an
operating system that is designed and built using the principles of object-oriented
programming (OOP).
In an OOOS, the operating system is viewed as a collection of objects, each
representing a different aspect of the system. For example, there may be objects for
processes, files, devices, and users. These objects encapsulate their respective data
and behaviour and interact with each other through well-defined interfaces.
Multitasking and Multithreading: Multitasking is the ability of an operating
system to run multiple programs or processes at the same time, allowing users to
switch between them seamlessly.
In a multitasking environment, the operating system allocates CPU time to
each program or process in small time slices, allowing each program to run for a
short period before switching to the next program. This gives the illusion of
multiple programs running simultaneously, even though only one program is
running at any given moment.
Multithreading, on the other hand, is the ability of a program to perform
multiple tasks or subtasks simultaneously within a single process. In a
multithreaded program, each thread can execute a separate set of instructions
simultaneously, allowing the program to perform multiple tasks concurrently. This
can improve program performance and responsiveness, particularly for applications
that require heavy processing or input/output operations.
The combination of multitasking and multithreading allows modern
operating systems to efficiently manage system resources and run multiple
programs or processes simultaneously. This enables users to perform multiple
tasks at once and allows for better utilization of system resources such as CPU
time and memory.
Symmetric Multiprocessing: Symmetric multiprocessing (SMP) is a type of
multiprocessing architecture in which two or more identical processors are
connected to a single shared main memory and run a single operating system. In
an SMP system, each processor can access any memory area and perform any
system task, such as running applications or managing input/output operations.
SMP systems are commonly used in servers, high-performance computing
clusters, and desktop computers with multiple processors. In these systems, the
workload can be divided among the available processors, allowing for improved
performance and faster execution of tasks.
Page 13 of 159
Operating System
MICROKERNEL ARCHITECTURE
A microkernel is a type of operating system kernel that is designed to provide
only the most basic services required for an operating system to function, such as
memory management and process scheduling. Other services, such as device
drivers and file systems, are implemented as user-level processes that
communicate with the microkernel via message passing (IPC i.e. inter-process
communication). This design allows the operating system to be more modular and
flexible than traditional monolithic kernels, which implement all operating system
services in kernel space. Microkernel Example: MINX (mini-UNIX), QNX, etc.
Salient Features
1. Modularity: Microkernels are designed with a modular structure, separating
the core functionalities into small, independent components which is easier to
add remove or update without affecting the entire system.
Page 14 of 159
Operating System
PROCESS MANAGEMENT
A process is a program in execution. For example, when we write a program
in C or C++ and compile it, the compiler creates binary code. The original code and
binary code are both programs. When we run the binary code, it becomes a
process. A process is an ‘active’ entity instead of a program, which is considered a
‘passive’ entity. A single program can create many processes when run multiple
times; for example, when we open a .exe or binary file multiple times, multiple
instances begin (multiple processes are created).
Page 15 of 159
Operating System
Page 16 of 159
Operating System
• Process State: A process can be in several states, some of them are ready,
suspend wait, and running.
• Process Control Block: The PCB is a data structure that contains information
related to a process. These blocks are stored in the process table.
• Resources: Processes request various types of resources such as files,
input/output devices, and network connections. The OS manages the allocation
of these resources.
• Priority: Each process has a scheduling priority. Higher-priority processes are
given preferential treatment and they receive more CPU time compared to lower-
priority processes.
• Execution Context: Each process has its execution context, which includes the
address of the next instruction to be executed, stack pointer, and register
values.
PROCESS STATES
These are the states in which a process might go during its execution. Let us
know more about them:
Page 17 of 159
Operating System
• Suspended: this state shows that the process is ready but it has not been
placed in the ready queue for execution.
OPERATIONS ON PROCESS
In an operating system, processes represent the execution of individual tasks
or programs. Process operations involve the creation, scheduling, execution, and
termination of processes. The OS allocates necessary resources, such as CPU time,
memory, and I/O devices, to ensure the seamless execution of processes. Process
operations in OS encompass vital aspects of process lifecycle management,
optimizing resource allocation, and facilitating concurrent and responsive
computing environments.
Page 18 of 159
Operating System
5. Initialization: Any initializations required for the process are performed at this
stage. This might involve initializing variables, setting default values, and
preparing the process for execution.
6. Process State: After the necessary setup, the new process is typically in a
"ready" or "waiting" state, indicating that it is prepared for execution but hasn't
started running yet.
Page 19 of 159
Operating System
1. I/O Operations: When a process requests data from an I/O device (such as
reading data from a disk or receiving input from a keyboard), it may be blocked
until the requested data is ready.
2. Synchronization: Processes often wait for synchronization primitives like
semaphores or mutexes to achieve mutual exclusion or coordinate their
activities.
3. Inter-Process Communication: Processes waiting for messages or data from
other processes through mechanisms like message queues or pipes may enter a
blocked state.
4. Resource Allocation: Processes requesting system resources, such as memory
or network connections, may be blocked until the resources are allocated.
Page 20 of 159
Operating System
the currently running process is pre-empted, and the OS selects the next
process to run.
3. Interrupt-Driven Preemption: Hardware or software interrupts can trigger
pre-emption. For example, an interrupt generated by a hardware device or a
system call request from a process may cause the OS to pre-empt the current
process and handle the interrupt.
4. Fairness and Responsiveness: Preemption ensures that no process is unfairly
blocked from accessing CPU time. It guarantees that even low-priority
processes get a chance to execute, preventing starvation.
5. Real-Time Systems: Preemption is crucial in real-time systems, where tasks
have strict timing requirements. If a higher-priority real-time task becomes
ready to run, the OS must pre-empt lower-priority tasks to ensure timely
execution.
CONCURRENT PROCESS
Concurrency in operating systems refers to the ability of an OS to manage
and execute multiple tasks or processes simultaneously. It allows multiple tasks to
Page 21 of 159
Operating System
Page 22 of 159
Operating System
• Race Conditions: They occur when multiple threads or processes access shared
resources simultaneously without proper synchronization. In the absence of
synchronization mechanisms, race conditions can lead to unpredictable
behaviour and data corruption. This can result in data inconsistencies,
application crashes, or even security vulnerabilities if sensitive data is involved.
• Deadlocks: A deadlock arises when two or more processes or threads become
unable to progress as they are mutually waiting for resources that are currently
held by each other. This situation can bring the entire system to a standstill,
causing disruptions and frustration for users.
• Priority Inversion: Priority inversion occurs when a lower-priority task
temporarily holds a resource that a higher-priority task needs. This can lead to
delays in the execution of high-priority tasks, reducing system efficiency and
responsiveness.
• Resource Starvation: Resource starvation occurs when some processes are
unable to obtain the resources they need, leading to poor performance and
responsiveness for those processes. This can happen if the OS does not manage
resource allocation effectively or if certain processes monopolize resources.
Page 23 of 159
Operating System
PROCESS THREADS
In computers, a single process might have multiple functionalities running
parallelly where each functionality can be considered as a thread. Each thread has
its own set of registers and stack space. There can be multiple threads in a single
process having the same or different functionality. Threads in operating systems
are also termed lightweight processes.
Thread is a sequential flow of tasks within a process. Threads in an
operating system can be of the same or different types. Threads are used to
increase the performance of the applications.
Each thread has its own program counter, stack, and set of registers.
However, the threads of a single process might share the same code and
data/file. Threads are also termed lightweight processes as they share common
resources.
E.g.: While playing a movie on a device the audio and video are controlled by
different threads in the background.
Types of Thread
User Level Thread: User-level threads are implemented and managed by the
user and the kernel is not aware of it. User-level threads are implemented using
user-level libraries and the OS does not recognize these threads. User-level threads
are faster to create and manage compared to kernel-level threads. If one user-level
Page 24 of 159
Operating System
thread performs a blocking operation then the entire process gets blocked.
E.g.: POSIX threads, Java threads, etc.
User Level Thread is a type of thread that is not created using system calls.
The kernel has no work in the management of user-level threads. User-level
threads can be easily implemented by the user. In case when user-level threads are
single-handed processes, kernel-level thread manages them. Let’s look at the
advantages and disadvantages of User-Level Thread.
Advantages of User-Level Threads
• Implementation of the User-Level Thread is easier than Kernel Level Thread.
• Context Switch Time is less in User Level Thread.
• User-Level Thread is more efficient than Kernel-Level Thread.
• Because of the presence of only Program Counter, Register Set, and Stack
Space, it has a simple representation.
Disadvantages of User-Level Threads
• There is a lack of coordination between Thread and Kernel.
• In case of a page fault, the whole process can be blocked.
The above diagram shows the functioning of user-level threads in user space
and kernel-level threads in kernel space.
A kernel-level thread is a type of thread that can recognize the Operating
system easily. Kernel Level Threads has its thread table which it keeps track of the
system. The operating System Kernel helps in managing threads. Kernel Threads
have somehow longer context switching time. Kernel helps in the management of
threads.
Page 25 of 159
Operating System
Advantages of Threading
• Threads improve the overall performance of a program.
• Threads increase the responsiveness of the program
• Context Switching time in threads is faster.
• Threads share the same memory and resources within a process.
• Communication is faster in threads.
• Threads provide concurrency within a process.
• Enhanced throughput of the system.
• Since different threads can run parallelly, threading enables the utilization
of the multiprocessor architecture to a greater extent and increases
efficiency.
1. Creation: The first stage in the lifecycle of a thread is its creation. In most
programming languages and environments, threads are created by
instantiating a thread object or invoking a thread creation function. During
creation, you specify the code or function that the thread will execute.
2. Ready/Runnable: After a thread is created, it enters the "ready" or
"runnable" state. In this state, the thread is ready to run, but the operating
system scheduler has not yet selected it to execute on the CPU. Threads in the
ready state are typically waiting for the scheduler to allocate CPU time to them.
Page 26 of 159
Operating System
3. Running: When the scheduler selects a thread from the pool of ready threads
and allocates CPU time to it, the thread enters the "running" state. In this
state, the thread's code is being executed on the CPU. A running thread will
continue to execute until it either voluntarily yields the CPU (e.g., through sleep
or wait operations) or is pre-empted by a higher-priority thread.
4. Blocked/Waiting: Threads can enter the "blocked" or "waiting" state when
they are waiting for some event to occur, such as I/O operations,
synchronization primitives (e.g., locks or semaphores), or signals from other
threads. When a thread is blocked, it is not eligible to run until the event it is
waiting for occurs.
5. Termination: Threads can terminate either voluntarily or involuntarily.
Voluntary termination occurs when a thread completes its execution or
explicitly calls a termination function. Involuntary termination can happen due
to errors (e.g., segmentation faults) or signals received from the operating
system.
6. Dead: Once a thread has terminated, it enters the "dead" state. In this state,
the thread's resources (such as memory and handles) are deallocated, and it no
longer exists as an active entity in the system. Dead threads cannot be
restarted or resumed.
MULTITHREADING
The term multithreading means a process and a thread. Process means a
program that is being executed. Processes are further divided into independent
units also known as threads, also known as collections of threads. It is a process
that is small and lightweight residing inside a process.
Page 27 of 159
Operating System
For example, client 1, client 2, and client 3 in the above example are
accessing the web server without having to wait for other tasks to be completed.
The threads in this are divided into user-level and kernel-level threads. The
user-level thread is used for handling an independent form of the kernel-level
thread without any support from the kernel. On the other hand, the kernel-level
threads are directly managed by the operating system.
Examples of Multithreading Operating Systems: Multithreading is widely
used by applications. Some of the applications are processing transactions
like online bank transfers, recharge, etc.
For instance, in the banking system, many users perform day-to-day
activities using bank servers like transfers, payments, deposits, `opening a new
account, etc. All these activities are performed instantly without having to wait for
another user to finish.
In this, all the activities get executed simultaneously as and when they arise.
This is where multithreading comes into the picture, wherein several threads
perform different activities without interfering with others.
Advantages:
• Multithreading allows the CPU to execute multiple tasks simultaneously, which
can boost performance.
• Multithreading reduces the amount of time that is spent waiting for a task to
finish.
• Multithreading can help to improve the scalability of a program.
• Interactive applications may allow a program to continue running even if part of
it is blocked or is performing a lengthy operation, thereby increasing
responsiveness.
Disadvantages of multi-threading
• Multithreading can be complex and challenging to implement.
• Multithreading can increase the complexity of a program.
• Multithreading can be error-prone.
• Programmers must carefully design their code to utilize multithreading
capabilities without introducing unwanted delays or fragmentation into their
programs’ execution.
Process Vs. Thread: Process simply means any program in execution while
the thread is a segment of a process. The main differences between process and
thread are mentioned below:
Process Thread
Processes use more resources and
Threads share resources and hence
hence they are termed as heavyweight
they are termed lightweight processes.
processes.
Creation and termination times of Creation and termination times of
processes are slower. threads are faster compared to
Page 28 of 159
Operating System
processes.
Processes have their code and Threads share code and data/files
data/file. within a process.
Communication between processes is Communication between threads is
slower. faster.
Context Switching in processes is
Context switching in threads is faster.
slower.
Threads, on the other hand, are
Processes are independent of each
interdependent. (i.e. they can read,
other.
write or change another thread’s data)
E.g.: Opening two tabs in the same
E.g.: Opening two different browsers.
browser.
MICROKERNELS
A kernel is the most important part of an operating system. It manages the
resources of the system and also acts as an interface between hardware and the
computer application. A microkernel is one type of kernel. It manages all system
resources.
Microkernel in an operating system is one of the kernel’s classifications. In
the microkernel, the user service, as well as the kernel service, are all implemented
on different kernel spaces.
In the user address space, all the user services are kept while in the kernel
address space, all the kernel services are available. Thus, by doing this, the size of
the kernel as well as the size of an operating system is reduced.
It is used for communicating between applications or client programs and
services that are running in the address space of the user are established by
message passing, thereby reducing the speed of the microkernel. It also provides
minimal service of memory management and process.
Since the user service and kernel service are isolated from each other, the
operating system remains unaffected if any user service is failed as it does not
affect the kernel service. It can be extended if any new service is to be added as
they are added to the user space and no modification is needed in the kernel space.
It is secure, portable, and reliable.
Page 29 of 159
Operating System
Scheduling of CPU: It refers to which process will be executed next. All the
processes reside in a queue and are executed one at a time. There are levels of
priority in every process and the process that has the highest process is executed
first. It helps in the optimization and utilization of the CPU to the maximum by
utilizing the resources efficiently. It minimizes the waiting time, response, and
turnaround times.
Memory management: It is the process of allocating space in memory for
processes. Virtual memory is also created if the process has a size bigger than that
of the actual memory by which the memory is divided into portions and stored. All
the processes wait in memory before CPU execution.
Page 30 of 159
Operating System
• Zircon
CPU SCHEDULING
CPU scheduling is a process that allows one process to use the CPU while
the execution of another process is on hold (in a waiting state) due to the
unavailability of any resource like I/O etc, thereby making full use of the CPU. CPU
scheduling aims to make the system efficient, fast, and fair.
Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed. The selection process is carried out by
the short-term scheduler (or CPU scheduler). The scheduler selects from among the
processes in memory that are ready to execute and allocates the CPU to one of
them. There is essential 4 conditions under which CPU scheduling decisions are
taken:
1. If a process is making the switch between the running state to the waiting state
(could be for an I/O request, or invocation of wait () for terminating one of its
child processes)
2. If a process is making the switch from the running state to the ready state (on
the occurrence of an interrupt, for example)
3. If a process is making the switch between waiting and ready state (e.g. when
its I/O request completes)
4. If a process terminates upon completion of execution.
So, in the case of conditions 1 and 4, the CPU does not have a choice of
scheduling, if a process exists in the ready queue the CPU's response to this would
be to select it for execution. In cases 2 and 3, the CPU has a choice of selecting a
particular process for executing next. There are mainly two types of CPU
scheduling:
Page 31 of 159
Operating System
For Example: In the image above, we can see that all the processes were executed
in the order in which they appeared, and none of the processes were interrupted by
another, making this a non-preemptive, FCFS (First Come, First Served) CPU
scheduling algorithm. P2 was the first process to arrive (arrived at time = 0) and
was hence executed first. Let's ignore the third column for a moment, we'll get to
that soon. Process P3 arrived next (at time = 1) and was executed after the previous
process - P2 was done executing, and so on.
Some examples of non-preemptive scheduling algorithms are - Shortest Job
First (SJF, non-preemptive), and Priority scheduling (non-preemptive).
Page 32 of 159
Operating System
SCHEDULERS
A scheduler is a software that helps schedule the processes in an operating
system. It helps to keep all computer resources busy and allows multiple users to
share system resources effectively. Let’s go through different schedulers in an
operating system.
1. Long-term schedulers: The processes that are created are in the NEW state.
The programs are admitted to the RAM for execution. So, before execution, the
processes are put in the READY queue. So, do they get into the ready queue
themselves? Here comes the role of long-term schedulers (LTS). It is also called
a job scheduler. These schedulers select processes from secondary memory and
put them into the ready queue. LTS runs less frequently. The main aim of LTS
is to maintain the degree of multiprogramming. Multiprogramming means
executing multiple programs by a single processor. But not all processes
simultaneously. It means if one process is not executing for some reason, then
another process will get a chance to get executed. An optimal level of
multiprogramming means
The average rate of process = the average departure rate of processes getting
executed and leaving the system.
Page 33 of 159
Operating System
SCHEDULING METHODOLOGY
In different environments, different scheduling methodologies are needed.
This situation arises because different application areas (and different kinds of
operating systems) have different goals. In other words, what the scheduler should
optimize for is not the same in all systems. Three environments are
1. Batch.
2. Interactive.
3. Real time.
Batch systems are still in widespread use in the business world for doing
payroll, inventory, accounts receivable, accounts payable, interest calculation (at
banks), claims processing (at insurance companies), and other periodic tasks. In
batch systems, there are no users impatiently waiting at their terminals for a quick
response to a short request. Consequently, non-preemptive algorithms, or
preemptive algorithms with long periods for each process, are often acceptable.
This approach reduces process switches and thus improves performance. The
batch algorithms are fairly general and often applicable to other situations as well,
which makes them worth studying, even for people not involved in corporate
mainframe computing.
In an environment with interactive users, preemption is essential to keep
one process from hogging the CPU and denying service to the others. Even if no
process intentionally ran forever, one process might shut out all the others
indefinitely due to a program bug. Preemption is needed to prevent this behaviour.
Servers also fall into this category, since they normally serve multiple (remote)
users, all of whom are in a big hurry.
In systems with real-time constraints, preemption is, oddly enough,
sometimes not needed because the processes know that they may not run for long
periods and usually do their work and block quickly. The difference with interactive
Page 34 of 159
Operating System
systems is that real-time systems run only programs that are intended to further
the application at hand. Interactive systems are general purpose and may run
arbitrary programs that are not cooperative or even malicious.
Page 35 of 159
Operating System
FCFS
The full form of FCFS Scheduling is First Come First Serve Scheduling.
FCFS Scheduling algorithm automatically executes the queued processes
and requests in the order of their arrival. It allocates the job that first arrived in the
queue to the CPU, then allocates the second one, and so on. FCFS is the simplest
and easiest CPU scheduling algorithm, managed with a FIFO queue. FIFO stands
for First In First Out. The FCFS scheduling algorithm places the arriving
processes/jobs at the very end of the queue. So, the processes that request the
CPU first get the allocation from the CPU first. As any process enters the FIFO
queue, its Process Control Block (PCB) gets linked with the queue’s tail. As the CPU
becomes free, the process at the very beginning gets assigned to it. Even if the CPU
starts working on a longer job, many shorter ones have to wait after it. The FCFS
scheduling algorithm works in most of the batches of operating systems.
Examples of FCFS scheduling: Buying a movie ticket at the counter. This
algorithm serves people in the queue order. The first person in line buys a ticket,
then the next person, and so on. This process will continue until the last person in
line has purchased a ticket. This method mimics the CPU process.
Advantages of FCFS:
• Simplicity. Orders are fulfilled in order, simplifying scheduling and processing.
Orders are simply performed in chronological sequence.
• User friendly. Order scheduling and code writing are straightforward for team
members. Easy scheduling saves time and labour. It’s a foolproof technique that
also reduces errors.
• Easy to implement. FCFS's simplicity makes it easier to integrate into existing
systems. FCFS order scheduling can be deployed quickly and inexpensively into
any scheduling system your company uses. FCFS can be used soon after its
implementation.
Limitation of FCFS:
• Long waiting time. FCFS processes orders in order since it is non-preemptive.
This means a business order can start processing once the previous order has
been completed. A CPU-allocated process will never release it until it finishes. If
the initial order has a long burst time, orders following it must wait for
fulfilment, regardless of their burst times.
• Lower device usage. Simple FCFS is inefficient. Longer wait periods accompany
this. If the CPU is busy processing a long order, all other orders lie idle, causing
a backup. FCFS is particularly wasteful because the CPU can only process one
order at a time.
• CPU over I/O. FCFS emphasizes CPU over I/O. The algorithm is more CPU-
friendly than I/O-friendly. This may dissuade I/O system users.
Example:
Process Burst Time
P1 24
Page 36 of 159
Operating System
P2 3
P3 3
If the processes arrive in the order P1, P2, and P3, and are served in FCFS
order, we get the result shown in the following Gantt chart:
P1 P2 P3
0 24 27 30
The waiting time is 0 milliseconds for process P1, 24 milliseconds for process
P2, and 27 milliseconds for process P3. Thus, the average waiting time is (0 + 24 +
27)/3 = 17 milliseconds. If the processes arrive in the order P2, P3, and P1,
however, the results will be as shown in the following Gantt chart:
P2 P3 P1
0 3 6 30
The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds. This
reduction is substantial. Thus, the average waiting time under an FCFS policy is
generally not minimal and may vary substantially if the process's CPU burst times
vary greatly.
SJF
Shortest Job First (SJF) algorithm is also known as Shortest Job Next
(SJN) or Shortest Process Next (SPN). It is a CPU processes scheduling algorithm
that sorts and executes the process with the smallest execution time first, and then
the subsequent processes with the increased execution time. Both preemptive and
non-preemptive scheduling strategies are possible in the SJF scheduling algorithm.
In SJF, there is a significant amount of reduction in the average waiting time for
other processes that are waiting to be executed.
However, it can be quite challenging to estimate the burst time required for a
process, making it difficult to apply this technique to the operating system
scheduling process.
Page 37 of 159
Operating System
The burst time for a process can only be approximated or predicted. Our
approximations must be correct to get the most out of the SJF algorithm.
Numerous methods can be used to predict a process's CPU burst time.
There are two types of SJF methods:
• Non-Preemptive SJF
• Preemptive SJF
In non-preemptive scheduling, once the CPU cycle is allocated to the
process, the process holds it till it reaches a waiting state or is terminated.
In Preemptive SJF Scheduling, jobs are put into the ready queue as they
come. A process with the shortest burst time begins execution. If a process with
even a shorter burst time arrives, the current process is removed or preempted
from execution, and the shorter job is allocated a CPU cycle.
Advantages of SJF: These are some of the Advantages of the SJF algorithm:
• Shortest Job First (SJF) has a shorter average waiting time as compared to the
First Come First Serve (FCFS) algorithm.
• SJF can be applied to long-term scheduling.
• SJF is ideal for jobs that run in batches and whose run times are known.
• SJF is probably the best concerning the average turnaround time of a process.
RR
The Round-robin scheduling algorithm is a kind of preemptive first-come,
first-served CPU Scheduling algorithm in which each process in the ready state
gets the CPU for a fixed time cyclically (turn by turn). It is the oldest scheduling
algorithm and is mainly used for multitasking.
The round-robin scheduling algorithm is one of the CPU scheduling
algorithms in which every process gets a fixed amount of time to execute.
In this algorithm, every process gets executed cyclically. This means that
processes that have their burst time remaining after the expiration of the time
quantum are sent back to the ready state and wait for their next turn to complete
the execution until it terminates. This processing is done in FIFO order, suggesting
that processes are executed on a first-come, first-served basis.
Page 38 of 159
Operating System
2. At first, the burst time of every process is compared to the time quantum of the
CPU.
3. If the burst time of the process is less than or equal to the time quantum in the
round-robin scheduling algorithm, the process is executed to its burst time.
4. If the burst time of the process is greater than the time quantum, the process is
executed up to the time quantum (TQ).
5. When the time quantum expires, it checks if the process is executed completely
or not.
6. On completion, the process terminates. Otherwise, it goes back again to
the ready state.
Advantages
1. This round-robin algorithm offers starvation-free execution of processes.
2. Each process gets equal priority and fair allocation of CPU.
3. Round Robin scheduling algorithm enables the Context switching method to
save the states of preempted processes.
4. It is easily implementable on the system because round-robin scheduling in
OS doesn’t depend upon burst time.
Disadvantages
1. The waiting and response times are higher due to the short time slot.
2. Lower time quantum results in higher context switching.
3. We cannot set any special priority for the processes.
Example: Consider the following set of processes that arrive at time 0, with the
length of the CPU burst given in milliseconds:
P1 24
P2 3
P3 3
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Page 39 of 159
Operating System
quantum, that process is pre-empted and is put back in the ready queue. The RR
scheduling algorithm is thus pre-emptive.
PRIORITY SCHEDULING
Priority scheduling in OS is the scheduling algorithm that schedules
processes according to the priority assigned to each process. Higher-priority
processes are executed before lower-priority processes.
In priority scheduling in OS, processes are executed based on their priority.
The jobs/processes with higher priority are executed first. Naturally, you might
want to know how the priority of processes is decided. Priority of processes depends
on some factors such as:
• Time limit
• Memory requirements of the process
• Ratio of average I/O to average CPU burst time
There can be more factors based on which the priority of a process/job is
determined. This priority is assigned to the processes by the scheduler.
These priorities of processes are represented as simple integers in a fixed range
such as 0 to 7, or maybe 0 to 4095. These numbers depend on the type of system.
Advantages:
• High-priority processes do not have to wait for their chance to be executed due
to the current running process.
• We can define the relative importance/priority of processes.
• The applications in which the requirements of time and resources fluctuate are
useful.
Disadvantages:
• Since we only execute high-priority processes, this can lead to starvation of the
processes that have a low priority. Starvation is the phenomenon in which a
Page 40 of 159
Operating System
process gets infinitely postponed because the resources that are required by the
process are never allocated to it, since other processes are executed before it.
You can research more about starvation on Google.
• If the system eventually crashes, all of the processes that have low priority will
get lost since they are stored in the RAM.
Example: As an example, consider the following set of processes, assumed to have
arrived at time 0, in the order P1, P2, • •, P5, with the length of the CPU burst given
in milliseconds:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Using priority scheduling, we would schedule these processes according to
the following Gantt chart:
P2 P5 P1 P3 P4
0 1 6 16 18 19
The average waiting time is 8.2 milliseconds.
Priorities can be defined either internally or externally. Internally defined
priorities use some measurable quantity or quantities to compute the priority of a
process. For example, time limits, memory requirements, the number of open files,
and the ratio of average I/O burst to average CPU burst have been used in
computing priorities. External priorities are set by criteria outside the operating
system, such as the importance of the process, the type and amount of funds being
paid for computer use, the department sponsoring the work, and other, often
political, factors.
Page 41 of 159
Operating System
After all the processes are available in the ready queue, No-preemption will be done
and the algorithm will work the same as SJF scheduling. In the Process Control
Block, the context of the process is saved, when the process is removed from the
execution, and when the next process is scheduled. The PCB is accessed on the
next execution of this process.
Advantages of SRTF: The main advantage of the SRTF algorithm is that it
makes the processing of the jobs faster than the SJF algorithm, mentioned its
overhead charges are not counted.
Disadvantages of SRTF: In SRTF, the context switching is done a lot more times
than in SJN due to more consumption of the CPU's valuable time for processing.
The consumed time of the CPU then adds up to its processing time which then
diminishes the advantage of fast processing of this algorithm.
Example
At the 0th unit of the CPU, there is only one process which is P1, so P1 gets
executed for the 1-time unit. At the 1st unit of the CPU, Process P2 arrives. Now,
the P1 needs 6 more units to be executed, and the P2 needs only 3 units. So, P2 is
executed first by pre-empting P1. At the 3rd unit of time, the process P3 arrives,
and the burst time of P3 is 4 units which is more than the completion time of P2
which is 1 unit, so P2 continues its execution. Now after the completion of P2, the
burst time of P3 is 4 units which means it needs only 4 units for completion while
P1 needs 6 units for completion. So, this algorithm picks P3 above P1 due to the
reason that the completion time of P3 is less than that of P1. P3 gets completed at
time unit 8, there are no new processes arrived. So again, P1 is sent for execution,
and it gets completed at the 14th unit.
The arrival Time and Burst time for three processes P1, P2, and P3 are given
in the above diagram. Let us calculate Turnaround time, completion time, and
waiting time.
FRAGMENTATION
Fragmentation is an unwanted issue that occurs in an operating system in
which a process is unloaded and loaded from memory, causing the free memory
space to become fragmented. As a result, the process cannot be assigned to the
memory blocks due to their small size.
Page 42 of 159
Operating System
P1 10 units 15 units
P2 20 units 25 units
P3 30 units 35 units
Page 43 of 159
Operating System
when memory blocks, once allocated, are freed up, leading to 'holes' of unused
memory spread across the system.
The issue is that these 'holes' or blocks may not be large enough to satisfy
subsequent allocation requests, despite collectively having sufficient space.
Consequently, the system is unable to use this memory effectively, leading to
wasted resources and decreased efficiency. Consider this simple representation of
external fragmentation:
Block 1 Used
Block 2 Free
Block 3 Used
Block 4 Free
Block 5 Used
Here, although there is free memory (Blocks 2 and 4), it is not contiguous,
resulting in external fragmentation.
REVIEW QUESTIONS
1. What is an Operating System? Explain the different functions of the operating
system.
2. Explain the following terms: (1) Process (2) Creation and termination
operation of process.
3. What is multithreading? Explain with a suitable example.
4. What is scheduling? Explain the SJP Shortest-Job First algorithm.
5. What is a process thread? Explain.
6. What is a thread? Explain the concept of multithreading with a suitable
example.
7. Explain schedulers and types of schedulers in detail.
8. Describe the Round Robin CPU scheduling algorithm.
9. What is Micro Kernel? Explain its architecture and benefits.
10. Explain the structure of the Operating System.
Page 44 of 159
Operating System
Page 45 of 159
Operating System
CHAPTER 2
PERFORMANCE AND DEADLOCK
PERFORMANCE COMPARISON
The performance of an operating system is dependent on a variety of factors,
such as the hardware specifications of the computer, the design and
implementation of the operating system, the type and number of applications
running on it, and the workload and environment. These elements can have
varying degrees of impact on the performance of an operating system, depending on
the situation and goal. For instance, the CPU, memory, disk, and network can
influence the kernel, scheduler, file system, security features, user interface,
background processes, and network services. Moreover, user input, system load,
and external events can also affect its performance.
There are many scheduling algorithms, each with its parameters. As a
result, selecting an algorithm can be difficult. The first problem is defining the
criteria to be used in selecting an algorithm. Criteria are often defined in terms of
CPU utilization, response time, or throughput. To select an algorithm, we must first
define the relative importance of these measures. Our criteria may include several
measures, such as:
• Maximizing CPU utilization under the constraint that the maximum response
time is 1 second
• Maximizing throughput such that turnaround time is (on average) linearly
proportional to total execution time
Once the selection criteria have been defined, we want to evaluate the
algorithms under consideration. Following are the various evaluation methods we
can use.
DETERMINISTIC MODELLING
In a deterministic model, when one starts running the model with the same
initial condition every time, the result or the outcome is the same. Moreover, a
deterministic model does not involve randomness; it works accordingly. In the case
of the deterministic model when some work starts at a particular time that is at the
same pace every time, then the output of the model always depends on the initial
conditions. For a well-defined linear model, the unique output is produced from a
unique input, and in the case of a non-linear model, multiple outputs are
produced. This model can be described in different stages of temporal variations
viz. time-independent, time-dependent, and dynamic. A deterministic system
assumes an exact relationship between variables. As a result, this relationship
between variables enables one to predict and notice how variables affect the other.
Page 46 of 159
Operating System
P1 P2 P3 P4 P5
0 10 39 42 49 61
The waiting time is 0 milliseconds for process P1, 10 milliseconds for process
P2, 39 milliseconds for process P3, 42 milliseconds for process P4, and 49
milliseconds for process P5. Thus, the average waiting time is (0 + 10 + 39 + 42 +
49)/5 = 28 milliseconds.
With non-preemptive SJF scheduling, we execute the processes as
P3 P4 P1 P5 P2
0 3 10 20 32 61
The waiting time is 10 milliseconds for process P1, 32 milliseconds for
process P2, 0 milliseconds for process P3, 3 milliseconds for process P 4, and 20
milliseconds for process P5. Thus, the average waiting time is (10 + 32 + 0 + 3 +
20)/5 = 13 milliseconds.
With the RR algorithm, we execute the processes as
P1 P2 P3 P4 P5 P2 P5 P2
0 10 20 23 30 40 50 52 61
The waiting time is 0 milliseconds for process P1, 10 milliseconds for process
P2, 20 milliseconds for process P3, 23 milliseconds for process P 4, and 40
milliseconds for process P5. Thus, the average waiting time is (0 + 32 + 20 + 23 +
40)/5 = 23 milliseconds.
We see that the average waiting time obtained with the SJF policy is less
than half that obtained with FCFS scheduling; the RR algorithm gives us an
intermediate value.
Deterministic modelling is simple and fast. It gives us exact numbers,
allowing us to compare the algorithms. However, it requires exact numbers for
input. The main uses of deterministic modelling are in describing scheduling
algorithms and providing examples. In cases where we are running the same
program over and over again and can measure the program's processing
Page 47 of 159
Operating System
QUEUING ANALYSIS
On many systems, the processes that are run vary from day to day, so there
is no static set of processes (or times) to use for deterministic modelling. These
distributions can be measured and then approximated or simply estimated. The
result is a mathematical formula describing the probability of a particular CPU
burst. Commonly, this distribution is exponential and is described by its mean.
Similarly, we can describe the distribution of times when processes arrive in the
system (the arrival-time distribution). From these two distributions, it is possible to
compute the average throughput, utilization, waiting time, and so on for most
algorithms.
The computer system is described as a network of servers. Each server has a
queue of waiting processes. The CPU is a server with its ready queue, as is the I/O
system with its device queues. Knowing arrival rates and service rates, we can
compute utilization, average queue length, average wait time, and so on. This area
of study is called queueing network analysis.
As an example, let n be the average queue length (excluding the process
being serviced), let W be the average waiting time in the queue, and let X be the
average arrival rate for new processes in the queue (such as three processes per
second). We expect that when a process waits, x W new processes will arrive in
the queue. If the system is in a steady state, then the number of processes leaving
the queue must be equal to the number of processes that arrive. Thus,
n=xW
This equation, known as Little's formula, is particularly useful because it is valid
for any scheduling algorithm and arrival distribution.
We can use Little's formula to compute one of the three variables if we know
the other two. For example, if we know that 7 processes arrive every second (on
average) and that there are normally 14 processes in the queue, then we can
compute the average waiting time per process as 2 seconds.
Queueing analysis can be useful in comparing scheduling algorithms, but it
also has limitations. At the moment, the classes of algorithms and distributions
that can be handled are fairly limited. The mathematics of complicated algorithms
and distributions can be difficult to work with. Thus, arrival and service
distributions are often defined in mathematically tractable but unrealistic ways. It
is also generally necessary to make several independent assumptions, which may
not be accurate. As a result of these difficulties, queueing models are often only
approximations of real systems, and the accuracy of the computed results may be
questionable.
Page 48 of 159
Operating System
SIMULATORS
The OS Simulator is designed to support two main aspects of a computer
system’s resource management: process management and memory management.
Once a compiled code is loaded into the CPU Simulator’s memory, its image is also
available to the OS Simulator. It is then possible to create multiple instances of the
program images as separate processes. The OS Simulator displays the running
processes, the ready processes, and the waiting processes. Each process is
assigned a separate Process Control Block (PCB) that contains information on the
process state. This information is displayed in a separate window. The main
memory display demonstrates the dynamic nature of page allocations according to
the currently selected placement policy. The OS maintains a separate page table for
each process which can also be displayed. The simulator demonstrates how data
memory is relocated and the page tables are maintained as the pages are moved in
and out of the main memory illustrating virtual memory activity.
The process scheduler includes various selectable scheduling policies that
include priority-based, pre-emptive, and round-robin scheduling with variable time
quanta. The OS can carry out context-switching which can be visually enhanced by
slowing down or suspending the progress at some key stage to enable the students
to study the states of CPU registers, stack, cache, pipeline, and PCB contents.
The simulator incorporates an input-output console device, incorporating a
virtual keyboard, and is used to display text and accept input data.
The OS simulator supports dynamic library simulation which is supported
by the appropriate language constructs in the teaching language. The benefits of
sharing code between multiple processes are visually demonstrated. There is also a
facility to link static libraries demonstrating the differences between the two types
of libraries and their benefits and drawbacks.
The simulator allows manual allocation and de-allocation of resources to
processes. This facility is used to create and demonstrate
process deadlocks associated with resources and enables experimentation with
deadlock prevention, detection, and resolution.
STARVATION
Starvation is a problem of resource management where in the OS, the
process does not have resources because it is being used by other processes. This
problem occurs mainly in a priority-based scheduling algorithm in which the
requests with high priority get processed first and the least priority process takes
time to get processed.
Page 49 of 159
Operating System
What Causes Starvation in OS? Here are a few reasons why starvation in
OS occurs:
• In starvation, a process with low priority might wait forever if the process with
higher priority uses a processor constantly.
• Because the low-priority processes are not interacting with resources the
deadlock does not occur but there are chances of starvation as the low-priority
processes are kept in a wait state.
• Hence starvation is precisely a fail-safe method, that is it prevents deadlock
temporarily but it affects the system in general.
• The important cause of starvation might be that there are not enough resources
to provide for every resource.
• If a process selection is random then there can be a possibility that a process
might have to wait for a long duration.
• Starvation can also occur when a resource is never provided to a process for
execution due to faulty allocation of resources.
Page 50 of 159
Operating System
Example of Starvation:
In the given example, the P2 process has the highest priority, and
process P1 has the lowest priority. In the diagram, it can be seen that there is n
number of processes ready for their execution. So, in the CPU, the process with the
highest priority will come in which is P2, and the process P1 will keep waiting for
its turn because all other processes are at a high priority concerning P1. This
situation in which the process is waiting is called starvation.
DEADLOCK
All the processes in a system require some resources such as a central
processing unit (CPU), file storage, input/output devices, etc to execute it. Once the
execution is finished, the process releases the resource it was holding. However,
when many processes run on a system, they also compete for the resources they
require for execution. This may cause a deadlock situation.
A deadlock is a situation in which more than one process is blocked
because it is holding a resource and also requires some resource that is acquired
by some other process. Therefore, none of the processes gets executed.
Example Of Deadlock
Page 51 of 159
Operating System
In the above figure, there are two processes and two resources. Process 1
holds "Resource 1" and needs "Resource 2" while Process 2 holds "Resource 2" and
requires "Resource 1". This creates a situation of deadlock because none of the two
processes can be executed. Since the resources are non-shareable, they can only be
used by one process at a time (Mutual Exclusion). Each process is holding a
resource and waiting for the other process the release the resource it requires.
None of the two processes releases their resources before their execution and this
creates a circular wait. Therefore, all four conditions are satisfied.
Methods of Handling Deadlocks: The first two methods are used to ensure
the system never enters a deadlock.
Deadlock Prevention: This is done by restraining the ways a request can be
made. Since deadlock occurs when all the above four conditions are met, we try to
prevent any one of them, thus preventing a deadlock.
Deadlock Avoidance: When a process requests a resource, the deadlock
avoidance algorithm examines the resource-allocation state. If allocating that
resource sends the system into an unsafe state, the request is granted.
Therefore, it requires additional information such as how many resources of
each type are required by a process. If the system enters an unsafe state, it must
step back to avoid deadlock.
Page 52 of 159
Operating System
Deadlock Detection and Recovery: We let the system fall into a deadlock and
if it happens, we detect it using a detection algorithm and try to recover. Some
ways of recovery are as follows.
• Aborting all the deadlocked processes.
• Abort one process at a time until the system recovers from the deadlock.
• Resource Preemption: Resources are taken one by one from a process and
assigned to higher priority processes until the deadlock is resolved.
Deadlock Ignorance: In the method, the system assumes that deadlock never
occurs. Since the problem of deadlock situations is not frequent, some systems
simply ignore it. Operating systems such as UNIX and Windows follow this
approach. However, if a deadlock occurs, we can reboot our system, and the
deadlock is resolved automatically.
Page 53 of 159
Operating System
Also, this graph contains all the information that is related to all the
instances of the resources which means the information about available resources
and the resources which are being used by the process. In this graph, the circle is
used to represent the process, and the rectangle is used to represent the resource.
Vertices: There are two kinds of vertices used in the resource allocation graph
and these are:
• Process Vertices
• Resource Vertices
Process Vertices: These vertices are used to represent process vertices. The
circle is used to draw the process vertices and the name of the process is
mentioned inside the circle.
Resource Vertices: These vertices are used to represent resource vertices. The
rectangle is used to draw the resource vertices and we use dots inside the circle to
mention the number of instances of that resource.
In the system, there may exist several instances and according to them,
there are two types of resource vertices and these are single instances and multiple
instances.
Single Instance: In a single instance resource type, there is a single dot inside
the box. The single dot mainly indicates that there is one instance of the resource.
Multiple Instance: In multiple instance resource types, there are multiple dots
inside the box, and these Multiple dots indicate that there are multiple instances of
the resources.
Edges: In the Resource Allocation Graph, Edges are further categorized into two:
Page 54 of 159
Operating System
Assign Edges: Assign Edges are mainly used to represent the allocation of
resources to the process. We can draw assigned edges with the help of an arrow in
which mainly the arrowhead points to the process, and the process mainly tail
points to the instance of the resource.
Single Instance RAG Example: Suppose there are Four Processes P1, P2,
P3, P4, and two resources R1 and R2, where P1 is holding R1 and P2 is holding R2,
P3 is waiting for R1 and R2 while P4 is waiting for resource R1.
Multiple Instance RAG Example: Suppose there are four processes P1, P2,
P3, P4 and there are two instances of resource R1 and two instances of resource
R2:
Page 55 of 159
Operating System
Advantages of RAG
• It is pretty effective in detecting deadlocks.
• The Banker's Algorithm makes extensive use of it.
• It's a graphical depiction of a system.
• A quick peek at the graph may sometimes tell us if the system is in a deadlock
or not.
• Understanding resource allocation using RAG takes less time.
Disadvantages of RAG
• RAG is beneficial when there are fewer procedures and resources.
• When dealing with a large number of resources or processes, it is preferable to
keep data in a table than an RAG.
• When there are a vast number of resources or processes, the graph becomes
challenging to interpret and very complex.
1. Mutual Exclusion: In this, two or more processes must compete for the
same resources. There must be some resources that can only be used one
process at a time. This means the resource is non-sharable. This could be a
physical resource like a printer or an abstract concept like a lock on a shared
data structure.
Deadlocks never occur with shared resources like read-only files but with
exclusive access to resources like tape drives and printers.
Page 56 of 159
Operating System
2. Hold and Wait: Hold and wait is when a process is holding a resource and
waiting to acquire another resource that it needs but cannot proceed because
another process is keeping the first resource. Each of these processes must
have a hold on at least one of the resources it’s requesting. If one process
doesn’t have a hold on any of the resources, it can’t wait and will give up
immediately.
When this condition exists, the process must be stopped if it
simultaneously holds one or more resources while waiting for another resource.
In the below diagram, Process 1 currently holding Resource 1 is waiting for
additional Resources 2.
4. Circular Wait: The circular wait is when two processes wait for each other to
release a resource they are holding, creating a deadlock. There must be a cycle
in the graph below. As you can see, process 1 is holding on to a resource R1
that process 2 in the cycle is waiting for. This is an example of a circular wait.
To better understand let’s understand with another example. For example,
Process A might be holding on to Resource X while waiting for Resource Y,
while Process B is holding on to Resource Y while waiting for Resource Z, and
so on around the cycle.
Page 57 of 159
Operating System
Here,
Process P1 waits for a resource held by process P2.
Process P2 waits for a resource held by process P3.
Process P3 waits for a resource held by process P4.
Process P4 waits for a resource held by process P1.
DEADLOCK PREVENTION
Deadlock prevention is eliminating one of the necessary conditions of
deadlock so that only safe requests are made to OS and the possibility of deadlock
is excluded before making requests. Here OS does not need to do any additional
tasks as it does in deadlock avoidance by running an algorithm on requests
checking for the possibility of deadlock.
Page 58 of 159
Operating System
(FCFS) basis. In this way, the process does not have to wait for the printer and it
continues its work after adding its job to the queue. We can understand the
workings of the Spooler directory better with the diagram given below:
Hold and Wait: Hold and wait is a condition in which a process is holding one
resource while simultaneously waiting for another resource that is being held by
another process. The process cannot continue till it gets all the required resources.
In the diagram given below:
Page 59 of 159
Operating System
1. If a process is holding some resources and waiting for other resources, then it
should release all previously held resources and put a new request for the
required resources again. The process can resume once it has all the required
resources.
For example: If a process has resources R1, R2, and R3 and it is waiting for
resource R4, then it has to release R1, R2, and R3 and put a new request for all
resources again.
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.
Circular Wait: In circular wait, two or more processes wait for resources in a
circular order. We can understand this better by the diagram given below:
DEADLOCK DETECTION
If a system does not employ either a deadlock-prevention or a deadlock
avoidance algorithm, then a deadlock situation may occur. In this environment, the
system must provide:
• An algorithm that examines the state of the system to determine whether a
deadlock has occurred
• An algorithm to recover from the deadlock
Single Instance of Each Resource Type- If all resources have only a single
instance, then we can define a deadlock-detection algorithm that uses a variant of
the resource-allocation graph, called a wait-for graph. We obtain this graph from
the resource-allocation graph by removing the resource nodes and collapsing the
appropriate edges.
Page 60 of 159
Operating System
P5
P5
R1 R3 R4
P1 P2 P3 P1 P2 P3
P4 P4
R2 R5
(a) (b)
Figure: (a) Resource-allocation graph (b) Corresponding wait-for graph
More precisely, an edge from Pi, to Pj, in a wait-for graph implies that
process Pi, is waiting for process Pj, to release a resource that Pi needs. An edge
Pi → P, exists in a wait-for graph if and only if the corresponding resource-
allocation graph contains two edges P → Rj and Rj → Pi for some resource Rj, for
example, in the figure, we present a resource-allocation graph and the
corresponding wait-for graph.
As before, a deadlock exists in the system if and only if the wait-for graph
contains a cycle. To detect deadlocks, the system needs to maintain the wait-for
graph and periodically invoke an algorithm that searches for a cycle in the graph.
An algorithm to detect a cycle in a graph requires an order of n1 operations, where
n is the number of vertices in the graph.
Several Instances of a Resource Type: The wait-for graph scheme does not
apply to a resource-allocation system with multiple instances of each resource type.
We turn now to a deadlock detection algorithm that is applicable to such a system.
The algorithm employs several time-varying data structures that are similar to
those used in the banker's algorithm:
• Available- A number of available resources of each type.
• Allocation- A number of resources of each type currently allocated to each
process.
• Request- A current request of each process.
We consider a system with five processes P1 through P4 and three resource
types A, B, and C. Resource type A has seven instances, resource type B has two
instances, and resource type C has six instances. Suppose that, at time T0, we have
the following resource-allocation state:
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
Page 61 of 159
Operating System
P4 002 002
Suppose now that process P2 makes one additional request for an instance of
type C. The Request matrix is modified as follows:
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
We claim that the system is now deadlocked. Although we can reclaim the
resources held by process Po, the number of available resources is not sufficient to
fulfil the requests of the other processes. Thus, a deadlock exists, consisting of
processes P1, P2, P3, and P4.
Page 62 of 159
Operating System
Page 63 of 159
Operating System
break the deadlock, this method requires the system to keep more information
about the state of all running processes.
3. Starvation- How do we ensure that starvation will not occur? That is, how can
we guarantee that resources will not always be preempted from the same
process?
In a system where victim selection is based primarily on cost factors, the
same process may always pick a victim. As a result, this process never completes
its designated task, a starvation situation that must be dealt with in any practical
system. We must ensure that a process can be picked as a victim only a (small)
finite number of times. The most common solution is to include the number of
rollbacks in the cost factor.
Recovery Strategies/Methods:
• Process Termination: One way to recover from a deadlock is to terminate one
or more of the processes involved in the deadlock. By releasing the resources
held by these terminated processes, the remaining processes may be able to
continue executing. However, this approach should be used cautiously, as
terminating processes could lead to loss of data or incomplete transactions.
• Resource Preemption: Resources can be forcibly taken away from one or
more processes and allocated to the waiting processes. This approach can break
the circular wait condition and allow the system to proceed. However, resource
preemption can be complex and needs careful consideration to avoid disrupting
the execution of processes.
• Process Rollback: In situations where processes have checkpoints or states
saved at various intervals, a process can be rolled back to a previously saved
state. This means that the process will release all the resources acquired after
the saved state, which can then be allocated to other waiting processes.
Rollback, though, can be resource-intensive and may not be feasible for all types
of applications.
• Wait-Die and Wound-Wait Schemes: As mentioned in the Deadlock
Detection in OS section, these schemes can also be used for recovery. Older
processes can preempt resources from younger processes (Wound-Wait), or
younger processes can be terminated if they try to access resources held by older
processes (Wait-Die).
• Kill the Deadlock: In some cases, it might be possible to identify a specific
process that is causing the deadlock and terminate it. This is typically a last
resort option, as it directly terminates a process without any complex recovery
mechanisms.
BANKER’S ALGORITHM
The banker algorithm in the Operating System is used to avoid deadlock and
for resource allocation safely to each process in the system. As the name suggests,
it is mainly used in the banking system to check whether a loan can be sanctioned
to a person or not.
Page 64 of 159
Operating System
1. Safety Algorithm: The safety algorithm checks for the safe state of the
system. If the system is in a safe state with any of the resource allocation
permutations, then deadlock can be avoided. The steps in the Safety Algorithm
are:
Step 1: Let there be two vectors Work and Finish of length m and n,
respectively. The work array represents the total available resources of each
type (R0, R1, R2, ...Rm) and the Finish array represents whether the particular
process Pi has finished execution or not. It can have a Boolean value of
true/false.
Work = Available
Page 65 of 159
Operating System
Page 66 of 159
Operating System
unsafe then the process Pi has to wait for the resources to fulfil its request
demands, and the old allocation state is again restored.
Page 67 of 159
Operating System
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.
Available = Available + Allocation
= (5, 3, 3) + (1, 1, 2) = (6, 4, 5) (New Available)
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.
Available = Available + Allocation
= (6,4,5) + (3,0,1) = (9,4,6) (NewAvailable)
7. Now again check for Process P3, Need = (8, 4, 4) and Available = (9, 4,
6) Clearly, the resources needed are less than equal to the available resources
within the system. Hence, the request for P3 is granted.
Available = Available + Allocation
= (9,4,6) + (0,2,0) = (9,6,6)
8. Now again check for Process P0, Need = (4, 3, 2), and Available (9, 6, 6) Clearly,
the request for P0 is also granted.
Safe sequence: <P1, P4, P2, P3, P0> <P1, P4, P2, P3, P0>
The system has allocated all the required number of resources to each
process in a particular sequence. Therefore, it is proved that the system is in a safe
state.
Page 68 of 159
Operating System
• This algorithm also consists of two other advanced algorithms for maximum
resource allocation.
Page 69 of 159
Operating System
REVIEW QUESTIONS
1. What is deadlock? Explain various conditions arising from deadlock.
2. Write short notes on (1) Resource Allocation Graph and (2) Simulators.
3. What is deadlock detection? Explain recovery of deadlock.
4. Write short notes on (1) Deterministic Modeling (2) Queuing Analysis.
5. Explain starvation.
6. Explain mutual exclusion in detail.
7. What is deadlock? Explain the resource allocation graph.
8. Explain the methods for recovery from deadlock.
9. Explain the Hold and Wait condition in brief.
10. What is deadlock? Explain the conditions for deadlock.
11. What is Deadlock Prevention? How can we prevent a deadlock?
12. Write a note on Multithreading.
13. Explain Banker's algorithm for deadlock avoidance.
14. Differentiate between program and process.
15. Explain circular wait conditions with examples.
Page 70 of 159
Operating System
CHAPTER 3
MEMORY MANAGEMENT
INTRODUCTION
The operating system manages the resources of the computer, controls
application launches, and performs tasks such as data protection and system
administration. The resource that the operating system uses the most is memory.
Memory is a storage area on the computer that contains the instructions and data
that the computer uses to run the applications.
When the applications or the operating system need more memory than is
available on the computer, the system must swap the current contents of the
memory space with the contents of the memory space that is being requested. In
the same way, different situations need different memory management techniques.
Some cases call for the use of paging, while others may require the use of an
on-disk cache. Ultimately, deciding which memory management technique to use is
a matter of optimizing the user interface for the available hardware and software.
Memory management is allocating, freeing, and re-organizing memory in a
computer system to optimize the available memory or to make more memory
available. It keeps track of every memory location (if it is free or occupied).
Page 71 of 159
Operating System
Physical Address Space: Physical addresses are addresses that specify actual
(real) physical locations in memory. It is a real memory location where the data is
stored. Hardware, such as the CPU and memory controller, can directly access
corresponding memory locations with physical addresses, and translation or
mapping is not involved.
Physical addresses are low-level addressing modes that refer to hardware
architecture and point to a specific computer's memory layout. The hardware uses
this mode directly to access memory locations and communicate with devices.
Page 72 of 159
Operating System
Logical Address Space: The logical addresses are the virtual addresses of a
CPU generated at run time. These do not exist in the memories as physical
addresses; they act as pointers for the CPU to access real memory locations.
Logical addresses are addresses used by software programs and operating
systems to simplify memory management and provide a more flexible and abstract
way of accessing memory or devices. Logical addresses are part of the virtual
memory abstraction, which allows programs to operate in a larger logical address
space than the available physical memory.
A logical address must be mapped or translated to its corresponding
physical addresses before the hardware can use it. This translation is typically
performed by a hardware component called the Memory Management Unit (MMU).
Using logical addresses allows for memory protection mechanisms, where different
programs or processes are isolated from each other's memory spaces, enhancing
security and stability.
Logical addresses are generally more portable and can be used across
different systems or architectures as long as the address translation mechanisms
are compatible.
The abstraction provided by logical addresses enables features like demand
paging, swapping, and shared memory, which are crucial for efficient memory
management and resource utilization.
A virtual or symbolic
Represents the actual
representation of memory
Representation physical location of data in
locations, used by software
memory or devices
programs
Generated based on the
Generated by the CPU while a
Generation hardware architecture and
program is running
memory configuration
Page 73 of 159
Operating System
SWAPPING
Swapping in OS is one of those schemes which fulfil the goal of maximum
utilization of CPU and memory management by swapping in and swapping out
processes from the main memory. Swap-in removes the process from the hard drive
(secondary memory) and swap-out removes the process from RAM (main memory).
Let's suppose several processes like P1, P2, P3, and P4 are ready to be
executed inside the ready queue, and processes P1 and P2 are very memory
consuming so when the processes start executing there may be a scenario where
the memory will not be available for the execution of the process P3 and P4 as there
is a limited amount of memory available for process execution.
Swapping in the operating system is a memory management scheme that
temporarily swaps out an idle or blocked process from the main memory to
secondary memory which ensures proper memory utilization and memory
availability for those processes that are ready to be executed.
Page 74 of 159
Operating System
There are two important concepts in the process of swapping which are as follows:
1. Swap In
2. Swap Out
Swap In: The method of removing a process from secondary memory (Hard Drive)
and restoring it to the main memory (RAM) for execution is known as the Swap
In method.
Swap Out: It is a method of bringing out a process from the main memory (RAM)
and sending it to the secondary memory (hard drive) so that the processes with
higher priority or more memory consumption will be executed known as the Swap
Out method.
Page 75 of 159
Operating System
• CPU can perform various tasks simultaneously with the help of swapping so that
processes do not have to wait much longer before execution.
• Swapping ensures proper RAM (main memory) utilization.
• Swapping creates a dedicated disk partition in the hard drive for swapped
processes which is called swap space.
• Swapping in OS is an economical process.
• Swapping method can be applied on priority-based process scheduling where
a high-priority process is swapped in and a low-priority process is swapped
out which improves the performance.
Page 76 of 159
Operating System
DYNAMIC LOADING
The process of getting a program from secondary storage (hard disk) to the
main memory (RAM) is known as loading. In simple words, loading loads the
program in the main memory.
The entire program and all data of a process must be in physical memory for
the process to execute. The size of a process is thus limited to the size of physical
memory. To obtain better memory-space utilization, we can use dynamic loading.
With dynamic loading, a routine is not loaded until it is called. All routines are kept
on disk in a relocatable load format. The main program is loaded into memory and
is executed. When a routine needs to call another routine, the calling routine first
checks to see whether the other routine has been loaded. If not, the relocatable
linking loader is called to load the desired routine into memory and to update the
program's address tables to reflect this change. Then control is passed to the newly
loaded routine.
The advantage of dynamic loading is that an unused routine is never loaded.
This method is particularly useful when large amounts of code are needed to
handle infrequently occurring cases, such as error routines. In this case, although
the total program size may be large, the portion that is used (and hence loaded)
may be much smaller.
Page 77 of 159
Operating System
Dynamic loading does not require special support from the operating system.
It is the responsibility of the users to design their programs to take advantage of
such a method. Operating systems may help the programmer, however, by
providing library routines to implement dynamic loading.
DYNAMIC LINKING
Linking all the required modules of the program to continue the program
execution is known as linking. It takes the object code through an assembler and
combines them to make an executable module.
Advantages:
• Memory Efficiency: Programs don't need to include full copies of libraries,
leading to smaller executables and efficient use of memory.
• Upgradability: Libraries can be updated independently of the programs. A bug
fix or update to the library will affect all programs that use it without needing
recompilation.
Page 78 of 159
Operating System
Disadvantages:
• Runtime Overhead: Loading libraries and resolving symbols at runtime
introduces a slight performance cost.
• Dependency Issues: If a required shared library is missing or incompatible, the
program might fail to execute properly. This is often referred to as "DLL hell" in
Windows.
Page 79 of 159
Operating System
Page 80 of 159
Operating System
0
R + Yes Is A
CPU
Physical Relocation A<L
Address register
L Error in Addressing
Max Limit
Register
Page 81 of 159
Operating System
sure that the logical address generated by the CPU is not bigger than the program's
size.
MULTIPLE PARTITIONS
This is also known as a static partitioning scheme as shown in the following
Figure. A simple memory management scheme divides memory into n (possibly
unequal) fixed-sized partitions, each of which can hold exactly one process. The
degree of multiprogramming is dependent on the number of partitions. IBM used
this scheme for systems 360 OS/MFT (Multiprogramming with a fixed number of
tasks). The partition boundaries are not movable (must reboot to move a job). We
can have one queue per partition or just a single queue for all the partitions.
d
Partition 3
c
Partition 2
Multiple Job Queues b
Partition 1
a
Partition 1
0
Multiple Partition System
Initially, whole memory is available for user processes and is like a large
block of available memory. The operating system keeps details of available memory
blocks and occupied blocks in tabular form. OS also keeps track of the memory
requirements of each process. As processes enter into the input queue and when
sufficient space for it is available, the process is allocated space and loaded. After
its execution is over it releases its occupied space and OS fills this space with other
processes in the input queue. The block of available memory is known as a Hole.
Holes of various sizes are scattered throughout the memory. When any process
arrives, it is allocated memory from a hole that is large enough to accommodate it.
This example is shown in the Figure given below:
OS OS OS
200K
Process A (50K) Process A (50K) Process A (50K)
Partition 1
(100K) Hole (50K) Process B Hole (50K) Process D Hole (50K)
Page 82 of 159
Operating System
Within a partition, if two holes are adjacent then they can be merged to
make a single large hole. However, this scheme suffers from a fragmentation
problem. Storage fragmentation occurs either because the user processes do not
completely accommodate the allotted partition or partition remains unused, if it is
too small to hold any process from the input queue. Main memory utilization is
extremely inefficient. Any program, no matter how small, occupies the entire
partition. In our example, process B takes 150K of partition2 (200K size). We are
left with a 50 K-sized hole. This phenomenon, in which there is wasted space
internal to a partition, is known as internal fragmentation. It occurs because the
initial process is loaded in a partition that is large enough to hold it (i.e., allocated
memory may be slightly larger than requested memory). “Internal” here means
memory that is internal to a partition, but is not in use.
OS OS OS
Terminated Arrives
Process B Process B Process B
COMPACTION
Compaction is a memory management technique in which the free space of a
running system is compacted, to reduce fragmentation problems and improve
memory allocation efficiency. Compaction is used by many modern operating
systems, such as Windows, Linux, and Mac OS X. As in the fig we have some used
memory (black color) and some unused memory (white color). The used memory is
combined. All the empty spaces are combined. This process is called compaction.
This is done to prevent and solve the problem of fragmentation, but it requires too
much CPU time.
Page 83 of 159
Operating System
In compaction, all the holes are contiguous, and OS combines all the loaded
processes in different partitions. Now, merged holes can accommodate new
processes according to their needs. This method is also known as de-
fragmentation. Let us explain through the diagram.
At the time of compaction, the CPU stops the execution of the current
process because it will resume the process from somewhere else after compaction.
If the CPU does not stop the execution of a process, then it may execute the
instructions from somewhere else locations instead of the next instruction of the
same process in memory.
Advantages of Compaction
• Reduces external fragmentation.
• Make memory usage efficient.
• Memory becomes contiguous.
• Since memory becomes contiguous more processes can be loaded to
memory, thereby increasing the scalability of OS.
• Fragmentation of the file system can be temporarily removed by compaction.
• Improves memory utilization as there is less gap between memory blocks.
Disadvantages of Compaction
• System efficiency is reduced and latency is increased.
• A huge amount of time is wasted in performing compaction.
• CPU sits idle for a long time.
• Not always easy to perform compaction.
• It may cause deadlocks since it disturbs the memory allocation process.
RELOCATION
Page 84 of 159
Operating System
PAGING
Paging is a memory management technique for retrieving processes from
secondary memory storage units as pages and stored in the main memory’s frame.
When a program needs to access data, it sends a request to access a process
to the operating system, which stores the process in the main memory from the
secondary memory. Each process is divided into small fixed-sized chunks
called pages; similarly, the main memory will be divided into equal-fixed-sized
pieces called frames. The process pages are stored at a different location in the
main memory. The thing to note here is that the size of the page and frame will be
the same.
Like in the above fig, every page in secondary memory is 2 Kb, and in the
same way, every frame in main memory is also 2 Kb.
The problem is that physical memory is finite. When all of the spaces in
physical memory are filled with requests, the operating system has to start
swapping the processes that are not in use to make room for new ones. This
process is called swapping.
Page 85 of 159
Operating System
Advantages of Paging
• Conserve memory by only keeping the active pages in memory. This is especially
helpful in large-scale systems where memory is scarce.
• Enables the operating system to manage more processes by allowing each
process to have its dedicated memory space. This maximizes efficiency and
performance by allowing the operating system to schedule and run each process
without conflicts.
• Allows for greater flexibility and scalability regarding the size and complexity of
the systems that can be created.
• Parts of the program are allowed to be stored at different locations in the main
memory.
• It solves the problem of external fragmentation.
• Swapping becomes very easy due to equal-sized pages and frames.
Disadvantages of Paging
• It can be very inefficient. When a process needs more memory, the operating
system must find a block of unused memory and copy it to the process.
• This process can take a long time and, in some cases, can even crash the
system.
• Paging can cause internal fragmentation, which makes the system run more
slowly.
• The page table is there, which takes some memory space.
• Have to maintain a page table for each process.
• Memory access time increases as the page table needs to be accessed.
SEGMENTATION
Page 86 of 159
Operating System
Page 87 of 159
Operating System
Advantages of Segmentation in OS
• No internal fragmentation is there in segmentation.
• Segment Table is used to store the records of the segments. The segment table
itself consumes less memory as compared to a page table in paging.
• Segmentation provides better CPU utilization as an entire module is loaded at
once.
• Segmentation is near to the user's view of physical memory. Segmentation allows
users to partition the user programs into modules. These modules are nothing
but the independent codes of the current process.
• The Segment size is specified by the user but in Paging, the hardware decides
the page size.
• Segmentation can be used to separate the security procedures and data.
Disadvantages of Segmentation in OS
• During the swapping of processes, the free memory space is broken into small
pieces, which is a major problem in the segmentation technique.
• Time is required to fetch instructions or segments.
• The swapping of segments of unequal sizes is not easy.
• There is an overhead of maintaining a segment table for each process as well.
• When a process is completed, it is removed from the main memory. After the
execution of the current process, the unevenly sized segments of the process are
removed from the main memory. Since the segments are of uneven length it
creates unevenly sized holes in the main memory. These holes in the main
memory may remain unused due to their very small size.
Page 88 of 159
Operating System
As shown in the image below, the base address of Segment-0 is 1400 and its
length is 1000, the base address of Segment-1 is 6300 and its length is 400, the
base address of Segment-2 is 4300 and its length is 400, and so on.
The pictorial representation of the above segmentation with its segment table
is shown below.
Page 89 of 159
Operating System
The page number is an index into the page table, each item of which
represents a page frame. The physical address is obtained by adding the PFN (page
frame number) and the offset. As a result, addressing may be defined by the
function:
va = (s,p,d)
here,
va is the virtual address,
s determines the number of segments (size of ST),
p determines the number of pages per segment (size of PT),
d determines page size.
Page 90 of 159
Operating System
Page 91 of 159
Operating System
segmentation.
Paging could result in internal Segmentation could result in external
fragmentation. fragmentation.
In paging, the logical address is split Here, the logical address is split into
into a page number and a page offset. segment number and segment offset.
Paging comprises a page table that Segmentation also comprises the
encloses the base address of every segment table which encloses the
page. segment number and segment offset.
In segmentation, the operating system
In paging, the operating system must
maintains a list of holes in the main
maintain a free frame list.
memory.
Paging is invisible to the user. Segmentation is visible to the user.
In paging, the processor needs the page In segmentation, the processor uses
number, and offset to calculate the segment number, and offset to calculate
absolute address. the full address.
It is hard to allow the sharing of Facilitates sharing of procedures
procedures between processes. between the processes.
n paging, a programmer cannot It can efficiently handle data
efficiently handle data structure. structures.
Easy to apply for protection in
This protection is hard to apply.
segmentation.
The size of the page needs to always be There is no constraint on the size of
equal to the size of the frames. segments.
A page is referred to as a physical unit A segment is referred to as a logical
of information. unit of information.
Segmentation results in a more efficient
Paging results in a less efficient system.
system.
PROTECTION
When several users share computer resources such as CPU, memory, and
other resources, security is more crucial. It is the job of the operating system to
provide a mechanism that protects each process from other processes. All assets
that require protection in a multiuser environment are categorized as objects, and
individuals who seek to access these things are referred to as subjects. Distinct
'access privileges are granted to different subjects by the operating system.
Protection is a method that limits the access of programs, processes, or
users to the resources defined by a computer system. Protection can be used to
allow several users to safely share a common logical namespace, such as a
directory or files, in multi-programming operating systems. It necessitates the
safeguarding of computer resources such as software, memory, and processors. As
assistance to multiprogramming OS, users should apply protective steps so that
Page 92 of 159
Operating System
several users can safely access a common logical namespace like a directory or
data. Maintaining secrecy, honesty, and availability in the OS might provide
protection. The device must be protected against unauthorized access, viruses,
worms, and other malware.
Need of protection OS
• Isolation: Protection OS ensures isolation between different processes and
users, preventing unauthorized access to resources.
• Security: It protects system resources, such as memory and files, from
unauthorized access, modification, or corruption.
• Stability: Protection OS enhances system stability by preventing one process
from interfering with or crashing other processes.
• Fairness: It ensures fair resource allocation among competing processes,
preventing one process from monopolizing system resources.
Page 93 of 159
Operating System
software that handles or manages the files (binary, text, pdf, docs, audio, video,
etc.) present in computer software. The file system in the operating system is
capable of managing individual as well as groups of files present in the computer
system.
The file management in the operating system manages all the files present in
the computer system with various extensions (such as .exe, .pdf, .txt, .docx, etc.)
We can also use the file system in the operating system to get details of any file(s)
present on our system. The details can be:
• 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.
For file management in the operating system or to make the operating
system understand a file, the file must be in a predefined structure or format.
There are three types of file structures present in the operating systems:
1. text file: A text file is a non-executable file containing a sequence of numbers,
symbols, and letters organized in the form of lines.
2. source file: A source file is an executable file that contains a series of functions
and processes. In simple terms, we can say that a source file is a file that
contains the instructions of a program.
3. object file: An object file is a file that contains object codes in the form of
assembling language code or machine language code. In simple terms, we can
say that object files contain program instructions in the form of a series of
bytes and are organized in the form of blocks.
Page 94 of 159
Operating System
• The file management in the operating system allows users to create a new file,
and modify and delete the old files present at different locations of the computer
system.
• The operating system file management software manages the locations of the file
store so that files can be extracted easily.
• As we know, process shares files so, one of the most important features of file
management in operating systems is to make files sharable between processes. It
helps the various processes to securely access the required information from a
file.
• The operating system file management software also manages the files so that
there is very little chance of data loss or data destruction.
• The file management in the operating system provides input-output operation
support to the files so that the data can be written, read, or extracted from the
file(s).
• It also provides a standard input-output interface for the user and system
processes. The simple interface allows the easy and fast modification of data.
• The file management in the operating system also manages the various user
permissions present on a file. There are three user permissions provided by the
operating system, they are: read, write, and execute.
• The file management in the operating system supports various types of storage
devices such as flash drives, hard disk drives (HDD), magnetic tapes, optical
disks, tapes, etc., and it also allows the user(s) to store and extract them
conveniently.
• It also organizes the files in a hierarchal manner in the form of files and folders
(directories) so that management of these files can be easier from the user's
perspective as well. Refer to the diagram below for better visualization.
Page 95 of 159
Operating System
Page 96 of 159
Operating System
• Sequential access will become slow if the next file record to be retrieved is not
present next to the currently pointed record.
• Adding a new record may need relocating a significant number of records of the
file.
Page 97 of 159
Operating System
search the indexes and then use the pointer-to-pointer concept to navigate to the
required file.
Primary index blocks contain the links of the secondary inner blocks which contain
links to the data in the memory.
FILE DIRECTORIES
On a computer, a directory is used to store, arrange, and segregate files and
folders. It is similar to a telephone directory in that it just contains lists of names,
phone numbers, and addresses rather than the real papers. It uses a hierarchical
structure to organize files and directories. On many computers, directories are
referred to as drawers or folders, much like a workbench or a standard filing
cabinet in an office. You may, for instance, create a directory for images and
another for all of your documents. You could easily access the type of file you
wanted to see by saving particular file types in a folder.
Page 98 of 159
Operating System
There are several logical structures of a directory, these are given below.
• Single-level directory
• Two-level directory
• Tree structure or hierarchical directory
• Acyclic graph directory
• General graph directory structure and Data Structure
Page 99 of 159
Operating System
users wanted to create a file with the same name (say report.doc), it was not
allowed in a single-level directory.
In a two-level directory structure, there is a master node that has a separate
directory for each user. Each user can store the files in that directory. It can be
practically thought of as a folder that contains many folders, each for a particular
user, and now each user can store files in the allocated directory just like a single-
level directory.
The pictorial representation of a two-level directory is shown below. For
every user, there is a separate directory. At the next level, every directory stores the
files just like a single-level directory. Although not very efficient, the two-level
directory is better than a single-level directory structure.
In the above image, we can see that a cycle is formed in the user 2 directory.
Although it provides greater flexibility, it is complex to implement this structure.
Advantages of General-graph directory
• Compared to the others, the General-Graph directory structure is more
flexible.
• Cycles are allowed in the directory for general graphs.
The operating system also maintains a directory table that includes the file
name along with the starting address and the length of the blocks allocated. The
length represents the number of disk blocks required by the file.
In the above figure, it can be seen that the file "os.pdf" requires four disk
blocks and its starting disk block address is 1. Therefore, the blocks allocated to
the file are 1, 2, 3, and 4. Similarly, for “dbms.doc” the blocks allocated are 7, 8,
and 9 since its starting address is 7 and length is 3.
Advantages of contiguous file allocation
• Since the blocks are allocated in sequential order, therefore it can be accessed
sequentially since the starting address and the length are already available in
the directory table.
• The block allocation is similar to the array. Given the starting address, we can
"jump" to any block address by simply adding the block size to the starting
address, just as we do while accessing any block in an array. Hence the
contiguous allocation also allows random access to the blocks.
• The seek time is less because of the contiguous allocation of blocks. This makes
it very fast.
Disadvantages of contiguous file allocation
• It suffers from internal fragmentation. Suppose the size of a block is 2KB, but
the file that has to be stored is just 1 KB. In that case, an extra 1KB remains
unutilized and the memory is wasted.
• It suffers from external fragmentation. If there are sufficient blocks available to
store a file, but if they are not contiguous, the file cannot be stored.
• The size of the file can be increased only if free contiguous disk blocks are
available.
• Although it allows fast access, memory management is poor.
Linked File Allocation: The linked allocation works just like the linked list.
The problem with contiguous allocation was that memory remained unutilized due
to external fragmentation. The solution to the problem was to allocate the disk
block in the form of a linked list where every block was a node.
The blocks are allocated in such a way that every block contains a pointer to
the next block that is allocated to the file.
The above image shows how the linked allocation works. The file "os.pdf" has
to be allocated some blocks. The first block allocated is 4. Block 4 will have a
pointer to block 8, block 8 will have a pointer to block 10, block 10 will have a
pointer to block 11, block 11 will have a pointer to block 2, and finally, block 2 will
point to 3. In this manner, a total of six blocks are allocated to the file non-
contiguously. The ending block (block 3) will not point to any other block.
Advantages of linked file allocation
• There is no external fragmentation because blocks can be allocated in random
order with the help of pointers. Contiguous allocation is not required.
• File size can be increased, even if the contiguous blocks are not available,
provided there are enough blocks to store the file.
• Judicious use of memory.
From the above image, we can see that block number 8 does not store the
file but contains the pointers to various other blocks, which store the file. The
directory table contains only the file name and the index block for the respective
files.
Below is the pictorial representation of index block 8, which contains the
pointers that determine the address of the blocks that store the "os.pdf" file.
Since the size of every block is limited, there will be problems if the numbers
of pointers to other blocks are very large in number such that a block is not
sufficient to store it.
Advantages of Indexed allocation
• No external fragmentation.
• Allows random access to disk blocks.
• Allows direct access, reducing complexity.
Disadvantages of Indexed allocation
• It is very complex.
• Extra memory for index blocks.
• Large pointer overhead.
• For very large files, a single index block may not be able to hold all the
pointers.
File Allocation Table (FAT): The File Allocation Table (FAT) is a file system
format commonly used in older computer systems and removable storage devices.
It organizes data storage by maintaining a table that tracks the allocation status of
individual file clusters on a storage medium. While less common today, FAT was
instrumental in early computing, providing a straightforward way to manage files
and directories.
Advantages of File Allocation Table (FAT)
• FAT is widely supported across different operating systems and devices, making
it easy to share data between various platforms without compatibility issues.
• FAT's straightforward structure makes it relatively easy to implement and
understand, which was particularly advantageous in the early days of
computing.
• The minimalistic design of FAT requires less storage space and processing power
compared to more modern file systems, making it suitable for systems with
limited resources.
• Due to its simplicity, FAT file systems are often recoverable using basic data
recovery tools, allowing for potential retrieval of lost or deleted data.
Inode: An inode, short for "index node," is a data structure in Unix-like file
systems that stores metadata about a file, such as its permissions, ownership, size,
and location of data blocks on the storage device. Inodes play a crucial role in
efficient file management and data retrieval, as they enable the operating system to
quickly locate and access files. Each file or directory on the file system corresponds
to an inode, allowing for organized and optimized storage allocation.
Advantages of Inode
• Inodes enable rapid access to file metadata and data blocks, making file
operations like opening, reading, and writing faster and more efficient.
• Inodes allow sparse files—files with unallocated gaps—to be managed effectively,
as they only allocate space for actual data blocks, optimizing storage usage.
• Inodes facilitate the creation of hard links, which are multiple directory entries
pointing to the same inode.
• Inodes enhance file system stability by maintaining data consistency and
reducing the risk of data corruption.
Disadvantages of Inode
• Inodes consume a fixed amount of storage space regardless of file size.
• File systems have a finite number of inodes available.
• As directories grow in size, the number of inodes used to represent directory
entries can increase.
• Inode allocation and management can contribute to storage fragmentation.
REVIEW QUESTIONS
1. Write short notes on (1) Swapping and (2) Memory Allocation.
2. What is dynamic loading and dynamic linking? Explain.
3. Describe the following memory allocation methods: (1) Single partition
allocation and (2) Multiple partition allocation.
4. What is paging? Explain.
5. Explain segmentation with paging.
6. Write short notes on (i) Paging and (ii) Compaction.
7. What is swapping? Explain the swap-in and swap-out process with a well-
labelled diagram.
8. Explain memory management requirements.
9. Explain: (i) Logical vs. physical address space and (ii) Internal vs. external
fragmentation.
10. Explain the single partition allocation mechanism with an example.
11. Explain the concept of paging.
12. What is Paging? Explain. Write the advantages and disadvantages of paging.
13. What is Segmentation? Explain. Write advantages of segmentation.
14. What is Long Term Scheduling?
15. Explain Relocation.
16. Explain the method of multiple partition memory management.
17. Write a short note on 'relocation and protection'.
18. What is disk space management? Explain record blocking.
19. Differentiate between logical and physical address space.
20. Differentiate between paging and segmentation.
UNIT – IV
INTRODUCTION
Linux is a community of open-source Unix-like operating systems that are
based on the Linux Kernel. It was initially released by Linus Torvalds on September
17, 1991. It is a free and open-source operating system and the source code can be
modified and distributed to anyone commercially or noncommercially under the
GNU General Public License.
Initially, Linux was created for personal computers and gradually it was
used in other machines like servers, mainframe computers, supercomputers, etc.
Nowadays, Linux is also used in embedded systems like routers, automation
controls, televisions, digital video recorders, video game consoles, smartwatches,
etc. The biggest success of Linux is Android (operating system) it is based on the
Linux kernel that is running on smartphones and tablets. Android Linux has the
largest installed base of all general-purpose operating systems. Linux is generally
packaged in a Linux distribution.
Kernel: Kernel is the core of the Linux-based operating system. It virtualizes the
common hardware resources of the computer to provide each process with its
virtual resources. This makes the process seem as if it is the sole process running
on the machine. The kernel is also responsible for preventing and mitigating
conflicts between different processes. Different types of the kernel are:
Monolithic Kernel
Hybrid kernels
Exo kernels
Microkernels
System Library: These are the special types of functions that are used to
implement the functionality of the operating system.
Shell: It is an interface to the kernel which hides the complexity of the kernel’s
functions from the users. It takes commands from the user and executes the
kernel’s functions.
Hardware Layer: This layer consists of all peripheral devices like RAM/ HDD/
CPU, etc.
Advantages of Linux
➢ The main advantage of Linux is that it is an open-source operating system.
This means the source code is easily available for everyone and you are allowed
to contribute, modify, and distribute the code to anyone without any
permissions.
➢ In terms of security, Linux is more secure than any other operating system. It
does not mean that Linux is 100 percent secure, it has some malware, but is
less vulnerable than any other operating system. So, it does not require any
anti-virus software.
➢ The software updates in Linux are easy and frequent.
➢ Various Linux distributions are available so that you can use them according to
your requirements or according to your taste.
➢ Linux is freely available to use on the internet.
Disadvantages of Linux
➢ It is not very user-friendly. So, it may be confusing for beginners.
➢ It has small peripheral hardware drivers as compared to Windows.
LOGOUT- To shut down the normal user terminal of system commands may
vary from different versions of the operating system. One may try following
commands to shutdown
➢ <ctrl> + d
➢ <ctrl> + z
➢ Logout
➢ logoff
On executing the above command, it disconnects our terminal from the
server. When a user logs out, the following message may appear on the screen.
Safe to power off
Or
Press any key to reboot
There are two ways of logging out
1. Logging Out of the system from a (text) terminal- To log out (exit) of a
text terminal, type logout.
2. Logging Out of the System from an X-Windows session- Use the mouse
to exit an X-Windows session; click the right button and choose the “Logout” or
“Exit” option.
ANATOMY OF LINUX OS
The Linux operating system's architecture mainly contains some of the
components: the Kernel, System Library, Hardware layer, System, and Shell utility.
Microkernels
Exo kernels
Hybrid kernels
Shell- It is an interface between the kernel and the user. It can afford the services
of kernel. It can take commands through the user and runs the functions of the
kernel. The shell is available in distinct types of OSes. These operating systems are
categorized into two different types, which are graphical shells and command-line
shells.
The graphical line shells facilitate the graphical user interface, while the
command line shells facilitate the command line interface. Thus, both of these
shells implement operations. However, the graphical user interface shells work
slower as compared to the command-line interface shells.
There are a few types of these shells which are categorized as follows:
Korn shell
Bourne shell
C shell
POSIX shell
DIRECTORY STRUCTURE
In Linux/Unix operating system, everything is a file, even directories are
files, files are files, and devices like mice, keyboards, printers, etc, are also files.
Here we are going to see the Directory Structure in Linux.
Directory Files- These types of files are a warehouse for other file types. It may
be a directory file within a directory (subdirectory).
The base of the Linux/Unix file system hierarchy begins at the root and
everything starts with the root directory.
These are the common top-level directories associated with the root
directory:
/Directory- The root directory, denoted by a single forward slash (/), is the
uppermost directory in the Linux directory structure. It contains all directories,
sub-directories, and files on your Linux system. It’s from the root directory where
the Linux directory hierarchy starts.
/root Directory- The /root directory is the home directory for the root user,
which is also referred to as the root user’s home directory (and not as the root (/)
directory).
The root account, also referred to as the superuser, administrative user,
system administrator or just the root user, has all the access to commands and
system files in Linux.
/boot Directory- This is one of the most vital directories in a Linux system. As
the name implies, the boot directory contains Linux boot files such as the
bootloader, the kernel, and its associated files. Files such as ‘vmlinuz’ represent the
compressed image of the Linux kernel.
/etc Directory - The /etc directory contains system configuration files for all
the services, scripts, and third-party applications that are installed. This directory
is considered the nerve center of the Linux system.
/opt Directory - The /opt directory is a directory that is reserved for add-on
packages and third-party software applications that are not included by default in
the system’s official repositories.
For example, when you install applications such as Skype, Discord, Spotify,
and Java, to mention a few, they get stored in the /opt directory.
/tmp Directory - The /tmp directory is a directory that stores temporary files
and many programs use this directory to create lock files and keep the temporary
storage of data.
Do not delete files under the/tmp directory unless you know exactly what
you are doing! Many of these files are critical for presently running programs and
removing them may affect a system crash.
/usr Directory - The /usr directory ranks as one of the most important
directories due to the enormous amount of data it holds. The directory contains
system-wide read-only files. These include libraries, user binaries and their
documentation, programs, and system utilities.
/dev Directory- The /dev directory contains device files or special files for
devices that are attached to the system such as the hard drive, keyboard, and
mouse. As mentioned, these are not regular that a user can read and write to.
These are abstractions of standard devices that applications on your system
interact with through input and output system calls.
/mnt Directory- The /mnt directory along with its subdirectories is a directory
intended to be used as a temporary mount point for mounting storage devices such
as Hard disk drives, USB drives, and CDROMs.
/sys Directory- The /sys is a virtual file system that contains a set of virtual
files that provide an interface to the Linux kernel.
/lib Directory- The /lib directory stores all the essential standard libraries
required by user binaries in the /bin directory.
/srv Directory- The /srv directory is the service directory and is abbreviated as
‘srv‘. This directory contains server-specific and service-related files.
Exploring directories and their usability: We know that Linux is a
very complex system that requires an efficient way to start, stop, maintain, and
reboot a system, unlike the Windows operating system. In the Linux system, some
well-defined configuration files, binaries, and man pages information files are
available for every process.
/usr DIRECTORY
The /usr directory consists of several subdirectories that contain additional
UNIX commands and data files. It is also the default location of the user’s home
directories.
Although /usr is pronounced like the user, remember that it stands for Unix
System Resources. The /usr hierarchy should contain shareable, read-only data.
Some people choose to mount /usr as read-only.
The /usr/bin directory contains more UNIX commands. These commands
are used less frequently or are considered nonessential to UNIX system operation.
/usr/bin is a standard directory on Unix-like operating systems that contains most
of the executable files (i.e., ready-to-run programs) that are not needed for booting
(i.e., starting) or repairing the system.
The /usr/include directory contains header files for compiling C programs.
The /usr/lib directory contains more libraries and data files used by various
UNIX commands.
The /usr/spool directory contains various directories for storing files to be
printed, mailed, or passed through networks.
The /usr/tmp directory contains more temporary files.
The /usr/adm directory contains data files associated with system
administration and accounting. In particular, the /usr/adm/messages and
/usr/adm/Syslog files contain a record of system error messages, including those
sent to the system console. These files are especially useful for locating system
problems, including hardware problems. For example, an unusual number of disk
errors on a drive indicates a defective or misaligned drive. Because messages in the
file can accumulate rapidly, these files must be deleted periodically.
FILE TYPES
A file type helps us in identifying the type of content that is saved in the file.
Linux supports seven different types of files. These file types are the Regular file,
Directory file, Link file, Character special file, Block special file, Socket file, and
Named pipe file.
System data files or Special files- Linux treats all hardware devices
(such as hard drives, printers, monitors, terminal emulators, and CD/DVD drives)
as special files. This means that an application program can access and use files
and devices in the same way. This feature makes developing programs in Linux
easier and more flexible.
Linux places all special files or device files under the /dev directory. There
are two types of special files: a character special file and a block special file. A
character special file represents a device that transfers data in bytes such as a
monitor or a printer. A block special file represents a device that transfers data in
blocks, such as a hard drive.
Link files- Link files allow us to use a file with a different filename and from a
different location. For this, we use link files. A link file is a pointer to another file.
There are two types of links: a hard link and a symbolic or soft link.
A hard link creates a mirror copy of the original file. A hard link cannot be
created to a directory or a file on another filesystem. A soft or symbolic link creates
a pointer to the original file. A soft link can be created to a directory or a file on
another filesystem.
Named pipe files- Linux allows us to send the output of any process or
command to another process or command as the input. This feature is known as
the pipe. Pipes work only when both processes are started by the same user and
exist in the same parent process space.
If processes are executed under different user names and permissions, then
standard pipes do not work. In such circumstances, named pipes are used. Named
pipes are similar to the standard pipes except that they can be accessed as part of
the filesystem.
Named pipe files are empty pipe files. The kernel processes named pipe files
without writing them to the file system. Named pipe files can exist anywhere in the
file system. Named pipe files are also known as the FIFO (First In First Out) files.
Executable files- An executable file is a file that is used to perform various
functions or operations on a computer. Unlike a data file, an executable file cannot
be read because it's compiled. On an IBM-compatible computer, common
executable files are .BAT, .COM, .EXE, and .BIN.
SHELL
The shell can be defined as a command interpreter within an operating
system like Linux/GNU or Unix. It is a program that runs other programs. The
shell facilitates every user of the computer as an interface to the Unix/GNU Linux
system. Hence, the user can execute different tools/utilities or commands with a
few input data.
The shell sends the result to the user over the screen when it has completed
running a program, which is the common output device. That's why it is known as
a "command interpreter".
The shell is not just a command interpreter. Also, the shell is a programming
language with complete constructs of a programming language such as functions,
variables, loops, conditional execution, and many others.
Category of Shell- Broadly, the shell is categorized into two main categories
which are explained below:
Types of Shell- There are various types of shells which are discussed as
follows:
a) Bash Shell- In the bash shell, bash means Bourne Again Shell. It is a default
shell over several distributions of Linux today. It is a sh-compatible shell. It
could be installed over Windows OS. It facilitates practical improvements on
the sh for interactive and programming use, which contains:
Job Control
Command-line editing
Shell Aliases and Functions
Unlimited size command history
Integer arithmetic in a base from 2-64
c) Ksh Shell- Ksh means Korn shell. It was developed and designed by David G.
Korn. Ksh shell is a high-level, powerful, and complete programming language,
and it is a reciprocal command language as well, just like various other
GNU/Unix Linux shells. The usage and syntax of the C shell are very similar to
that of the C programming language.
Key index
Function index and various others that we could find out within the
man pages.
All these shells do a similar job but take different commands and
facilitate distinct built-in functions.
e) Fish- Fish stands for "friendly interactive shell". It was produced in 2005. Fish
shell was developed to be fully user-friendly and interactive just like other
shells. It contains some good features which are mentioned below:
Web-based configuration
Man page completions
Auto-suggestions
Support for term256 terminal automation
Completely scripted with clean scripts
Create a User in Linux (Ubuntu)- There are two most common ways to
add a user to a Linux server.
Graphically through the user manager
By the useradd command (Terminal)
Step 4: Enter the user details like username and password and account type. We
can create two types of accounts, which are Standard and Administrator. The
standard account does not contain the sudo privilege. However, we can provide it
later.
Step 5: Now, we have successfully created a new user called JTP2. Consider the
below image.
Create a user with a home directory- Sometimes the newly created user
may or may not assign a home directory. To create a user and forcefully assign a
home directory to it, execute the below command:
Sudo useradd -m Demo
The above command will ask for the system administration password and
create a directory home/Demo for the user Demo.
Command Line Shell- Shell can be accessed by the user using a command
line interface. A special program called Terminal in linux/macOS or Command
Prompt in Windows OS is provided to type in human-readable commands such as
“cat”, “ls” etc. and then it is executed. The result is then displayed on the terminal
to the user. A terminal in the Ubuntu 16.4 system looks like this –
In the above screenshot, the “ls” command with the “-l” option is executed. It
will list all the files in the current working directory in a long listing format.
Working with a command line shell is a bit difficult for beginners because it’s hard
to memorize so many commands. It is very powerful; it allows users to store
commands in a file and execute them together. This way any repetitive task can be
easily automated. These files are usually called batch files in Windows and Shell
Scripts in Linux/macOS systems.
SHELL PROGRAM
Usually, shells are interactive that mean, they accept the command as input
from users and execute them. However, sometimes we want to execute a bunch of
commands routinely, so we have to type in all commands each time in the
terminal.
As a shell can also take commands as input from the file, we can write these
commands in a file and execute them in the shell to avoid this repetitive work.
These files are called Shell Scripts or Shell Programs. Shell scripts are similar
to the batch file in MS-DOS. Each shell script is saved with a .sh file extension, eg,
myscript.sh.
A shell script has syntax just like any other programming language. If you
have any prior experience with any programming language like Python, C/C++,
etc., it would be very easy to get started with it.
A shell script comprises the following elements-
➢ Shell Keywords – if, else, break, etc.
➢ Shell commands – cd, ls, echo, pwd, touch, etc.
➢ Functions
➢ Control flow – if..then..else, case and shell loops, etc.
Need for shell scripts- There are many reasons to write shell scripts:
➢ To avoid repetitive work and automation
➢ System admins use shell scripting for routine backups
➢ System monitoring
➢ Adding new functionality to the shell, etc.
BASH SHELL
BASH is an acronym for Bourne Again Shell, a punning name, which is a
tribute to Bourne Shell (i.e., invented by Steven Bourne).
Bash is a shell program written by Brian Fox as an upgraded version of the
Bourne Shell program 'sh'. It is an open-source GNU project. It was released in
1989 as one of the most popular shell distributions of GNU/Linux operating
systems. It provides functional improvements over Bourne Shell for both
programming and interactive uses. It includes command line editing, key bindings,
command history with unlimited size, etc.
In basic terms, Bash is a command line interpreter that typically runs in a
text window where the user can interpret commands to carry out various actions.
The combination of these commands as a series within a file is known as a Shell
Script. Bash can read and execute the commands from a Shell Script.
Bash is the default login shell for most Linux distributions and Apple's mac
OS. It is also accessible for Windows 10 with a version and default user shell in
Solaris 11.
When you first launch the shell, it uses a startup script located in the
.bashrc or .bash_profile file which allows you to customize the behavior of the shell.
When a shell is used interactively, it displays a $ when it is waiting for a command
from the user. This is called the shell prompt.
[username@host ~] $
If the shell is running as root, the prompt is changed to #. The superuser
shell prompt looks like this:
[root@host ~] #
Bash is very powerful as it can simplify certain operations that are hard to
accomplish efficiently with a GUI. Remember that most servers do not have a GUI,
and it is best to learn to use the powers of a command line interface (CLI).
Example: If you want to change the prompt so that it will display the time, the
date, the current working directory, and the $ prompt, then type PS1= [\t \d \w]\$‘
and press Enter. The shell prompt will change to display the following: [12:18:40
Sat Jan 21 aditi] $
Once you log out of your account, the shell prompt will return to the default
shell prompt.
COMMANDS
The Linux command is a utility of the Linux operating system. All basic and
advanced tasks can be done by executing commands. The commands are executed
on the Linux terminal. The terminal is a command-line interface to interact with
the system, which is similar to the command prompt in Windows OS. Commands
in Linux are case-sensitive.
Linux provides a powerful command-line interface compared to other
operating systems such as Windows and macOS. We can do basic work and
advanced work through its terminal. We can do some basic tasks such as creating
a file, deleting a file, moving a file, and more. In addition, we can also perform
advanced tasks such as administrative tasks (including package installation, and
user management), networking tasks (ssh connection), security tasks, and many
more.
Linux terminal is a user-friendly terminal as it provides various support
options. To open the Linux terminal, press the "CTRL + ALT + T" keys together, and
execute a command by pressing the 'ENTER' key.
file in the listing. The parameter tells the command to list the contents of the
/usr/doc directory instead of the current working directory.
A command parameter can be a file, a directory, or a period. Parameters are
not preceded by a dash. Parameters specify which file or directory command is to
be acted upon, or they can set a time limit in which the command is to be
executed.
How to find the Home Directory? There are multiple ways to access
and return to the home directory. Some commands are very helpful for the
directories such as cd, pwd, mkdir, pwd, ls, and rmdir. To access the home
directory graphically, open the Files application, and click on the Home option from
the left side menu. Consider the below image:
ls COMMAND
ls is a Linux shell command that lists directory contents of files and
directories. Some practical examples of the ls command are shown below.
This will open the last file you edited (i.e., second.txt)
1st Character – File Type: The first character specifies the type of the file. In
the example above, the hyphen (-) in the 1st character indicates that this is a
normal file. Following are the possible file type options in the 1st character of the ls
-l output.
Field Explanation
– normal file
d: directory
s: socket file
l: link file
Field 1- File Permissions: Next 9 characters specify the permission of the file.
Every 3 characters specify read, write, and execute permissions for the user(root),
group, and others respectively in order. Taking the above example, -rw-rw-r–
indicates read-write permission for the user(root), read permission for the group,
and no permission for others respectively. If all three permissions are given to the
user(root), group, and others, the format looks like this -rwxrwxrwx
Field 2- Number of links: Second field specifies the number of links for that file.
In this example, 1 indicates only one link to this file.
Field 3- Owner: The third field specifies the owner of the file. In this example,
this file is owned by the username ‘maverick’.
Field 4- Group: Fourth field specifies the group of the file. In this example, this
file belongs to the ”maverick” group.
Field 5- Size: Fifth field specifies the size of the file in bytes. In this example,
‘1176’ indicates the file size in bytes.
Field 6- Last modified date and time: The sixth field specifies the date and
time of the last modification of the file. In this example, ‘Feb 16 00:19’ specifies the
last modification time of the file.
Field 7- File name: The last field is the name of the file. In this example, the file
name is 1.c.
When you use “ls -l” you will get the details of the directory's content. But if
you want the details of the directory then you can use the -d option as., For
example, if you use ls -l /etc, it will display all the files under the etc directory. But,
if you want to display the information about the /etc/ directory, use the -ld option
as shown below.
$ ls -l /etc
$ ls -ld /etc
$ ls -A: To show the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent
directory).
$ ls -R /etc/apt: To show all the files recursively. When you do this from /, it
shows all the unhidden files in the whole file system recursively.
$ ls -i /etc/apt
mkdir COMMAND
In Linux, if you want to create a new directory, you can do so by using the
mkdir command. The mkdir command is used to make a directory in Linux.
Before jumping into how we can create a directory (or in Windows
terminology - a folder), first, let's see how the mkdir command works.
Using the mkdir Command- Here is the syntax for the mkdir command:
mkdir [OPTION] [DIRECTORY]
We can provide some options and the path of one or more directories while
using the mkdir command. To see the available options, you can run the following
command in your terminal: mkdir --help.
Now let's see how we can create a directory using the mkdir command in Linux.
The above command will create a new directory with the name studytonight
in the present working directory. You can use the ls command to list down the files
and directories in the current directory.
ls -l
Output- drwxrwxr-x 2 nobody nobody 6 Jul 2 17:29 studytonight
By default, the directory is created inside the current directory. If we want to
create the directory in some other location, then we can provide the complete path
while creating the directory.
Consider that we have a directory structure:
home: - someuser: - learn-linux - test-linux
And we are currently inside the directory someuser and want to create a new
directory with a name sample inside the directory test-linux, we can do so using
the following command:
mkdir text-linux/sample
Consider that you are currently inside the directory test-linux and you want
to create a directory inside the directory learn-linux with a name sample, we can do
this using the following command:
mkdir ../learn-linux/sample
The two dots, in the beginning, represents that we want to move out of the
test-Linux directory, then go inside the learn-linux directory and create a directory
named sample there.
Example 2: Remove the directories mydir1, mydir2, and mydir3, if they are
empty. If any of these directories are not empty, then an error message will be
printed for that directory, and the other directories will be removed.
rmdir mydir1 mydir2 mydir3
stat COMMAND
The stat is a command which gives information about the file and filesystem.
Stat command gives information such as the size of the file, access permissions
and the user ID and group ID, and birth time access time of the file. Stat command
has another feature, by which it can also provide the file system information. This
is the best tool to use when we want the information on any file.
The basic syntax of using the stat command is as follows:
stat --options filenames
Stat can accept one or more filenames as input to the stat command. For
example
stat /etc/resolv.conf
Output:
The information we get from stat- The following is the information we get
about the file when we run the stat command.
➢ File: The name of the provided file. If the provided file is a symlink, then the
name will be different.
➢ Size: The size of a given file in Bytes.
➢ Blocks: Total number of allocated blocks to the file to store on the hard disk.
➢ IO Block: The size of every allocated block in bytes.
➢ File type: The file may be of the following types: Regular files, special files,
directories, or symbolic links.
➢ Device: Device number in hexadecimal format.
➢ Inode: Inode number of the file.
➢ Links: Number of hard links of the file.
➢ Access: Access permissions in the numeric and symbolic methods.
➢ Context: The field stores the SELinux security context.
➢ Access: The last time at which the file was accessed.
➢ Modify: The last time at which the file was modified.
➢ Change: The last time the at which file’s attribute or content was changed.
➢ Birth: The time at which the file was created.
We can also provide the directory or file system as input to the stat
command as follows:
stat -f /
The information we get for the filesystem from the stat
Using Stat with Multiple Files- To get the information about multiple
files using the stat command, just mention the filenames separated by space:
stat locale.conf login.defs
This will show the information about the mentioned files.
cat COMMAND
cat(concatenate) command is very frequently used in Linux. It reads data
from the file and gives its content as output. It helps us to create, view, and
concatenate files. So let us see some frequently used cat commands.
Create a file
Command: $ cat > newfile
Output- Will create a file named newfile
Cat command can append the contents of one file to the end of another
file.
Command: $cat file1 >> file2
Output- Will append the contents of one file to the end of another file
Cat command can display content in reverse order using tac command.
Command: $tac filename
Output- Will display content in reverse order
If you want to use the -v, -E, and -T options together, then instead of writing
-vET in the command, you can just use the -A command line option.
Command $cat -A "filename"
Cat command if the file has a lot of content and can’t fit in the
terminal.
Command: $cat "filename" | more
Output- Will show that much content, that could fit in the terminal, and will
ask to show more.
Output- Will merge the contents of the file in respective order and will insert
that content in "merged_filename".
Cat command to display the content of all text files in the folder.
Command: $cat *.txt
Output- This will show the content of all text files present in the folder.
rm COMMAND
rm stands for remove here. The rm command is used to remove objects such
as files, directories, symbolic links, and so on from the file system like UNIX. To be
more precise, rm removes references to objects from the filesystem, where those
objects might have had multiple references (for example, a file with two different
names). By default, it does not remove directories. This command normally works
silently and you should be very careful while running the rm command because
once you delete the files then you are not able to recover the contents of files and
directories.
Syntax: rm [OPTION]... FILE...
Example- Let us consider 5 files having names a.txt, b.txt, and so on till e.txt.
$ ls
Output- a.txt b.txt c.txt d.txt e.txt
Options:
1. -i (Interactive Deletion): Like in cp, the -i option makes the command ask
the user for confirmation before removing each file, you have to press y to
confirm the deletion, any other key leaves the file un-deleted.
$ rm -i d.txt
mv COMMAND
mv stands for the move. mv is used to move one or more files or directories
from one place to another in a file system like UNIX. It has two distinct functions:
(i) It renames a file or folder.
(ii) It moves a group of files to a different directory.
No additional space is consumed on a disk during renaming. This command
normally works silently means no prompt for confirmation.
Syntax: mv [Option] source destination
Examples- Let us consider 4 files having names a.txt, b.txt, and so on till d.txt. To
rename the file a.txt to geek.txt (does not exist):
$ ls
a.txt b.txt c.txt d.txt
$ mv a.txt geek.txt
$ ls
b.txt c.txt d.txt geek.txt
If the destination file doesn’t exist, it will be created. In the above command,
mv simply replaces the source filename in the directory with the destination
filename (new name). If the destination file exists, then it will be overwritten and
the source file will be deleted. By default, mv doesn’t prompt for overwriting the
existing file.
For example, moving palash.txt to b.txt(exist):
$ ls
b.txt c.txt d.txt geek.txt
$ cat palash.txt
India
$ cat b.txt
Bachelor of Computer Application
$ mv geek.txt b.txt
$ ls
b.txt c.txt d.txt
$ cat b.txt
India
Options:
-i (Interactive): Like in cp, the -i option makes the command ask the user for
confirmation before moving a file that would overwrite an existing file, you have to
press y for confirm moving, any other key leaves the file as it is. This option doesn’t
work if the file doesn’t exist, it simply renames it or moves it to a new location.
$ ls
b.txt c.txt d.txt geek.txt
$ cat palash.txt
India
$ cat b.txt
bca
$ mv -i geek.txt b.txt
mv: overwrite 'b.txt'? y
$ ls
b.txt c.txt d.txt
$ cat b.txt
bca
-b(backup): With this option, it is easier to take a backup of an existing file that
will be overwritten as a result of the mv command. This will create a backup file
with the tilde character (~) appended to it.
$ ls
b.txt c.txt d.txt geek.txt
$ mv -b geek.txt b.txt
$ ls
b.txt b.txt~ c.txt d.txt
cp COMMAND
cp stands for a copy. This command is used to copy files or groups of files or
directories. It creates an exact image of a file on a disk with a different file name. cp
command requires at least two filenames in its arguments.
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
The first and second syntax is used to copy the Source file to the Destination
file or Directory. The third syntax is used to copy multiple Sources(files) to the
Directory.
cp command works on three principal modes of operation and these
operations depend upon the number and type of arguments passed in the cp
command:
Two file names: If the command contains two file names, then it copies the
contents of 1st file to the 2nd file. If the 2nd file doesn’t exist, then first it creates
one, and content is copied to it. But if it existed then it is simply overwritten
without any warning. So be careful when you choose the destination file name.
cp Src_file Dest_file
Suppose there is a directory named bca having a text file a.txt.
Example:
$ ls
a.txt
$ cp a.txt b.txt
$ ls
a.txt b.txt
directory with the same name, created if not exist but if already existed then it will
be overwritten.
cp Src_file1 Src_file2 Src_file3 Dest_directory
Suppose there is a directory named bca having a text file a.txt, b.txt and a directory
name new in which we are going to copy all files.
Example:
$ ls
a.txt b.txt new
Initially new is empty
$ ls new
$ cp a.txt b.txt new
$ ls new
a.txt b.txt
For this case, the last argument must be a directory name. For the above
command to work, Dest_directory must exist because the cp command won’t create
it.
Options: There are many options for the cp command, here we will discuss some
of the useful options:
Suppose a directory named bca contains two files having some content
named a.txt and b.txt. This scenario is useful in understanding the following
options.
$ ls bca
a.txt b.txt
$ cat a.txt
LOS
$ cat b.txt
LOS
-i (interactive): i stands for Interactive copying. With this option system first
warns the user before overwriting the destination file. cp prompts for a response, if
you press y then it overwrites the file and with any other option leaves it uncopied.
$ cp -i a.txt b.txt
-b (backup): With this option, the cp command creates the backup of the
destination file in the same folder with a different name and a different format.
$ ls
a.txt b.txt
$ cp -b a.txt b.txt
$ ls
a.txt b.txt b.txt~
-f(force): If the system is unable to open the destination file for writing operation
because the user doesn’t have writing permission for this file then by using the -f
option with the cp command, the destination file is deleted first, and then copying
of content is done from source to destination file.
$ ls -l b.txt
-r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt
Users, groups, and others don’t have writing permission.
Without the -f option, command not executed
$ cp a.txt b.txt
cp: cannot create regular file 'b.txt': Permission denied
With the -f option, the command is executed successfully
$ cp -f a.txt b.txt
-r or -R: Copying directory structure. With this option, the cp command shows its
recursive behavior by copying the entire directory structure recursively.
Suppose we want to copy the bca directory containing many files, and
directories into the bca1 directory (which does not exist).
$ ls bca/
a.txt b.txt b.txt~ Folder1 Folder2
Without the -r option, the error
$ cp bca bca1
cp: -r not specified; omitting directory 'geeksforgeeks'
With -r, execute successfully
$ cp -r bca bca1
$ ls bca1/
a.txt b.txt b.txt~ Folder1 Folder2
For the preservation of characteristics, you must be the root user of the
system, otherwise, characteristics change.
$ ls -l a.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt
$ cp -p a.txt c.txt
$ ls -l c.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt
As we see above both a.txt and c.txt (created by copying) have the same
characteristics.
Examples:
Copying using * wildcard: The star wildcard represents anything i.e., all files
and directories. Suppose we have many text documents in a directory and want to
copy them to another directory, it takes lots of time if we copy files 1 by 1 or the
command becomes too long if specify all these file names as the argument, but by
using * wildcard it becomes simple.
Initially, Folder1 is empty
$ ls
a.txt b.txt c.txt d.txt e.txt Folder1
$ cp *.txt Folder1
$ ls Folder1
a.txt b.txt c.txt d.txt e.txt
1. To list out all the users in Linux, use the awk command with the -F option.
Here, we are accessing a file and printing only the first column with the help of
print $1 and awk.
awk -F':' '{ print $1}' /etc/passwd
2. Using the id command, you can get the ID of any username. Every user has
an id assigned to it and the user is identified with the help of this id. By
default, this id is also the group id of the user.
id username
Example: id test
3. The command to add a user. useradd command adds a new user to the
directory. The user is given the ID automatically depending on which category
it falls in. The username of the user will be as provided by us in the command.
sudo useradd username
Example: sudo useradd geeks
4. Using the passwd command to assign a password to a user. After using this
command we have to enter the new password for the user and then the
password gets updated to the new password.
passwd username
Example: passwd geeks
8. You can change the user login name using the usermod command. The below
command is used to change the login name of the user. The old login name of
the user is changed to the new login name provided.
sudo usermod -l new_login_name old_login_name
Example: sudo usermod -c John_Wick John_Doe
9. The command to change the home directory. The below command changes
the home directory of the user whose username is given and sets the new home
directory as the directory whose path is provided.
usermod -d new_home_directory_path username
Example: usermod -d new_home_directory test
10. You can also delete a user name. The below command deletes the user whose
username is provided. Make sure that the user is not part of a group. If the
user is part of a group then it will not be deleted directly, hence we will have to
first remove him from the group and then we can delete him.
sudo userdel -r username
Example: sudo userdel -r new_geeks
CHANGING PASSWORD
Both Unix and Linux-like operating systems apply the passwd commands for
changing the password of the user. The passwd command is used for updating the
authentication password or token of the user which is saved in the /etc/shadow
file.
The passwd command changes the passwords for group accounts and users.
A normal user might only modify the password for her/his account, the root user
or super user might modify the password for other accounts.
The group administrator might modify the password for a group. Also, the
passwd command changes the information of an account like the user's full name,
the login shell of the user, or the interval and expiry date of the password.
Type the following passwd command for changing our password:
$ passwd
Initially, the user is requested for her/his previous password when one is
available. The password is further compared and encrypted. The password is
compared to the stored or saved password.
The user has only a single chance of entering an accurate password. The
root user is allowed to bypass this step. Hence, the forgotten passwords might be
modified. A newer password is checked for complexity. The passwords must be a
Options:
➢ -f, –force: This option forces the command to silently abort if the group with
the given already exists. If this option is used with the -g or –gid option and the
group id given already exists, the command forcefully ignores the given group
id and creates a new and unique group id.
Example- $groupadd developers
Goupadd: group ‘developers’ already exists.
$groupadd -f developers
$
➢ -g, –gid GID: This option is used to provide a group id (numeric) to the new
group, and it should be non-negative and unique unless explicitly created to be
non-unique (using the -o option). If this option is not used, the default id is
assigned, which is greater than every other group already present.
➢ -h, –help: Display help message and exit.
➢ -K, –key KEY=VALUE: Overrides /etc/login.defs defaults (GID_MIN,
GID_MAX and others). Multiple -K options can be specified. GID_MIN and
GID_MAX are the parameters set in /etc/login.defs which defines the minimum
and maximum values that a group id can take.
Example: groupadd -K GID_MIN=500 -K GID_MAX=700
➢ -o, –non-unique: This option permits to add of a group with a non-unique
GID.
➢ -p, –password PASSWORD: The encrypted password, as returned by crypt
(3). The default is to disable the password. This password is visible to the
users. You should make sure the password respects the system’s password
policy. The group passwords are stored in the “/etc/gshadow” file.
➢ -r, –system: Create a system group. The numeric identifiers of new system
groups are chosen in the SYS_GID_MIN-SYS_GID_MAX range, defined in
login.defs, instead of GID_MIN and GID_MAX.
➢ -R, –root CHROOT_DIR: Apply changes in the CHROOT_DIR directory and
use the configuration files from the CHROOT_DIR directory.