Python programming! Please help! Please include threads  One master thread aggregating and sum the result of  n slave-threads that each process  sums a different range of values in a given array of 1000 random integers. Each thread will receive an segment of array of numbers, it will fork/spawn two new slaves each perform summing on different half of the received array segment, and the parent aggregate and return the total from its two slaves to its parent thread. Start with 7 nodes thread tree, when you are comfortable, you can extend it to a full thread tree. Given code:  import multiprocessing  import array as arr def find_sum(a,n1,n2,q):      s=0     for i in range(n1-1,n2):         s=s+a[i]     q.put(s)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Python programming! Please help! Please include threads 

One master thread aggregating and sum the result of  n slave-threads that each process  sums a different range of values in a given array of 1000 random integers. Each thread will receive an segment of array of numbers, it will fork/spawn two new slaves each perform summing on different half of the received array segment, and the parent aggregate and return the total from its two slaves to its parent thread. Start with 7 nodes thread tree, when you are comfortable, you can extend it to a full thread tree.

Given code: 

import multiprocessing 
import array as arr
def find_sum(a,n1,n2,q): 
    s=0
    for i in range(n1-1,n2):
        s=s+a[i]
    q.put(s)
  
if __name__ == "__main__": 
    a=arr.array('i',[0,1,2,3,4,5,6,7,8,9])
    q = multiprocessing.Queue()
    # creating processes 
    p1 = multiprocessing.Process(target=find_sum, args=(a,1,4,q )) 
    p2 = multiprocessing.Process(target=find_sum, args=(a,5,7,q)) 
    p3 = multiprocessing.Process(target=find_sum, args=(a,8,10,q)) 
    sumarray=0
    # starting process 1 
    p1.start() 
    p1.join()
    # starting process 2 
    p2.start() 
    p2.join()
    # starting process 3
    p3.start()
    p3.join()
    for i in range(0,3):
        sumarray+=q.get()
    print("Array sum: ",sumarray)

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY