[go: up one dir, main page]

0% found this document useful (0 votes)
26 views4 pages

CPU Scheduling Algorithms

The document outlines various CPU scheduling algorithms including First Come First Serve (FCFS), Shortest Job First (SJF), Shortest Remaining Time First (SRTF), Priority Scheduling (both preemptive and non-preemptive), Round Robin, and Multilevel Feedback Queue (MLFQ). Each algorithm is implemented as a function that calculates the scheduling based on process arrival times, burst times, and other criteria. The document provides the structure for each scheduling method but leaves the MLFQ implementation as a placeholder.

Uploaded by

klubk192
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

CPU Scheduling Algorithms

The document outlines various CPU scheduling algorithms including First Come First Serve (FCFS), Shortest Job First (SJF), Shortest Remaining Time First (SRTF), Priority Scheduling (both preemptive and non-preemptive), Round Robin, and Multilevel Feedback Queue (MLFQ). Each algorithm is implemented as a function that calculates the scheduling based on process arrival times, burst times, and other criteria. The document provides the structure for each scheduling method but leaves the MLFQ implementation as a placeholder.

Uploaded by

klubk192
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#First Come First Serve (FCFS)

def fcfs_scheduling(processes):
processes = sorted(processes, key=lambda x: x["Arrival"])
time = 0
schedule = []
for process in processes:
start_time = max(time, process["Arrival"]) # Wait if the process arrives later
finish_time = start_time + process["Burst"]
schedule.append({
"Process": process["Process"],
"Start": start_time,
"Finish": finish_time,
"Turnaround": finish_time - process["Arrival"],
"Waiting": (finish_time - process["Arrival"]) - process["Burst"]
})
time = finish_time
return schedule
----------------------------------------------------------------------------------------------------------------------

#Shortest Job First (SJF)

# Function to calculate SJF scheduling (Non-Preemptive)

def sjf_scheduling(processes):
processes = sorted(processes, key=lambda x: (x["Arrival"], x["Burst"]))
time = 0
schedule = []
completed = []
while len(completed) < len(processes):
available = [p for p in processes if p["Arrival"] <= time and p not in completed]
if not available:
time += 1
continue
next_process = min(available, key=lambda x: x["Burst"])
start_time = time
finish_time = start_time + next_process["Burst"]
schedule.append({
"Process": next_process["Process"],
"Start": start_time,
"Finish": finish_time,
"Turnaround": finish_time - next_process["Arrival"],
"Waiting": (finish_time - next_process["Arrival"]) - next_process["Burst"]
})
time = finish_time
completed.append(next_process)
return schedule
----------------------------------------------------------------------------------------------------------------------

#Shortest Remaining Time First (SRT)

# Function to calculate SRTF scheduling (Preemptive SJF)

def srtf_scheduling(processes):
processes = sorted(processes, key=lambda x: x["Arrival"])
time = 0
remaining_processes = [{"Process": p["Process"], "Arrival": p["Arrival"], "Remaining":
p["Burst"]} for p in processes]
schedule = []
while remaining_processes:
available = [p for p in remaining_processes if p["Arrival"] <= time]
if not available:
time += 1
continue
current_process = min(available, key=lambda x: x["Remaining"])
current_process["Remaining"] -= 1
schedule.append({"Time": time, "Process": current_process["Process"]})
if current_process["Remaining"] == 0:
remaining_processes.remove(current_process)
time += 1
return schedule
----------------------------------------------------------------------------------------------------------------------

#Priority Scheduling (Non-Preemptive)

# Function for Priority Scheduling (Non-Preemptive)

def priority_non_preemptive(processes):
processes = sorted(processes, key=lambda x: (x["Arrival"], x["Priority"]))
time = 0
schedule = []
completed = []
while len(completed) < len(processes):
available = [p for p in processes if p["Arrival"] <= time and p not in completed]
if not available:
time += 1
continue
next_process = min(available, key=lambda x: x["Priority"])
start_time = time
finish_time = start_time + next_process["Burst"]
schedule.append({
"Process": next_process["Process"],
"Start": start_time,
"Finish": finish_time,
"Turnaround": finish_time - next_process["Arrival"],
"Waiting": (finish_time - next_process["Arrival"]) - next_process["Burst"]
})
time = finish_time
completed.append(next_process)
return schedule
----------------------------------------------------------------------------------------------------------------------

#Priority Scheduling (Preemptive)

# Function for Priority Scheduling (Preemptive)

def priority_preemptive(processes):
processes = sorted(processes, key=lambda x: x["Arrival"])
time = 0
remaining_processes = [{"Process": p["Process"], "Arrival": p["Arrival"], "Remaining":
p["Burst"], "Priority": p["Priority"]} for p in processes]
schedule = []
while remaining_processes:
available = [p for p in remaining_processes if p["Arrival"] <= time]
if not available:
time += 1
continue
current_process = min(available, key=lambda x: x["Priority"])
current_process["Remaining"] -= 1
schedule.append({"Time": time, "Process": current_process["Process"]})
if current_process["Remaining"] == 0:
remaining_processes.remove(current_process)
time += 1 return schedule

#Round Robin
def round_robin(processes, quantum):
time = 0
schedule = []
ready_queue = [{"Process": p["Process"], "Arrival": p["Arrival"], "Remaining": p["Burst"]}
for p in processes]
while ready_queue:
current_process = ready_queue.pop(0)
if current_process["Remaining"] <= quantum:
start_time = time
time += current_process["Remaining"]
schedule.append({"Process": current_process["Process"], "Start": start_time, "Finish":
time})
else:
start_time = time
time += quantum
current_process["Remaining"] -= quantum
ready_queue.append(current_process)
schedule.append({"Process": current_process["Process"], "Start": start_time, "Finish":
time})
return schedule

----------------------------------------------------------------------------------------------------------------------
#Multilevel Feedback Queue (MLFQ)

def mlfq(processes, q1, q2):


queues = [
{"quantum": q1, "type": "RR", "processes": []},
{"quantum": q2, "type": "RR", "processes": []},
{"quantum": None, "type": "FCFS", "processes": []}
]
schedule = []
time = 0
processes = sorted(processes, key=lambda x: x["Arrival"])
ready_queue = [{"Process": p["Process"], "Arrival": p["Arrival"], "Remaining": p["Burst"]}
for p in processes]
while ready_queue or any(q["processes"] for q in queues):
# Implement MLFQ logic here
pass # Placeholder for detailed implementation

return schedule

You might also like