PROCESS MANAGEMENT
Module-2
by
S Pramod Kumar
Assistant Professor,
[Link] ECE,KIT,
Tiptur
OS View of Processes
PCB
Fundamental State Transitions,
Threads, Kernel and User level Threads,
Non-preemptive scheduling- FCFS and SRN
Preemptive Scheduling- RR and LCN
Long term, medium term and short term
scheduling in a time sharing system
2
14-Sep-17 O.S.@K.I.T
1
A program is a set of functions and
procedures
Functions may be separate processes, or they
may constitute the code part of a single process
14-Sep-17 O.S.@K.I.T
In Operating System’s view, a processes is an execution of a
program
A process comprises six components:
(id, code, data, stack, resources, CPU state)
where
id is the unique id assigned by the OS
code is the code of the program (it is also called the text of a program)
data is the data used in the execution of the program, including data
from files
stack contains parameters of functions and procedures called during
execution of the program, and their return addresses
resources is the set of resources allocated by the OS
CPU state is composed of contents of the PSW and the general-
purpose registers (GPRs) of the CPU (we assume that the stack pointer
is maintained in a GPR)
14-Sep-17 O.S.@K.I.T
2
OS view of a process consists of two parts:
[Link], data, and stack of the process, and
information concerning memory and other
resources, such as files, allocated to it.
[Link] concerning execution of a program,
such as the process state, the CPU state including
the stack pointer
14-Sep-17 O.S.@K.I.T
14-Sep-17 O.S.@K.I.T
3
14-Sep-17
14-Sep-17
4
Context save function:
Saves CPU state in PCB, and saves information
concerning context
Changes process state from running to ready
Scheduling function:
Uses process state information from PCBs to select a
ready process for execution and passes its id to
dispatching function
Dispatching function:
Sets up context of process, changes its state to
running, and loads saved CPU state from PCB into
CPU
14-Sep-17 O.S.@K.I.T
Fundamental functions to control processes
14-Sep-17 O.S.@K.I.T
5
An OS contains two processes P1 and P2, with P2 having a higher priority
than P1. Let P2 be blocked on an I/O operation and let P1 be running. The
following actions take place when the I/O completion event occurs for the
I/O operation of P2:
1. The context save function is performed for P1 and its state is changed to
ready.
2. Using the event information field of PCBs, the event handler finds that the
I/O operation was initiated by P2, so it changes the state of P2 from blocked
to ready.
3. Scheduling is performed. P2 is selected because it is the highest-priority
ready process.
4. P2’s state is changed to running and it is dispatched.
14-Sep-17 O.S.@K.I.T
The newly scheduled process may not have
any part of its address space in the cache, and
so it may perform poorly until it builds
sufficient information in the cache
14-Sep-17 O.S.@K.I.T
6
14-Sep-17 O.S.@K.I.T
A state transition for a process is a change in
its state
Caused by the occurrence of some event such as
the start or end of an I/O operation
14-Sep-17 O.S.@K.I.T
7
14-Sep-17 O.S.@K.I.T
14-Sep-17 O.S.@K.I.T
8
• A system contains two processes P1 and P2, P1 CPU bust time
15msec Followed by I/O operation for 100msec. P2 bust
time 30msec Followed by I/O operation for 60msec
14-Sep-17 O.S.@K.I.T
Process switching overhead
Intrinsic reasons (execution related overhead)
OS reasons (resource use related overhead)
Overhead can be reduced by using threads
within processes
14-Sep-17 O.S.@K.I.T
9
A thread is an alternative model of program
execution
A process creates a thread through a system
call
Thread operates within process context
Switching between threads incurs less
overhead than switching between processes
14-Sep-17 O.S.@K.I.T
Threads incur smaller context switching
overhead
Q: How is this achieved?
14-Sep-17 O.S.@K.I.T
10
A process context switch involves:
1. saving the context of the process in operation
2. saving its CPU state
3. loading the state of the new process
4. loading its CPU state
A threads is a program execution within the
context of a process; many threads can be
created within the same process
Switching between threads of the same process
involves much less context switching overhead
14-Sep-17 O.S.@K.I.T
Where are threads useful?
If two processes share the same address
space and the same resources, they have
identical context
-- switching between these processes
involves saving and reloading of their
contexts. This overhead is redundant.
In such situations, it is better to use threads.
14-Sep-17 O.S.@K.I.T
11
14-Sep-17 O.S.@K.I.T
12
Kernel-Level Threads
Threads are managed by the kernel
User-Level Threads
Threads are managed by thread library
Hybrid Threads
Combination of kernel-level and user-level
threads
14-Sep-17 O.S.@K.I.T
A kernel-level thread is like a process except that
it has a smaller amount of state information
Switching between threads of same process
incurs the overhead of event handling
14-Sep-17 O.S.@K.I.T
13
Fast thread switching because kernel is not involved
Blocking of a thread, blocks all threads of the process
Threads of a process: No concurrency or parallelism
14-Sep-17 O.S.@K.I.T
• Can provide a combination of parallelism and low overhead
14-Sep-17 O.S.@K.I.T
14
A process is a model of execution of a
program
Can create other processes by making requests to
the OS through system calls
▪ Each of these processes is called its child process
14-Sep-17 O.S.@K.I.T
To control operation of the process, OS uses
notion of a process state
Ready, running, blocked, terminated, suspended
OS keeps information concerning each process
in a process control block (PCB)
Process state and CPU state associated with process
Scheduler selects a ready process and dispatcher
switches CPU to selected process through
information found in its process context and the PCB
14-Sep-17 O.S.@K.I.T
15
A thread is an alternative model of execution of
a program
Overhead of switching between threads is much less
than the overhead of switching between processes
Three models of threads:
Kernel-level threads
User-level threads
Hybrid threads
Thread models have different implications for
switching overhead, concurrency, and
parallelism
14-Sep-17 O.S.@K.I.T
14-Sep-17 O.S.@K.I.T
16
Scheduling Terminology and Concepts
• Scheduling is the activity of selecting the next
request to be serviced by a server
– In an OS, a request is the execution of a job or a
process, and the server is the CPU
O.S.
K.I.T.
Scheduling Terminology and Concepts
(continued)
O.S.
K.I.T.
17
• Turnaround time (ta) Time between the submission of a job
or process and its completion by the system. This concept is
meaningful for noninteractive jobs or processes only.
• Weighted turnaround (w) Ratio of the turnaround time of a
job or process to its own service time.
O.S.
K.I.T.
• A server always services a scheduled request
to completion
• Attractive because of its simplicity
• Some nonpreemptive scheduling policies:
– First-come, first-served (FCFS) scheduling
– Shortest request next (SRN) scheduling
O.S.
K.I.T.
18
O.S.
K.I.T.
FCFS Scheduling
O.S.
K.I.T.
19
Shortest Request Next (SRN)
Scheduling
May cause
starvation of
long processes
O.S.
K.I.T.
Preemptive Scheduling Policies
• In preemptive scheduling, server can switch to
next request before completing current one
– Preempted request is put back into pending list
– Its servicing is resumed when it is scheduled again
• A request may be scheduled many times before it
is completed
– Larger scheduling overhead than with nonpreemptive
scheduling
• Used in multiprogramming and time-sharing OSs
O.S.
K.I.T.
20
O.S.
K.I.T.
Round-Robin Scheduling with Time-
Slicing (RR) In this example, δ = 1
O.S.
K.I.T.
21
Example: Variation of Response Time
in RR Scheduling
• rt for a request may be higher for smaller
values of δ
Time slice 5 ms 10 ms 15 ms 20 ms
Average rt for subsequent subrequest (ms) 280 240 240 220
O.S.
K.I.T.
Least Completed Next (LCN)
Issues:
- Short processes will finish
ahead of long processes
- Starves long processes of
CPU attention
- Neglects existing processes
if new processes keep
arriving in the system
O.S.
K.I.T.
22
Scheduling in Practice
• To provide a suitable combination of system
performance and user service, OS has to adapt its
operation to the nature and number of user
requests and availability of resources
– A single scheduler using a classical scheduling policy
cannot address all these issues effectively
• Modern OSs employ several schedulers
– Up to three schedulers
• Some of the schedulers may use a combination of
different scheduling policies
O.S.
K.I.T.
• These schedulers perform the following
functions:
– Long-term: Decides when to admit an arrived process
for scheduling, depending on:
• Nature (whether CPU-bound or I/O-bound)
• Availability of resources
– Medium-term: Decides when to swap out a process
from memory and when to load it back, so that a
sufficient number of ready processes are in memory
– Short-term: Decides which ready process to service
next on the CPU and for how long
• Also called the process scheduler, or scheduler
O.S.
K.I.T.
23
Example: Long, Medium-, and Short-
Term Scheduling in Time-Sharing
O.S.
K.I.T.
24