A process running on CPU is preempted by a new process iff the latter one has smaller execution time than the current one. We can implement the algorithm for preemptive shortest remaining time next scheduling using the following python function and simulate the execution of the processes on CPU:
import pandas as pd
def SRTN(df): # df is the data frame with arrival / burst time of processes
queue = []
cpu, cur_pdf = None, None
alloc, dalloc = {}, {}
time = 0
while True: # simulate the CPU scheduling algorithm
# check if all processes finished execution
if df['RemainingTime'].max() == 0:
break
# get current process assigned to cpu, if any
if cpu:
cur_pdf = df[df.Process == cpu]
# check if a process arrived at this time instance and put it into wait queue
pdf = df[df.ArrivalTime == time]
if len(pdf) > 0:
for p in pdf['Process'].values:
queue.append(p)
if len(queue) > 0:
pdf = df[df['Process'].isin(queue)]
# find the process with shortest remaining time
if len(pdf) > 0:
pdf = pdf[pdf['RemainingTime']==pdf['RemainingTime'].min()]
# allocate a process to CPU, pre-empt the running one if required
if (cpu is None) or (len(pdf) > 0 and pdf['RemainingTime'].values[0] < cur_pdf['RemainingTime'].values[0]):
if cpu:
# prempt the current process
dalloc[cpu] = dalloc.get(cpu, []) + [time]
queue.append(cpu)
print('Process {} deallocated from CPU at time {}'.format(cpu, time))
cur_pdf = pdf
cpu = cur_pdf['Process'].values[0]
queue.remove(cpu)
print('Process {} allocated to CPU at time {}'.format(cpu, time))
alloc[cpu] = alloc.get(cpu, []) + [time]
df.loc[df['Process']==cpu,'RemainingTime'] -= 1
time += 1 # increment timer
# deallocate process
if df[df['Process']==cpu]['RemainingTime'].values[0] == 0:
print('Process {} deallocated from CPU at time {}'.format(cpu, time))
dalloc[cpu] = dalloc.get(cpu, []) + [time]
cpu = cur_pdf = None
return alloc, dalloc
Now, run SRTN on the following data (process arrival / burst times):
df = pd.DataFrame({'Process':['A','B','C','D'], 'BurstTime':[3,5,3,2], 'ArrivalTime':[0,2,5,6]})
df.sort_values('ArrivalTime', inplace=True)
df['RemainingTime'] = df.BurstTime
df

alloc, dalloc = SRTN(df)
# Process A allocated to CPU at time 0
# Process A deallocated from CPU at time 3
# Process B allocated to CPU at time 3
# Process B deallocated from CPU at time 8
# Process D allocated to CPU at time 8
# Process D deallocated from CPU at time 10
# Process C allocated to CPU at time 10
# Process C deallocated from CPU at time 13
# alloc
# {'A': [0], 'B': [3], 'D': [8], 'C': [10]}
# dalloc
# {'A': [3], 'B': [8], 'D': [10], 'C': [13]}
The following animation shows how the Gantt chart for the preemptive SRTN scheduling algorithm, obtained using the above implementation:

Let's consider the following input table for the arrival of the following 3 processes and run SRTN on the data frame to obtain the corresponding Gantt chart:

alloc, dalloc, events = SRTN(df)
# Process A allocated to CPU at time 0
# Process A deallocated from CPU at time 1
# Process B allocated to CPU at time 1
# Process B deallocated from CPU at time 5
# Process A allocated to CPU at time 5
# Process A deallocated from CPU at time 11
# Process C allocated to CPU at time 11
# Process C deallocated from CPU at time 19
The Gantt chart corresponding to the above table is shown in the following animation, obtained using the above algorithm:
