0% found this document useful (0 votes)
7 views67 pages

Process and Thread Management in OS

Unit II covers process and thread management in operating systems, detailing the lifecycle of processes, their attributes, and the various states they can be in. It explains process scheduling, including different types of schedulers and the advantages and disadvantages of various scheduling algorithms like FCFS and SJF. Additionally, the document discusses threads, their types, and the differences between user-level and kernel-level threads.

Uploaded by

priyanshuasitiz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views67 pages

Process and Thread Management in OS

Unit II covers process and thread management in operating systems, detailing the lifecycle of processes, their attributes, and the various states they can be in. It explains process scheduling, including different types of schedulers and the advantages and disadvantages of various scheduling algorithms like FCFS and SJF. Additionally, the document discusses threads, their types, and the differences between user-level and kernel-level threads.

Uploaded by

priyanshuasitiz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit II - Process and

Thread
Management
FACULTY: PIYUSHA VYAVAHARE
DEPT OF COMPUTER ENGINEERING
SUBJECT: OPERATING SYSTEMS
Introduction to Process
management
 Process management in an operating system is the
mechanism for handling and controlling running programs,
known as processes. It encompasses creating, scheduling,
synchronizing, and terminating processes, ensuring efficient
resource allocation and system stability.
 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 actually run the binary code, it
becomes a process.
 A process is an 'active' entity instead of a program, which is
considered a 'passive' entity.
How Does a Process Look Like in
Memory?
A process in memory is divided into several distinct sections,
each serving a different purpose. Here's how a process typically
looks in memory:

 Text Section: A text or code segment contains executable


instructions. It is typically a read only section
 Stack: The stack contains temporary data, such as function
parameters, returns addresses, and local variables.
 Data Section: Contains the global variable.
 Heap Section: Dynamically memory allocated to process
during its run time.
Attributes of a Process

 A process has several important attributes that help the


operating system manage and control it. These
attributes are stored in a structure called the Process
Control Block (PCB)
 (sometimes called a task control block). The PCB keeps
all the key information about the process, including:
 Process ID (PID): A unique number assigned to each
process so the operating system can identify it.
 Process State: This shows the current status of the
process, like whether it is running, waiting, or ready to
execute.
 Priority and other CPU Scheduling Information: Data that helps
the operating system decide which process should run next,
like priority levels and pointers to scheduling queues.
 I/O Information: Information about input/output devices the
process is using.
 File Descriptors: Information about open files files and network
connections.
 Accounting Information: Tracks how long the process has run,
the amount of CPU time used, and other resource usage
data.
 Memory Management Information: Details about the memory
space allocated to the process, including where it is loaded
in memory and the structure of its memory layout (stack,
heap, etc.)
States of Process
Process Management Tasks
 Process management is a key part in operating systems
with multi-programming or multitasking.
Process Operations
 Please remember a process goes through different
states before termination and these state changes
require different operations on processes by an
operating system.
 Process Creation
Process creation in an operating system (OS) is the act of
generating a new process. This new process is an instance of a
program that can execute independently.
 Scheduling
Once a process is ready to run, it enters the "ready queue." The
scheduler's job is to pick a process from this queue and start its
execution.
 Execution
Execution means the CPU starts working on the process. During this
time, the process might:
 Move to a waiting queue if it needs to perform an I/O operation.
 Get blocked if a higher-priority process needs the CPU.
 Killing the Process
 After the process finishes its tasks, the operating system ends it
and removes its Process Control Block (PCB).
States of a Process in Operating
Systems
 When you run a program (which becomes a process), it goes
through different phases before it completion. These phases, or
states, can vary depending on the operating system, but the most
common process lifecycle includes two, five, or seven states.
 The Two-State Model
The simplest way to think about a process's lifecycle is with just two
states:
When a new process is created, it starts in the not
running state. Initially, this process is kept in a program
called the dispatcher.

The scheduler chooses processes that must be run, the


dispatcher is in charge of passing these processes to the
CPU
2-step model

CPU scheduling is a process used by the operating system to decide


which task or process gets to use the CPU at a particular time.
The Five-State Model

 The five-state process lifecycle is an expanded version


of the two-state model. The two-state model works well
when all processes in the not running state are ready to
run.
 However, in some operating systems, a process may not
be able to run because it is waiting for something, like
input or data from an external device.
 To handle this situation better, the not running state is
divided into two separate states:
five-state process model:
The Seven-State Model
What is Process Scheduling?

 Process scheduling is the activity of the process manager


