Operating System (CS2701)
Preemptive &
Non Preemptive
Scheduling
Dr. Rourab Paul
Computer Science Department, SNU University, Chennai
Operating System
Process State
2
Preemptive Scheduling
Operating System
3
OS can forcefully take CPU away from a running process/thread at almost any time (like a referee blowing a whistle in the middle of play).�
Context switch can happen:
Because switching can occur mid-operation, you get the race condition scenario we discussed earlier if you don’t synchronize access.
Non Preemptive Scheduling
Operating System
4
Once a process gets the CPU, it keeps running until:
Context switch still happens, but only at safe points — between processes, not mid-computation.�
This greatly reduces (but does not entirely remove) race condition risks, because a thread won’t lose CPU control in the middle of an update unless it explicitly calls something that causes it to block.
Dispatcher
Operating System
5
Another component involved in the CPU-scheduling function is the dispatcher.
The dispatcher is the module that gives control of the CPU’s core to the process
selected by the CPU scheduler. This function involves the following:
• Switching context from one process to another
• Switching to user mode
• Jumping to the proper location in the user program to resume that program
vmstat
Operating System
6
An interesting question to consider is, how often do context switches
occur? On a system-wide level, the number of context switches can be obtained
by using the vmstat command that is available on Linux systems.
vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
0 0 937684 1200884 1086024 5321172 1 4 22 164 921 2 1 1 98 0 0 0
vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
0 0 937684 972156 1086180 5327188 1 4 22 164 921 2 1 1 98 0 0 0
0 0 937684 977220 1086180 5326632 0 0 0 0 1019 1466 1 0 99 0 0 0
0 0 937684 988416 1086180 5318312 0 0 0 0 1274 2150 1 0 99 0 0 0
1 → sample the stats every 1 second
3 → collect 3 samples in total (including the header + 3 lines of data)
vmstat
Operating System
7
An interesting question to consider is, how often do context switches
occur? On a system-wide level, the number of context switches can be obtained
by using the vmstat command that is available on Linux systems.
vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
0 0 937684 1200884 1086024 5321172 1 4 22 164 921 2 1 1 98 0 0 0
vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
0 0 937684 972156 1086180 5327188 1 4 22 164 921 2 1 1 98 0 0 0
0 0 937684 977220 1086180 5326632 0 0 0 0 1019 1466 1 0 99 0 0 0
0 0 937684 988416 1086180 5318312 0 0 0 0 1274 2150 1 0 99 0 0 0
1 → sample the stats every 1 second
3 → collect 3 samples in total (including the header + 3 lines of data)
vmstat
Operating System
8
vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
0 0 937684 1200884 1086024 5321172 1 4 22 164 921 2 1 1 98 0 0 0
1. procs
2. memory (in KB)
vmstat
Operating System
9
vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
0 0 937684 1200884 1086024 5321172 1 4 22 164 921 2 1 1 98 0 0 0
3. swap
4. io (I/O)
vmstat
Operating System
10
vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
0 0 937684 1200884 1086024 5321172 1 4 22 164 921 2 1 1 98 0 0 0
5. system
6. cpu
Percentages are relative to all CPU cores:
Thank You
Operating System
11