You are on page 1of 41

PROCESS MANAGEMENT

Chapter 3: Processes
Chapter 5: CPU Scheduling
Chapter 3: Processes
 Process Concept
 Process Scheduling
 Operations on Processes
Process Concept
 PROCESS – a program in execution; process
execution must progress in sequential fashion
 Program = static file ;passive entity
 Process = executing program = program + execution
state; active entity.
 A PROCESS is the basic unit of execution in an
operating system
 Textbook uses the terms job and process almost
interchangeably
 Process execution requires the following
resources:
 Memory to contain the program code and data
 A set of CPU registers to support execution
Processes/Programs(ANALOGY)
BAKING
To bake a cake, you need:

1. A Recipe -PROGRAMS

2. Ingredients -DATA
3. A Baker -PROCESSOR
4. Baking the cake -PROCESS
Process Structure
1. Text Section – contains the program code.
 Program Counter
 Processor’s Register
2. Data Section – contains global variables.
3. Heap Section – a memory that is dynamically
allocated during process runtime.
4. Stack Section – contains temporary data.
 Function parameters
 Return addresses
 Local variables
Process in Memory
Process State
 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
Diagram of Process State
Process Control Block (PCB)
Information associated with each process
 Process state
 Program counter
 CPU registers
 CPU scheduling information
 Memory-management information
 Accounting information
 I/O status information
Process Control Block (PCB)
CPU Switch From Process to Process
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 among the various
queues
Schedulers
 Long-term scheduler (or job
scheduler) – selects which
processes should be brought into
the ready queue
 Short-term scheduler (or CPU
scheduler) – selects which
process should be executed next
and allocates CPU
Schedulers (Cont.)
 Short-term scheduler is invoked very frequently
(milliseconds)  (must be fast)
 Long-term scheduler is invoked very 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
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
 Context-switch time is overhead; the
system does no useful work while
switching
 Time dependent on hardware support
Process Creation
 Parent process create children processes,
which, in turn create other processes,
forming a tree of processes
 Resource sharing
 Parent and children share all resources
 Children share subset of parent’s resources
 Parent and child share no resources
 Execution
 Parent and children execute concurrently
 Parent waits until children terminate
Process Creation (Cont.)
 Address space
 Child duplicate of parent
 Child has a program loaded into it
 UNIX examples
 fork system call creates new process
 exec system call used after a fork to
replace the process’ memory space
with a new program
Process Termination
 Process executes last statement and asks the
operating system to delete it (exit)
 Output data from child to parent (via wait)
 Process’ resources are deallocated by operating system
 Parent may terminate execution of children processes
(abort)
 Child has exceeded allocated resources
 Task assigned to child is no longer required
 If parent is exiting
 Some operating system do not allow child to continue if its
parent terminates
 All children terminated - cascading termination
Chapter 5: CPU Scheduling
 Basic Concepts
 Scheduling Criteria
 Scheduling Algorithms
Basic Concepts
 Maximum CPU utilization
obtained with multiprogramming
 CPU–I/O Burst Cycle – Process
execution consists of a cycle of
CPU execution and I/O wait
 CPU burst distribution
Alternating Sequence of CPU And I/O Bursts
Histogram of CPU-burst Times
CPU Scheduler
 Selects from among the processes in memory that
are ready to execute, and allocates the CPU to one
of them
 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
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
Scheduling Criteria
1. Burst time
2. Waiting time
3. Turnaround time
Scheduling Criteria
 Burst  time -that is required to
complete execution of particular task
or process.
 Waiting time – amount of time a
process has been waiting in the
ready queue
 Turnaround time – amount of time
to execute a particular process
Optimization Criteria
 Max CPU utilization
 Max throughput
 Min turnaround time
 Min waiting time
 Min response time
Scheduling Algorithms
 First-Come, First-Served (FCFS)
Scheduling
 Shortest-Job First (SJF)
Scheduling
First-Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3
P1 P2 P3

0 24 27 30

 Suppose that the processes arrive in the order: P1 ,


P2 , P3
The Gantt Chart for the schedule is:
 Waiting time for P1 = 0; P2 = 24; P3 = 27
 Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order P2 ,
P3 , P1 .
P2 P3 P1

0 3 6 30

 The Gantt chart for the schedule is:


 Waiting time for P1 = 6; P2 = 0; P3 = 3
 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case.
 Convoy effect short process behind long process
DRILL: FCFS Scheduling
Process Arrival Time Burst Time
P1 0.0 8
P2 0.4 4
P3 1.0 1
Compute the ff: Draw a Gantt Chart
1. Turnaround time (TT) Time in milliseconds
2. Waiting time (WT)
3. Average Turnaround time (ATT)
4. Average Waiting time (AWT)
SEATWORK: FCFS
Process Arrival Time Burst Time
P1 5.0 8
P2 0.0 4
P3 4.0 1
P4 2.0 3
P5 8.0 5
Compute the ff: Draw a Gantt Chart
1. Turnaround time (TT) Time in milliseconds
2. Waiting time (WT)
3. Average Turnaround time (ATT)
4. Average Waiting time (AWT)
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:
 nonpreemptive – once CPU given to the process it
cannot be preempted until completes its CPU burst.
 preemptive – if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is known as the
Shortest-Remaining-Time-First (SRTF).
 SJF is optimal – gives minimum average waiting
time for a given set of processes.
Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
P1 P3 P2 P4

0 7 8 12 16
 SJF (non-preemptive)
 Average waiting time = (0 + 6 + 3 + 7)/4 = 4
DRILL: SJF (Non-Preemptive)

Process Arrival Time Burst Time


P1 5.0 8
P2 0.0 4
P3 4.0 1
P4 2.0 3
P5 8.0 5
Compute the ff: Draw a Gantt Chart
1. Turnaround time (TT) Time in milliseconds
2. Waiting time (WT)
3. Average Turnaround time (ATT)
4. Average Waiting time (AWT)
SEATWORK: SJF (Non-Preemptive)

Process Arrival Time Burst Time


P1 0.0 8
P2 2.0 6
P3 2.0 1
P4 1.0 9
P5 3.0 3
Compute the ff: Draw a Gantt Chart
1. Turnaround time (TT) Time in milliseconds
2. Waiting time (WT)
3. Average Turnaround time (ATT)
4. Average Waiting time (AWT)
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P22.0 4
P34.0 1
P45.0 4
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

 SJF (preemptive)
 Average waiting time = (9 + 1 + 0 +2)/4 = 3
Priority Scheduling
 A priority number (integer) is associated with each
process
 The CPU is allocated to the process with the highest
priority (smallest integer  highest priority).
 Preemptive
 nonpreemptive
 SJF is a priority scheduling where priority is the predicted
next CPU burst time.
 Problem  Starvation – low priority processes may never
execute.
 Solution  Aging – as time progresses increase the
priority of the process.
DRILL: PRIORITY (Non-Preemptive)

Process Burst Time Priority


P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2
Compute the ff: Draw a Gantt Chart
1. Turnaround time (TT) Time in milliseconds
2. Waiting time (WT)
3. Average Turnaround time (ATT)
4. Average Waiting time (AWT)
SEATWORK: PRIORITY (Non-Preemptive)

Process Burst Time Priority


P1 10 2
P2 4 2
P3 5 2
P4 8 1
P5 2 3
Compute the ff: Draw a Gantt Chart
1. Turnaround time (TT) Time in milliseconds
2. Waiting time (WT)
3. Average Turnaround time (ATT)
4. Average Waiting time (AWT)

44
End of Chapter 3 & 5
Process Management

You might also like