Process Management - Scheduling

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

Operating Systems

Principles of Process Management


Scheduling & Context Switching
Areas of OS Responsibility
 Hardware
 CPU (managed by processes)
 Memory
 I/O Devices

 File Systems
 Security
 Networking
What is a Process?
 An executable (active)program
 Associated data needed by the program
 Execution context of the program
 Allinformation the operating system needs to
manage the process (memory contents,
register values, internal representation …etc)

3
What is a Process?
 The process is the unit of work in an OS.
 System processes are created by the OS to
perform tasks on the user’s or system’s
behalf.
 User processes are created more directly by
the user’s actions. They include application
programs and system utilities.
Process versus Program
 A program is static
 just a bunch of bytes
 A process is dynamic
 transitions between different states.
 No one-to-one mapping between Program
and Process
What is in a Process?

 A process consists of:


 Address Space
 Code for the running program
 Data for the running program
 Execution Stack and stack pointer (SP)
 traces state of procedure calls made

 Program counter (PC)


 indicating the next instruction

 General-purpose processor registers and their


values
 OS resources
 open files, network connections, sound channels, …
Process State
 Each process has an
execution state, which
indicates what it is
currently doing

 As a process
executes, it moves
from state to state
Process is
preempted
Process State
Process is
 Running: currently Process makes a scheduled
request that cannot be
being executed immediately satisfied
on the CPU

 Ready: not waiting on any


event, but not currently
using the CPU

 Blocked: waiting on an Request is satisfied


event, often I/O

8
Process Operations
 Create/terminate process
 Set/get process parameters
 Block process
 Awaken process
 Switch process
 Schedule process

9
Process Table
 Used by the OS to manage processes
 One entry for each process
 Typical contents in a process table entry:
 Saved registers
 Process state Called
 Process ID PCB
 Owner/group ID “Process
Control Block”
 Priority
 Memory usage
 Open files
 Cumulative running time

10
Processes
 I/O Bound processes
 CPU Bound processes
Threads
 Created when we allow more than one locus of
control in a single memory space
 Called (LWP: light weight processes or Threads)

12
Scheduling
Scheduling

 The mechanism by which the OS selects which


process gets the CPU next time.

 Considers processes in the ready state (Ready


List)

 Preventing a process from getting CPU time


when it is ready is called Starvation.

14
Criteria for good Scheduling
algorithm
 Fairness
 Efficiency
 Response time
 Turnaround time
 Throughput
Scheduling algorithms
 Preemptive scheduling: allows
logically runnable processes to be
temporarily suspended
 Nonpreemptive scheduling: classic
batch systems, run to completion
Terminology
 Waiting time
 Burst time
 Turnaround time
 Average turnaround time
 Weighted turnaround time
 Average weighted turnaround time
First Come, First Served (FCFS)
 Batch scheduling technique
 Jobs processed in the order they arrived
 Implemented with a queue
 Disadvantage:
 Long jobs introduce large delays until short
jobs are executed.
 i.e. overall average time to wait is affected by the
order in which jobs/processes arrive.

18
Example
Shortest Job First (SJF)
 Batch scheduling technique
 Process jobs in increasing order of running
time
 Optimal: minimizes average turnaround time
 Requires knowledge of running time

20
Shortest Remaining Time Next
(SRTN)

 Preemptive SJF.
 The process in the ready queue with the shortest
execution time is chosen to execute.
 New process with run time less than the
remaining time of the current process preempt.
Round-Robin
 Time-sharing analog of FCFS
 Processes taken in turn
 Processes given a time slice (called quantum),
then pre-empted
 Time slice ~ 0.1-0.01 seconds
 Implemented with a circular queue

22
Example (FCFS vs RR)
 Example
 10 jobs and each takes 100 seconds
 FCFS (non-preemptive scheduling)
 job 1: 100s, job2: 200s, ... , job10: 1000s
 Round Robin (preemptive scheduling)
 time slice 1sec and no overhead
 job1: 991s, job2: 992s, ... , job10: 1000s
 Comparisons
 Round robin is much worse (turnaround time) for jobs about the
same length
 Round robin is better for short jobs and different length jobs
Priority Scheduling
 Each process has priority
 Often give preference to interactive processes over compute-
bound processes
 Or associate specific user processes with priorities
 Or e.g. microkernel design (OS processes have highest priority)
 Process with the highest priority is selected

 Static and dynamic priorities

 Upper and lower limits common on dynamic priorities

24
Multilevel Feedback Queue
 A technique for implementing dynamic
priorities
 One queue per priority level
 Processes can be boosted to higher
priority
 E.g. when they are blocked for sometime
 Processes move to lower priority queues at
the end of time slices

25
Multilevel Feedback Queue
(cont.)

26
Priority Inversion
 Occurs with Static Priorities.
 When a high priority process is waiting on a low priority
process that is starved by a medium priority process.
 Example:
 Processes a (3), b (5), c (7)
 c is waiting on  a
 a is waiting on  b
 b is compute-bound and never blocks
 Process a does not have a chance to run
 Process c will not have a chance to run either although its
priority is 7!!  as if b is higher than c
Dynamic priority
 Solves Priority Inversion directly
 Involves temporarily raising the priority of a
process if a higher priority process blocks
waiting on it.
 Example:
 Change priority of a to 7
 Process b cannot starve a
 a completes task
 Thus c completes task
 Return priority of a to 3
Scheduling Parameters
 Quantum is the length of a time slice
 Longer quanta give more run time
 In some OSs, lower priority levels had
longer quanta
 longer quanta for “foreground” process
 In some OSs I/O Bound process gets
higher priority.

29
Two-Level Scheduling
 When a new process is allocated & there is not enough
memory
  write the memory of a process (that has not run in a long time)
to the disk.
 When it is scheduled,
 read it and write another one to disk
 With Round Robin,
 The scheduled process will always be the one on the disk
 This is called Thrashing
 Solution: two levels of scheduling
 Long term: decide which processes are resident in memory
 Short term: decide next process to run among only those in
memory

30
Context Switching
 Reassigning CPU among competing processes
rapid enough that we have the illusion that they
run simultaneously.

 The configuration of a process is saved in such a


way that it can be copied back

 Context Switching is the mechanism that


moves the CPU from one process to another.

31
Context Switching
 Give CPU to another process:

 Transfer control to OS (usually interrupt)


 Save current process configuration
 Schedule next process
 Restore next process configuration (saved
previously)
 Return control flow to the scheduled process

32
Context Switching (cont.)

Note: Not all


interrupts trigger
a context switch

33
Process Creation
 Fork creates copy of parent
 Execv produces a process running new
program
 Mechanism:
 Createnew process table entry
 Copy parent memory (fork)
 Load new program (for execv)

34
Process Termination
 Initiated by ending process or another
process
 Mechanism:
 Close open files
 Reduce reference count on shared resources
 Free nonshared memory
 Send exit status to parent (or other waiting
process)

35

You might also like