Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

BACS1024

INTRODUCTION TO
COMPUTER SYSTEMS

Chapter 7: Processor management

1
Chapter Outline
7.1 Processor Manager
7.2 Scheduling Submanagers
7.2.1 Job Scheduler
7.2.2 Process Scheduler
7.2.3 Middle-level Scheduler
7.3 Process State and Transition
7.3.1 Job and Process States
7.3.2 Process Control Block (PCB)
7.4 Context switch
7.5 Process scheduling policies
7.5.1 System limitations
7.5.2 Good Scheduling Policy
7.5.3 Process Scheduling Policies
7.5.4 Process scheduling Algorithms

2
7.1 Processor Manager
7.1.1 Processor Manager (Recap)
 Allocates CPU time
 Keeps track of each process status - waiting, executing
 Sets up necessary registers and tables
 Reclaims processor when job is finished and/or time for job has expired
 Two levels of responsibility
1. Accepts or rejects the incoming jobs (handled by Job Scheduler)
2. Decides which process gets the CPU and for how long (Handled by the
Process Scheduler)

3
7.1 Processor Manager
7.1.1 Processor Manager
 To allocate CPU(s) to jobs, the processor manager performs job scheduling,
process scheduling and interrupt management.
a) In single-user systems, processor is busy only when user is executing a
job—at all other times it is idle. Processor management is simple.
b) In multiprogramming environment, processor must be allocated to each
job in a fair and efficient manner. Requires scheduling policy and a
scheduling algorithm.
 Active multiprogramming is a time-sharing system which allows
each program to use a preset slice of each CPU time. When the time
expires, the job is interrupted and another job is allowed to begin.
 Passive multiprogramming allows each program to be serviced in
turn, one after another by an interrupt. It ties up CPU time while all
other jobs have to wait.

4
7.1 Processor Manager
The relationship between program, job and process.

To facilitate job allocation to CPU, the processor manager has 2 sub-managers:


a) Job Scheduler and
b) Process Scheduler
5
7.2 Scheduling Submanagers
7.2.1 Job Scheduler
 Job Scheduler  in charge of job scheduling.
 High-level scheduler.
 Selects jobs from a queue of incoming jobs.
 Places them in process queue (batch or interactive), based on each
job’s characteristics.
 Goal is to put jobs in a sequence that uses all system’s resources as
fully as possible.
 Strives for balanced mix of jobs with large I/O interaction and jobs
with lots of computation.
 Tries to keep most system components busy most of time.

6
7.2 Scheduling Submanagers
7.2.2 Process Scheduler
 Process Scheduler  in charge of process scheduling.
 Low-level scheduler – assigns the CPU to execute processes of those jobs
placed on ready queue by Job Scheduler.
 After a job has been placed on the READY queue by Job Scheduler,
Process Scheduler will takes over.
 Determines which jobs will get CPU, when, and for how long.
 Decides when processing should be interrupted.
 Determines queues job should be moved to during execution.
 Recognizes when a job has concluded and should be terminated.

7
7.2 Scheduling Submanagers
7.2.2 Process Scheduler
 To schedule CPU, Process Scheduler uses common trait among most
computer programs: they alternate between CPU cycles and I/O cycles.
READ A,B I/O cycle
C = A+B
D = (A*B)–C CPU cycle
E = A–B
F = D/E
WRITE A,B,C,D,E,F I/O cycle
STOP terminate execution
END
 I/O-bound jobs (such as printing a series of documents) have many
brief CPU cycles and long I/O cycles.
 CPU-bound jobs (such as finding the first 300 prime numbers) have
8

long CPU cycles and shorter I/O cycles.


8
7.2 Scheduling Submanagers
7.2.3 Middle Level Scheduler
 In a highly interactive environment. Third layer of the processor manager.
 When the system is overloaded, it will removes active jobs from memory
to reduce degree of multiprogramming and allows other jobs to be
completed faster.
 Problem is Thrashing. Happen when a process is busy swapping pages in
