Practical File
Practical File
Practical – 1
Objective:
Write a program to implement CPU scheduling for first come first serve.
Code:
# calculating turnaround
# time by adding bt[i] + wt[i]
for i in range(n):
tat[i] = bt[i] + wt[i]
# Function to calculate
# average time
wt = [0] * n
tat = [0] * n
total_wt = 0
total_tat = 0
if __name__ =="__main__":
# process id's
n = int(input("Enter number of Processes: "))
processes = [i for i in range(1, n+1)]
Output:
Practical – 2
Objective:
Write a program to implement CPU scheduling for Shortest Job First(SJF).
Code:
def main():
# Taking the number of processes
n = int(input("Enter number of process: "))
# Matrix for storing Process Id, Burst Time, Average Waiting Time & Average Turn Around
Time.
A = [[0 for j in range(4)] for i in range(100)]
total, avg_wt, avg_tat = 0, 0, 0
print("Enter Burst Time:")
for i in range(n): # User Input Burst Time and alloting Process Id.
A[i][1] = int(input(f"P{i+1}: "))
A[i][0] = i + 1
avg_tat = total / n
print(f"Average Waiting Time= {avg_wt}")
print(f"Average Turnaround Time= {avg_tat}")
if __name__ == "__main__":
main()
Output:
Practical – 3
Objective:
Write a program to perform priority scheduling.
Code:
proc = []
for i in range(5):
l = []
for j in range(4):
l.append(0)
proc.append(l)
wavg = 0
tavg = 0
stime = [0] * 5
ctime = [0] * 5
stime[0] = 1
ctime[0] = stime[0] + tat[0]
print("Process_no\tStart_time\tComplete_time",
"\tTurn_Around_Time\tWaiting_Time")
print()
print()
print()
for i in range(totalprocess):
proc[i][0] = arrivaltime[i]
proc[i][1] = bursttime[i]
proc[i][2] = priority[i]
proc[i][3] = i + 1
Objective:
Write a program to implement CPU scheduling for Round Robin.
Code:
if __name__ =="__main__":
# Process id's
n = int(input("Enter no. of processes: "))
# Time quantum
quantum = int(input("Enter the time quantum for this scheduling: "));
print()
findavgTime(processes, n, burst_time, quantum)
print()
Output:
Practical – 5
Objective:
Write a program to simulate the concept of dining philosopher problem.
Code:
import threading
import time
import random
mutex.acquire()
left_fork_index = index
right_fork_index = (index + 1) % num_forks
forks[left_fork_index].acquire()
forks[right_fork_index].acquire()
mutex.release()
print(f"Philosopher {index} is eating...")
time.sleep(random.randint(1, 5))
forks[left_fork_index].release()
forks[right_fork_index].release()
for i in range(num_philosophers):
philosopher_threads.append(threading.Thread(target=philosopher, args=(i,)))
Output:
As we can see from the output, each philosopher takes turns thinking and eating, and there is no
deadlock or starvation.
Practical – 5
Objective:
Write a program to implement reader/writer problem using semaphore.
Code:
import threading
import time
class ReaderWriter():
def __init__(self):
self.rd = threading.Semaphore() #initializing semaphores using Semaphore class in
threading module for reading and wrting
self.wrt = threading.Semaphore()
def reader(self):
while True:
self.rd.acquire() #wait on read semaphore
time.sleep(3)
def writer(self):
while True:
self.wrt.acquire() #wait on write semaphore
time.sleep(3)
def main(self):
# calling mutliple readers and writers
t1 = threading.Thread(target = self.reader)
t1.start()
t2 = threading.Thread(target = self.writer)
t2.start()
t3 = threading.Thread(target = self.reader)
t3.start()
t4 = threading.Thread(target = self.reader)
t4.start()
t6 = threading.Thread(target = self.writer)
t6.start()
t5 = threading.Thread(target = self.reader)
t5.start()
if __name__=="__main__":
c = ReaderWriter()
c.main()
Output:
Thus, the semaphore ‘wrt‘ is queued on both readers and writers in a manner such that preference is
given to readers if writers are also there. Thus, no reader is waiting simply because a writer has
requested to enter the critical section.