that handles the removal of the running process from the
CPU and the selection of another process based on a
particular strategy.
 Throughout its lifetime, a process moves between
various scheduling queues, such as the ready queue,
waiting queue or devices queue.
1. Long Term or Job Scheduler
 Long Term Scheduler loads a process from disk to main
memory for execution. The new process to the 'Ready
State’.
Short-Term or CPU Scheduler
 CPU Scheduler is responsible for selecting one process from
the ready state for running (or assigning CPU to it).
 STS (Short Term Scheduler) must select a new process for the
CPU frequently to avoid starvation.
 The CPU scheduler uses different scheduling algorithms to
balance the allocation of CPU time.
 It picks a process from ready queue.
 Its main objective is to make the best use of CPU.
 It mainly calls dispatcher.
 Fastest among the three (that is why called Short Term).
Medium-Term Scheduler
 Medium Term Scheduler (MTS) is responsible for moving a process
from memory to disk (or swapping).
 It reduces the degree of multiprogramming (Number of processes
present in main memory).
 A running process may become suspended if it makes an I/O
request. A suspended processes cannot make any progress towards
completion. In this condition, to remove the process from memory
and make space for other processes, the suspended process is
moved to the secondary storage. This process is called swapping
and the process is said to be swapped out or rolled out. Swapping
may be necessary to improve the process mix (of CPU bound and IO
bound)
 When needed, it brings process back into memory and pick up right
where it left off.
 It is faster than long term and slower than short term.
Comparison Among Scheduler
Preemptive and Non-Preemptive
Scheduling
Thread in Operating System
 A thread is a single sequence stream within a process. Threads are
also called lightweight processes as they possess some of the
properties of processes. Each thread belongs to exactly one
process.
 In an operating system that supports multithreading, the process
can consist of many threads. But threads can be effective only if
the CPU is more than 1 otherwise two threads have to context
switch for that single CPU.
 All threads belonging to the same process share - code section,
data section, and OS resources (e.g. open files and signals)
 But each thread has its own (thread control block) - thread ID,
program counter, register set, and a stack
 Any operating system process can execute a thread. we can say
that single process can have multiple threads.
Why Do We Need Thread?
Components of Threads

 These are the basic components of the Operating


System.

 Stack Space: Stores local variables, function calls,


and return addresses specific to the thread.
 Register Set: Hold temporary data and intermediate
results for the thread's execution.
 Program Counter: Tracks the current instruction
being executed by the thread.
Types of Thread in Operating
System
 Threads are of two
types. These are
described below.
 User Level Thread
 Kernel Level Thread
1. User Level Thread
 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.
Disadvantages of User-Level
Threads
 The operating system is unaware of user-level
threads, so kernel-level optimizations, like load
balancing across CPUs, are not utilized.
 If a user-level thread makes a blocking system
call, the entire process (and all its threads) is
blocked, reducing efficiency.
 User-level thread scheduling is managed by the
application, which can become complex and
may not be as optimized as kernel-level
scheduling.
Kernel-Level Thread

 In operating systems basically, the threads are the unit of


execution within a process. and the kernel level threads are
also kinds of threads which is directly handled via kernel
threads management.
 The Kernel-level threads are directly handled by the OS
directly whereas the thread's management is done by the
kernel.
 In the Kernel Level Threads, Each thread is self-organizing and
the kernel provides each thread with its own context with
information about the thread's status, such as its name, group,
and priority.
The example of
Kernel-level
threads are Java
threads, POSIX
threads, etc.
Features of Kernel-Level Threads

 It is basically implemented by Operating System.


 These threads have more Context switch time.
 Kernel-Level Threads can be determined as
multithreaded.
 Scheduling multiple threads that belong to the
same process on different processors is possible
in kernel-level threads.
Advantages of the Kernel-Level
Threads
 The Kernel Level Threads also behave like a
multithreaded.
 Kernel-Level Threads are more aware of remaining the
threads.
 They are managed by the kernel.
 In terms of context switching and scheduling the Kernel-
Level Threads are more efficient compared to user-level
threads.
 Kernel-Level Threads have the control of multiple
processors or cores to execute threads concurrently.
Disadvantages of the Kernel-Level
Threads
 As the Kernel-level threads have control over the
threads, so it's very complex when we implement any
thread synchronization techniques.
 The kernel is responsible for managing all threads and
creating and destroying threads will have more
overhead.
 The Kernel-level threads are slower to create and also
