Scheduling algorithm in python

0 votes
asked Nov 28, 2018 by Syed Faiq Yazdani (120 points)
I need to implement first come first served ,shortest job next ,shortest remaining time and round robin i am new to python i have implemented first come first served this is my code

arrival_time=[]

burst_time=[]

ch=int(input("Enter number of processes:"))

i=0

while i<ch:
    
a=int(input("Enter arrival time:"))
    
b=int(input("Enter burst time:"))
    
arrival_time.insert(i,a)
    
burst_time.insert(i,b)
    
i=i+1

print("Process Arrival Time  Burst Time ")

print(arrival_time,burst_time)

for j in range(ch):
    
for j in range(j+1,ch-1):
        
if arrival_time[j]>arrival_time[j+1]:
        
    temp=arrival_time[j]
            
    arrival_time[j]=arrival_time[j+1]
         
    arrival_time[j+1]=temp

print(arrival_time)  

k=0

sum=0
print("Gantt Chart")

while k<ch:
    

if k==0:
  
sum=sum+burst_time[k]      
print(arrival_time[k],"------",sum)
    
elif k>0:
 
sum1=sum+burst_time[k]       
print("-------",sum1)
   
 k=k+1

1 Answer

0 votes
answered Dec 10, 2018 by Tomas Z (180 points)

https://onlinegdb.com/rk1YAx2JN
# input values:
ar = [ 5,  9, 1,  7, 3]
bu = [10, 13, 9, 10, 6]

tg = sorted( list( zip(ar, bu) ))

print("Process arrival Time, burst Time")
print("{}\n{}\n{}\n".format(ar, bu, tg) )

print("Gantt Chart")

for i in range(len(ar)):
    p  = tg[i]
    s  = " "* (p[0]-1)           # ar[i], spaces before
    s += str(  p[0]  )           # ar[i], numeric value 1
    s += "-"* (p[1]-p[0]-1)   # bu[i], graphic line
    s += str(  p[1]  )            # bu[i], numeric value 2
    
    print(s)

'''
Process arrival Time, burst Time
[5, 9, 1, 7, 3]
[10, 13, 9, 10, 6]
[(1, 9), (3, 6), (5, 10), (7, 10), (9, 13)]

Gantt Chart
1-------9
  3--6
    5----10
      7--10
        9---13
'''

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...