[go: up one dir, main page]

0% found this document useful (0 votes)
13 views6 pages

Computer Network Lab

The document outlines an experiment focused on Inter Process Communication (IPC) using message queues to facilitate communication between two processes, a sender and a receiver. It details the objectives, tasks, program flow, and provides code snippets for implementing the IPC using a Python script. The experiment demonstrates message passing, retrieval, and synchronization between processes.
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)
13 views6 pages

Computer Network Lab

The document outlines an experiment focused on Inter Process Communication (IPC) using message queues to facilitate communication between two processes, a sender and a receiver. It details the objectives, tasks, program flow, and provides code snippets for implementing the IPC using a Python script. The experiment demonstrates message passing, retrieval, and synchronization between processes.
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/ 6

Experiment No – 2

Name of Experiment – Inter Process Communication


(IPC)

Objective
Implement IPC using message queues to enable
communication between two processes.

Tasks:-
• Create two processes - sender and receiver.
• Implement message queues for communication
between them.
• Demonstrate message passing and retrieval with
appropriate synchronization.
• Display messages sent and received.

, Madhushri
Srijoy Tarafdar ECE2023L10
mitra, ECE2022089, DeptRCCIIT
Dept of ECE, of ECE(B) RCCIIT
Methods of IPC

File

bidirectional

Shared memory
[Message queue]
Process -1 Process - 2

bidirectional

Message
pipe
One way communication

, Madhushri
Srijoy Tarafdar ECE2023L10
mitra, ECE2022089, DeptRCCIIT
Dept of ECE, of ECE(B) RCCIIT
Program Flow

Import Modules Create Methods


write_to
read_from
write_read
Create container
read_write
object

Create process
objects

Synchronize
Run processes
processes

End

, Madhushri
Srijoy Tarafdar ECE2023L10
mitra, ECE2022089, DeptRCCIIT
Dept of ECE, of ECE(B) RCCIIT
2_IPC.py
import netpack as npk
import IPC_methods as ipc

# Create Queue object


Q=npk.create_queue()

# Create list object


val_list=list(range(5))

flag=1

# -------------------------------
# SET-1
# -------------------------------
if flag==1:
P1=npk.create_process(ipc.write_to, [Q,val_list])
P2=npk.create_process(ipc.read_from, [Q])

npk.start_process(P1)
npk.start_process(P2)

npk.join_process(P1)
npk.join_process(P2)

# To check the process synchronization using join_process():


print("Main ends")

# ------------------------------
# SET-2
# ------------------------------
elif flag==2:
P3=npk.create_process(ipc.write_read, [Q,val_list])
P4=npk.create_process(ipc.read_write, [Q])

, Madhushri
Srijoy Tarafdar ECE2023L10
mitra, ECE2022089, DeptRCCIIT
Dept of ECE, of ECE(B) RCCIIT
npk.start_process(P3)
npk.start_process(P4)

npk.join_process(P3)
npk.join_process(P4)

# To check the process synchronization using join_process():


print("Main ends")

IPC_methods.py

import netpack as npk


import time

def write_to(container, arr):


print("P1 has been started")
for data in arr:
container=npk.write_queue(container, data)
print("Printing from P1 & write Data = {}".format(data))

def read_from(container):
print("P2 has been started")
time.sleep(1)
while True:
data = npk.read_queue(container)
if data is not None:
print('Printing from P2 & read Data = {}'.format(data))
else:
break

def write_read(container, arr):


print("P3 has been started")
for data in arr:
container=npk.write_queue(container, data)
print("Printing from P3 & write Data = {}".format(data))
print('[=] Reading in P3 & data is {}'.format(container.get()))
, Madhushri
Srijoy Tarafdar ECE2023L10
mitra, ECE2022089, DeptRCCIIT
Dept of ECE, of ECE(B) RCCIIT
def read_write(container):
print("P4 has been started")
time.sleep(1)
while True:
data = npk.read_queue(container)
if data is not None:
print('Printing from P4 & read Data = {}'.format(data))
else:
break

container = npk.write_queue(container, 1000)


data = npk.read_queue(container)
if data is not None:
print('[=] Writing in P4 & the data is {}'.format(data))

output:-
P1 has been started
P2 has been started
Printing from P1 & write Data = 0
Printing from P1 & write Data = 1
Printing from P1 & write Data = 2
Printing from P1 & write Data = 3
Printing from P1 & write Data = 4
Printing from P2 & read Data = 0
Printing from P2 & read Data = 1
Printing from P2 & read Data = 2
Printing from P2 & read Data = 3
Printing from P2 & read Data = 4
Main ends
P3 has been started
P4 has been started
Printing from P3 & write Data = 0
Printing from P3 & write Data = 1
Printing from P3 & write Data = 2
Printing from P3 & write Data = 3
Printing from P3 & write Data = 4
[=] Reading in P3 & data is 0
Printing from P4 & read Data = 1
Printing from P4 & read Data = 2
Printing from P4 & read Data = 3
Printing from P4 & read Data = 4
[=] Writing in P4 & the data is 1000
Main ends

, Madhushri
Srijoy Tarafdar ECE2023L10
mitra, ECE2022089, DeptRCCIIT
Dept of ECE, of ECE(B) RCCIIT

You might also like