Question: Interprocess Communication and Thread Coding: I have finished the coding part of the assignment. The .py file as well as the output are attached: Questions:

Interprocess Communication and Thread Coding: I have finished the coding part of the assignment. The .py file as well as the output are attached:

Interprocess Communication and Thread Coding: I have finished the coding part ofthe assignment. The .py file as well as the output are attached:Questions: 1. Assuming your program properly executed to completion, why were theresome instances of pauses of several seconds between output messages? 2. Given

Questions:

1. Assuming your program properly executed to completion, why were there some instances of pauses of several seconds between output messages?

2. Given that each of the five threads were spawned within the space of a second, or two, why did they reach completion at considerably (within the scope of the exercise) different times?

Please answer both questions thoroughly. I'm especially confused about the first question. In other words, when I ran the command python3 threads_student.py in the terminal, it took 0 second for the first 5 threads to finish: Thread1-0, Delay: 0 seconds. Thread2-0, Delay: 0 seconds. Thread3-0, Delay: 0 seconds. Thread4-0, Delay: 0 seconds. Thread5-0, Delay: 0 seconds. Then I wait for about 4 seconds before the next few threads show up on the terminal: Thread2-1, Delay: 4 seconds. Thread4-1, Delay: 5 seconds, etc. And this happen several times before the program executed to completion. What's the reason for the pauses?

For the second question, I'm mainly confused about what determines the order each thread reaches completion. For example, in this run, Thread 5 reaches completion first, as Thread5-9 was executed very early. Thread 2 reaches completion the last, as Thread2-9 was the last thread to be executed.

