You are on page 1of 33

MODULE II (1)

PROCESS MANAGEMENT

1
Process

A Process is a program in execution.


A process will need certain resources-such as CPU time,
memory, files, and I/O devices to accomplish its task.


These resources are allocated to the process either
when it is created or while it is executing.


Systems consist of a collection of processes:

2
Program vs Process
 A program is a passive entity, such as a file containing a list of
instructions stored on disk (often called an executable file).
 A process is an active entity, with a program counter specifying
the next instruction to execute and a set of associated resources.
 A program becomes a process when an executable file is loaded
into memory.
Process State

4
Process State
• As a process executes, it changes state.

• Each process may be in one of the following states:


• New: The process is being created. When a program in secondary
memory is started for execution, the process is said to be in a new
state.

5
Process State
◦ Ready: After being loaded into the main memory and
ready for execution, a process transitions from a new to a
ready state. The process will now be in the ready state,
waiting for the processor to execute it.
◦ Running: After being allotted the CPU for execution, a
process passes from the ready state to the run state.
◦ Waiting: The process is waiting for some event to occur
(such as an I/0 completion or reception of a signal).
◦ Terminated: When a process’s execution is finished, it
goes from the run state to the terminate state.

6
Process Control Block (PCB)
• Each process is represented in
the operating system by a
process control block(PCB) also
called a task control block.
• It contains many pieces of
information associated with a
specific process, including
these:

7
Process Control Block
(PCB)
Contains information associated with each process
 Process State - new, ready, running etc.
 Program Counter - address of next instruction to be
executed
 CPU registers - general purpose registers, stack pointer etc.
 CPU scheduling information (pointer) - process priority
 Memory Management information - base/limit information
 Accounting information - time limits, process number
 I/O Status information - list of I/O devices allocated

8
Switching From Process to Process

9
Schedulers
 Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue.
 Long-term scheduler is invoked very infrequently (seconds,
minutes)  (may be slow).

 Short-term scheduler (or CPU scheduler) – selects which


process should be executed next and allocates CPU.
 Short-term scheduler is invoked very frequently
(milliseconds)  (must be fast).

10
Long-term scheduler (or Job scheduler)
 Often, more processes are submitted than can be
executed immediately.
 These processes are spooled to a mass-storage device
(typically a disk), where they are kept for later
execution.
 Job scheduler, selects processes from this pool and
loads them into memory for execution.

11
Short-term scheduler (or CPU scheduler)
 Whenever the CPU becomes idle, the operating system must
select one of the processes in the ready queue to be executed.
 The selection process is carried out by the short-term scheduler,
or CPU scheduler.
 The scheduler selects a process from the processes in memory
that are ready to execute and allocates the CPU to that process.
 All the processes in the ready queue are lined up waiting for a
chance to run on the CPU. The records in the queues are
generally process control blocks (PCBs) of the processes.

12
Medium Term Scheduling

 A medium-term scheduler helps to remove a process from


memory and thus reduce the degree of multiprogramming.
 Later, the process can be reintroduced into memory, and its
execution can be continued where it left off. This scheme is
called swapping.
 The process is swapped out, and is later swapped in, by the
medium-term scheduler.

13
Context Switch
Switching the CPU to another process requires
performing a state save of the current process and a
state restore of a different process.
This task is known as a context switch.
When a context switch occurs, the kernel saves the
context of the old process in its PCB and loads the
saved context of the new process scheduled to run.

14
CPU Scheduler (Short-term Scheduler)

 When the CPU becomes idle, the operating


system selects one of the processes from the
ready queue to be executed.
 The selection process is carried out by the
short-term scheduler, or CPU scheduler.
 The scheduler selects a process from the
processes in memory that are ready to
execute and allocates the CPU to that process.

15
Process Scheduling Queues

 Job queue – set of all processes in the system.


 Ready queue – set of all processes residing in main
memory, ready and waiting to execute.
 Device queues – set of processes waiting for an I/O
device.
 Processes migrate between the various queues.

16
Preemptive & Non-preemptive Scheduling
 Non-preemptive Scheduling
o Once CPU has been allocated to a process, the process
keeps the CPU until
 Process exits OR

 Process switches to waiting state

 Preemptive Scheduling
o Process can be interrupted and must release the CPU.
 Need to coordinate access to shared data

17
CPU Scheduling Decisions
 CPU scheduling decisions may take place when
a process:
1. switches from running state to waiting state
2. switches from running state to ready state
3. switches from waiting to ready
4. terminates
 Scheduling under 1 and 4 is non-preemptive.
 All other scheduling is preemptive.

18
Dispatcher

 Dispatcher module gives control of the CPU to the