and out.
 E.g.: Process with lower priority / paging faulting frequently / process
taking large memory.

9
7.2 Scheduling Submanagers
 The relationship between job scheduler, process scheduler and middle
level scheduler.
Middle Level Scheduler
Swapped Out Processes
Swap-in Swap-out
Disk
Virtual Ready Queue CPU End
Memory
Job Process
Scheduler Scheduler

I/O I/O Waiting Queue


10
7.3 Process State and Transition
7.3.1 Job and Process States
 Job status/process status -- one of the 5 states that a job takes as it moves through the
system.

 NEW/HOLD: The process is being created.


 READY: The process is waiting to be assigned to a process. (waiting for CPU)
 RUNNING: Instructions are being executed.
 WAITING: The process is waiting for some event to occur. (wait for I/O)
 FINISHED/TERMINATED: The process has finished execution. 11
7.3 Process State and Transition
7.3.1 Job and Process States
 Transition Among Process States
 HOLD to READY: Job Scheduler using a predefined policy.
 READY to RUNNING : Process Scheduler using some predefined
algorithm
 RUNNING back to READY: Process Scheduler according to some
predefined time limit or other criterion.
 RUNNING to WAITING: Process Scheduler and is initiated by an
instruction in the job.
 WAITING to READY: Process Scheduler and is initiated by signal from
I/O device manager that I/O request has been satisfied and job can
continue.
 RUNNING to FINISHED: Process Scheduler or Job Scheduler

12
7.3 Process State and Transition
7.3.2 Process Control Block
 Each process in the system is represented by a data structure called a PCB
that performs the same function as a traveler’s passport.
 Contents of each job’s PCB.
 Process identification
 Process status (HOLD, READY, RUNNING, WAITING)
 Process state (process status word, register contents, main memory
info, resources, process priority)
 Accounting (CPU time, total amount of time, I/O operations, number
input records read, etc.)

13
7.3 Process State and Transition
7.3.2 Process Control Block
 PCBs and Queuing
 PCB of job created when Job Scheduler accepts it
 updated as job goes from beginning to termination.
 Queues use PCBs to track jobs.
 PCBs, not jobs, are linked to form queues.
 E.g., PCBs for every ready job are linked on READY queue; all
PCBs for jobs just entering system are linked on HOLD queue.
 Queues must be managed by process scheduling policies and
algorithms.

14
7.4 Context Switch
 Context switching is the acts of saving a job’s processing information in its
PCB so the current job can be swapped out of memory and loads the
processing information from the PCB of another new coming or interrupted job
into the appropriate registers.
 Context Switching Is Required by All Preemptive Algorithms
 When Job A is preempted
 All of its processing information must be saved in its PCB for later
(when Job A’s execution is continued).
 Contents of Job B’s PCB are loaded into appropriate registers so it can
start running again (context switch).
 Later when Job A is once again assigned to processor, another context
switch is performed.
 Info from preempted job is stored in its PCB.
 Contents of Job A’s PCB are loaded into appropriate registers.

15
7.4 Context Switch
 Context switching

Context
Switch

Context
Switch

16
7.4 Context Switch

 Pros
 Hardware mechanism saves almost all of the CPU state
 Software can be more selective and save only that portion that actually
needs to be saved & reloaded.
 It permits better control over the validity of the data that is being
loaded
 Cons
 It requires considerable processor time 17
7.5 Process scheduling policies
7.5.1 System limitations
 Before operating system can schedule all jobs in a multiprogramming
environment, it must resolve three limitations of system:
 finite number of resources (such as disk drives, printers, and tape
drives)
 some resources can’t be shared once they’re allocated (such as
printers)
 some resources require operator intervention (such as tape drives).

18
7.5 Process scheduling policies
7.5.2 Good Scheduling Policy
 Maximize throughput by running as many jobs as possible in a given
amount of time.
 Maximize CPU efficiency by keeping CPU busy 100 % of time.
 Ensure fairness for all jobs by giving everyone an equal amount of CPU
