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

Os Practical

This document contains solutions to operating system practical questions. It includes implementations of FCFS CPU scheduling, SJF CPU scheduling, disk scheduling, memory management algorithms like first fit, best fit and worst fit, shared resource problems using threads, bankers algorithm and page replacement using LRU policy.

Uploaded by

Raj Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Os Practical

This document contains solutions to operating system practical questions. It includes implementations of FCFS CPU scheduling, SJF CPU scheduling, disk scheduling, memory management algorithms like first fit, best fit and worst fit, shared resource problems using threads, bankers algorithm and page replacement using LRU policy.

Uploaded by

Raj Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Raj ghosh

D053

40315200041

Operating system practical

1.To implement fcfs cpu scheduling algorithm

Ans.

#Wt=waiting time

#Cbc=cpu burst cycle

#Tat=turn around time

#Avgtime=average time

def findingWt(interpret, num, cbc, wt):

wt[0] = 0

for i in range(1, num ):

wt[i] = cbc[i - 1] + wt[i - 1]

def findingTat(interpret, num, cbc, wt, tat):

for i in range(num):

tat[i] = cbc[i] + wt[i]

def findingavgTime( interpret, num, cbc):

wt = [0] * num

tat = [0] * num

total_wt = 0

total_tat = 0

findingWt(interpret, num, cbc, wt)

findingTat(interpret, num, cbc, wt, tat)


print( "interpreting Cpu burst cycle" + " Waiting time "
+ " Turn around time")

for i in range(num):

total_wt = total_wt + wt[i]

total_tat = total_tat + tat[i]

print(" " + str(i + 1) + "\t\t" +

str(cbc[i]) + "\t " +

str(wt[i]) + "\t\t " +

str(tat[i]))

print( "Average waiting time = "+ str(total_wt / num))

print("Average turn around time = "+ str(total_tat /


num))

if __name__ =="__main__":

interpret = [ 1, 2, 3, 4, 5]

num = len(interpret)

cbc = [2, 8, 12, 10, 6]

findingavgTime(interpret, num, cbc )

output:
2.To implement sfj cpu scduling program

Ans.

#avg=average

#bt=burst time

#wt=watch time

#tat=turn around time

#pro=processes

max_processes = 12

num=int(input("Enter the number of processes: "))

pro = []

bt = []

wt = [0]*max_processes

tat = [0]*max_processes

avg_tat = 0

avg_wt = 0
for i in range(num):

pro.append(input("Enter the Process Id: "))

bt.append(int(input("Enter the Cpu Burst Cycle: ")))

for i in range(num):

for j in range(num-i-1):

if bt[j]>bt[j+1]:

bt[j], bt[j+1] = bt[j+1], bt[j]

pro[j] , p[j+1] = p[j+1], p[j]

for i in range(num):

wt[i] = 0

tat[i] = 0

for j in range(i):

wt[i] = wt[i] + bt[j]

tat[i] = wt[i] + bt[i]

avg_tat = avg_tat + tat[i]

avg_wt = avg_wt + wt[i]

print("Process \t\t burst time \t\t wt \t\t tat")

print()

for i in range(num):

print(" ", pro[i], "\t\t\t\t", bt[i], "\t\t", wt[i], "\t\t", tat[i])

print()

print("Average Tat: ", avg_tat/num)

print("Average Wt: ", avg_wt/num)

output:
3.To implement disc scheduling algorithm

size = 7;

def FCFS(array, head):

seek_count = 0;

distance, current_track = 0, 0;

for i in range(size):

currrent_track = array[i];

distance = abs(current_track - head);

seek_count += distance;

head = current_track;

print("No. of seek operations performed = ",seek_count);

print("Seek Sequence will be");

for i in range(size):

print(array[i]);

if __name__ == '__main__':

array = [ 56, 45, 42, 10 ,112 , 71, 97, 45 ];


head = 50;

FCFS(array, head);

Output:

Implementation of First Fit


A.
def first_fit(block_size, m, process_size, num):
alloc = [-1] * num
for i in range(num):
for j in range(m):
if block_size[j] >= process_size[i]:
alloc[i] = j
block_size[j] -= process_size[i]
break
print(" Process Number Process Size Block Number")
for i in range(num):
print(" ", i + 1, " ", process_size[i],
" ", end = " ")
if alloc[i] != -1:
print(alloc[i] + 1)
else:
print("Not Allocated")
if __name__ == '__main__':
block_size = [15, 12, 10, 10, 25]
process_size = [8, 7, 5, 1, 6, 2]
m = len(block_size)
num = len(process_size)

