1 To 2 Practical OS
1 To 2 Practical OS
Aim:- Wite
Consumer
program io give a ocluion to the producer
problem ucing shdred memorY.
impert threding
import queue
Sq = queue Queue (J
Class
Producey (threadinq. Thread) :
def un (sejp):
for i in range (9)
Item f" jtem 3"
SyPat (item)
print (f procd uced item3")
Sq put ( None)
class Consumer (thyegdinq Thredd):
def un (self):
While True :
item z S9etl)
if item isNone :
beak
pint (p Consu medfitem")
P= Produce()
C=ConSumerc)
p Stat ()
Ci start )
P yoin
cansumecTt
J4em
edeced
ConsUm ed
Tlcm
2 consumed
Tterm
edecd
3 Consumecl
Isodtced Ttem
educcd Tlem 44 consume
SAer4
froct 1B
Page No.
Date
-Aim: Wite
program to gve a soluion to the prod9
cer -
Consune?problem Using mess age passing
frem iprocessing impovt
import tume Pro cess, dueue
produces i4em
predueey produred item 4
podteey corSUmed tem
corsumey
CTeduted item
corsumed iHem 4
Cettsumey
Hern
Censune copef med
Date
Air - Write
proqam to work
cth single
thread
Pracical 2A;
import Hme
t on(
te- doiny
tojoiny
Print ("Main program completed ")
F-ndme= nmaun_'":
cdpul 2.8;
eterted
Main progam
Task Tas ki' starto Task Task 2 tart ed Tosk
Task 3'
Task ompleted
Task Task l completed
Task Task 3' compleed
NMan pogrsm campleted
fractic al: 2c
Aim: he Fibonacci
O, , , 2, 3,S,8, Sequence ie the seies of numbe
Cibo = Formally, it can be e pressed 46:
Myeaded O,hbl=,ibn - f b-1 +fib -2,
write a musi
rogTam that generoles yhe
Code :
fioonacci sequen
im pozt +hreading
lass Fibonacc Thread
def
init--(self, (threading
n): Thread):
threading
Ser f n =n
Thread --init (selF)
def mun CselF):
fib-sequene
print CF"
= selF -gen erte
fibonacci (9elf,n)
ibonacci' sequence for Sself. n3, Pib-S
Auence ?ln)
def
9eperae-Fibonacci
fib-sequence = Lo,3
(sefP;
for I in range
C2n):
Fib- sequence .append
+
Fib-Sequence Çi-21) (Ab-sequence Ci
Yeturn fib- sequence
--ndme-= main!
h=)S
exlng") adthre ("Mun print
JoinU Ahyead2
()join |. cad 4hre
Start
0 2.adHhre
fibonacci(nT) bread Start
0 4hread
1.
(n) thrcad2
cd
Thrfibonacc
e
thread1
Practiceal :2C output
Fibonceei sequence for lo :LO,!, !,23, S,8, 19,2|, 34)
Sequence foy S: [o,,,,3,5) 8,19,21,34,te
fibonacci
44,239,377J