0% found this document useful (0 votes)
50 views

Lab08: Objectives: Example Code

This document discusses three scheduling algorithms: FCFS, SJF, and RR. It provides: 1. An overview of classes and objects in Python. 2. Pseudocode for the FCFS algorithm and an example code implementation. 3. A high-level description of the SJF and RR algorithms. 4. Exercises to implement each algorithm using classes and potentially with a GUI.

Uploaded by

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

Lab08: Objectives: Example Code

This document discusses three scheduling algorithms: FCFS, SJF, and RR. It provides: 1. An overview of classes and objects in Python. 2. Pseudocode for the FCFS algorithm and an example code implementation. 3. A high-level description of the SJF and RR algorithms. 4. Exercises to implement each algorithm using classes and potentially with a GUI.

Uploaded by

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

Lab08:

Objectives
To learn
1. Classes and objects
2. Implementation of FCFS, SJF and RR

1. Classes and Objects


A class is a special data type which defines how to build a certain kind of object.
The class also stores some data items that are shared by all the instances of this class
Instances are objects that are created which follow the definition given inside of the class
Python doesn’t use separate class interface definitions as in some languages
You just define the class and then use it.
Generic class skeleton
class MyClass : ////class name
def __init__ (self,p1,p2): /////Constructor
self.var1 = p1
self.var2= p2

def get_var1 (self): //////Function with return


return self.var1
Example code:
#!/usr/bin/python
class myclass:
def __init__(self,a):
self.a=a
def disp(self):
print("this is class func",self.a)

o1=myclass(2)
o1.disp()
2. FCFS algorithm

ALGORITHM:

1. Create a class or structure “Process”.


2. Create an array of the processes
3. Take input from user, id and service time of the processes.
4. initialize
∑ pr[1].W_time=0;
∑ pr[1].T_time=pr[1].S_time;
5. Calculate the Total waiting time and turnaround time the remaining processes.

∑ W_time(pn) = T_time(pn-1)
∑ T_time(pn) = W_time(pn) + S_time(pn).

6. Total waiting time is calculated by adding the waiting time of all processes.
7. Calculate Average
∑ Average_W_time=total_waiting _time/#of processes;

8. Calculate Average turnaround time .


9. Display the result.
Example code:
#!/usr/bin/python
TAT=0
p=[]
at=[]
et=[]
wt=[0]
for i in range(1,6):
at.append(input("enter a time :"))
for i in range(1,6):
et.append(input("enter e time: "))
for i in range(1,6):
p.append(raw_input("enter process: "))
print("process ",p)
print(“process execution time:”,et)
print(“process Arrival Time:”,at)
for i in range(0,5):
TAT=TAT+et[i]
wt.append(TAT)

#Hint: Await=wt[1]+wt[2]+wt[3]+wt[4]
p="Turnaround time :"+repr(TAT)
print (p)

1. SJF (Shortest Job First)

ALGORITHM:

Step1:Get the number of process.


Step2:Get the id and service time for each process.
Step3:Initially the waiting time of first short process as 0 and total time of first short is
process the service time of that process.
Step4:Calculate the total time and waiting time of remaining process.
Step5:Waiting time of one process is the total time of the previous process.
Step6:Total time of process is calculated by adding the waiting time and service time of each
process.
Step7:Total waiting time calculated by adding the waiting time of each process.
Step8:Total turnaround time calculated by adding all total time of each process.
Step9:calculate average waiting time by dividing the total waiting time by total number of
process.
Step10:Calculate average turnaround time by dividing the total waiting time by total number
of process.
Step11:Display the result.

2. RR (Round Robin)
ALGORITHM:

Step 1: Initialize all the structure elements


Step 2: Receive inputs from the user to fill process id,burst time and arrival time.
Step 3: Calculate the waiting time for all the process id.
i) The waiting time for first instance of a process is calculated as:
a[i].waittime=count + a[i].arrivt
ii) The waiting time for the rest of the instances of the process is
calculated as:
a) If the time quantum is greater than the remaining burst time then waiting time is
calculated as:
a[i].waittime=count + tq
b) Else if the time quantum is greater than the remaining burst time then waiting time is
calculated as:
a[i].waittime=count - remaining burst time
Step 4: Calculate the average waiting time and average turnaround time
Step 5: Print the results of the step 4.

Exercises

Exercise 1

Implement FCFS using concept of class.


Implement SJF.
Implement RR.

Exercise 2

Implement RR with GUI

You might also like