Ch.2 - Process Management
Ch.2 - Process Management
OPERATING SYSTEMS
Roshni M Balakrishnan
Department of Computer Science &
Engineering,
Amrita School of Engineering, Bengaluru
1
Topics
• Process Concept
• Process Scheduling
• Operations on Processes
2
Introduction
• Process – a program in execution; process execution must
progress in sequential fashion
• Multiple parts
• The program code, also called text section
• Current activity including program counter, processor registers
• Stack containing temporary data
• Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
3
• Program is passive entity stored on disk (executable file)
• But process is active
• Program becomes process when executable file loaded into memory
• Execution of program started via GUI mouse clicks,
command line entry of its name, etc
• One program can be several processes
• Consider multiple users executing the same program
4
Process Structure
5
Process States
• As a process executes, it changes state
• new: The process is being created
• running: Instructions are being executed
• waiting: The process is waiting for some event to occur
• ready: The process is waiting to be assigned to a processor
• terminated: The process has finished execution
6
Life Cycle of a Process
7
Process Control Block
Information associated with each process
(also called task control block)
• Process state – running, waiting, etc
• Program counter – location of instruction to
next execute
• CPU registers – contents of all process-centric
registers
• CPU scheduling information- priorities,
scheduling queue pointers
• Memory-management information – memory
allocated to the process
• Accounting information – CPU used, clock
time elapsed since start, time limits
• I/O status information – I/O devices allocated
to process, list of open files
8
CPU Switch From Process to Process
9
Process Scheduling
• Maximize CPU use, quickly switch processes onto CPU for
time sharing
• Process scheduler selects among available processes for
next execution on CPU
• Maintains scheduling queues of processes
• 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 among the various queues
10
Representation of Scheduling
Queueing diagram represents queues, resources, flows
11
12
Schedulers
• Short-term scheduler (or CPU scheduler) – selects which process should be
executed next and allocates CPU
• Sometimes the only scheduler in a system
• Short-term scheduler is invoked frequently (milliseconds) (must be fast)
• Long-term scheduler (or job scheduler) – selects which processes should be
brought into the ready queue
• Long-term scheduler is invoked infrequently (seconds, minutes) (may be
slow)
• The long-term scheduler controls the degree of multiprogramming
• Processes can be described as either:
• I/O-bound process – spends more time doing I/O than computations, many
short CPU bursts
• CPU-bound process – spends more time doing computations; few very long
CPU bursts
• Long-term scheduler strives for good process mix
13
Addition of Medium Term Scheduling
• Medium-term scheduler can be added if degree of multiple
programming needs to decrease
• Remove process from memory, store on disk, bring back in from disk to continue
execution: swapping
14
CPU-I/O Burst Cycle
Context Switch
• When CPU switches to another process, the system must
save the state of the old process and load the saved state
for the new process via a context switch
• Context of a process represented in the PCB
• Context-switch time is overhead; the system does no useful
work while switching
• The more complex the OS and the PCB the longer the context
switch
• Time dependent on hardware support
• Some hardware provides multiple sets of registers per CPU
multiple contexts loaded at once
16
Scheduler
Short-term scheduler selects from among the processes in
ready queue, and allocates the CPU to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
Consider access to shared data
Consider preemption while in kernel mode
Consider interrupts occurring during crucial OS activities
17
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
18
Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their execution
per time unit
• Turnaround time – amount of time to execute a particular
process
• Waiting time – amount of time a process has been waiting
in the ready queue
• Response time – amount of time it takes from when a
request was submitted until the first response is produced,
not output (for time-sharing environment)
19
Optimization of Scheduling Criteria
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
20
Equations to be noted
21
First Come First Serve (FCFS)
Process Burst Time
P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30
22
Shortest Job First (SJF)
• Associate with each process the length of its next CPU burst
• Use these lengths to schedule the process with the shortest time
• SJF is optimal – gives minimum average waiting time for a
given set of processes
• The difficulty is knowing the length of the next CPU request
• Could ask the user
23
ProcessArriva l TimeBurst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
24
Wait time
P4= 0-0=0
P1= 3-2=1
P2= 9-5=4
P5= 11-4=7
P3= 15-1=14
25
SJF – Preemptive (SRTF)
• In Preemptive SJF Scheduling, jobs are put into the ready queue as they
come.
• A process with shortest burst time begins execution.
• If a process with even a shorter burst time arrives, the current process is
removed or preempted from execution, and the shorter job is allocated
CPU cycle.
26
Wait time
P4= 0-0=0
P1= (3-2) + 6 =7
P2= 5-5 = 0
P5= 4-4+2 =2
P3= 15-1 = 14
27
28
29
Thank
you !!!!!
30