-1

Consider the following code that takes input processes and their arrival times and sort them according to FCFS algorithm, so i've been thinking about algorithms to compute avg waiting time but all is false so any suggestions ?

Processes = input(" Enter the processes times & arrival times separated by a comma: ")

    BurstTimes = Processes[::2]
    ArrivalTimes = Processes[1::2]
    print '   Burst Times:', BurstTimes, '\n', '   Arrival Times', ArrivalTimes,'\n',
    list1, list2 =  BurstTimes, ArrivalTimes
    indices1 = range(len(list1))
    indices1.sort(key=lambda x: list1[x])
    list1 = map(lambda i: list1[i], indices1)
    print 'The Processes executed in this order: ', list1 
    Wt_Time = 0 

the code for calculating the total time:

print 'The Processes executed in this order: ', list1

    process_queue = []
    total_wtime = 0
    for i in range(len(list2)):
        process_queue[i].append(int(list1[i])
        total_wtime += process_queue[i][1]

and it gives error at the last line

10
  • "all is false" - what does that mean? What have you tried, and what exactly is the problem with it?
    – jonrsharpe
    Apr 18, 2014 at 12:19
  • the ouput give wrong values Apr 18, 2014 at 12:19
  • What values? What values are you expecting? And where is the code?
    – jonrsharpe
    Apr 18, 2014 at 12:21
  • waitingTime = startTime - arrivalTime turnaroundTime = burstTime + waitingTime = finishTime- arrivalTime Apr 18, 2014 at 12:25
  • Please edit the question to add the code, properly formatted, and provide the two other pieces of information I have asked for.
    – jonrsharpe
    Apr 18, 2014 at 12:29

3 Answers 3

0

process_queue[i].append(int(list1[i])

you are appending the i'th element of list1 to the process_queue list , You should note that list1[i] is not a list but an integer . and in next line you are trying to access

total_wtime += process_queue[i][1]

1st element of the process_queue[i] but it is not a 2d array

total_wtime += process_queue[i]

should work .

0
# Author:Geraldo Braho
process_queue = []
total_wtime = 0
n = int(raw_input('Enter the total numbers of processes: '))
for i in xrange(n):
    process_queue.append([])#append a list object to the list
    process_queue[i].append(raw_input('Enter process name: '))
    process_queue[i].append(int(raw_input('Enter process arrival Time : ')))
    total_wtime += process_queue[i][1]
    process_queue[i].append(int(raw_input('Enter Process CPU bustTime : ')))
    print ''

process_queue.sort(key = lambda process_queue:process_queue[1])

print 'ProcessName\tArrivalTime\tBurstTime'
for i in xrange(n):
    print process_queue[i][0],'\t\t',process_queue[i][1],'\t\t',process_queue[i][2]

print 'Total waiting time: ',total_wtime
print 'Average waiting time: ',(total_wtime/n)
0

Concept: https://www.geeksforgeeks.org/program-fcfs-scheduling-set-2-processes-different-arrival-time/

processes = [
    {"pid": "P0", "burst_time": 5, "arrival_time": 0, "service_time": None, "waiting_time": None},
    {"pid": "P1", "burst_time": 3, "arrival_time": 1, "service_time": None, "waiting_time": None},
    {"pid": "P2", "burst_time": 8, "arrival_time": 2, "service_time": None, "waiting_time": None},
    {"pid": "P3", "burst_time": 6, "arrival_time": 3, "service_time": None, "waiting_time": None}
]

# No waiting time for first process
processes[0]["waiting_time"] = 0
# Service_time of the first process is it's arrival time
processes[0]["service_time"] = processes[0]["arrival_time"]

for i in range(1, len(processes)):
    # Case where process comes when processor is in idle state
    if processes[i]["arrival_time"] > (processes[i - 1]["service_time"] + processes[i - 1]["burst_time"]):
        processes[i]["service_time"] = processes[i]["arrival_time"]
    else:
        processes[i]["service_time"] = processes[i - 1]["service_time"] + processes[i - 1]["burst_time"]

    processes[i]["waiting_time"] = processes[i]["service_time"] - processes[i]["arrival_time"]

total_waiting_time = 0

for process in processes:
    total_waiting_time += process["waiting_time"]

avg_waiting_time = total_waiting_time / len(processes)

print("Average Waiting Time:", avg_waiting_time)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.