Professional Documents
Culture Documents
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.
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).
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
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)
15
Process Scheduling 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
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
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.
22
First-Come, First-Served(FCFS) Scheduling
23
FCFS Scheduling (cont.)
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
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
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
Typically, higher average turnaround time than SRTF, but better response
33