Unit 6
Unit 6
Process
An active instance of program.
Not only composed of program codes, but
also the data set, register contents, program
state (READY, RUNNING, WAITING,
NEW), resources, timers, file lists etc.
These dynamic values are kept in a data
structure called “Process control block”.
Process Control Block (PCB)
Also called Task Control Block (TCB)
Items stored :
Process Id (PID)
Process State
Program Counter
Registers Values
……
State Transition of an active process
during its lifetime
When all the necessary
Resources for this new
x ecution process is acquired.
n e
W h en a is g o t . New Ready
nd
comma
.
d led Quantum When the process is
h an
re expired Selected for execution
ts a
ev en by the OS.
s e
t ho
Wh
e n Running
p ro cess ts, When process finished its tasks.
o n o f the nal even
e cuti me exter
Waiting h e
e x
n the ait for so All al
lo
W
ee d s to w Terminate are re cated resou
leased rces
n I/O.
h a s
su c
Threads
The context block is quite large and need to
be saved in the memory while doing
switching. This is a time-consuming action.
Thread is also called “Light weight
process”. As the name suggest, it is light
weight in the sense that, its creation and
termination is easier than that of a process.
Threads
Threads of a same process share the same
memory space, global variables and
resources, though they have their own
program counter, register set, stack and
other state information.
This sharing characteristic makes threads
having no protection between them.
CPU scheduling
First Come First Serve
Shortest Job First
Shortest Remaining time
Round Robin
Priority Scheduling
Highest response ratio next
Multi-level Queues
CPU scheduling
Non-preemptive algorithm
Once a process is given the CPU, the process is
allowed to run till termination or until it
voluntarily relinquishes the CPU to wait for an
I/O.
Preemptive algorithm
Any external event might cause the scheduler to
select a new process for execution.
CPU scheduling
Burst Time – The continuous execution
time by the CPU, of a
process.
CPU scheduling(FCFS) B 1 3
C 3 5
Process A B C D E average D 5 7
Waiting 0 3 4 7 11 5.0 E 8 1
time
Turnaroud 4 6 9 14 12 9.0
time
A B C D E
A A A A B B B C C C C C D D D D D D D E
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
B BC C CD D DE E
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
A B C D E
Process Arrival Burst
Time Time
A 0 4
CPU scheduling(SJF-NP) B 1 3
C 3 5
Process A B C D E average D 5 7
Waiting 0 3 4 8 4 3.8 E 8 1
time
Turnaroud 4 6 9 15 5 7.8
time
E
A B C D
A A A A B B B C C C C C E D D D D D D D
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
A B C D E
Process Arrival Burst
Time Time
A 0 4
CPU scheduling(SJF-P) B 1 3
C 3 5
Process A B C D E average D 5 7
Waiting 0 3 5 8 0 3.2 E 8 1
time
A A A A B B B C E C C C C D D D D D D D
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
A B C D E
Process Arrival Burst
Time Time
A 0 4
CPU scheduling(RR-Q2) B 1 3
C 3 5
Process A B C D E average D 5 7
Waiting 2 5 9 8 5 5.8 E 8 1
time
A A B B A A C C B D D C C E D D C D D D
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
A B C D E D7C3E1
C5B1D7
CPU scheduling(Priority
Scheduling)
CPU scheduling
Time Time
A 0 4 1
(Priority Scheduling) B
C
1
3
3
5
4
2
D 5 7 1
E 8 1 3
Process A B C D E average
Waiting 0 16 1 4 8 5.8
time
Turnaro 4 19 6 11 9 9.8
ud time
A D E B
C
A A A A C C C C C D D D D D D D E B B B
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
B CB B DB DEB EB B
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
A B C D E
CPU scheduling
(Highest Response Ratio Next)
To overcome the possible starvation caused by
preemptive scheduling scheme.
Priority is calculated by a mathematic expression
which takes waiting time into account. An
example:
Priority=(service time + waiting time)/service time
The larger the priority value, the higher the
priority.
Since the priority value is dynamic, re-calculation
of this value is needed every time a new job is to
be selected.
CPU scheduling
(Multi-Level Queue)
Jobs are categorized into different priorities.
Jobs of equal priority are waiting in a queue
belong to that priority.
Only when all the jobs in higher priority queues
are all served will a job in lower priority queue
being served.
Jobs may move from 1 queue to another (from
lower priority to higher priority, or from higher
priority to lower priority), based on some pre-
defined criteria.
CPU scheduling
(Multi-Level Queue)
Timeout
… CPU
…
…
New Arrival
Queue N Job dispatch Job Completed
Timeout
Process synchronization
Suppose 2 processes share the same
variable counter.
P0 P1
R1=counter; R1=counter;
R1=R1+1; R1=R1-1;
counter=R1; counter=R1;
Critical section
A section of code which should be protected
against other processes entering if one has already
been in it.
To achieve such protection, as well as every other
processes has their chances of entering, 3 criteria
must be satisfied:
Mutual exclusion
Progress
Bounded waiting
Semaphore
It is a tool for handling critical section,
usually supported by hardware.
2 atomic operations can be applied to a
semaphore : signal and wait.
wait(S) signal(S) repeat
wait(S);
while (S<=0) S=S+1; critical section;
; signal(S);
S=S-1; remainder section;
until false;
Semaphore –
Producer/Consumer scenario
semaphore : access //initialized to 1
full //initialized to 0
empty //initialized to the max buffer size N
Producer: Consumer:
loop loop
produce data wait(full)
wait(empty) wait(access)
wait(access) read data from buffer
write data to buffer signal(access)
signal(access) signal(empty)
signal(full) consume data
end loop end loop
Deadlock
For a deadlock situation occurs, 4 criteria
must be met :
Mutual exclusion
No pre-emption
Circular wait
2 different approaches to handle
deadlock
Deadlock prevention – ensure that at least 1
of the 4 necessary criteria does not exist.
Deadlock avoidance – use additional
information about resource requirements by
different processes and to determine a
sequence of resource allocation so that
deadlock can be avoided.
Banker’s algorithm
Allocation Maximum Need (Max-Alloc)
R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4
P1 3 4 0 4 P1 7 6 9 9 P1 4 2 9 5
P2 2 3 5 2 P2 6 3 5 4 P2 4 0 0 2
P3 1 3 1 2 P3 4 3 2 4 P3 3 0 1 2
P4 1 0 1 0 P4 4 3 4 4 P4 3 3 3 4
P3 finished first
{4, 3, 4, 4}
Available : {3, 0, 3, 2}
Then comes P4
{5, 3, 5, 4} Safe sequence :
Is there a sequence that every process Next is P2 P3, P4, P2, P1
can finish its job? {7, 6, 10, 6}
Finally, P1
{10, 10, 10, 10}
Deadlock recovery