manage the threads compare to User-level threads.
Some characteristics of KLTs include

 Thread management: KLTs are managed directly by the operating system


kernel, which provides scheduling, synchronization, and communication
mechanisms.
 Kernel-level context switching: KLTs switch context at the kernel level,
which typically involves saving and restoring the entire processor context,
including registers, program counter, and stack pointer.
 Resource allocation: KLTs are allocated kernel-level resources such as CPU
time, memory, and I/O devices.
 Scalability: KLTs can take advantage of hardware resources such as multi-
core processors, as the kernel can schedule threads across multiple
processors.
 Performance: KLTs can provide better performance than user-level threads,
as they do not require any user-level library or support, and can take
advantage of kernel-level optimizations.
Difference between process and thread
FCFS - First Come First Serve CPU
Scheduling
 FCFS Scheduling is a non-preemptive algorithm, meaning once a
process starts running, it cannot be stopped until it voluntarily
relinquishes the CPU, typically when it terminates or performs I/O.
 This method schedules processes in the order they arrive, without
considering priority or other factors.
Example of FCFS CPU Scheduling

Consider the following table of arrival time


and burst time for three processes P1, P2
and P3
Now, let's calculate average waiting time
and turn around time

AT : Arrival Time
BT : Burst Time or
CPU Time
TAT : Turn Around
Time
WT : Waiting Time
Scenario 2: Processes with Different
Arrival Times
Advantages of FCFS

 The simplest and basic form of CPU Scheduling algorithm


 Every process gets a chance to execute in the order of its
arrival. This ensures that no process is arbitrarily prioritized
over another.
 Easy to implement, it doesn't require complex data
structures.
 Since processes are executed in the order they arrive,
there’s no risk of starvation
 It is well suited for batch systems where the longer time
periods for each process are often acceptable.
Disadvantages of FCFS
 As it is a Non-preemptive CPU Scheduling Algorithm, FCFS can
result in long waiting times, especially if a long process arrives
before a shorter one. This is known as the convoy effect, where
shorter processes are forced to wait behind longer processes,
leading to inefficient execution.
 The average waiting time in the FCFS is much higher than in
the others
 Since FCFS processes tasks in the order they arrive, short jobs
may have to wait a long time if they arrive after longer tasks,
which leads to poor performance in systems with a mix of long
and short tasks.
 Processes that are at the end of the queue, have to wait
longer to finish.
 It is not suitable for time-sharing operating systems where each
process should get the same amount of CPU time.
Shortest Job First or SJF CPU
Scheduling
 In the Shortest Job First scheduling algorithm, the
processes are scheduled in ascending order of their CPU
burst times, i.e. the CPU is allocated to the process with
the shortest execution time.
Variants of SJF Scheduling
1. SJF non-preemptive scheduling
 In the non-preemptive version, once a process is
assigned to the CPU, it runs into completion. Here, the
short term scheduler is invoked when a process
completes its execution or when a new process(es)
arrives in an empty ready queue.
SJF preemptive scheduling
 This is the preemptive version of SJF scheduling and is also
referred as Shortest Remaining Time First (SRTF) scheduling
algorithm.
 Here, if a short process enters the ready queue while a longer
process is executing, process switch occurs by which the
executing process is swapped out to the ready queue while the
newly arrived shorter process starts to execute.
 Thus the short term scheduler is invoked either when a new
process arrives in the system or an existing process completes its
execution.
Features of SJF Algorithm

 SJF allocates CPU to the process with shortest execution time.


 In cases where two or more processes have the same burst time,
arbitration is done among these processes on first come first serve
basis.
 There are both preemptive and non-premptive versions.
 It minimises the average waiting time of the processes.
 It may cause starvation of long processes if short processes continue
to come in the system.
Examples of Non-Preemptive SJF
Algorithm
 Example 1
 Suppose that we have a set of four processes that have
arrived at the same time in the order P1, P2, P3 and P4.
The burst time in milliseconds of each process is given by
the following table −
GANTT Chart for the set of
processes using SJF
 Process P3 has the shortest burst time and so it executes first. Then
we find that P1 and P4 have equal burst time of 6ms. Since P1
arrived before, CPU is allocated to P1 and then to P4. Finally P2
executes. Thus the order of execution is P3, P1, P4, P2 and is given by
the following GANTT chart −

Let us compute the average turnaround time and


average waiting time from the above chart.
Round Robin

 In RR scheduling, each process gets equal time slices (or time


quanta) for which it executes in the CPU in turn wise manner.
 When a process gets its turn, it executes for the assigned time