first_fit(block_size, m, process_size, num)


output:
B.
Implementation of Best - Fit algorithm
def best_fit(block_size, m, process_size, num):
alloc = [-1] * num
for i in range(num):
Index = -1
for j in range(m):
if block_size[j] >= process_size[i]:
if Index == -1:
Index = j
elif block_size[Index] > block_size[j]:
Index = j
if Index != -1:
alloc[i] = Index
block_size[Index] -= process_size[i]
print("Process Number Process Size Block Number")
for i in range(num):
print(i + 1, " ", process_size[i],
end = "
")
if alloc[i] != -1:
print(alloc[i] + 1)
else:
print("Not Allocated")
if __name__ == '__main__':
block_size = [15, 12, 10, 10, 25]
process_size = [8, 7, 5, 1, 6, 2]
m = len(block_size)
num = len(process_size)

best_fit(block_size, m, process_size, num)


output:

C.
Implementation of worst - Fit algorithm
def worst_fit(block_size, m, process_size, num):
alloc = [-1] * num
for i in range(num):
Index = -1
for j in range(m):
if block_size[j] >= process_size[i]:
if Index == -1:
Index = j
elif block_size[Index] < block_size[j]:
Index = j
if Index != -1:
alloc[i] = Index
block_size[Index] -= process_size[i]
print("Process Number Process Size Block Number")
for i in range(num):
print(i + 1, " ",
process_size[i], end = " ")
if alloc[i] != -1:
print(alloc[i] + 1)
else:
print("Not Allocated")
if __name__ == '__main__':
block_size = [15, 12, 10, 10, 25]
process_size = [8, 7, 5, 1, 6, 2]
m = len(block_size)
num = len(process_size)
worst_fit(block_size, m, process_size, num)
output:
6.Shared resource java prg
Ans.
class room
{
public synchronized void givesPresentation(String name) throws
InterruptedException
{
System.out.println(name + " done with projector arrangement...");
Thread.sleep(250);
System.out.println(name + " starts PPT...");
Thread.sleep(500);
System.out.println(name + " completed......");
Thread.sleep(250);
}
}
class Student implements Runnable
{
String name;
room r;
Thread t;
Student(String n,room r)
{
name = n;
t = new Thread(this, n);
this.r = r;
}
public void start()
{
t.start();
}
public void run()
{
try
{
r.givesPresentation(name);
}
catch(InterruptedException e)
{
System.out.println(name + " Error.....");
}
}
}
class SharedResource
{
public static void main(String args[])
{
room r = new room();
Student neha = new Student("Neha's Gang",r);
Student kevin = new Student("kevin",r);
Student Shree = new Student("Shree data",r);
neha.start();
kevin.start();
Shree.start();
}}
Output:

7.Bankers algorithm
Ans.
if __name__=="__main__":
num = 3
resource = 1
alloc = [[1, 0, 2 ],[ 0, 3, 1 ],[1, 0, 2 ]]
max = [[4, 1, 2 ],[1, 5, 3 ],[ 1, 2, 3 ]]
avail = [2, 2, 0]
f = [0]*num
result = [0]*num
temp = 0
for k in range(num):
f[k] = 0
need = [[ 0 for i in range(resource)]for i in range(num)]
for i in range(num):
for j in range(resource):
need[i][j] = max[i][j] - alloc[i][j]
y=0
for k in range(3):
for i in range(num):
if (f[i] == 0):
flag = 0
for j in range(resource):
if (need[i][j] > avail[j]):
flag = 1
break
if (flag == 0):
result[temp] = i
temp += 1
for y in range(resource):
avail[y] += alloc[i][y]
f[i] = 1

print("The SAFE Sequence will be:")

for i in range(num - 1):


print(" P", result[i], " ->", sep="", end="")
print(" P", result[num - 1], sep="")
output:

Implementation of page fault


Ans.
from queue import Queue

def pgFaults(pg, num, temp):

s = set()

index = Queue()

pg_faults = 0

for i in range(num):

if (len(s) < temp):


if (pg[i] not in s):

s.add(pg[i])

pg_faults += 1

index.put(pg[i])

else:

if (pg[i] not in s):

value = index.queue[0]

index.get()

s.remove(value)

s.add(pg[i])

index.put(pg[i])

pg_faults += 1

return pg_faults

if __name__ == '__main__':

pg = [6, 0, 1, 2, 4, 3, 0, 2, 6, 3, 2, 0, 1, 6]

num = len(pg)

temp = 3

print(pgFaults(pg, num, temp))

output:

You might also like