0% found this document useful (0 votes)
20 views11 pages

1 To 2 Practical OS

The document outlines various programming tasks involving threading in Python, including a producer-consumer problem using shared memory and message passing. It also describes single-threaded and multi-threaded implementations for executing tasks and generating Fibonacci sequences. Each section includes code snippets demonstrating the implementation of these concepts.

Uploaded by

Mihir Raut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

1 To 2 Practical OS

The document outlines various programming tasks involving threading in Python, including a producer-consumer problem using shared memory and message passing. It also describes single-threaded and multi-threaded implementations for executing tasks and generating Fibonacci sequences. Each section includes code snippets demonstrating the implementation of these concepts.

Uploaded by

Mihir Raut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Pract IA

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

def producey (queue)


fo i in Tange (G):
item =pn
Ttenmi+13
queue. put (item)
"
print (f" .Producer Poduced item")
time sleep (i)
def consumer (queue)
While True
item = queue. gety
item is Mone:
breqk
print (Consumer Consumed item3 )
timey sleep (z)
--na me == mcçnI;
queue = Queue c)

producer-Proces -frocessCtarget = producer ,


args = (queue ))
Consumer_Process Pro cess (target = conssmer,
ags Cqueue))
ProduceY process, Start O
consume p0ces. Start O
Pro d4cer proces join ()
uee-put (None)
Consume-proces. Jo in O
Aredue
ey cesumeel item
CorsUme Prootuded iter 2
Predueey consumed Hem 1

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

Aef taek (name, delay):


print (F" Task' }name 3'
time sleep (delay) started )
prìnt (P" Task' name ?'
completed)
def
maun0:
print ( main proqram starte")
task (" Task I ",2)
task ("Task2 )
+48k Task3",3)
Print (" tnaun
program completed ")
1fname
maun
Mein pegya alled
Tast elavled
Task
Task Tk Campleled
Task Task 2' lar ted
Task" Tk 2 Compleled
Task Task
Task " Task 31
comçleled
Main progam compleled
Cate

Write a proqram fo 00Tk with muli thredds.


threading
iperttlme

task (name, delay):


ozint (F lask >ng me 3'starhel )
Hime. sleep (delay)
int (f" Task 2name y' completed")
def maun (0:
pint ("Maùn program Started ")
print
1=
t1 threading Threal taget =task, args =(" Task )!2)
te - threading- Thread (target task, gs -( Jask 2".1))
t3 threading Thread (tcrget =task, args- (" Task 33))
t tiStart()
ti Stdrt )
t3 .start J

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

Moin threao eviting

You might also like