
Threads ICS332 | Operating Systems Henri Casanova ([email protected]) Spring 2018 Henri Casanova ([email protected]) Threads Concurrent computing: several computations are performed during overlapping time periods As opposed to sequential execution, which is one computation to completion, followed by one computation to completion, etc. Concurrency: A feature of a program that can do multiple things \at the same time" A program is concurrent if it consists of units that can be executed independently or partially out of order without changing the output of the program Concurrent Computing Henri Casanova ([email protected]) Threads Concurrency: A feature of a program that can do multiple things \at the same time" A program is concurrent if it consists of units that can be executed independently or partially out of order without changing the output of the program Concurrent Computing Concurrent computing: several computations are performed during overlapping time periods As opposed to sequential execution, which is one computation to completion, followed by one computation to completion, etc. Henri Casanova ([email protected]) Threads A program is concurrent if it consists of units that can be executed independently or partially out of order without changing the output of the program Concurrent Computing Concurrent computing: several computations are performed during overlapping time periods As opposed to sequential execution, which is one computation to completion, followed by one computation to completion, etc. Concurrency: A feature of a program that can do multiple things \at the same time" Henri Casanova ([email protected]) Threads Concurrent Computing Concurrent computing: several computations are performed during overlapping time periods As opposed to sequential execution, which is one computation to completion, followed by one computation to completion, etc. Concurrency: A feature of a program that can do multiple things \at the same time" A program is concurrent if it consists of units that can be executed independently or partially out of order without changing the output of the program Henri Casanova ([email protected]) Threads I want to output a boolean array where each element is true if and only if the corresponding integer in the input array is > 50 f false, true, false, true, true ...g Assume that it takes one millisecond to test an integer value and update the output array Sequential programming: Iterating through the array would take 10,000 milliseconds. Concurrent programming: If I create 10 \units of execution" to compute 1000 output values, i.e., 1/10-th of the work, each unit takes 1,000 milliseconds Now if I can execute these 10 units independently (on 10 CPUs), the whole execution takes 1,000 milliseconds, i.e., 10 times faster In practice, we can't go exactly 10 times faster due to various overheads and bottlenecks But we will go much faster than sequential, provided be have multiple CPUs (which we all do in this multicore era!) Concurrent Computing: Example Consider an input array of 10,000 integers with values between 0 and 100 f 23, 56, 7, 68, 68 ...g Henri Casanova ([email protected]) Threads Assume that it takes one millisecond to test an integer value and update the output array Sequential programming: Iterating through the array would take 10,000 milliseconds. Concurrent programming: If I create 10 \units of execution" to compute 1000 output values, i.e., 1/10-th of the work, each unit takes 1,000 milliseconds Now if I can execute these 10 units independently (on 10 CPUs), the whole execution takes 1,000 milliseconds, i.e., 10 times faster In practice, we can't go exactly 10 times faster due to various overheads and bottlenecks But we will go much faster than sequential, provided be have multiple CPUs (which we all do in this multicore era!) Concurrent Computing: Example Consider an input array of 10,000 integers with values between 0 and 100 f 23, 56, 7, 68, 68 ...g I want to output a boolean array where each element is true if and only if the corresponding integer in the input array is > 50 f false, true, false, true, true ...g Henri Casanova ([email protected]) Threads Sequential programming: Iterating through the array would take 10,000 milliseconds. Concurrent programming: If I create 10 \units of execution" to compute 1000 output values, i.e., 1/10-th of the work, each unit takes 1,000 milliseconds Now if I can execute these 10 units independently (on 10 CPUs), the whole execution takes 1,000 milliseconds, i.e., 10 times faster In practice, we can't go exactly 10 times faster due to various overheads and bottlenecks But we will go much faster than sequential, provided be have multiple CPUs (which we all do in this multicore era!) Concurrent Computing: Example Consider an input array of 10,000 integers with values between 0 and 100 f 23, 56, 7, 68, 68 ...g I want to output a boolean array where each element is true if and only if the corresponding integer in the input array is > 50 f false, true, false, true, true ...g Assume that it takes one millisecond to test an integer value and update the output array Henri Casanova ([email protected]) Threads Concurrent programming: If I create 10 \units of execution" to compute 1000 output values, i.e., 1/10-th of the work, each unit takes 1,000 milliseconds Now if I can execute these 10 units independently (on 10 CPUs), the whole execution takes 1,000 milliseconds, i.e., 10 times faster In practice, we can't go exactly 10 times faster due to various overheads and bottlenecks But we will go much faster than sequential, provided be have multiple CPUs (which we all do in this multicore era!) Concurrent Computing: Example Consider an input array of 10,000 integers with values between 0 and 100 f 23, 56, 7, 68, 68 ...g I want to output a boolean array where each element is true if and only if the corresponding integer in the input array is > 50 f false, true, false, true, true ...g Assume that it takes one millisecond to test an integer value and update the output array Sequential programming: Iterating through the array would take 10,000 milliseconds. Henri Casanova ([email protected]) Threads In practice, we can't go exactly 10 times faster due to various overheads and bottlenecks But we will go much faster than sequential, provided be have multiple CPUs (which we all do in this multicore era!) Concurrent Computing: Example Consider an input array of 10,000 integers with values between 0 and 100 f 23, 56, 7, 68, 68 ...g I want to output a boolean array where each element is true if and only if the corresponding integer in the input array is > 50 f false, true, false, true, true ...g Assume that it takes one millisecond to test an integer value and update the output array Sequential programming: Iterating through the array would take 10,000 milliseconds. Concurrent programming: If I create 10 \units of execution" to compute 1000 output values, i.e., 1/10-th of the work, each unit takes 1,000 milliseconds Now if I can execute these 10 units independently (on 10 CPUs), the whole execution takes 1,000 milliseconds, i.e., 10 times faster Henri Casanova ([email protected]) Threads Concurrent Computing: Example Consider an input array of 10,000 integers with values between 0 and 100 f 23, 56, 7, 68, 68 ...g I want to output a boolean array where each element is true if and only if the corresponding integer in the input array is > 50 f false, true, false, true, true ...g Assume that it takes one millisecond to test an integer value and update the output array Sequential programming: Iterating through the array would take 10,000 milliseconds. Concurrent programming: If I create 10 \units of execution" to compute 1000 output values, i.e., 1/10-th of the work, each unit takes 1,000 milliseconds Now if I can execute these 10 units independently (on 10 CPUs), the whole execution takes 1,000 milliseconds, i.e., 10 times faster In practice, we can't go exactly 10 times faster due to various overheads and bottlenecks But we will go much faster than sequential, provided be have multiple CPUs (which we all do in this multicore era!) Henri Casanova ([email protected]) Threads To make programs more responsive Structuring a program as concurrent activities can make it more responsive because while one activity blocks waiting for some event, another can do something e.g., in some server, spawn a new activity to answer each client request e.g., in some GUI, while one activity is updating some display another activity is listening for mouse clicks What is Concurrency Used for? Make programs faster Running multiple activities at once can use the machine more effectively because there are multiple hardware components e.g., while one activity computes on the CPU, another activity sends data to the network card e.g., while one activity computes on one CPU, another computes on another CPU (this is the example in the previous slide) Henri Casanova ([email protected]) Threads What is Concurrency Used for? Make programs faster Running multiple activities at once can use the machine more effectively because there are multiple hardware components e.g., while one activity computes on the CPU, another activity sends data to the network card e.g., while one activity computes on one CPU, another computes on another CPU (this is the example in the previous slide) To make programs more responsive Structuring a program as concurrent activities can make it more responsive because while one activity blocks waiting for some event, another can do something e.g., in some server, spawn a new activity to answer each client request e.g., in some GUI, while one activity is updating some display another activity is listening for mouse clicks Henri Casanova ([email protected]) Threads But because the OS virtualizes memory, processes don't share memory naturally You can make it happen with special system calls, but it's cumbersome This can make it difficult to program processes that have complicated cooperative behaviors And so come threads..
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages116 Page
-
File Size-