Operating System : Chapter Two
Processes and Process Management
Processes and Process Management
Course Objectives
• Enhance Efficiency & Reduce Costs: Streamlining tasks, minimizing waste, and optimizing resource use.
• Improve Quality & Consistency: Standardizing operations ensures predictable, high-quality outputs.
• Align with Strategy: Ensuring processes directly support the company's vision and goals.
• Risk Management: Identifying and mitigating operational risks.
• Boost Adaptability: Creating processes that are easier to update and scale with change.
• Increase Visibility & Accountability: Making roles and progress clear, improving communication.
• Foster Continuous Improvement: Creating a culture of ongoing refinement (e.g., using Lean, Six Sigma).
• Improve Stakeholder Satisfaction: Better products/services for customers and a more engaged workforce.
2
Process Concept or Definition
• A process is a running piece of code or a program in execution.
• The term process, used somewhat interchangeably with 'task' or 'job'.
• A process is more than the program code. It also includes the current activity,
as represented by the value of the program counter and the contents of the
processor’s registers.
• The execution of a process must progress in a sequential fashion.
• As a process executes, it changes state.
• A Process has:
• Program counter: instructions that need to be executed.
• Program counter: keeps track of “which instruction is being executed?”.
• Stack: stores temporary data. (like local variables, function parameters, return
addresses, etc).
• Heap: dynamically allocated memory during execution.
…and others.
3
Program vs Process
Note: Process ≠ Program:
• A program by itself is not a process. It is a static entity made up of program
statement while process is a dynamic entity.
• Program contains the instructions to be executed by processor.
• A program takes a space at single place in main memory and continues to stay
there. A program does not perform any action by itself.
• A program becomes a process when an executable file is loaded into memory.
• Examples of Program
• Nature: Passive, static file on disk (e.g., [Link]).
• Storage: Secondary memory (hard drive).
• Resources: None, it's just code.
• Lifespan: Long (until deleted).
• Example: The Word document file, the Photoshop installer. 4
Cont...
Example of Process
Nature: Active, dynamic execution of a program.
Storage: Main memory (RAM).
Resources: CPU, memory, I/O, Process Control Block (PCB).
Lifespan: Short (until task completion).
Example: An open tab in Chrome, a running antivirus scan, the running Word application.
5
The Components of A Process
• The program to be executed
• The data on which the program will execute
• The resources required by the program– such as memory and file (s)
• The status of execution
• program counter
• Stack
• Heap
• Process Control Block (PCB)
• Process ID (PID): Unique identifier.
• Memory Management Info: Page tables, limits, segment details.
• I/O Status: Open files, I/O devices used.
• Accounting Info: CPU time used, time limits.
6
Some of the OS activities related with process management
are:-
Process creation and deletion.
Process suspension and resumption.
Provision of process communication and resource sharing.
7
Process States
A process goes through a series of discrete/isolated process states.
New State: The new process being created.
Ready State: The new process waiting to be assigned to the processor.
Running State: A process is said to be running state, if it using the CPU at
that particular instant.
Blocked (or waiting) State: A process is said to be blocked, if it is
waiting for some event to be happen.
Terminated state: The process has finished execution
8
Flow Diagram of Process State
9
Process Control Block (PCB)
• A process in OS is represented by a data structure known as a Process Control
Block (PCB)or process descriptor.
• It can keep the track of process information.
• For each process, it holds information related to that process including :
Process state:- new, ready, running, waiting, halt (terminate), …
Program counter:- indicates address of the next instruction to be executed for
this process.
CPU registers:- include accumulators, index registers, stack pointers, and
general-purpose registers, plus any condition-code information
CPU-scheduling information
Memory-management information
Accounting information
I/O status information
10
Process States
• It is important to realize that only one process can be running on any
processor at any instant.
• But more than one process can be at ready and waiting state.
• Process State changes
New → Ready
• The process is created and loaded into memory.
• It waits in the ready queue for CPU allocation.
• Example: A user starts a program, and the OS prepares it for execution.
• Generally, in this state transition, OS creates a process and prepares the process
to be executed, then OS moved the process into ready queue.
11
Process State changes….
Ready → Running
• The CPU scheduler selects a process from the ready queue.
• The process gets CPU time and starts execution.
• Example: The OS assigns the CPU to a process that was waiting.
• OS selects one of the processes (Jobs) from ready Queue and move it from ready to
Running.
Running → Ready
(Preemption by OS Scheduler)
• The process is forced to stop due to time-sharing or priority scheduling.
• It moves back to the ready queue to wait for another CPU cycle.
• Example: In a Round Robin scheduling, a process gets a time slice but must wait again.
• When the time slot of the processor expired (or) If the processor received any interrupt
signal, the OS shifted Running -> Ready State. 12
Process State changes….
Running → Waiting
(I/O Request or Event Wait)
• The process requires an I/O operation (disk read, network request, etc.).
• It moves to the waiting queue until the operation completes.
• Example: A process requests to read a file from disk.
• A process is put into the waiting state, if the process need an event occur (or)
an I/O Device require.
Waiting → Ready
(I/O Completion)
• The required I/O operation is completed.
• The process moves back to the ready queue for CPU execution.
• Example: A file read operation completes, and the process is now ready to
continue execution.
• A process is moved to the ready state, if the event for which it is waiting has occurred. 13
Process State changes….
Running → Terminated
(Process Completion or Error)
• The process completes execution or encounters an error.
• It is removed from memory by the OS.
• Example: A user closes an application, or a process crashes due to an invalid
instruction.
• The currently running process is terminated if it has signaled its completion or it is
aborted.
• Note that: Running process may be aborted due to:
• Time exceeded,
• memory unavailable,
• access violation,
• protection Error,
• I/O failure and so on.
14
Process Control Block
• In the operating system, each process is represented by a process control block
also called Task control block.
• It contains many pieces‘ of information associated with specific process, such as:
• Process state: the State may be new, ready, running, and waiting, Terminated.
• Process ID: every process has to be represented by a unique ID.
• Program counter: indicate the address of the next line of instruction that has
to be executed for that particular process.
• CPU registers: registers include accumulators, stack pointers, general purpose
registers.
15
Cont..
examples of Process Pontrol Block 16
PCB:
• CPU-scheduling information: information about order of process execution.
• Memory-management information: represents the memory used by that
process.
• includes page tables, segmentation tables, value of base and limit registers.
• I/O status information:
• This information includes the list of I/O devices allocated to the process, a list of open
files, and so on.
• Accounting information: This information includes the amount of CPU and real time
used, time limits, account numbers, job or process numbers, and so on.
17
Representation of Process Scheduling
• A new process is initially put in the ready queue, once a process is allocated a CPU, the
following events may occur.
• A process could issue an I/O request
• A process could create a new process
• The process could be removed forcibly from CPU, as a result of an interrupt.
• When process terminates, it is removed from all queues.
• Process control block and its other resources are de-allocated
18
The Threads concept
• A Thread is a flow of execution through the process code, with its own
program counter, system register and stack. Threads are a popular way
to improve application performance through parallelism. A thread is
sometimes called a light weight process.
• Threads represent a software approach to improving performance of
operating system by reducing the overhead. Thread is equivalent to a
classical process. Each thread belongs to exactly one process and no
thread can exist outside a process. Each thread represents a separate
flow of control.
19
Thread components
Thread id
Program counter
Register set
Stack
Threads belonging to the same process share
its code
its data section
other OS resources
20
Advantages of Thread
1. Thread minimizes context switching time.
2. Use of threads provides concurrency within a process.
3. Efficient communication.
4. Economy- It is more economical to create and context switch threads.
5. Utilization of multiprocessor architectures
21
22
Processes and Threads
S.N. Process Thread
Process is heavy weight or resource Thread is light weight, taking lesser resources than
1
intensive. a process.
Process switching needs interaction with Thread switching does not need to interact with
2
operating system. operating system.
In multiple processing environments, each
All threads can share same set of open files, child
3 process executes the same code but has its
processes.
own memory and file resources.
If one process is blocked, then no other
While one thread is blocked and waiting, a second
4 process can execute until the first process is
thread in the same task can run.
unblocked.
Multiple processes without using threads use
5 Multiple threaded processes use fewer resources.
more resources.
In multiple processes each process operates One thread can read, write or change another
6
independently of the others. thread's data.
23
Single and Multithreaded Processes
In a Single Threaded Process Model, the representation of a process includes its
PCB, user address space, as well as user and kernel stacks.
• When a process is running. The contents of these registers are controlled by that
process, and the contents of these registers are saved when the process is not running.
• Traditionally there is a single thread of execution per process.
• Example: MSDOS supports a single user process and single thread.
• UNIX supports multiple user processes but only support one thread per
process.
24
Cont’d
In a multi-threaded environment
Multithreading refers to the ability on an operating system to support multiple threads of
execution within a single process
• There is a single PCB and address space,
• However, there are separate stacks for each thread as well as separate control blocks
for each thread containing register values, priority, and other thread related state
information
• Java run time environment is an example of one process with multiple threads.
• Examples of supporting multiple processes, with each process supporting
multiple threads
25
Cont..
26
Benefits of Multithreading
• Responsiveness
• one thread can give response while other threads are blocked or slowed down
doing computations
• Resource Sharing
• Threads share common code, data and resources of the process to which they
belong.
• Economy
• Creating and allocating processes is expensive, while creating threads is cheaper
as they share the resources of the process to which they belong.
• Scalability/utilization of multi-processor architectures
• The benefits of multithreading is increased in a multi-processor architecture,
where threads are executed in parallel. A single-threaded process can run on one
CPU, no matter how many are available.
27
Types of Thread
1. User Level
All of the work of thread management is done by the application and the
kernel is not aware of the existence of threads.
2. Kernel Level
Thread management done by the Kernel. There is no thread management
code in the application area. Kernel threads are supported directly by the
operating system. Any application can be programmed to be multithreaded.
28
Advantage of user level thread over Kernel level thread
1. Thread switching does not require Kernel mode privileges.
2. User level thread can run on any operating system.
3. Scheduling can be application specific.
4. User level threads are fast to create and manage.
Disadvantages of user level thread:
1. In a typical operating system, most system calls are blocking.
2. Multithreaded application cannot take advantage of multiprocessing.
29
Advantages of Kernel level thread
1. Kernel can simultaneously schedule multiple threads from the same
process on multiple process.
2. If one thread in a process is blocked, the Kernel can schedule another
thread of the same process.
3. Kernel routines themselves can multithreaded.
• Disadvantages:
1. Kernel threads are generally slower to create and manage than the user
threads.
2. Transfer of control from one thread to another within same process
requires a mode switch to the Kernel.
30
Inter-process communication(IPC)
• Processes frequently need to communicate with other processes.
• That communication between processes in the control of the OS is called
as Inter-process Communication or simply IPC.
• Inter-process communication is simple and easy when used occasionally.
• In some operating systems, processes that are working together often
share some common storage area that each can read and write.
• If there are many processes sharing many resources, then the mechanism
becomes cumbersome- difficult to handle.
• Threads are created to make this kind of resource sharing simple &
efficient
31
Race Condition
• The situation where several processes access and manipulate shared data
concurrently.
• The final value of the shared data depends upon which process finishes last.
• The key to preventing trouble here and in many other situations involving
shared memory, shared files, and shared everything else is to find some way
to prohibit more than one process from reading and writing the shared data at
the same time.
• To prevent race conditions, concurrent processes must coordinate or be
synchronized
32
Mutual Exclusion
Prohibit more than one process from reading and writing the shared data at
the same time.
Critical Section Problem
• If there are N processes (P1, P2, P3… Pn) competing to use some shared data.
• Each process has a code segment, called Critical Section (CS), in which the
shared data is accessed.
• A critical section is a piece of code in which a process or thread accesses a
common shared resource. The important features of the system are that –
ensure that when one process is executing in its CS, no other process is
allowed to execute in its CS. i.e. no two processes are executed in their
critical sections at the same time
33
Cont’d
• When a process executes code that manipulates shared data (or resource),
we say that the process is in its Critical Section (for that shared data).
• The execution of critical sections must be mutually exclusive: at any time,
only one process is allowed to execute in its critical section (even with
multiple processors).
• So each process must first request permission to enter its critical section.
• The section of code implementing this request is called the Entry Section
(ES).
• The critical section (CS) might be followed by a Leave/Exit Section (LS).
• The remaining code is the Remainder Section (RS).
34
General structure of process Pi(other process Pj)
Do {
Entry section
Critical section
Leave/exit section
Reminder section
} while (1);
35
Solution to Critical-Section Problem
• Mutual Exclusion - If process Pi is executing in its critical section,
then no other processes can be executing in their critical sections.
• Progress - If no process is executing in its critical section and there exist
some processes that wish to enter their critical section, then only those
processes that are not executed in their remainder sections can
participate in the decision on which will enter its critical section next ,
and this selection cannot be postponed indefinitely.
• Bounded Waiting - A bound must exist on the number of times that
other processes are allowed to enter their critical sections after a process
has made a request to enter its critical section and before that request is
granted.
36
Cont…
• Advantages
• Applicable to any number of processes on either a single processor or multiple
processors sharing main memory
• It is simple and therefore easy to verify
• It can be used to support multiple critical sections
• Disadvantages
• Busy-waiting consumes processor time
• Starvation is possible when a process leaves a critical section and more than
one process is waiting.
• Deadlock-If a low priority process has the critical region and a higher priority
process needs, the higher priority process will obtain the processor (CPU) to
wait for the critical region
37
How can several processes share one CPU?
• Os must make sure that processes do not interfere with each other.
This means By context Switching
• Most modern computer systems allow more than one process to be
executed simultaneously. This is called Multitasking systems.
• Context switching means switching the CPU to different processes. It
requires saving of the state of the current process into the PCB and
load the saved PCB of the previous or new process.
38
How does CPU switch between different processes?
• Dispatcher/Scheduler: means the inner-most portion of the OS that
runs processes without interference.
Scheduler supports and facilitates the following activities
Run process
Save its state.
Load state of another process
Run the new process and
Reload the suspended\previous process
39
When process scheduling is required?
• When more than one process is running, the operating system must
decide which one should first run.
• The part of the operating system concerned with this decision is called
the scheduler, and algorithm it uses is called the scheduling algorithm.
• In a multitasking and uniprocessor system scheduling is needed
because more than one process is in ready and waiting state. It helps no
processes wait for a long time
40
Process Scheduling Queues
• Scheduling is to decide which process to execute and when
• The objective of multi-programming
• To have some process running at all times.
• Timesharing: Switch the CPU frequently that users can interact with the
program while it is running.
Scheduling queues:
• Job queue – set of all processes in the system.
• Ready queue – set of all processes residing in main memory, ready and
waiting to execute.
• Device (waiting) queues – set of processes waiting for an I/O device.
• Each device has its own queue.
41
Three Types of Schedulers
1. Long Term Scheduler
It is also called job scheduler. Long term scheduler determines which
programs are admitted to the system for processing. Job scheduler
selects processes from the queue and loads them into memory for
execution. Process loads into the memory for CPU scheduler.
2. Short Term Scheduler
It is also called CPU scheduler. Main objective is increasing system
performance in accordance with the chosen set of criteria. It is the
change of ready state to running state of the process. CPU scheduler
selects from among the processes that are ready to execute and
allocates the CPU to one of them.
42
Cont..
Short term scheduler also known as dispatcher, execute most
frequently and makes the fine-grained decision of which process to
execute next. Short term scheduler is faster than long tern scheduler.
3. Medium Term Scheduler
Medium term scheduling is part of the swapping function. It removes
the processes from the memory. It reduces the degree of
multiprogramming. The medium-term scheduler is in charge of
handling the swapped out-processes.
43
Comparison Between Scheduler
Long Term Short Term Medium Term
1 It is job scheduler It is CPU Scheduler It is swapping
2 Speed is less than short term scheduler Speed is very fast Speed is in between both
3 It controls degree of multiprogramming Less control over degree of Reduce the degree of
multiprogramming multiprogramming.
4 Absent or minimal in time sharing system. Minimal in time sharing system. Time sharing system use medium
term scheduler.
5 It select processes from pool and load them into memory It select from among the processes Process can be reintroduced into
for execution. that are ready to execute. memory and its execution can be
continued.
6 Process state is (New to Ready) Process state is (Ready to Running) -
7 Select a good process, mix of I/O bound and CPU bound. Select a new process for a CPU -
quite frequently.
44
What The Scheduler Try To Achieve?
(Scheduling Criteria)
• Fairness: Each process gets its fair share of the CPU and no process can
suffer indefinite postponement/delay.
• Efficiency: Keep the system/CPU busy 100% of the time when possible.
• Response Time: Time from submission of the job until the first
response is produced
• Waiting Time :Time spent to get into memory and time spent waiting in
the ready queue.
• Turnaround: A time between the submissions of a job to the time of
completion. It is cpu time + Wait time.
• Throughput: Maximize the number of jobs processed per unit time.
45
The Purpose of a Scheduling algorithm
Maximum CPU utilization
Fair allocation of CPU
Maximum throughput
Minimum turnaround time
Minimum waiting time
Minimum response time
46
Various Times Related to the Process
47
Cont...
• Arrival Time :The time at which the process enters into the
ready queue is called the arrival time.
• Burst Time :The total amount of time required by the CPU to
execute the whole process is called the Burst Time. This does
not include the waiting time. It is confusing to calculate the
execution time for a process even before executing it hence the
scheduling problems based on the burst time cannot be
implemented in reality.
48
Cont...
• Completion Time : The Time at which the process enters into the
completion state or the time at which the process completes its
execution, is called completion time.
• Turnaround time : The total amount of time spent by the process
from its arrival to its completion, is called Turnaround time.
• Waiting Time : The Total amount of time for which the process
waits for the CPU to be assigned is called waiting time.
49
Two General Categories of Schedulers
• The Scheduling algorithms can be divided into two categories with
respect to how they deal with interrupts.
1. Preemptive Scheduling:-The strategy of allowing processes that are
logically running to be temporarily suspended is called Preemptive
Scheduling.
2. Non Preemptive Scheduling:-A scheduling discipline is non
preemptive if once a process has been given the CPU, the CPU
cannot be taken away from that process until the assigned process
completes its execution. Short jobs are made to wait by longer jobs.
50
Scheduling Algorithms
The following are some scheduling algorithms:
First Come First Served (FCFS) Scheduling.
Shortest Job First (SJF) Scheduling.
Shortest Remaining Time (SRT) Scheduling.
Round Robin Scheduling.
Priority Scheduling
51
First Come First Served (FCFS) Scheduling
• Jobs are executed on first come, first serve basis. i.e. Processes are
dispatched according to their arrival time on the ready queue.
• Easy to understand and implement.
• First-Come-First-Served algorithm is the simplest scheduling
algorithm. Processes are dispatched according to their arrival time on
the ready queue. Being a non preemptive discipline, once a process
gets a CPU, it runs to completion
52
Cont...
• The FCFS scheduling is fair in the formal sense or human sense of
fairness but it is unfair in the sense that long jobs make short jobs wait a
long period.
• FCFS scheme is not useful in scheduling interactive users because it
cannot guarantee good response time.
• The code for FCFS scheduling is simple to write and understand. One
of the major drawbacks of this scheme is that the average time is often
quite long.
53
Example
54
Cont...
55
Shortest-Job-First (SJF) Scheduling
• Shortest-Job-First (SJF) is a non-preemptive discipline in which
waiting job (or process) with the smallest estimated run-time-to-
completion is run first. In other words, when CPU is available, it is
assigned to the process that has smallest next CPU burst.
• The SJF algorithm favors for short jobs (or processes) at the expense
of longer ones. Since the SJF scheduling algorithm gives the minimum
average time for a given set of processes, it is probably optimal.
56
Cont...
57
Shortest Remaining Time First (SRTF)
• This Algorithm is the preemptive version of SJF scheduling.
• In SRTF, the execution of the process can be stopped after certain
amount of time. At the arrival of every process, the short term
scheduler schedules the process with the least remaining burst time
among the list of available processes and the running process.
• Once all the processes are available in the ready queue, No
preemption will be done and the algorithm will work as SJF
scheduling.
58
Example
• In this Example, there are five jobs P1, P2, P3, P4, P5 and P6. Their
arrival time and burst time are given below in the table.
59
Round Robin Scheduling
• In the round robin scheduling, processes are dispatched in a FIFO
manner but are given a limited amount of CPU time called a time- slice
or a quantum.
• If a process does not complete before its CPU-time expires, the CPU is
interrupted and given to the next process waiting in a queue.
60
Cont...
• The only interesting issue with round robin scheme is the length of the
quantum.
• Setting the quantum too short causes too many context switches and
lower the CPU efficiency.
• On the other hand, setting the quantum too long may cause poor
response time and approximates FCFS.
• So the slice time should not be too short and too long.
61
Cont...
62
Priority Scheduling
• The basic idea is straightforward: each process is assigned a priority,
and priority is allowed to run.
• Equal-Priority processes are scheduled in FCFS order or in RR.
Example:
63
Deadlock
• In a multiprogramming environment several processes may compete
for a finite number of processes. Under normal mode of operation, a
process may utilize a resource in only the following sequence: -
• Request
• Use
• Release
• A process requests resources; if the resources are not available at that
time, the process enters a wait state. It may happen that waiting
processes will never again change state, because the resources they
have requested are held by other waiting processes.
64
Necessary Conditions
A deadlock situation can arise if the following four conditions hold simultaneously in
a system:
• Mutual exclusion: only one process at a time can use the resource. If another
process requests that resource, the requesting process must be delayed until the
resource has been released.
• Hold and wait: There must exist a process that is holding at least one resource and
is waiting to acquire additional resources that are currently being held by other
processes.
• No preemption: Resources cannot be preempted; that is, a resource can be released
only voluntarily by the process holding it after that process has completed its task.
• Circular wait: There must exist a set {P0,P1P2,……..,Pn} of waiting processes such
that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is
held by P2, ….. , Pn-1 is waiting for a resource that is held by Pn, and Pn is waiting for
a resource that is held by P0.
65
Cont...
66
Mechanisms for handling deadlocks
Principally, there are Three different methods for dealing with the
deadlock problem:
1. We can use a protocol to ensure that the system will never enter a
deadlock state.
2. We can allow the system to enter a deadlock state and then recover.
3. We can ignore the problem all together, and pretend that deadlocks
never occur in the system. To ensure that deadlocks never occur, the
system can use either deadlock prevention or a deadlock-avoidance
scheme.
67
Deadlock Prevention
Deadlock prevention is a set of methods for ensuring that at least one
of the necessary conditions cannot hold.
• Mutual Exclusion – not required for sharable resources; must hold for
non-sharable resources.
• Hold and Wait – must guarantee that whenever a process requests a
resource, it does not hold any other resources.
• Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of
enumeration.
• No Preemption –If a process that is holding some resources requests
another resource that cannot be immediately allocated to it, then all
resources currently being held are released.
68
Deadlock Avoidance
• Deadlock avoidance requires that the operating system be given in advance
additional information concerning which resources a process will request and
use during its lifetime. With this additional knowledge, we can decide for
each request whether or not the process should wait.
Safe State: System is in safe state if there exists a safe sequence of all
processes.
• Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still
request can be satisfied by currently available resources + resources held by
all the Pj, with j<i.
• If Pi resource needs are not immediately available, then Pi can wait until all Pj have
finished.
• When Pj is finished, Pi can obtain needed resources, execute, return allocated
resources, and terminate.
• When Pi terminates, Pi+1 can obtain its needed resources, and so on.
69
Basic Facts
• If a system is in safe state no deadlocks.
• If a system is in unsafe state possibility of deadlock.
• Avoidance ensures that a system will never enter an unsafe state.
70
Deadlock Detection
• If a system does not employ either a deadlock-prevention or a
deadlock-avoidance algorithm, then a deadlock situation may occur.
• An algorithm that examines the state of the system to determine
whether a deadlock has occurred.
• An algorithm to recover from the deadlock.
Simply it is to mean that:-
• Allow system to enter deadlock state.
• Detection algorithm.
• Apply a recovery scheme.
71
72