Introduction To Operating Systems
Introduction To Operating Systems
Introduction to Operating
Systems
Prepared by,
Dr. Swetha P C
Introduction
Any computer system can be divided into 4 broad categories:
• The Hardware
• The User
The Hardware
The Operating System
The Application Programs
What is software?
• Software is a set of instructions, data or programs used to operate
computers and execute specific tasks.
• The two main categories of software are application software and system
software.
✓An application is software that fulfils a specific need or performs tasks.
✓System software is designed to run a computer's hardware and provides a
platform for applications to run on top of.
Introduction to Operating System
• An Operating System (OS) is a program that manages the computer
hardware.
Operating System
Computer Resources like CPU,
Hardware Memory, I/O Devices
Functions of Operating System
i. User Interface
ii. File Management
iii. Hardware and Peripherals Management
iv. Processor Management
v. Interrupt Handling
vi. Security
vii. Memory Management
viii. Network Communication
What Operating Systems Do..?
Depends on the point of view.
• These setups are intended for individual use, allowing one person to
fully utilize the system's resources.
• Alongside the kernel, there are system programs, that are related to
the operating system but are not a part of the kernel, and the
application programs, that are unrelated to system operation.
• The CPU and device controllers can operate in parallel, competing for
access to the memory and a memory controller is present to co-ordinate
the access to the shared memory.
Interrupts
• In operating systems, an interrupt is a signal that temporarily halts
the CPU's current operations to allow it to respond to a particular
event or condition.
• Once the CPU addresses this event, it can return to its previous task.
Interrupts are crucial for efficient multitasking and real-time
processing.
Types of Interrupts
• Hardware Interrupts: Generated by hardware devices (e.g., keyboard,
mouse, disk drives) to signal that they require attention.
• Only one interrupt is generated per block, to tell the device driver
that the operation has completed, rather than the one interrupt per
byte generated for low-speed devices. While the device controller is
performing these operations, the CPU is available to accomplish other
work.
• The operations for inserting and removing items from a stack are
known as push and pop, respectively.
• Parameters, local variables, and the return address are pushed onto
the stack when a function is called; returning from the function call
pops those items off the stack.
• A queue, in contrast, is a sequentially ordered data structure that
uses the first in, first out (FIFO) principle: items are removed from a
queue in the order in which they were inserted.
• Whereas searching for a data item through a list of size n can require
up to O(n) comparisons, using a hash function for retrieving data from
a table can be as good as O(1), depending on implementation details.
001011101
Resources 2, 4, 5, 6, and 8 are unavailable; resources 0, 1, 3, and 7 are
available.
Computing Environments
1. Traditional Computing
• As computing has evolved, the distinctions between traditional
computing environments have blurred.
• Mobile devices can sync with PCs and connect to wireless and cellular
networks to access company web portals and other resources.
• Today, mobile devices are utilized for various purposes, including playing
music and videos, reading e-books, taking photos, and recording high-
definition video.
• The growth of applications for these devices is significant, with
developers leveraging unique features like GPS, accelerometers, and
gyroscopes.
• In this model, clients and servers are not distinguished from one another.
• Instead, all nodes within the system are considered peers, and each may
act as either a client or a server, depending on whether it is requesting or
providing a service.
• Users pay per month based on how much of those resources they
use.
• There are actually many types of cloud computing, including the following:
• Public cloud—a cloud available via the Internet to anyone willing to pay
for the services
• Private cloud—a cloud run by a company for that company’s own use
• Hybrid cloud—a cloud that includes both public and private cloud
components
• Software as a service (SaaS)—one or more applications (such as word
processors or spreadsheets) available via the Internet
• Platform as a service (PaaS)—a software stack ready for application use
via the Internet (for example, a database server)
• Infrastructure as a service (IaaS)—servers or storage available over the
Internet (for example, storage available for making backup copies of
production data)
6. Real-Time Embedded Systems
• The systems they run on are usually primitive, and so the operating
systems provide limited features.
Chapter 2
Operating System structure
Operating-System Services
• An OS provides an environment for the execution of programs.
• It provides certain services to programs and the users of those programs.
1. User interface
• Almost all operating systems have a user interface (UI). This interface can
take several forms. Most commonly, a graphical user interface (GUI) is
used.
• Another option is a command-line interface (CLI), which uses text commands and a
method for entering them.
2. Program execution
• The system must be able to load a program into memory and to run
that program. The program must be able to end its execution, either
normally or abnormally.
3. I/O operations
• A running program may require I/O, which may involve a file or an
I/O device. For specific devices, special functions may be desired.
• For efficiency and protection, users usually cannot control I/O devices
directly. Therefore, the operating system must provide a means to do
I/O.
4. File-system manipulation
• They also need to create and delete them by name, search for a given
file, and list file information.
• Errors may occur in the CPU and memory hardware (such as a memory
error or a power failure), in I/O devices (such as a parity error on disk, a
connection failure on a network, or lack of paper in the printer), and in the
user program (such as an arithmetic overflow or an attempt to access an
illegal memory location).
• For each type of error, the operating system should take the appropriate
action to ensure correct and consistent computing.
• We want to keep track of which programs use how much and what
kinds of computer resources.
• This record keeping may be used for accounting (so that users can be
billed) or simply for accumulating usage statistics.
• Advantages of GUIs
• User-Friendly
• Visual Interaction
• Multitasking
• Visual Feedback
3. Touch-Screen Interface
• A touch-screen interface is a type of user interface that allows users
to interact with a device through touch gestures on a screen.
• The user interface can vary from system to system and even from user
to user within a system; however, it typically is substantially removed
from the actual system structure. The design of a useful and intuitive
user interface is therefore not a direct function of the operating
system.
UNIT 2
CHAPTER 1
PROCESSES
Process
• A question that arises in discussing operating systems involves what to call
all the CPU activities.
• Early computers were batch systems that executed jobs, followed by the
emergence of time-shared systems that ran user programs, or tasks.
• However, the stack and heap sections can shrink and grow
dynamically during program execution.
• It contains many pieces of information associated with a specific process, including these:
✓ Process state. The state may be new, ready, running, waiting, halted, and so on.
✓ Program counter. The counter indicates the address of the next instruction to be executed for this
process.
✓ CPU registers. The registers vary in number and type, depending on the computer architecture.
They include accumulators, index registers, stack pointers, and general-purpose registers. Along
with the program counter, this state information must be saved when an interrupt occurs, to
allow the process to be continued correctly afterward when it is rescheduled to run.
✓CPU-scheduling information. This information includes a process
priority, pointers to scheduling queue.
• This single thread of control allows the process to perform only one
task at a time.
• Thus, the user cannot simultaneously type in characters and run the
spell checker.
• Resource Sharing: Threads within the same process can share resources like memory and files,
reducing the overhead of resource allocation.
• Economy: It's more economical in terms of system resource consumption to create and manage
threads as opposed to processes.
• Efficient Communication: Since threads share the same memory space, inter-thread
communication can be more efficient than inter-process communication.
Process Scheduling
• The objective of multiprogramming is to have some process running
at all times so as to maximize CPU utilization.
• If there are more processes than cores, excess processes will have to
wait until a core is free and can be rescheduled.
• An I/O-bound process is one that spends more of its time doing I/O
than it spends doing computations.
• Two types of queues are present: the ready queue and a set of wait
queues.
• The circles represent the resources that serve the queues, and the
arrows indicate the flow of processes in the system.
• A new process is initially put in the ready queue. It waits there until it
is selected for execution, or dispatched.
Queueing-diagram representation of process scheduling
• Once the process is allocated a CPU core and is executing, one of several
events could occur:
✓The process could issue an I/O request and then be placed in an I/O wait queue.
✓The process could create a new child process and then be placed in a wait queue
while it awaits the child’s termination.
✓The process could be removed forcibly from the core, as a result of an interrupt
or having its time slice expire, and be put back in the ready queue.
• In the first two cases, the process eventually switches from the
waiting state to the ready state and is then put back in the ready
queue.
• The goal of CPU scheduling is to optimize the use of the CPU and
ensure that all processes are executed efficiently and fairly.
• The aim is to keep the CPU as busy as possible, ideally striving for
100% utilization.
• A process migrates among the ready queue and various wait queues
throughout its lifetime.
• The role of the CPU scheduler is to select from among the processes
that are in the ready queue and allocate a CPU core to one of them.
• The CPU scheduler must select a new process for the CPU frequently.
• Later, the process can be reintroduced into memory, and its execution
can be continued where it left off.
• This scheme is known as swapping because a process can be
“swapped out” from memory to disk, where its current status is
saved, and later “swapped in” from disk back to memory, where its
status is restored.
• When a context switch occurs, the kernel saves the context of the old
process in its PCB and loads the saved context of the new process
scheduled to run.
5. Response Time: Response time is the time spent when the process
is in the ready state and gets the CPU for the first time. For
example, here we are using the First Come First Serve CPU
scheduling algorithm for the below 3 processes:
• Here, the response time of all the 3 processes are:
• P1: 0 ms
• P2: 7 ms because the process P2 have to wait for 8 ms during the execution of P1
and then after it will get the CPU for the first time. Also, the arrival time of P2 is 1
ms. So, the response time will be 8-1 = 7 ms.
• P3: 13 ms because the process P3 have to wait for the execution of P1 and P2 i.e.
after 8+7 = 15 ms, the CPU will be allocated to the process P3 for the first time.
Also, the arrival of P3 is 2 ms. So, the response time for P3 will be 15-2 = 13 ms.
• Response time = Time at which the process gets the CPU for the first time - Arrival
time
Types of CPU scheduling
There are two primary types of CPU scheduling:
➢Preemptive
• Preemptive scheduling is used when a process switches from the running
state to the ready state or from the waiting state to the ready state.
• The resources (mainly CPU cycles) are allocated to the process for a limited
amount of time and then taken away, and the process is again placed back
in the ready queue if that process still has CPU burst time remaining.
• That process stays in the ready queue till it gets its next chance to execute.
➢Non-preemptive
• Instead, it waits till the process completes its CPU burst time, and then it
can allocate the CPU to another process.
Parameter PREEMPTIVE SCHEDULING NON-PREEMPTIVE SCHEDULING
Once resources(CPU Cycle) are allocated to a
In this resources(CPU Cycle) are allocated to
Basic process, the process holds it till it completes its
a process for a limited time.
burst time or switches to waiting state
If a process having high priority frequently If a process with a long burst time is running
Starvation arrives in the ready queue, a low priority CPU, then later coming process with less CPU
process may starve burst time may starve
Overhead It has overheads of scheduling the processes It does not have overheads
Waiting Time Preemptive scheduling waiting time is less Non-preemptive scheduling waiting time is high
The OS has greater control over the The OS has less control over the scheduling
Process control
scheduling of processes of processes
Higher overhead due to frequent context Lower overhead since context switching is
Overhead
switching less frequent
• The dispatcher is the module that gives control of the CPU’s core to
the process selected by the CPU scheduler.
• The time it takes for the dispatcher to stop one process and start
another running is known as the dispatch latency.
• When the CPU is free, it is allocated to the process at the head of the
queue.
• The average waiting time under the FCFS policy, is often quite long.
• If the processes arrive in the order P1, P2, P3 and are served in FCFS
order, we get the result as shown in the following Gantt Chart.
• Once the CPU has been allocated to a process, that process keeps the
CPU until it releases the CPU, either by terminating or by requesting
the I/O.
• Each resource type consists of identical instances (e.g., four CPUs or two network
interfaces).
• Synchronization tools like mutex locks and semaphores are also considered
system resources and are common sources of deadlock.
• Threads must request resources before use and release them afterward, ensuring
that requests do not exceed available resources (e.g., not requesting two network
interfaces if only one exists).
Under the normal mode of operation, a thread may utilize a resource in
only the following sequence:
• Use. The thread can operate on the resource (for example, if the
resource is a mutex lock, the thread can access its critical section).
• For each resource that is allocated, the table also records the thread
to which it is allocated.
➢Hold and wait. A thread must be holding at least one resource and
waiting to acquire additional resources that are currently being
held by other threads.
➢No preemption. Resources cannot be preempted; that is, a
resource can be released only voluntarily by the thread holding it,
after that thread has completed its task.
➢Circular wait. A set {T0, T1, ..., Tn} of waiting threads must exist
such that T0 is waiting for a resource held by T1, T1 is waiting for a
resource held by T2, ..., Tn−1 is waiting for a resource held by Tn,
and Tn is waiting for a resource held by T0.
Livelock example
• Livelock is a situation in an operating system where two or more
processes continuously change their state in response to each other
without making any actual progress.
• Unlike a deadlock, where processes are stuck waiting for each other
indefinitely, in a livelock, the processes remain active and responsive
but fail to advance in their tasks.
Resource allocation graph
• A Resource Allocation Graph (RAG) is a graphical representation used in
operating systems to illustrate the allocation of resources to processes and to
help in detecting deadlocks.
• It visually maps the relationships between processes and the resources they
require, making it easier to understand resource allocation and contention.
• The set of vertices V is partitioned into two different types of nodes: T = {T1,
T2, ..., Tn}, the set consisting of all the active threads in the system, and R =
{R1, R2, ..., Rm}, the set consisting of all resource types in the system.
• A directed edge from thread Ti to resource type Rj is denoted by Ti →
Rj; it signifies that thread Ti has requested an instance of resource
type Rj and is currently waiting for that resource.
• Thread states:
◦ Thread T1 is holding an instance of resource type R2 and is waiting for
an instance of resource type R1.
◦ Thread T2 is holding an instance of R1 and an instance of R2 and is
waiting for an instance of R3.
◦ Thread T3 is holding an instance of R3.
• Given the definition of a resource-allocation graph, it can be shown
that, if the graph contains no cycles, then no thread in the system is
deadlocked.
• If each resource type has several instances, then a cycle does not
necessarily imply that a deadlock has occurred.
T1 → R1 → T2 → R3 → T3 → R2 → T1
T2 → R3 → T3 → R2 → T2
✓We can ignore the problem altogether and pretend that deadlocks
never occur in the system.
✓We can allow the system to enter a deadlocked state, detect it,
and recover.
• Deadlock prevention provides a set of methods to ensure that at least
one of the necessary conditions cannot hold. These methods prevent
deadlocks by constraining how requests for resources can be made.
• With this additional knowledge, the operating system can decide for
each request whether or not the thread should wait.
• Sharable resources do not require mutually exclusive access and thus cannot be involved
in a deadlock.
• Read-only files are a good example of a sharable resource. If several threads attempt to
open a read-only file at the same time, they can be granted simultaneous access to the
file.
• One protocol that we can use requires each thread to request and be
allocated all its resources before it begins execution. This is, of course,
impractical for most applications due to the dynamic nature of
requesting resources.
• To ensure that this condition does not hold, we can use the following
protocol. If a thread is holding some resources and requests another
resource that cannot be immediately allocated to it (that is, the
thread must wait), then all resources the thread is currently holding
are preempted. In other words, these resources are implicitly
released.
• The preempted resources are added to the list of resources for which
the thread is waiting. The thread will be restarted only when it can
regain its old resources, as well as the new ones that it is requesting.
• Alternatively, if a thread requests some resources, we first check
whether they are available. If they are, we allocate them.
• If they are not, we check whether they are allocated to some other
thread that is waiting for additional resources.
• If so, we preempt the desired resources from the waiting thread and
allocate them to the requesting thread.
• If the resources are neither available nor held by a waiting thread, the
requesting thread must wait.
• However, the fourth and final condition for deadlocks — the circular-
wait condition — presents an opportunity for a practical solution by
invalidating one of the necessary conditions.
• One way to ensure that this condition never holds is to impose a total
ordering of all resource types and to require that each thread
requests resources in an increasing order of enumeration.
• To illustrate, we let R = {R1, R2, ..., Rm} be the set of resource types.
We assign to each resource type a unique integer number, which
allows us to compare two resources and to determine whether one
precedes another in our ordering.
• Formally, we define a one-to-one function F: R → N, where N is the
set of natural numbers.