Page 124 - Computer_Science_F5
P. 124

Exercise 2.9

               1.  Imagine a multithreaded program downloading a large file (Thread A) and
                   performing a complex mathematical calculation (Thread B) concurrently. How
                   might data dependencies between these threads impact their execution? Explain
                   your answer.
          FOR ONLINE READING ONLY
               2.  Imagine you have a program that involves a large number of independent tasks.   Chapter Two: Performance and optimisation of computer processor
                   Discuss the potential advantages and disadvantages of using SMT versus a
                   multi-core processor with separate processes for each task.

               3.  Modern operating systems employ scheduling algorithms to manage threads
                   and processes. Research different scheduling algorithms used for multithreading
                   and explain how they impact performance and resource utilisation.
               4.  Imagine a single CPU core with SMT that supports two logical cores. The
                   program has two threads:
                         # Thread 1 (Print Numbers)
                               def print_numbers():
                                for i in range(1, 6):
                                  print(i)
                          # Thread 2 (Calculate Factorial)
                                      def calculate_factorial(n):
                                        result = 1
                                      for i in range(1, n + 1):
                                        result *= i
                                      print(f”Factorial of {n} is: {result}”)
                          # Main Program
                                      start_thread(print_numbers)  # Start thread to print numbers 1-5
                                      start_thread(calculate_factorial, 5)  # Start thread to calculate facto
                                      rial of 5

                           # Wait for threads to finish (pseudocode)
                                      wait_for_threads()
                      (a)  Explain the expected output of the program. In what order will the
                           numbers be printed (from Thread 1) and the factorial result (from
                           Thread 2) be displayed?
                      (b)  Imagine running this program with a single thread instead of two. How
                           might the output and execution order differ?
                      (c)  Considering the scenario above, explain how multithreading can
                           potentially improve the responsiveness of the program, especially for
                           a user interacting with it.
                      (d)  While this program demonstrates multithreading, can SMT further
                           improve its performance within a single core? Explain your reasoning.


                                                    115
               Student’s Book  Form Five



     Computer Science Form 5.indd   115                                                     23/07/2024   12:33
   119   120   121   122   123   124   125   126   127   128   129