Skip to content

Instantly share code, notes, and snippets.

@ShamaUgale
Created January 7, 2021 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShamaUgale/2dd43bc280cc4914fafc17cc943c9b7e to your computer and use it in GitHub Desktop.
Save ShamaUgale/2dd43bc280cc4914fafc17cc943c9b7e to your computer and use it in GitHub Desktop.
Make a python program that creates 1 child process which pushes random integers until the parent terminates to the parent process every 1 second for N seconds, not known until after the child process is created, have the parent read and print the integers, then have the program terminate correctly.
import os
import random
import multiprocessing
def createChildProcess(sec):
q = multiprocessing.Queue()
n = os.fork()
for i in range(1, sec):
if n==0:
randomInt =random.randint(1, 99999)
print("data: " , randomInt)
q.put(randomInt)
print("Child id : ", os.getpid())
else:
print("getting data in parent process : ", q.get())
createChildProcess(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment