Question
One of the most challenging aspects of both Real Time Embedded and Distributed Systems is process coordination coordinating computational processes which are running in parallel.
One of the most challenging aspects of both Real Time Embedded and Distributed Systems is process coordination coordinating computational processes which are running in parallel. Threads are a concept used to execute different code on different CPUs simultaneously on the same machine. We will use this concept, and implementations of it in the Python programming language, to explore issues associated with parallel processing[1].
Consider Assignment5.py. It starts with a vector (100 elements 1 to 100) and uses 200 threads to increment each element of the vector by 1 (+1 each).
Part 1 [5 points]. Review and run Assignment5.py several times, and explain the results. Are they what you expect? Why or why not? Note: I have checked this on several machines, but I cant guarantee that you will always see the anomalies Im trying to demonstrate. The numerically correct answer is range(201,301). If you do see that correct answer, imagine that the following was generated, instead, for the purposes of answering this question..
Yes this what I expect. The thread did not define the specified range of worklist that should be obtain. They are random from the 200 threads given.
Part 2 [5 points]. One line of python code may translate to several instructions that occur on the CPU. Consider the self.l[self.iteration] = self.l[self.iteration] +1 statement. In it, at least 3 things are happening serially within a thread: read the current value of the vector element, calculate the new value of the element, and write the new value of the element. Why is this relevant?
Part 3 [5 points]. Remove the comments around the lock.acquire(), lock.release(), and lock = threading.Lock() calls in Assignment5.py and run it several times. Explain the results. Are they what you expect? Why or why not?
Part 4 [15 points]. This is a simple (and small) example of process coordination. Real applications and/or distributed systems can have 100s of threads and/or processes all doing different activities. How does this complexity impact the Software Systems Engineering of such systems?
Imagine that youd like to take Assignment5.py, and instead of distributing the work to individual threads, youd like to use individual machines (i.e., a distributed system).
Part 1 [10 points] What sort of distributed system architecture would you use for this problem, and why?
Part 2 [10 points] Which aspects of building the solution would be similar to the thread solution? Which aspects would be different?
In Assignment5.py, consider the use of the range() function.
Part 1 [10 points]. Why is it better to reuse this function than write out a 100 element vector?
Part 2 [10 points]. Now imagine that the range function was another piece of code that you could reuse, but you had to pay for the right to use it. What factors would go into your decision on whether you use it (i.e., pay for it) vice not (i.e., writing out the vector using another tactic)?
Part 3 [10 points]. If, instead of range(), the reuse question was for random.randint() or all of the thread functions, would that change the answer to your question? Why or why not?
Explain the difference between Service Oriented Architect and Web Services. (20 points)
import Queue import threading import time import random
THREADS = 200
class addingThread (threading.Thread): def __init__(self, l): threading.Thread.__init__(self) self.threadID = 3 self.name = "addingThread" self.l = l self.iteration = 0 def run(self): #delay start by a random time between 0 and 0.1 sec sleep_time= random.randint(1,1000)/10000.0 time.sleep(sleep_time) while self.iteration < len(self.l): #remove the comment for Question 1.3 #lock.acquire() self.l[self.iteration] = self.l[self.iteration] +1 #remove the comment for Question 1.3 #lock.release() self.iteration= self.iteration + 1 #remove the comment for Question 1.3 #lock = threading.Lock() workList = range(1,101) threads = []
# Create new threads thread_num=0 while thread_num < THREADS: thread = addingThread(workList) threads.append(thread) thread_num = thread_num +1
# Start threads for t in threads: t.start()
# Wait for all created threads to finish for t in threads: t.join()
#print final list print "Final list: " print workList
print "Exiting Main Thread"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started