You are on page 1of 24

TM2033 Platform Technology

Lecture # 6
Process Management

By Dr. Faizan Qamar

1
The Journey
1 Computer 2 CPU, Memory 3 ISA 4 Computer
System System and Input
Performance
Output

6 Process 7 Memory and File


8 Operating
5 Operating System System Design
Management Management

9 Mobile Computing 10 Parallel Computing


Platform

2
Definition
Process Management
⚫ The basic unit of software that the operating system deals with is … a
process.
⚫ A process is defined as a running program (It exists in memory, is
performing some action and can be controlled by a user, an
application or the operating system).
⚫ A process needs certain resources, including CPU time, memory, files,
and I/O devices, to accomplish its task.
⚫ A program is only regarded as a process when it is being executed.
Examples of processes are :
⚫ A game being played
⚫ MS Word running
⚫ Data being transmitted along a network
⚫ Antivirus software running
3
Creation of a process
⚫ When a new process is added,
O/S builds various data structures
to manage the newly added
process. It also allocate space to
the process in memory.

⚫ Reasons to create a new process


◦ New batch job
◦ New user logs on
◦ Created by O/S to provide a
service
◦ E.g. Open Firefox and notice
Task Manager

4
Process Termination Reasons
• Normal: Completion
• Time limit Exceeded: interactive process
• Memory Unavailable: limit on max no of processes
• Location Violation: access to memory location that is not allowed
• Protection Error: writing to read only file
• Arithmetic Error: divide by zero
• Time Overrun: waiting for I/O for infinite time
• I/O Failure: reading from line printer
• Invalid Instruction: branch to data area and trying to execute data
• Parent Termination

5
Process State diagram
Process State Possible Transitions
Processes can be any of the following states :

1. New - The process is being created but is not yet ready to execute.
2. Ready - The process is ready to execute and waiting to be Null  New : New process is
assigned to a processor. created
⚫ New  Ready
3. Running - Instructions are being executed by processor.
4. Waiting - The process is waiting for some event to occur (such
as an I/O completion or reception of a signal).
⚫ Ready  Running (Dispatch)
5. Terminated - The process has finished execution will be destroyed by
the OS.
⚫ Running  Terminated (Exit)
⚫ Running  Ready (interrupt)
⚫ Running  Waiting (I/O)
⚫ Waiting  Ready
⚫ Ready  Terminated (Running)
⚫ Waiting  Terminated (Suspension)

6
Reasons for process suspension

1. Swapping: O/S needs to release sufficient main memory to


bring in a process that is ready to execute.
2. Error: O/S may suspend a background process or it
suspects that process can cause an error.
3. Timing: Process exceed with periodic execution time.
4. Parent Process Request: To co-ordinate / examine the
child processes, parents suspends child process.
5. Interactive user input: User wants to suspend the process for
debugging purpose.

7
Process Control Block (PCB)
There is a Process Control Block (PCB) for each process, enclosing
all the information about the process. It is a data structure, which
contains the following :
⚫ Process State - It can be running, waiting etc.
⚫ Process ID and parent process ID.
⚫ CPU registers and Program Counter. Program Counter
holds the address of the next instruction to be executed for
that process.
⚫ CPU Scheduling information - Such as priority information and
pointers to scheduling queues.
⚫ Memory Management information – E.g. page tables or
segment tables.
⚫ Accounting information - User and Kernel CPU time
consumed, account numbers, limits, etc.
⚫ I/O Status information -Devices allocated, open file
tables, etc.

8
CPU scheduling
⚫ CPU scheduling is a process which allows one process to use the CPU while the
execution of another process is on hold (in waiting state) due to unavailability of any
resource like I/O etc, thereby making full use of CPU.
⚫ Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed.
⚫ The aim of CPU scheduling is to make the system efficient, fast and fair.

9
Types of Schedular
They are mainly responsible for selecting the jobs to be submitted into the system and deciding which
process to run.

There are 3 kinds of schedulers-


1.Long-term scheduler
2.Short-term scheduler
3.Medium-term scheduler

1. Long-term Scheduler-

The primary objective of long-term scheduler is to maintain a good degree of multiprogramming.


•Long-term scheduler is also known as Job Scheduler.
•It selects a balanced mix of I/O bound and CPU bound processes from the secondary memory (new
state).
•Then, it loads the selected processes into the main memory (ready state) for execution.

10
Types of Schedular
2. Short-term Scheduler-

The primary objective of short-term scheduler is to increase the system performance.


•Short-term scheduler is also known as CPU Scheduler.
•It decides which process to execute next from the ready queue.
•After short-term scheduler decides the process, Dispatcher assigns the decided process to the CPU
for execution.

3. Medium-term Scheduler-
The primary objective of medium-term scheduler is to perform swapping.
•Medium-term scheduler swaps-out the processes from main memory to secondary memory to free up
the main memory when required.
•After some time when main memory becomes available, medium-term scheduler swaps-in the
swapped-out process to the main memory and its execution is resumed from where it left off.

11
Process Scheduling
⚫ The act of determining which process in the ready state should be
moved to the running state is known as Process Scheduling.
⚫ For achieving this, the scheduler must apply appropriate rules for
swapping processes IN and OUT of CPU.
⚫ Some process can be re-introduced into memory after some time and
its execution can be continued where it left off.

12
Types of Process Scheduling
Schedulers fell into one of the two general categories :
Non pre-emptive scheduling :
⚫ Under non-preemptive scheduling, once the CPU has been allocated to a
process, the process keeps the CPU occupied until it releases the CPU
either by terminating or by switching to the waiting state.