and I/O time.
 Minimize response time by quickly turning around interactive requests.
 Minimize turnaround time by moving entire jobs in/out of system quickly.
 Minimize waiting time by moving jobs out of READY queue as quickly as
possible.

19
7.5 Process scheduling policies
7.5.3 Process Scheduling Policies
7.5.3.1 Preemptive scheduling policy
Preemptive scheduling policy interrupts processing of a job and transfers
the CPU to another job.

7.5.3.2 Non-preemptive scheduling policy


Non-preemptive scheduling policy functions without external interrupts.
 Once a job captures processor and begins execution, it remains in
RUNNING state uninterrupted until it issues an I/O request (natural wait)
or until it is finished (exception for infinite loops).

20
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
There are 5 key process scheduling algorithms:
1. First Come First Served (FCFS)
2. Shortest Job Next (SJN)
3. Priority Scheduling (PS)
4. Shortest Remaining Time (SRT)
5. Round Robin (RR)

 Wait Time = Finish Time – CPU Cycle – Arrival Time


 Turnaround Time = Finish Time – Arrival Time

 Note: Natural wait is a common term used to identify an I/O request from a
program in a multiprogramming environment that would cause a process to
wait “naturally” before resuming execution. 21
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.1 First Come First Served (FCFS)
 Non-preemptive.
 Handles jobs according to their arrival time -- the earlier they arrive, the
sooner they’re served.
 Simple algorithm to implement -- uses a FIFO queue.
 Good for batch systems; not so good for interactive ones.
 Turnaround time is unpredictable.

22
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.2 Shortest Job Next (SJN)
 Non-preemptive.
 Handles jobs based on length of their CPU cycle time.
 Use lengths to schedule process with shortest time.
 Optimal – gives minimum average waiting time for a given set of processes.
 optimal only when all of jobs are available at same time and the CPU
estimates are available and accurate.
 Doesn’t work in interactive systems because users don’t estimate in
advance CPU time required to run their jobs.

23
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.3 Priority Scheduling (PS)
 Can be preemptive or non-preemptive.
 Non-preemptive algorithm, which is commonly used in batch systems.
 Preemptive algorithm, which is commonly used in time critical systems.
 Gives preferential treatment to important jobs.
 Allows the program with the highest priority to be processed first and
these high priority jobs are not interrupted until their CPU cycles (run
times) are completed or a natural wait occurs.
 If two or more jobs with equal priority, then uses FCFS policy within the
same priority group

24
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.4 Shortest Remaining Time (SRT)
 Preemptive version of the SJN algorithm.
 Processor allocated to job closest to completion.
 This job can be preempted if a newer job in READY queue has a “time
to completion” that's shorter.
 Can’t be implemented in interactive system -- requires advance knowledge
of CPU time required to finish each job.
 SRT involves more overhead than SJN.
 OS monitors CPU time for all jobs in READY queue and performs
“context switching”.

25
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.4 Shortest Remaining Time (SRT)
 Context Switching Is Required by All Preemptive Algorithms
 When Job A is preempted
 All of its processing information must be saved in its PCB for later (when Job
A’s execution is continued).
 Contents of Job B’s PCB are loaded into appropriate registers so it can start
running again (context switch).
 Later when Job A is once again assigned to processor, another context switch is
performed.
 Info from preempted job is stored in its PCB.
 Contents of Job A’s PCB are loaded into appropriate registers.

26
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.5 Round Robin (RR)
 Preemptive.
 Used extensively in interactive systems because it’s easy to implement.
 Isn’t based on job characteristics but on a predetermined slice of time
that’s given to each job.
 Ensures CPU is equally shared among all active processes and isn’t
monopolized by any one job.
 Time slice is called a time quantum
 size is crucial to system performance (100 ms to 1-2 secs)

27
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.5 Round Robin (RR)
 If Job’s CPU Cycle < Time Quantum
 If job’s last CPU cycle & job is finished, then all resources allocated to
it are released & completed job is returned to user.
 If CPU cycle was interrupted by I/O request, then info about the job is
