0% found this document useful (0 votes)
259 views31 pages

Process Scheduling and Switching

Operating system process scheduling involves three levels: long-term scheduling selects which processes enter the ready queue, medium-term scheduling swaps processes between main memory and disk, and short-term scheduling selects the next process to execute from the ready queue. Process queues include the job queue, ready queue, and I/O device queues. The dispatcher performs context switches to save the state of one process and load another for execution.

Uploaded by

Paolo Legaspi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
259 views31 pages

Process Scheduling and Switching

Operating system process scheduling involves three levels: long-term scheduling selects which processes enter the ready queue, medium-term scheduling swaps processes between main memory and disk, and short-term scheduling selects the next process to execute from the ready queue. Process queues include the job queue, ready queue, and I/O device queues. The dispatcher performs context switches to save the state of one process and load another for execution.

Uploaded by

Paolo Legaspi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Operating Systems

Process Scheduling
and Switching
Processor 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.
2
Types of Schedulers

1. Long-term scheduler (jobs scheduler) –


selects which programs/processes should be
brought into the ready queue.
2. Medium-term scheduler (emergency
scheduler) – selects which job/process should
be swapped out if system is loaded.
3. Short-term scheduler (CPU scheduler) –
selects which process should be executed next
and allocates CPU.
3
Long-Term Scheduling

• Determines which programs are admitted to the


system for processing.
• Controls the degree of multiprogramming.
• If more processes are admitted:
– less likely that all processes will be blocked –
better CPU usage.
– each process has less fraction of the CPU.
• Long-term scheduler strives for good process
mix.
4
Short-Term Scheduling

• Determines which process is going to execute next


(also called CPU scheduling).
• The short term scheduler is also known as the
dispatcher (which is part of it).
• Is invoked on a event that may lead to choose
another process for execution:
– clock interrupts
– I/O interrupts
– operating system calls and traps
5 – signals
Long/Short-Term Scheduling

Short-
term

Long-
term

6
Aspects of Schedulers
• Long-term scheduler is invoked very infrequently
(seconds, minutes)  (may be slow).
• The long-term scheduler controls the degree of
multiprogramming.
• Short-term scheduler is invoked very frequently
(milliseconds)  (must be fast).
• 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.
7
Medium-Term Scheduling
• So far, all processes have to be (at least partly)
in main memory.
• Even with virtual memory, keeping too many
processes in main memory will deteriorate the
system’s performance.
• The OS may need to swap out some processes
to disk, and then later swap them back in.
• Swapping decisions based on the need to
manage multiprogramming.
8
Schematic View of Swapping

9 A. Frank - P. Weisberg
Dynamics of Swapping
• A process can be swapped temporarily out of memory to a
backing store, and then brought back into memory for
continued execution
• Backing store – fast disk large enough to accommodate copies
of all memory images for all users; must provide direct access
to these memory images.
• Roll out, roll in – swapping variant used for priority-based
scheduling algorithms; lower-priority process is swapped out so
higher-priority process can be loaded and executed.
• Major part of swap time is transfer time; total transfer time is
directly proportional to the amount of memory swapped.
• Modified versions of swapping are found on many systems (i.e.,
UNIX, Linux, and Windows).
• System maintains a ready queue of ready-to-run processes
which have memory images on disk
10
Swapping Example

11 A. Frank - P. Weisberg
Addition of Medium Term Scheduling

Medium-
term

Short-
term
Long-
term

12
Support for Swapping

• The OS may need to suspend some processes,


i.e., to swap them out to disk and then swap
them back in.
• We add 2 new states:
– Blocked Suspend: blocked processes which have
been swapped out to disk.
– Ready Suspend: ready processes which have been
swapped out to disk.

13
A Seven-state Process Model

14 A. Frank - P. Weisberg
New state transitions
• Blocked –> Blocked Suspend
– When all processes are blocked, the OS will make room to
bring a ready process in memory.
• Blocked Suspend –> Ready Suspend
– When the event for which it has been waiting occurs
(state info is available to OS).
• Ready Suspend –> Ready
– when no more ready processes in main memory.
• Ready –> Ready Suspend (unlikely)
– When there are no blocked processes and must free
memory for adequate performance.

15
Classification of Scheduling Activity

16 A. Frank - P. Weisberg
Another view of the 3 levels of scheduling

17 A. Frank - P. Weisberg
Queuing Diagram for Scheduling

18
Dispatcher (short-term scheduler)
• Is an OS program that moves the processor
from one process to another.
• It prevents a single process from monopolizing
processor time.
• It decides who goes next according to a
scheduling algorithm.
• The CPU will always execute instructions from
the dispatcher while switching from process A
to process B.
19
Process Scheduling Queues
• Process queue – set of all processes in the
system.
• Ready queue – set of 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.

20
A Queuing Discipline

• When event n occurs, the corresponding process is


moved into the ready queue

21
Ready Queue and various I/O Device Queues

22 A. Frank - P. Weisberg
Process Switch

23 A. Frank - P. Weisberg
When to Switch a Process?
• A process switch may occur whenever the OS
has gained control of CPU. i.e., when:
– Supervisor Call
• explicit request by the program (example: file open) –
the process will probably be blocked.
– Trap
• an error resulted from the last instruction –
it may cause the process to be moved to terminated state.
– Interrupt
• the cause is external to the execution of the current
instruction – control is transferred to Interrupt Handler.
24
Reasons for Process Switch

25 A. Frank - P. Weisberg
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.
• This is called context switch.
• Context of a process represented in the PCB.
• The time it takes is dependent on hardware
support.
• Context-switch time is overhead; the system
does no useful work while switching.
26
Context switch between processes (1)

27 A. Frank - P. Weisberg
Context switch between processes (2)

28 A. Frank - P. Weisberg
Steps in Context Switch
• Save context of processor including program counter
and other registers.
• Update the PCB of the running process with its new
state and other associate information.
• Move PCB to appropriate queue – ready, blocked,
• Select another process for execution.
• Update PCB of the selected process.
• Restore CPU context from that of the selected process.

29
Example of Context Switch
p1 p2 p3 kernel I/O
}
scheduler
I/O request

device driver {
} scheduler

Time slice exceeded

} schedulerInterrupt
{
device driver
} scheduler
30
Mode Switch
• It may happen that an interrupt does not produce a
context switch.
• The control can just return to the interrupted program.
• Then only the processor state information needs to be
saved on stack.
• This is called mode switch (user to kernel mode when
going into Interrupt Handler).
• Less overhead: no need to update the PCB like for
context switch.

31

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy