FCFS and SJF
Teachers in class ask that no matter what language they use to write, they first serve FCFS and SJF for short assignments. They greed for time and use python to write. Online resources are also scarce. There's no difficulty. It's a purely logical problem. I'm afraid I'll forget it myself, so write it down for later use.
The version used is Python 3.0
The software is pycharm
#coding:utf-8
###python3.5
__author__ = 'DD'
global N
N = 5
pcb = []
#Input function
def inPcb():
global N
i=0
while (i < N):
print("****************************************")
pName = input("Please enter the process name:") #Process name
inTime = input("Please enter the entry time:") #Entry time
serverTime = input("Please enter the service time:") #service time
#Data is stored sequentially in the list startTime = start time finishTime = finish time zzTime = turn-around time dqzzTime = weighted turn-around time
# Process Name Entry Time Service Time Starts Completing Turnover Right
pcb.append([pName, inTime, serverTime, 0, 0, 0, 0])
i = i + 1
#First come first served
def fcfs():
global N
pcb.sort(key=lambda x:x[1], reverse=False) ##Ascending order x:x[1] sorted by entry time
i = 0
for i in range(N):
if(i==0):
startTime = int(pcb[i][1])
pcb[i][3] = startTime
pcb[i][4] = startTime + int(pcb[i][2])
else:
startTime = pcb[i-1][4]
pcb[i][3] = startTime
pcb[i][4] = startTime + int(pcb[i][2])
i = i + 1
for i in range(N): #Calculating Turnover Time and Weighted Turnover Time
pcb[i][5] = float(pcb[i][4]) - int(pcb[i][1]) #Turnover time
pcb[i][6] = float(pcb[i][5]) / int(pcb[i][2]) #Weighted Turnover Time
i = i + 1
#Output the results
pcb.sort(key=lambda x:x[0], reverse=False) ##Sort by process name in ascending order
print('Operation results:')
for i in range(N):
print("Process name:%s Entry time:%d Service hours:%d Start time:"
"%d Completion time:%d Turnover time:%d Weighted turnaround time:%.2f"
% (pcb[i][0],int(pcb[i][1]),int(pcb[i][2]),int(pcb[i][3]),
int(pcb[i][4]),int(pcb[i][5]),float(pcb[i][6])))
i +=1
#Short assignment priority
def sjf():
sjf_pcb=pcb
temp_pcb = pcb[1:len(sjf_pcb)] #Slices temporarily store the backup queue len(sjf_pcb) to obtain the length.
temp_pcb.sort(key=lambda x:x[2], reverse=False) ##Sort back queues by service time
sjf_pcb[1:len(sjf_pcb)]=temp_pcb
i = 0
for i in range(N):
if(i==0):
startTime = int(sjf_pcb[0][1])
pcb[0][3] = startTime
pcb[0][4] = startTime + int(sjf_pcb[0][2])
else:
startTime = sjf_pcb[i-1][4]
pcb[i][3] = startTime
pcb[i][4] = startTime + int(sjf_pcb[i][2])
i = i + 1
for i in range(N):
sjf_pcb[i][5] = int(sjf_pcb[i][4]) - int(sjf_pcb[i][1]) #Turnover time
sjf_pcb[i][6] = float(sjf_pcb[i][5]) / int(sjf_pcb[i][2]) #Weighted Turnover Time
i = i + 1
#Output results
pcb.sort(key=lambda x:x[0], reverse=False) ##Sort by process name in ascending order
print('Short assignment priority SJF Operation results:')
for i in range(N):
print("Process name:%s Entry time:%d Service hours:%d Start time:"
"%d Completion time:%d Turnover time:%d Weighted turnaround time:%.2f"
% (sjf_pcb[i][0],int(sjf_pcb[i][1]),int(sjf_pcb[i][2]),int(sjf_pcb[i][3]),
int(sjf_pcb[i][4]),float(sjf_pcb[i][5]),float(sjf_pcb[i][6])))
if __name__ == '__main__':
inPcb()
fcfs()
sjf()
The results are as follows:
**************************************** Please enter the process name:a Please enter entry time:0 Please enter service time:4 **************************************** Please enter the process name:b Please enter the entry time:1 Please enter service time:3 **************************************** Please enter the process name:c Please enter the entry time:2 Please enter service time:5 **************************************** Please enter the process name:d Please enter the entry time:3 Please enter service time:2 **************************************** Please enter the process name:e Please enter the entry time:4 Please enter service time:4 Operation results: Process Name: a Entry Time: 0 Service Time: 4 Start Time: 0 Completion Time: 4 Turnover Time: 4 Weighted Turnover Time: 1.00 Process Name: b Entry Time: 1 Service Time: 3 Start Time: 4 Completion Time: 7 Turnover Time: 6 Weighted Turnover Time: 2.00 Process Name: c Entry Time: 2 Service Time: 5 Start Time: 7 Completion Time: 12 Turnover Time: 10 Weighted Turnover Time: 2.00 Process Name: d Entry Time: 3 Service Time: 2 Start Time: 12 Completion Time: 14 Turnover Time: 11 Weighted Turnover Time: 5.50 Process Name: e Entry Time: 4 Service Time: 4 Start Time: 14 Completion Time: 18 Turnover Time: 14 Weighted Turnover Time: 3.50 Short Job Priority SJF Running Results: Process Name: a Entry Time: 0 Service Time: 4 Start Time: 0 Completion Time: 4 Turnover Time: 4 Weighted Turnover Time: 1.00 Process Name: b Entry Time: 1 Service Time: 3 Start Time: 6 Completion Time: 9 Turnover Time: 8 Weighted Turnover Time: 2.67 Process Name: c Entry Time: 2 Service Time: 5 Start Time: 13 Completion Time: 18 Turnover Time: 16 Weighted Turnover Time: 3.20 Process Name: d Entry Time: 3 Service Time: 2 Start Time: 4 Completion Time: 6 Turnover Time: 3 Weighted Turnover Time: 1.50 Process Name: e Entry Time: 4 Service Time: 4 Start Time: 9 Completion Time: 13 Turnover Time: 9 Weighted Turnover Time: 2.25