Os Record BSC Program Only
Os Record BSC Program Only
Program Code:
from multiprocessing import Process
import os
def info(title):
print(title)
print('modue name:',__name__)
print('parent process:',os.getppid())
print('process id:',os.getpid())
def f(name):
info('function f')
print('hello',name)
if __name__=='__main__':
info('main line')
p=Process(target=f,args=('bob',))
p.start()
p.join()
Program Code:
file1=open("myfile.txt","w")
file1.write("Hello \n")
file1.writelines(L)
file1.close()
file1=open("myfile.txt","r+")
print(file1.read())
print()
file1.seek(0)
print(file1.readline())
print()
file1.seek(0)
print(file1.read(9))
print()
file1.seek(0)
print(file1.readline(9))
print()
file1.seek(0)
print(file1.readlines())
print()
file1.close()
Output:
Output of Read function is
Hello
This is Delhi
This is Paris
This is London
Hello
Hello
Th
Hello
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
2. SHORTEST JOB FIRST SCHEDULING ALGORITHM
Program Code:
n=int(input("enter no of processes:"))
bt=[0]*(n+1)
at=[0]*(n+1)
abt=[0]*(n+1)
for i in range(n):
bt[i]=[abt[i],at[i],i]
bt.pop(-1)
print(abt)
print(bt)
sumbt=0
i=0
ll=[]
for i in range(0,sum(abt)):
l.sort(key=lambda x:x[0])
bt[bt.index(l[0])][0]-=1
for k in bt:
if k[0]==0:
t=bt.pop(bt.index(k))
ll.append([k,i+1])
ct=[0]*(n+1)
tat=[0]*(n+1)
wt=[0]*(n+1)
for i in ll:
ct[i[0][2]]=i[1]
for i in range(len(ct)):
tat[i]=ct[i]-at[i]
wt[i]=tat[i]-abt[i]
ct.pop(-1)
wt.pop(-1)
tat.pop(-1)
abt.pop(-1)
at.pop(-1)
print('PNO\tBT\tAT\tCT\tTAT\tWT')
for i in range(len(ct)):
print("{}\t{}\t{}\t{}\t{}\t{}\n".format(i+1,abt[i],at[i],ct[i],tat[i],wt[i]))
[8, 4, 9, 5, 0]
PNO BT AT CT TAT WT
1 8 0 17 17 9
2 4 1 5 4 0
3 9 2 26 24 15
4 5 3 10 7 2
Program Code:
def findWaitingTime(processes,n,bt,wt):
wt[0]=0
for i in range(1,n):
wt[i]=bt[i-1]+wt[i-1]
def findTurnAroundTime(processes,n,bt,wt,tat):
for i in range(n):
tat[i]=bt[i]+wt[i]
def findavgTime(processes,n,bt):
wt=[0]*n
tat=[0]*n
total_wt=0
total_tat=0
findWaitingTime(processes,n,bt,wt)
findTurnAroundTime(processes,n,bt,wt,tat)
print("Processes Burst Time "+"Waiting Time "+"Turn Around Time")
for i in range(n):
total_wt=total_wt+wt[i]
total_tat=total_tat+tat[i]
print(" "+str(i+1)+"\t\t"+str(bt[i])+"\t\t"+str(wt[i])+"\t\t"+str(tat[i]))
print("Average waiting time ="+str(total_wt/n))
print("Average Turn around time ="+str(total_tat/n))
if __name__=="__main__":
processes=[1,2,3]
n=len(processes)
burst_time=[24,3,3]
findavgTime(processes,n,burst_time)
Output:
Processes Burst Time Waiting Time Turn Around Time
1 24 0 24
2 3 24 27
3 3 27 30
Program Code:
def findWaitingTime(processes,n,bt,wt,quantum):
rem_bt=[0]*n
for i in range(n):
rem_bt[i]=bt[i]
t=0
while(1):
done=True
for i in range(n):
if(rem_bt[i]>0):
done=False
if(rem_bt[i]>quantum):
t+=quantum
rem_bt[i]-=quantum
else:
t=t+rem_bt[i]
wt[i]=t-bt[i]
rem_bt[i]=0
if (done==True):
break
def findTurnAroundTime(processes,n,bt,wt,tat):
for i in range(n):
tat[i]=bt[i]+wt[i]
def findavgTime(processes,n,bt,quantum):
wt=[0]*n
tat=[0]*n
total_wt=0
total_tat=0
findWaitingTime(processes,n,bt,wt,quantum)
findTurnAroundTime(processes,n,bt,wt,tat)
for i in range(n):
total_wt=total_wt+wt[i]
total_tat=total_tat+tat[i]
print(" "+str(i+1)+"\t\t"+str(bt[i])+"\t\t"+str(wt[i])+"\t\t"+str(tat[i]))
if __name__=="__main__":
processes=[1,2,3]
n=len(processes)
burst_time=[24,3,3]
quantum=4
findavgTime(processes,n,burst_time,quantum)
Output:
Processes Burst Time Waiting Time Turn Around Time
1 24 6 30
2 3 4 7
3 3 7 10
Program Code:
def findWaitingTime(processes,n,wt):
wt[0]=0
for i in range(1,n):
wt[i]=processes[i-1][1]+wt[i-1]
def findTurnAroundTime(processes,n,wt,tat):
for i in range(n):
tat[i]=processes[i][1]+wt[i]
def findavgTime(processes,n):
wt=[0]*n
tat=[0]*n
total_wt=0
total_tat=0
findWaitingTime(processes,n,wt)
findTurnAroundTime(processes,n,wt,tat)
for i in range(n):
total_wt=total_wt+wt[i]
total_tat=total_tat+tat[i]
print(" ",processes[i][0],"\t\t",processes[i][1],"\t\t",wt[i],"\t\t",tat[i])
def priorityScheduling(proc,n):
proc=sorted(proc,key=lambda proc:proc[2],reverse=True)
print(i[0],end="")
findavgTime(proc,n)
if __name__=="__main__":
proc=[[1,10,1],[2,5,0],[3,8,1]]
n=3
priorityScheduling(proc,n)
Output:
Priority Scheduling Algorithm
132
1 10 0 10
3 8 10 18
2 5 18 23
Program Code:
import threading
import time
class ReaderWriterProblem():
def __init__(self):
self.mutex=threading.Semaphore()
self.wrt=threading.Semaphore()
self.r_c=0
def reader(self):
while True:
self.mutex.acquire()
self.r_c+=1
if self.r_c==1:
self.wrt.acquire()
self.mutex.release()
self.mutex.acquire()
self.r_c-=1
if self.r_c==0:
self.wrt.release()
self.mutex.release()
time.sleep(3)
def writer(self):
while True:
self.wrt.acquire()
print("write data....")
print("-"*20)
self.wrt.release()
time.sleep(3)
def main(self):
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=ReaderWriterProblem()
c.main()
Output:
Reader 1 is reading
Reader 2 is reading
Reader 3 is reading
Reader 4 is reading
>>>
write data....
--------------------
write data....
--------------------
Reader 1 is reading
Reader 2 is reading
Reader 3 is reading
Reader 4 is reading
write data....
--------------------
write data....
--------------------
Reader 1 is reading
Reader 1 is reading
Reader 2 is reading
Reader 3 is reading
write data....
--------------------
write data....
--------------------
6. BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE
Program Code:
P=0
R=0
def calculateNeed(need,maxm,allot):
for i in range(P):
for j in range(R):
need[i][j]=max[i][j]-allot[i][j]
def isSafe(processes,avail,maxm,allot):
need=[]
for i in range(P):
l=[]
for j in range(R):
l.append(0)
need.append(l)
calculateNeed(need,maxm,allot)
finish=[0]*P
safeSeq=[0]*P
work=[0]*R
for i in range(R):
work[i]=avail[i]
count=0
while(count<P):
found=False
for p in range(P):
if(finish[p]==0):
for j in range(R):
if(need[p][j]>work[j]):
break
if(j==R-1):
for k in range(R):
work[k]+=allot[p][k]
safeSeq[count]=p
count+=1
finish[p]=1
found=True
if(found==False):
return False
print(*safeSeq)
return True
if __name__=="__main__":
processes=[0,1,2,3,4]
avail=[3,3,2]
maxm=[[7,5,3],[3,2,2],[9,0,2],[2,2,2],[4,3,3]]
allot=[[0,1,0],[2,0,0],[3,0,2],[2,1,1],[0,0,2]]
isSafe(processes,avail,maxm,allot)
Output:
System is in safe state.
Program Code:
from queue import Queue
def pageFaults(pages,n,capacity):
s=set()
indexes=Queue()
page_faults=0
for i in range(n):
if(len(s)<capacity):
if(pages[i] not in s):
s.add(pages[i])
page_faults+=1
indexes.put(pages[i])
else:
if(pages[i] not in s):
val=indexes.queue[0]
indexes.get()
s.remove(val)
s.add(pages[i])
indexes.put(pages[i])
page_faults+=1
print(s,end="")
print("Page Fault count",page_faults)
return page_faults
if __name__=="__main__":
pages=[1,2,3,1,2,3,1,2,3]
n=len(pages)
capacity=3
print("total page fault count",pageFaults(pages,n,capacity))
Output:
{1}Page Fault count 1
{1, 2}Page Fault count 2
{1, 2, 3}Page Fault count 3
{1, 2, 3}Page Fault count 3
{1, 2, 3}Page Fault count 3
{1, 2, 3}Page Fault count 3
{1, 2, 3}Page Fault count 3
{1, 2, 3}Page Fault count 3
{1, 2, 3}Page Fault count 3
total page fault count 3
8. LEAST RECENTLY USED ALGORITHM
Program Code:
capacity=3
processList=[0,1,2,0,3,0,4,2,3,0,3,2]
s=[]
pageFaults=0
for i in processList:
if i not in s:
if(len(s)==capacity):
s.remove(s[0])
s.append(i)
else:
s.append(i)
pageFaults+=1
else:
s.remove(i)
s.append(i)
print(s,end="")
print("Page Fault count",pageFaults)
print("Total page Faults",pageFaults)
Output:
[0]Page Fault count 1
[0, 1]Page Fault count 2
[0, 1, 2]Page Fault count 3
[1, 2, 0]Page Fault count 3
[2, 0, 3]Page Fault count 4
[2, 3, 0]Page Fault count 4
[3, 0, 4]Page Fault count 5
[0, 4, 2]Page Fault count 6
[4, 2, 3]Page Fault count 7
[2, 3, 0]Page Fault count 8
[2, 0, 3]Page Fault count 8
[0, 3, 2]Page Fault count 8
Total page Faults 8
9(a). FIRST FIT ALGORITHM FOR MEMORY MANAGEMENT
Program Code:
def FirstFit(blocksize, blocks, processSize, processes):
allocation=[-1]*processes
occupied=[False]*blocks
for i in range(processes):
for j in range(blocks):
if not occupied[j] and (blockSize[j]>=processSize[i]):
allocation[i]=j
occupied[j]=True
break
print("Process No. \t Process Size \t\t Block No.")
for i in range(processes):
print(i+1,"\t\t ",processSize[i],"\t\t\t",end=" ")
if allocation[i]!=-1:
print(allocation[i]+1)
else:
print("Not Allocated")
if __name__=="__main__":
blockSize=[100,50,30,120,35]
processSize=[20,60,70,40]
m=len(blockSize)
n=len(processSize)
print("\t\t\tBest Fit Algorithm")
FirstFit(blockSize,m,processSize,n)
Output:
Best Fit Algorithm
Process No. Process Size Block No.
1 20 1
2 60 4
3 70 Not Allocated
4 40 2
9(b). BEST FIT ALGORITHM FOR MEMORY MANAGEMENT
Program Code:
def bestFit(blocksize, m, processSize, n):
allocation=[-1]*n
for i in range(n):
bestIdx=-1
for j in range(m):
if blockSize[j]>=processSize[i]:
if bestIdx==-1:
bestIdx=j
elif blockSize[bestIdx]>blockSize[j]:
bestIdx=j
if bestIdx!=-1:
allocation[i]=bestIdx
blockSize[bestIdx]-=processSize[i]
print("Process No. \t Process Size \t\t Block No.")
for i in range(n):
print(i+1,"\t\t ",processSize[i],"\t\t\t",end=" ")
if allocation[i]!=-1:
print(allocation[i]+1)
else:
print("Not Allocated")
if __name__=="__main__":
blockSize=[100,500,200,300,600]
processSize=[212,417,112,426]
m=len(blockSize)
n=len(processSize)
print("\t\t\tBest Fit Algorithm")
bestFit(blockSize,m,processSize,n)
Output:
Best Fit Algorithm
Process No. Process Size Block No.
1 212 4
2 417 2
3 112 3
4 426 5
9(c). WORST FIT ALGORITHM FOR MEMORY MANAGEMENT
Program Code:
def worstFit(blocksize, m, processSize, n):
allocation=[-1]*n
for i in range(n):
wstIdx=-1
for j in range(m):
if blockSize[j]>=processSize[i]:
if wstIdx==-1:
wstIdx=j
elif blockSize[wstIdx]<blockSize[j]:
wstIdx=j
if wstIdx!=-1:
allocation[i]=wstIdx
blockSize[wstIdx]-=processSize[i]
print("Process No. \t Process Size \t\t Block No.")
for i in range(n):
print(i+1,"\t\t ",processSize[i],"\t\t\t",end=" ")
if allocation[i]!=-1:
print(allocation[i]+1)
else:
print("Not Allocated")
if __name__=="__main__":
blockSize=[100,500,200,300,600]
processSize=[212,417,112,426]
m=len(blockSize)
n=len(processSize)
print("\t\t\tworst Fit Algorithm")
worstFit(blockSize,m,processSize,n)
Output:
worst Fit Algorithm
Process No. Process Size Block No.
1 212 5
2 417 2
3 112 5
4 426 Not Allocated
10. PROGRAM FOR INTER PROCESS COMMUNICATION
Program Code:
from multiprocessing import Process, Value, Array
def f(n,a):
n.value=3.1415297
for i in range(len(a)):
a[i]= -a[i]
if __name__=="__main__":
num=Value('d',0.0)
arr=Array('i',range(10))
arr1=Array('i',range(1,20,2))
print("\t\tIPC using Shared Memory")
p1=Process(target=f, args=(num, arr))
p1.start()
p1.join()
p2=Process(target=f, args=(num, arr1))
p2.start()
p2.join()
print(num.value)
print(arr[:])
print(arr1[:])
Output:
IPC using Shared Memory
3.1415297
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
[-1, -3, -5, -7, -9, -11, -13, -15, -17, -19]