Pre-emptive scheduling :
⚫ In this type of scheduling, the tasks are usually assigned with
priorities.
⚫ At times it is necessary to run a certain task that has a higher priority
before another task although it is running.
⚫ Therefore, the running task is interrupted for some time and resumed
later when the priority task has finished its execution.
13
Scheduling Criteria
There are many different criteria to check when considering the "best" scheduling
algorithm :
⚫ CPU utilization - To make out the best use of CPU and not to waste any CPU
cycle, CPU would be working most of the time(Ideally 100% of the time).
Considering a real system, CPU usage should range from 40% (lightly loaded) to
90% (heavily loaded.)
⚫ Throughput - It is the total number of processes completed per unit time or rather
say total amount of work done in a unit of time. This may range from 10/second to
1/hour depending on the specific processes.
⚫ Turnaround time- It is the amount of time taken to execute a particular process,
i.e., the interval from time of submission of the process to the time of completion
of the process(Wall clock time).

Turn around Time = Burst Time + Waiting Time


14
Scheduling Criteria Cont…
⚫ Waiting time - The sum of the periods spent waiting in the ready queue
amount of time a process has been waiting in the ready queue to acquire
get control on the CPU.
Waiting Time = Service Time - Arrival Time
⚫ Load average - It is the average number of processes residing in the
ready queue waiting for their turn to get into the CPU.
⚫ Response time - The amount of time it takes from when a request was
submitted until the first response is produced. Remember, it is the time till
the first response and not the completion of process execution(final
response).
⚫ In general CPU utilization and Throughput are maximized and other
factors are reduced for proper.

15
Scheduling Algorithms

Four major scheduling algorithms here which are following :


⚫ FirstIn First Out(FIFO) Scheduling
⚫ Shortest-Job-First(SJF) Scheduling
⚫ Priority Scheduling
⚫ Round Robin(RR) Scheduling

16
First In First Out (FIFO) Scheduling
⚫ Also known as First Come First Serve(FCFS)
⚫ Jobs are executed on first come, first serve basis.
⚫ Easy to understand and implement.
⚫ Poor in performance as average wait time is high.
⚫ The main disadvantage of this algorithm is aging. Due to this problem, big jobs have to
wait for the CPU for a long time.

17
FIFO
Calculate Average Waiting Time and Average Turnaround Time

Waiting Time = Service Time - Arrival Time


P1 = 0-0=0
P2 = 7-2=5
P3 = 11-4=7
P4 = 12-5=7
Turn around Time = Burst Time + Waiting Time

P1 P2 P3 P4 P1 = 7+0=7
P2 = 4+5=9
0 Service Time 7 11 12 16
P3 = 1+7=8
P4 = 4+7=11

⚫ Average Waiting Time = (0 + 5 + 7 + 7)/4 = 4.75


⚫ Average Turnaround Time = (7+9+8+11)/4=8.75
18
Shortest-Job-First(SJF) Scheduling
⚫ Best approach to minimize waiting time.
⚫ Actual time taken by the process is already known to processor.
⚫ Difficult to implement because knowing the length of the next CPU burst
time is difficult.

19
SJF (non-preemptive)
Calculate Average Waiting Time and Average Turnaround Time
Waiting Time = Service Time - Arrival Time
Process Arrival Time Burst Time
P1 0.0 7 P1 = 0-0=0
P2 2.0 4 P3 = 7-4=3
P2 = 8-2=6
P3 4.0 1 P4 = 12-5=7
P4 5.0 4
Turn around Time = Burst Time + Waiting Time

P1 = 7+0=7
P1 P3 P2 P4 P3 = 1+3=4
P2 = 4+6=10
P4 = 4+7=11
Service Time 0 7 8 12 16

⚫ Average waiting time = (0 + 3 + 6 + 7)/4 = 4


⚫ Average turnaround time=(7+4+10+11)/4= 8
20
SJF (preemptive)
Waiting Time = Service Time - Arrival Time
Calculate Average Waiting Time and Average Turnaround Time
Waiting Time = Completion time – Burst Time –
Process Arrival Time Burst Time Arrival Time
P1 0.0 7 P1 = 16-7-0 = 9
P2 2.0 4 P2 = 7-4-2 = 1
P3 = 5-1-4 = 0
P3 4.0 1 P4 = 11-4-5=2

P4 5.0 4
Turn around Time = Burst Time + Waiting Time

P1 = 7+9=16
P1 P2 P3 P2 P4 P1
P2 = 4+1=5
P3 = 1+0=1
0 2 4 5 7 11 16 P4 = 4+2=6
(P1=5) ( P2=2) (P3=0) (P2=0) (P4=0) (P1=0)

• Average waiting time = (9 + 1 + 0 +2)/4 = 3


• Average turnaround time= (16+5+1+6)/4=7 21
Priority Scheduling
⚫ Priority is assigned for each process.
⚫ Process with highest priority is executed first and
so on.
⚫ Processes with same priority are executed in FIFO manner.
⚫ Priority can be decided based on memory requirements, time
requirements or any other resource requirement.

22
Round Robin (RR)
⚫ This algorithm is especially implemented for time sharing systems.
⚫ It is similar to FIFO but preemption (Interrupt) is added to switch between
processes.
⚫ A small unit of time called as Time Quantum.
⚫ The algorithm’s performance heavily depends upon the time quantum.
⚫ It works like FIFO if time quantum is large.

Process Arrival Time Burst Time Time Quantum =2


P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 5 (P1=5) (P2=2) (P3=0) (P4=3) (P1=3) (P2=0) (P4=1) (P1=1) (P4=0) (P1=0)

23
THANK YOU

You might also like