[5/5] SPPU SPOSL - SJF Preemptive Scheduling in Python

Опубликовано: 30 Май 2026
на канале: Visual Notes Log
1,404
12

Code:

def sjf(n,a,b):
q,t = [""],[]
max = 0
for i in range(len(a)):
if(a[i] is greater than max):
max = a[i]
for j in range(0,len(a)-i-1):
if(a[j] is greater than a[j+1]):
n[j],n[j+1] = n[j+1],n[j]
a[j],a[j+1] = a[j+1],a[j]
b[j],b[j+1] = b[j+1],b[j]
elif(a[j]==a[j+1] and b[j] is greater than b[j+1]):
n[j],n[j+1] = n[j+1],n[j]
a[j],a[j+1] = a[j+1],a[j]
b[j],b[j+1] = b[j+1],b[j]
c,cs = -1,99999999
for i in range(0,max+1):
flag = 0
print(i)
for j in range(len(a)):
if(a[j]==i and b[j] is less than cs):
flag = 1
c = j
cs = b[j] - 1
if(i!=max):
b[j] -= 1
if(c!=-1):
if(i!=max):
q.append(n[c])
t.append(i)
break
if(flag!=1):
cs -= 1
b[c] -= 1
gantt(q,t)
for i in range(len(a)):
for j in range(0,len(a)-i-1):
if(b[j] is greater than b[j+1]):
n[j],n[j+1] = n[j+1],n[j]
a[j],a[j+1] = a[j+1],a[j]
b[j],b[j+1] = b[j+1],b[j]

for i in range(len(n)):
if(b[i]!=0):
q.append(n[i])
t.append(t[len(t)-1]+b[i])
gantt(q,t)

def main():
x = int(input("Enter the no. of process : "))
n,a,b = [],[],[]
for i in range(x):
n.append(input("Enter the name of Process "+str(i+1)+" : "))
a.append(input("Enter the Arrival Time of Process "+str(i+1)+" : "))
b.append(input("Enter the Burst Time of Process "+str(i+1)+" : "))
sjf(n,a,b)
main()