4 # Example of a process spawning multiple threads 5 # Also illustrates Asynchronous communication 7 import threading 8 import datetime 9 import time 10 import random 12 number of iterations10 13 max-delay- 10 14 15 def thread1(): 16 17 18 19 20 21 delay 0 for i in range (number_of iterations): timeR datetime.datetime.now() print Thread1-' + str(i) + '' Delay: delay = random . randrange (max-delay) time.sleep(delay) + str(delay) + seconds. Time : + str(tineR)) 23 24 25 def thread2): 26 27 28 29 30 31 32 delay = 0 for i in range(number_of iterations): timeR datetime.datetime.now() print( Thread2-' + str() + '' Delay: delay - random.randrange (max_delay) time.sleep(delay) + str(delay) + seconds. Time: + str(tineR)) 34 35 def thread3): 36 37 38 39 40 41 42 43 delay 0 for i in range (numberof iterations): timeR datetime.datetime.now() print('Thread3- str(i)+ ', Delay: '+ str(delay) seconds. Time: '+ str(timeR)) delay - random.randrange (max_delay) time.sleep (delay) 45 def thread4(): 47 48 49 50 51 52 53 54 55 def thread5): 56 57 58 59 60 61 62 63 64 65 66 def main): 67 68 69 70 71 72 73 74 75 76 delay for i in range(number_of_iterations): timeRdatetime.datetime.now() print('Thread4-'+ str(i)+ ', Delay: 'str(delay)seconds. Time:'str(timeR)) delay - random.randrange (max_delay) time.sleep(delay) delay = 0 for i in range(number_of_iterations): timeRdatetime.datetime.now() print('Thread5-'+ str(i)+ ', Delay: 'str(delay)seconds. Time:'str(timeR)) delay - random.randrange (max_delay) time.sleep(delay) start-time datetime.datetime . now()| print('BEGIN:' + str(start time)) t1 - threading.Thread(target -thread1) t2 - threading. Thread(target- thread2) t3 - threading. Thread(target- thread3) t4 - threading.Thread(target thread4) t5 - threading.Thread(target thread5) t1.start) t2.start() t3.start() t4.start() t5.start() 78 79 80 81 82 83 84 85 86 87 #In Python's threading package, the join() operator is used to close a thread #(By [re-]joining the thread with it's parent process upon completion) t1.join() t2.join() t3.join() t4.join() t5.join) BEGIN: 2019-02-03 16:44:00.453504 Thread1-0, Delay: 0 seconds. Time: 2019-02-03 16:44:00.453910 hread2-0, Delay: 0 seconds. Time: 2019-02-03 16:44:00.454076 Thread3-0, Delay: 0 seconds. Time: 2019-02-03 16:44:00.454235 Thread4-0, Delay: 0 seconds. Time: 2019-02-03 16:44:00.454401 Thread5-0, Delay: 0 seconds. Time: 2019-02-03 16:44:00.454572 Thread2-1, Delay: 4 seconds. Time: 2019-02-03 16:44:04.459673 Thread4-1, Delay: 5 seconds. Time: 2019-02-03 16:44:05.459621 Thread5-1, Delay: 5 seconds. Time: 2019-02-03 16:44:05.459777 Thread1-1, Delay: 5 seconds. Time: 2019-02-03 16:44:05.459839 Thread5-2, Delay: 0 seconds. Time: 2019-02-03 16:44:05.459955 Thread3-1, Delay: 8 seconds. Time: 2019-02-03 16:44:08.454959 Thread5-3, Delay: 4 seconds. Time: 2019-02-03 16:44:09.461116 Thread5-4, Delay: 0 seconds. Time: 2019-02-03 16:44:09.461248 Thread1-2, Delay: 5 seconds. Time: 2019-02-03 16:44:10.460327 Thread3-2, Delay: 3 seconds. Time: 2019-02-03 16:44:11.460593 Thread4-2, Delay: 6 seconds. Time: 2019-02-03 16:44:11.460749 Thread2-2, Delay: 9 seconds. Time: 2019-02-03 16:44:13.463123 Thread5-5, Delay: 4 seconds. Time: 2019-02-03 16:44:13.463285 Thread1-3, Delay: 5 seconds. Time: 2019-02-03 16:44:15.465930 Thread2-3, Delay: 3 seconds. Time: 2019-02-03 16:44:16.463437 Thread5-6, Delay: 3 seconds. Time: 2019-02-03 16:44:16.463571 Thread4-3, Delay: 8 seconds. Time: 2019-02-03 16:44:19.462364 Thread3-3, Delay: 9 seconds. Time: 2019-02-03 16:44:20.461601 Thread3-4, Delay: 1 seconds. Time: 2019-02-03 16:44:21.462219 Thread5-7, Delay: 5 seconds. Time: 2019-02-03 16:44:21.463909 Thread1-4, Delay: 6 seconds. Time: 2019-02-03 16:44:21.469161 Thread4-4, Delay: 4 seconds. Time: 2019-02-03 16:44:23.466685 Thread1-5, Delay: 2 seconds. Time: 2019-02-03 16:44:23.474510 Thread2-4, Delay: 9 seconds. Time: 2019-02-03 16:44:25.465961 Thread5-8, Delay: 4 seconds. Time: 2019-02-03 16:44:25.466063 Thread5-9, Delay: 0 seconds. Time: 2019-02-03 16:44:25.466241 Thread1-6, Delay: 2 seconds. Time: 2019-02-03 16:44:25.479907 Thread3-5, Delay: 6 seconds. Time: 2019-02-03 16:44:27.462679 Thread1-7, Delay: 2 seconds. Time: 2019-02-03 16:44:27.483476 Thread2-5, Delay: 6 seconds. Time: 2019-02-03 16:44:31.469958 Thread4-5, Delay: 9 seconds. Time: 2019-02-03 16:44:32.472025 Thread4-6, Delay: 0 seconds. Ti Thread4 Thread3-6, Delay: 6 seconds Thread4-8, Delay: 1 seconds. Time: 2019-02-03 16:44:33.477624 Thread2-6, Delay: 3 seconds. Time: 2019-02-03 16:44:34.475384 Thread4-9, Delay: 1 seconds. Time: 2019-02-03 16:44:34.482998 Thread1-8, Delay: 7 seconds. Time: 2019-02-03 16:44:34.489029 Thread2-7, Delay: 3 seconds. Time: 2019-02-03 16:44:37.480997 Thread1-9, Delay: 6 seconds. Time: 2019-02-03 16:44:40.489453 Thread3-7, Delay: 8 seconds. Time: 2019-02-03 16:44:41.463923 Thread3-8, Delay: 0 seconds. Time: 2019-02-03 16:44:41.464022 Thread3-9, Delay: 0 seconds. Time: 2019-02-03 16:44:41.464056 me: 2019-02-03 16:44:32.472194 me 2019-02-03 16:44:32.472249 me: 2019-02-03 16:44:33.463035 Delay: 0 se Thread2-9, Delay: 7 seconds. Time: 2019-02-03 16:44:53.487185 END: 2019-02-03 16:44:56.491312

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!