Os Practical
Os Practical
D053
40315200041
Ans.
#Wt=waiting time
#Avgtime=average time
wt[0] = 0
for i in range(num):
wt = [0] * num
total_wt = 0
total_tat = 0
for i in range(num):
str(tat[i]))
if __name__ =="__main__":
interpret = [ 1, 2, 3, 4, 5]
num = len(interpret)
output:
2.To implement sfj cpu scduling program
Ans.
#avg=average
#bt=burst time
#wt=watch time
#pro=processes
max_processes = 12
pro = []
bt = []
wt = [0]*max_processes
tat = [0]*max_processes
avg_tat = 0
avg_wt = 0
for i in range(num):
for i in range(num):
for j in range(num-i-1):
if bt[j]>bt[j+1]:
for i in range(num):
wt[i] = 0
tat[i] = 0
for j in range(i):
print()
for i in range(num):
print()
output:
3.To implement disc scheduling algorithm
size = 7;
seek_count = 0;
distance, current_track = 0, 0;
for i in range(size):
currrent_track = array[i];
seek_count += distance;
head = current_track;
for i in range(size):
print(array[i]);
if __name__ == '__main__':
FCFS(array, head);
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
s = set()
index = Queue()
pg_faults = 0
for i in range(num):
s.add(pg[i])
pg_faults += 1
index.put(pg[i])
else:
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
output: