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

Exp 3

cpu scheduling

Uploaded by

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

Exp 3

cpu scheduling

Uploaded by

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

Demonstration of the following IPC mechanisms.

a. Pipes (pipe.py)
import multiprocessing
def child_process(conn):
print("Child process started.") # Debugging print
# Receive the message from the parent process
msg_from_parent = conn.recv()
print(f"Child received: {msg_from_parent}")

# Send a response back to the parent process


response = "Hello from the child process!"
conn.send(response)

def parent_process():
print("Parent process started.") # Debugging print
# Create a pipe
parent_conn, child_conn = multiprocessing.Pipe()

# Create and start the child process


process = multiprocessing.Process(target=child_process,
args=(child_conn,))
process.start()

# Send a message to the child process


message = "Hello from the parent process!"
print("Parent sending: {message}") # Debugging print
parent_conn.send(message)

# Receive a response from the child process


response = parent_conn.recv()
print(f"Parent received: {response}")
# Wait for the child process to complete
process.join()

if __name__ == "__main__":
print("Main execution started.") # Debugging print
parent_process()

output:

Main execution started.


Parent process started.
Parent sending: Hello from the parent process!
Child process started.
Child received: Hello from the parent process!
Parent received: Hello from the child process!

Shared Memory
 Creates a multiprocessing.Array to hold the shared
memory.
 Writer process Writes a message to the shared memory.
 Saves the shared memory contents to a file
(shared_memory.bin) so it can be read later.
 Reader process Reads the saved shared memory data
from the file and prints it.
import multiprocessing
def writer(shared_memory):
# Write data to the shared memory
data = b'Hello from writer process!'
# Ensure the data fits within the allocated size
shared_memory[:len(data)] = data

def reader(shared_memory):
# Read data from the shared memory
data_read = bytes(shared_memory[:4096])
print('Data read from shared memory:',
data_read.decode().strip())

if __name__ == "__main__":
# Create a shared memory object
shared_memory = multiprocessing.Array('c', 4096) #
Create a shared memory array with 4096 bytes

# Start the writer process


writer_process =
multiprocessing.Process(target=writer,
args=(shared_memory,))
writer_process.start()
writer_process.join() # Ensure writer has finished before
reading

# Start the reader process


reader_process =
multiprocessing.Process(target=reader,
args=(shared_memory,))
reader_process.start()
reader_process.join() # Ensure reader has finished before
exiting
explanation:
shared_memory[:len(data)] = data writes this data into
the shared memory array. The slice of the shared
memory is replaced with the data value.
decode()- This method converts the bytes object into a
string
strip()- This method removes any leading and trailing
whitespace characters from the string

MESSAGE PASSING:
Message passing is a method of inter-process communication
(IPC) where processes send and receive messages to each
other. In Python, one of the common ways to implement
message passing between processes is by using the
multiprocessing.Queue class from the multiprocessing module.

import multiprocessing
import time

def sender(queue):
messages = [
"Message 1 from sender",
"Message 2 from sender",
"Message 3 from sender"
]
for msg in messages:
queue.put(msg) # Put each message onto the queue
print(f"Sender: Sent '{msg}'")
time.sleep(1) # Simulate some delay between messages

def receiver(queue):
while True:
msg = queue.get() # Get the next message from the
queue
if msg == "END": # End message signal to break the loop
break
print(f"Receiver: Received '{msg}'")

if __name__ == "__main__":
queue = multiprocessing.Queue()

# Create sender and receiver processes


sender_process = multiprocessing.Process(target=sender,
args=(queue,))
receiver_process = multiprocessing.Process(target=receiver,
args=(queue,))

# Start both processes


sender_process.start()
receiver_process.start()

# Wait for the sender to finish


sender_process.join()
# Send a special "END" message to signal the receiver to
stop
queue.put("END")

# Wait for the receiver to finish


receiver_process.join()
OUTPUT
Sender: Sent 'Message 1 from sender'
Receiver: Received 'Message 1 from sender'
Sender: Sent 'Message 2 from sender'
Receiver: Received 'Message 2 from sender'
Sender: Sent 'Message 3 from sender'
Receiver: Received 'Message 3 from sender'

Multiple Messages in Sender:


 The sender function now has a list of messages
(messages). It iterates over this list and sends each
message to the queue using queue.put(msg).
Receiving Messages in a Loop:
 The receiver function runs in an infinite loop (while True).
It retrieves each message from the queue using
queue.get() and prints it.
 The loop breaks when it receives a special "END"
message, which signals that no more messages will be
sent.
Sending an "END" Message:
 After the sender_process finishes sending all messages,
the main process sends an "END" message to the queue.
This is used by the receiver to know when to stop.
Delays Between Messages:
 The time.sleep(1) in the sender function simulates a delay
between sending each message. This makes it easier to
observe the message passing.

You might also like