saved in its PCB & it is linked at end of the appropriate I/O queue.
 Later, when I/O request has been satisfied, it is returned to end of
READY queue to await allocation of CPU.
 Time Slices Should Be ...
 Long enough to allow 80 % of CPU cycles to run to completion.
 Flexible - depends on the system.

28
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.6 Working
 To draw timeline and to do calculations for process scheduling
algorithms
 E.g.: Given the following information, draw a timeline for each Job AT CT P
of the following algorithms. Also calculate the average waiting
time and average turnaround time for each of them. A 0 8 5
 FCFS (First Come First Served)
B 1 5 3
 SJN (Shortest Job Next)
 SRT (Shortest Remaining Time) C 2 3 1
 Round Robin (Time Quantum = 3) D 3 1 4
 Priority scheduling
E 4 7 2
 Wait Time = Finish Time – CPU Cycle – Arrival Time
 Turnaround Time = Finish Time – Arrival Time

29
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.6 Working - FCFS (NP, AT)

A B C D E
0 8 13 16 17 24

Job AT CT FT TT WT
A 0 8 8 8 0
B 1 5 13 12 7
C 2 3 16 14 11
D 3 1 17 14 13
E 4 7 24 20 13
Total: 24 Total: 68 44
Average: 13.6 8.8
30
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.6 Working - SJN (NP, AT + CT)

A D C B E
0 8 9 12 17 24

Job AT CT FT TT WT
A 0 8 8 8 0
B 1 5 17 16 11
C 2 3 12 10 7
D 3 1 9 6 5
E 4 7 24 20 13
Total: 24 Total: 60 36
Average: 12 7.2
31
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.6 Working - PS (NP, AT + P)

A C E B D
0 8 11 18 23 24

Job AT CT P FT TT WT
A 0 8 5 8 8 0
B 1 5 3 23 22 17
C 2 3 1 11 9 6
D 3 1 4 24 21 20
E 4 7 2 18 14 7
Total: 24 Total: 74 50
Average: 14.8 10
32
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.6 Working - PS (P, AT + P)

A B C C C E B D A

0 1 2 3 4 5 12 16 17 24

Job AT CT P FT TT WT
A 0 8 5 24 24 16
B 1 5 3 16 15 10
C 2 3 1 5 3 0
D 3 1 4 17 14 13
E 4 7 2 12 8 1
Total: 24 Total: 64 40
Average: 12.8 8.0
33
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.6 Working - SRT (P, AR + RCT)

A B C D C B A E

0 1 2 3 4 6 10 17 24

Job AT CT FT TT WT
A 0 8 17 17 9
B 1 5 10 9 4
C 2 3 6 4 1
D 3 1 4 1 0
E 4 7 24 20 13
Total: 24 Total: 51 27
Average: 10.2 5.4

34
7.5 Process scheduling policies
7.5.4 Process Scheduling Algorithms
7.5.4.6 Working - RR (P, AR + Q + RQ)
Given: Quantum = 3 time slices question

A B C D A E B A E E

0 3 6 9 10 13 16 18 20 23 24

Job AT CT FT TT WT
A 0 8 20 20 12
Ready Queue
B 1 5 18 17 12 B,C,D,A,E,B,A,E,E
C 2 3 9 7 4
D 3 1 10 7 6
E 4 7 24 20 13
Total: 24 Total: 71 47
Average: 14.2 9.4
35
Chapter 7: Processor Management
Self-review

7.1 Processor Manager


7.2 Scheduling Submanagers
7.2.1 Job Scheduler
7.2.2 Process Scheduler
7.2.3 Middle-level Scheduler
7.3 Process State and Transition
7.3.1 Job and Process States
7.3.2 Process Control Block (PCB)
7.4 Context switch
7.5 Process scheduling policies
7.5.1 System limitations
7.5.2 Good Scheduling Policy
7.5.3 Process Scheduling Policies
7.5.4 Process scheduling Algorithms

36

You might also like