1 of 11

Operating System (CS2701)

Preemptive &

Non Preemptive

Scheduling

Dr. Rourab Paul

Computer Science Department, SNU University, Chennai

Operating System

2 of 11

Process State

2

3 of 11

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:

  • When time slice expires (timer interrupt).
  • When a higher-priority process arrives.
  • On I/O interrupt.

Because switching can occur mid-operation, you get the race condition scenario we discussed earlier if you don’t synchronize access.

4 of 11

Non Preemptive Scheduling

Operating System

4

Once a process gets the CPU, it keeps running until:

  • It voluntarily yields.
  • It blocks for I/O or some other wait.
  • It finishes execution.

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.

Sample Codes For race Condition

5 of 11

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

6 of 11

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)

7 of 11

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)

8 of 11

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

  • r (running): 0 → No process is waiting to run (CPU idle).
  • b (blocked): 0 → No process is blocked on I/O.

2. memory (in KB)

  • swpd: 937684 → Amount of virtual memory (swap) used: ~915 MB in swap.� ⚠️ Your system is using swap space — but the si/so columns below show little swapping activity, so it’s likely inactive.
  • free: 1200884 → ~1.14 GB RAM completely unused.
  • buff: 1086024 → ~1.03 GB used for buffers (disk metadata, inodes).
  • cache: 5321172 → ~5.07 GB page cache (file data kept in RAM).

9 of 11

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

  • si (swap in): 1 KB/s from swap to RAM (very low).
  • so (swap out): 4 KB/s from RAM to swap (very low).� → No active swapping pressure.

4. io (I/O)

  • bi: 22 KB/s read from block devices (like disks).
  • bo: 164 KB/s written to block devices.�

10 of 11

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

  • in (interrupts): 921 interrupts/sec.
  • cs (context switches): 2 per second → very low, system mostly idle.

6. cpu

Percentages are relative to all CPU cores:

  • us (user): 1% in user space.�sy (system): 1% in kernel space.
  • id (idle): 98% CPU idle.
  • wa (iowait): 0% waiting on I/O.
  • st (steal): 0% stolen time (not used by hypervisor).
  • gu (guest): 0% running guest VMs.

11 of 11

Thank You

Operating System

11