process selected by the short-term scheduler. This
involves:
 switching context
 switching to user mode
 jumping to the proper location in the user program to restart
that program

 Dispatch Latency:
 Time it takes for the dispatcher to stop one process and start
another running.
 Dispatcher must be fast.

19
Scheduling Criteria

 CPU Utilization
o Keep the CPU and other resources as busy as possible
 Throughput
o No of processes that complete their execution per time unit.
 Turnaround time
o amount of time to execute a particular process from its entry time.
 Waiting time
o amount of time a process has been waiting in the ready queue.
 Response Time (in a time-sharing environment)
o amount of time it takes from when a request was submitted until the first
response is produced, NOT output.

20
Scheduling Algorithms
 CPU scheduling deals with the problem of
deciding which of the processes in the ready
queue is to be allocated the CPU.

21
1. First Come First Serve (FCFS) Scheduling
Process that requests the CPU FIRST is allocated the CPU FIRST.
FCFS is a non-preemptive algorithm.

Implementation - using FIFO queues


 incoming process is added to the tail of the queue.
 Process selected for execution is taken from head of queue.

Performance metric - Average waiting time in queue.


Gantt Charts - are used to visualize schedules.

22
First-Come, First-Served(FCFS) Scheduling

Example Suppose the arrival order


Process Burst Time for the processes is
P1 24  P1, P2, P3
P2 3
P3 3 Waiting time
 P1 = 0;
 P2 = 24;
Gantt Chart for Schedule  P3 = 27;

P1 P2 P3 Average waiting time


 (0+24+27)/3 = 17
0 24 27 30

23
FCFS Scheduling (cont.)

Suppose the arrival order for


Example the processes is
P2, P3, P1
Process Burst Time
P1 24 Waiting time
P2 3 P1 = 6; P2 = 0; P3 = 3;
P3 3
Average waiting time
Gantt Chart for Schedule (6+0+3)/3 = 3 , better..

P2 P3 P1 Convoy Effect:
0 3 6 short process behind long process,
30
e.g. 1 CPU bound process, many I/O
bound processes.

24
2. Shortest-Job-First(SJF) Scheduling
Associate with each process the length of its next CPU
burst. Use these lengths to schedule the process with
the shortest time.
Two Schemes:
 Scheme 1: Non-preemptive
 Once CPU is given to the process it cannot be preempted until it completes its CPU burst.
 Scheme 2: Preemptive
 If a new CPU process arrives with CPU burst length less than remaining time of current
executing process, preempt. Also called Shortest-Remaining-Time-First (SRTF).
 SJF is optimal - gives minimum average waiting time for a given set of processes.

25
Non-Preemptive SJF Scheduling
Example
Process Arrival TimeBurst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4

Gantt Chart for Schedule

P1 P3 P2 P4

0 7 8 12 16
Average waiting time =
(0+6+3+7)/4 = 4

26
Preemptive SJF Scheduling(SRTF)
Example
Process Arrival TimeBurst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Gantt Chart for Schedule

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16
Average waiting time =
(9+1+0+2)/4 = 3

27
3. Priority Scheduling
 A priority value (integer) is associated with each
process.
The CPU is allocated to the process with the
highest priority.
Equal-priority processes are scheduled in FCFS
order.

28
Priority Scheduling
An SJF algorithm is simply a priority algorithm
where the priority (p) is the inverse of the
(predicted) next CPU burst. The larger the CPU
burst, the lower the priority, and vice versa.
 CPU is allocated to process with the highest
priority.
 Preemptive
 Nonpreemptive

29
Priority Scheduling (cont.)
SJN is a priority scheme where the priority is the predicted next CPU
burst time.
Problem
 Starvation/indefinite blocking - Low priority processes may never execute.

Solution
 Aging - as time progresses increase the priority of the process.

30
Priority Scheduling (cont.)
Consider the following set of processes, assumed to
have arrived at time 0 in the order P1, P2, · · ·, P5, with
the length of the CPU burst given in milliseconds:

Gantt chart
The average waiting time is 8.2 milliseconds.

31
4. Round Robin (RR)
Each process gets a small unit of CPU time
 Time quantum usually 10-100 milliseconds.
 After this time has elapsed, the process is preempted and added to the end of the ready
queue.
 n processes, time quantum = q
 Each process gets 1/n CPU time in chunks of at most q time units at a time.
 No process waits more than (n-1)q time units.
 Performance
 Time slice q too large - FIFO behavior
 Time slice q too small - Overhead of context switch is too expensive.

32
Round Robin - Example
Time Quantum = 20

Process Burst Time


P1 53
P2 17
P3 68
P4 24
Gantt Chart for Schedule

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Typically, higher average turnaround time than SRTF, but better response

33

You might also like