Demo entry 2638177

SRTN scheduling policy

   

Submitted by anonymous on Sep 06, 2015 at 21:51
Language: Python. Code size: 3.4 kB.

fp = open("process.txt" , "r")
proccesses = {}
Wait = []
Running = []
Terminated = 0


for i in fp.readlines():
    i.split(" ")
    #print (i)
    proccesses[i[0]] = [int(i[2]), int(i[4]) , int(i[6]),0,0,int(i[4])] #arrival time, service time, priority, waiting time, hrrn,rst

Total_proccesses = len(proccesses)




def schedule():
        Terminated = 0
        t=0
        Waiting_times = []
        Finish_times = []
        Turnaround_times = []
        schedule = []
        start_times =[]
        proccess_x = sorted(proccesses.items() , key = lambda x:x)
        print(proccess_x)
        
        while Terminated != Total_proccesses and t<20:
            arrived = 0            
            for i in range ( 0 , len(proccess_x) ):
                proccess = proccess_x[i] 
                
                at = proccess_x[i][1][0]
                st = proccess_x[i][1][1]
                wt = proccess_x[i][1][3]
                
                
                if (t == at):
                    arrived = 1
                    print("proccess", proccess, " arriving at ", at)
                    Wait.append(proccess) 
                    
                    
                if(len(Running)!=0):
                    print ("Running",Running, "at time ", t)
                    print ("Waiting",Wait, "at time ", t)
                    if (len(Wait)!=0 and Wait[0][1][5] < Running[0][1][5]):
                        if(arrived == 1 or t > proccess_x[-1][1][0]):
                            SRTN()
                            Wait.append(Running[0]) 
                            Running.remove(Running[0])
                            Finish_times.append(t)                        
                            Running.append(Wait[0])
                            Waiting_times.append(Running[0][1][3])
                            schedule.append(Running[0])
                            print("Schedule = ", schedule)
                            execution_start_time = t
                            start_times.append(execution_start_time)
                            Wait.pop(0)
                else:
                    if((len(Wait)!=0 and arrived==1) or t > proccess_x[-1][1][0]):
                        SRTN()
                        Running.append(Wait[0])
                        Waiting_times.append(Running[0][1][3])
                        schedule.append(Running[0])
                        execution_start_time = t
                        start_times.append(execution_start_time)
                        Wait.pop(0)
                if(len(Running)!=0 and Running[0][1][5] <= 0):
                    Running.remove(Running[0])
                    Terminated = Terminated + 1
                    Finish_times.append(t) 
            t = t+1
            if(len(Running)!=0) :
                Running[0][1][5] -=1
            for i in range (0 , len(Wait)) :
                Wait[i][1][3] += 1
        Finish_times.append(t)    
        print("Schedule  Starting time   Finish Time  Waiting Time")
        for i in range(0,len(schedule)):
            print (schedule[i][0],"           ",start_times[i], "              ", Finish_times[i], "             ",Waiting_times[i])

def SRTN():
    Wait.sort(key=lambda x:x[1][5], reverse=False)
schedule()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).