OS_Unit-2
OS_Unit-2
GTU # 3140702
UNIT 2
Process and
Threads
Management
Process concept?
● Process is a program under execution.
● It is an instance of an executing program, including the current values of
the pro-
● gram counter, registers & variables.
● Process is an abstraction of a running program.
● each process has its own address space
1)text region: store code of process
2)data region :store dynamically allocated
memory
3)stack region :store instruction and
What is Process?
● Process can run to completion after getting all the resources and
requested hardware and software
● process may be a independent of other processes in the system
● It has its own private memory area and virtual CPU in which its run
● Process is an abstraction of a running program.
Difference between process and program
What is Process Control Block (PCB)?
? A Process Control Block (PCB) is a data structure maintained by the
operating system for every process.
? PCB is used for storing the collection of information about the processes.
3. Running: Process that is currently being executed and all the resources
are allocated
Time-
out
Event
Even Wait
t Blocked
Occu Queue
Process Creation
❏ When process finishes its normal execution that is delete using exit()
❏ process memory space become free
❏ OS passes the child exit status to the parent process
❏ deallocates the resources hold by the process
❏ Reasons
❏ Normal completion of operation
❏ memory is not available
❏ time slices expired
❏ parent termination
❏ FAilure of I/O
❏ Request from parent process
Process Termination
P
3 Parent
process
P P
5 6 Child
process
Handle
? When a process is created, the parent
process is given a special token called
handle. P
1 Parent
? This handle is used to control the child process
process. P
P
P
Child
2 4
? A process is free to pass this token to 3 process
some other process.
Multiprogramming
? The real CPU switches back and forth from process to process.
? This rapid switching back and forth is called multiprogramming.
? The number of processes loaded simultaneously in memory is called
degree of multiprogramming.
Multiprogramming execution
P1 P P Memor
2 3 yLogical
Program Counter
P1
Process
or
Logical Physical
Program Counter Program Counter
P2
Logical
Program Counter
P3
? There are three processes, one processor (CPU), three logical program
counter (one for each processes) in memory and one physical program
counter in processor.
? Here CPU is free (no process is running).
? No data in physical program counter.
Multiprogramming execution
P1 P P Memor
2 3 yLogical
Program Counter
P1
P Process
1 or
Logical Physical
Program Counter Program Counter
P2
Logical
Program Counter
P3
Logical
Program Counter
P3
Thread
s
What is Threads?
? Thread is light weight process created by a process.
? Thread is a single sequence stream within a process.
? Thread has it own
? program counter that keeps track of which instruction to execute next.
? system registers which hold its current working variables.
? stack which contains the execution history.
THREAD PROCESS
Lightweight process heavyweight process
operating system is not operating system is required for
required for thread switching thread switching
One thread can read ,write or each process operates
even completely clean another independently
threads stacks
If one thread is blocked and If one process is blocked then
waiting then second thread in other process can not execute
the same task can run until first process is unblocked
use fewer resources use more resources
Similarities between Process & Thread
? Like processes threads share CPU and only one thread is running at
a time.
? Like processes threads within a process execute sequentially.
? Like processes thread can create children's.
? Like a traditional process, a thread can be in any one of several
states: running, blocked, ready or terminated.
? Like process threads have Program Counter, Stack, Registers and
State.
Benefits/Advantages of Threads
? Threads minimize the context switching time.
? Use of threads provides concurrency within a process.
? Efficient communication.
? It is more easy to create and context switch threads.
? Threads can execute in parallel on multiprocessors.
? With threads, an application can avoid per-process overheads
⮩ Thread creation, deletion, switching easier than processes.
3. Running: Highest priority ready thread enters the running running state
6. Sleeping: Sleep for specified time after the expiration of time it enters
into ready state
User
Level
Threa
ds
Kernel
Level
Threa
ds
User level thread
? User-level threads are small and much faster than kernel level threads.
? They are represented by a program counter(PC), stack, registers
? there is no kernel involvement in synchronization for user-level threads.
? Created by runtime libraries that are transparent to the OS
? Do not invoke the Kernel for scheduling decision
? Also called many to one mapping
Advantages
● User-level threads are easier and faster to create
● They can also be more easily managed.
● User-level threads can be run on any operating system.
● There are no kernel mode privileges required for thread switching in user-level
threads.
Disadvantages
● Multithreaded applications in user-level threads cannot use multiprocessing to
their advantage.
Kernel level thread
? handled by the operating system directly and the thread management is
done by the kernel.
? slower than user-level threads.
? Thread controlled and created by system call
Advantages
● Multiple threads of the same process can be scheduled on different
processors
● It can also be multithreaded.
● If l thread is blocked, another thread of the same process can be scheduled
by the kernel.
Disadvantages
● Slower than the user level thread
Difference between user level and kernel level thread
objective
? Share time fairly
? prevent starvation of a process
? Use processor efficiently
? Have low overhead
SCHEDULING
characteristics of good scheduling algorithm
A new process is initially put in the Ready queue. It waits in the ready queue
until it is selected for execution(or dispatched). Once the process is assigned
to the CPU and is executing, one of the following several events can occur:
● The process could issue an I/O request, and then be placed in the I/O
queue.
● The process could create a new subprocess and wait for its termination.
● The process could be removed forcibly from the CPU, as a result of an
interrupt, and be put back in the ready queue.
Type of Scheduler
MEDIUM SHORT
TERM TERM
SCHEDUL SCHEDUL
ER ER
Type of Scheduler
1)LONG TERM SCHEDULER
● It decide which program must get into the job queue. From the job queue,
the Job Processor, selects processes and loads them into the memory for
execution.
● Primary aim is to maintain a good degree of Multiprogramming. An
optimal degree of Multiprogramming means the average rate of process
creation is equal to the average departure rate of processes from the
execution memory.
● used in real time OS
Preemptive Scheduling
the tasks are mostly assigned with their priorities. Sometimes it is important to run
a task with a higher priority before another lower priority task, even if the lower
priority task is still running. The lower priority task resumes when the higher priority
task finishes its execution.
Difference between Preemptive and non Preemptive
EXAMPLE.1
● p1+p2+p3+p4
● Average waiting time: (0+21+24+30)/4 =18.75ms
● Turnaround Time=Waiting Time+Burst Time
p1=0+21=21
p2=21+3=24
p3=24+6=30
p4=30+2=32
1)FCFS (FIRST COME FIRST SERVE)
● p1+p2+p3+p4
● Average waiting time: (0+21+24+30)/4 =18.75ms
EXAMPLE.2
PROCESS BURST TIME
P1 4
P2 7
P3 3
P4 3
P5 5
EXAMPLE.3
P1 10
P2 6
P3 12
P4 15
SOLUTION OF EXAMPLE.2
PROCESS BURST TIME/
priority
P1 4 3
P2 7 5
P3 3 2
P4 3 1
● Average waiting time: (0+4+11+14+17)/5 =9.2ms
P5 5 4
SOLUTION OF
EXAMPLE 3
PROCESS BURST TIME
P1 10
P2 6
P3 12
● Average waiting time: (0+10+16+28)/ =13.5ms
P4 15
2) SJF (SHORTEST JOB FIRST)
● It based on the length of their CPU cycle time
● reduce average time over FCFS
● non preemptive algorithm
ALGORITHM:-1) Sort all the process according to the arrival time.
1)Then select that process which has minimum arrival time and
minimum Burst time.
2)After completion of process make a pool of process which after till the
completion of previous process and select that process among the
pool which
PROCESS BURST TIME is having minimum Burst time.
EXAMPLE:
P1 5
P2 2
PROCESS WAITING TIME TURNAROUND TIME
P3 6
P1 6 6+5=11
P4 4
P2 0 0+2=2
Average waiting time: (6+0+11+2)/4 =4.75ms P3 11 11+6=17
P4 2 2+4=6
EXAMPLE 1.
PROCESS BURST
TIME/PRIORITY
P1 10 2
P2 6 1
P3 12 4
P4 15 3
EXAMPLE 2.
PROCESS BURST
TIME/priority
P1 4 2
P2 7 1
P3 3 3
P4 2 4
SOLUTION OF EXAMPLE
PROCESS WAITING TIME TURNAROUND TIME
PROCESS BURST TIME
P1 6 6+10=16
P1 10
P2 0 0+6=6
P2 6
P3 16 16+12=28
P3 12
P4 28 28+15=43
P4 15
P1 4 P1 5 5+4=9
P2 7 P2 9 9+7=16
P3 3 P3 2 2+3=5
P4 2 P4 0 0+2=2
EXAMPLE:
PROC BURST Priority
ESS TIME
P1 5 3
P2 2 4
P2 15 15+2=17
Average waiting time: (10+15+0+6)/4 =7.75msP3 0 0+6=6
P4 6 6+4=10
4) ROUND ROBIN SCHEDULING
● Round Robin is the preemptive process scheduling algorithm.
● Each process is provided a fix time to execute, it is called a quantum.
● Once a process is executed for a given time period, it is preempted and other
process executes for a given time period.
● Context switching is used to save states of preempted processes.
● QUANTUM SLICE 2 UNIT
PROCESS WAITING TIME TURNAROUND TIME
P1 0+(9-2)+(13-11)=9 9+5=14
P2 2+(11-4)=9 9+3=12
P3 4 4+1=5
P4 5 5+2=7
P5 7+(12-9)=10 10+3=13
DISADVANTAGE
● This algorithm spends more time on context switches.
● For small quantum, it is time-consuming scheduling.
● This algorithm offers a larger waiting time and response time.
● In this, there is low throughput.
● If time quantum is less for scheduling then its Gantt chart seems to be too big.
EXAMPLE
A 4
B 1
C 8
D 1
QUANTUM SLICE 2
UNIT
PROCESS BURST TIME
P1 6
P2 5
P3 2
P4 3
P5 7
SOLUTION EXAMPLE
PROCESS WAITING TIME TURNAROUND TIME
PROCESS BURST TIME
A 0+3+1+1=5 5+4=9
A 4
B 1 1+1=2
B 1
C 2+2+1+1=6 6+8=14
C 8
D 3 3+1=4
D 1
FCFS is inconvenient to use in the It is mainly designed for the time sharing
time sharing system. system and hence convenient to use.
The process is simply processed in the It is similar like FCFS in processing but
order of their arrival in FCFS. uses time quantum.
non non
non preemptive in
POLICY preemptive preemptive
preemptive nature
require
Average indefinite
DISADVANTA selection of starvation
waiting is postponement
GE good time problem
more of some job
slices
THREAD SCHEDULING
LOAD SHARING FCFS:-on arrival of advantage : disadvantage:
Tread are not thread it is place in No centralized High degree of
assigned to queue and next scheduler is coordination is
processor ,it thread selected required required
selects a thread when processor is load distribution is
from global queue idle equal
serving all
processors Smaller no of
load is evenly thread:- organized
distributed according to the
priority and selects
highest priority
thread
Preemptive
smallest number of
thread first:-
highest priority is
THREAD SCHEDULING
GANG SCHEDULING ● Group of related thread are scheduled as a
unit
CHARACTERISTICS
Here, the feasible schedules are identified dynamically (at run time). It carries
a certain fixed time interval and a process is executed if and only if satisfies
the time constraint.
T2 5 15
T3 1 5
ADVANTAGE: It is optimal
Give best CPU utilization
DISADVANTAGE: need dynamic priority
performance degrade under overloaded
EARLIEST DEADLINE FIRST SCHEDULING (EDF)