slice and then relinquishes the CPU for the next process in
queue.
 If the process has burst time left, then it is sent to the end of the
queue. Processes enter the queue on first come first serve basis.
 Round Robin scheduling is preemptive, which means that a
running process can be interrupted by another process and sent
to the ready queue even when it has not completed its entire
execution in CPU.
 It is a preemptive version of First Come First Serve (FCFS)
scheduling algorithm.
Features of Round Robin
Scheduling
 RR is a fair scheduling strategy where all processes get equal
share to execute in turn wise manner.
 It is a preemptive scheduling method where an executing
process must give up its control of the CPU once its time
quantum expires.
 Through RR scheduling strategy, none of the processes go into
starvation.
 It is widely used for its simple working principle.
 The performance of RR scheduling is vastly dependent on the
chosen time quantum.
Working Principle of Round Robin
Scheduling
 Any new process that arrives the system is inserted at the end of the
ready queue in FCFS manner.
 The first process in the queue is removed and assigned to the CPU.
 If the required burst time is less than or equal to the time quantum, the
process runs to completion. The scheduler is invoked when the
process completes executing to let in the next process in the ready
queue to the CPU.
 If the required burst time is more than the time quantum, the process
executes up to the allotted time quantum. Then its PCB (process
control block) status is updated and it is added to the end of the
queue. Context switch occurs and the next process in the ready
queue is assigned to the CPU.
 The above steps are repeated until there are no more processes in
the ready queue.
Example of Round Robin
Scheduling
 Let us consider a system that has four processes which
have arrived at the same time in the order P1, P2, P3
and P4. The burst time in milliseconds of each process
is given by the following table
GANTT CHART
 Let us consider time quantum of 2ms and
perform RR scheduling on this. We will draw
GANTT chart and find the average turnaround
time and average waiting time.
Advantages of Round Robin
Scheduling
 Round Robin scheduling is the most a fair scheduling
algorithm whereby all processes are given equal time
quantum for execution.
 Starvation of any process caused by indefinite waiting in
ready queue is totally eliminated in RR scheduling.
 It does not require any complicated method to calculate the
CPU burst time of each process prior to scheduling.
 It is pretty simple to implement and so finds application in a
wide range of situations.
 Convoy effect does not occur in RR scheduling as in First
Come First Serve CPU (FCFS) scheduling.
Disadvantages of Round Robin
Scheduling
Priority Process Scheduling in UNIX
and Windows
 Priority Scheduling is a non-preemptive or preemptive
scheduling algorithm where each process is assigned a
priority, and the CPU is allocated to the process with the
highest priority.
 Key Concepts: Lower number = Higher priority (in many
systems).
 Used in both Windows and UNIX OS but implemented
differently.
 Can lead to starvation; solved using aging.
PRIORITY SCHEDULING IN UNIX
 Priority scheduling in UNIX-like operating systems is a CPU
scheduling algorithm that assigns a priority level to each
process. The scheduler then prioritizes the execution of
processes with higher priority values over those with lower
priority values. This ensures that more critical or time-sensitive
tasks receive CPU time sooner.
 Key aspects of priority scheduling in UNIX:
Priority Assignment:
 Each process is assigned a numerical priority. In many UNIX
systems, a smaller numerical value indicates a higher priority
(e.g., a process with priority 0 is higher than a process with
priority 5).
 Preemption: UNIX systems typically employ preemptive
priority scheduling. This means that if a higher-priority
process becomes ready to run while a lower-priority
process is currently executing, the lower-priority process
will be interrupted (preempted) to allow the higher-priority
process to run immediately.
 Dynamic Priorities:
 Priorities in UNIX are often dynamic, meaning they can
change over time. This helps to prevent starvation, where
a low-priority process might never get to run. Techniques
like "aging" gradually increase the priority of processes
that have been waiting for a long time in the ready
queue.
 nice Command: Users can influence the priority of their
processes using the nice command. A lower nice value
corresponds to a higher priority (less "nice" to other
processes), while a higher nice value corresponds to a
lower priority (more "nice" )to other processes). Superusers
can set negative nice values, giving processes even
higher priority.
 System Processes: Kernel and system-critical processes
often run at very high, fixed priorities to ensure system
stability and responsiveness.
 Handling Equal Priorities: When multiple processes have
the same priority, UNIX systems typically use a round-robin
or First-Come, First-Served (FCFS) approach to schedule
them, ensuring fair allocation of CPU time among equally
prioritized tasks.

You might also like