Lecture 03 - Threads.pdf
Document Details
Uploaded by AstonishedHeptagon
Tags
Full Transcript
Hong Bang International University Department of Information Technology 04793 Operating Systems Threads Adapted from the textbook Operating System Concepts – 10th Edition Instructor: Hoang Ngoc Long Outline and Objectives Outline...
Hong Bang International University Department of Information Technology 04793 Operating Systems Threads Adapted from the textbook Operating System Concepts – 10th Edition Instructor: Hoang Ngoc Long Outline and Objectives Outline Objectives Overview To introduce the notion of a thread — a fundamental unit Multithreading Models of CPU utilization that forms the basis of multithreaded computer systems Thread Libraries To discuss the APIs for the Pthreads, Win32, and Java Implicit Threading thread libraries Operating System Examples To examine issues related to multithreaded programming Windows XP Threads Linux Threads Re-entrancy Thread specific data Threading Issues 2 Threading Concept Adapted from the textbook Operating System Concepts – 10th Edition Instructor: Hoang Ngoc Long Threads and Thread Usage A process has normally a single thread of control (execution sequence/flow). – Always at least one thread exists – If blocks, no activity can be done as part of the process – Better: be able to run concurrent activities (tasks) as part of the same process. – Now, a process can have multiple threads of control (multiple concurrent tasks). Threads run in pseudo-parallel manner (concurrently), share text(code) and data 4 Benefits of using threads for an App Responsiveness – One thread blocks, another one runs. – One thread may always wait for the user Resource Sharing – Threads can easily share resources Economy – Creating a thread is fast – Context switching among threads may be faster Scalability – Multiprocessors can be utilized better 5 Threads and Thread Usage data data CPU blocks blocks blocks code blocks code run enough single-threaded process multi-threaded process 6 a multithreaded process’ execution flows: threads Instructions of the Program main() Thread0 Thread2 Thread1 time Lifetime of the process 7 Multithreading Concept CPU single-threaded process multi-threaded process 8 Multithreading Concept Process Process thread thread thread thread P1.T1 P2.T1 P2.T2 P2.T3 Schedulable Entities We can select one of them and run 9 Multithreading Concept thread1 thread2 thread3 thread4 function1(…) { …. { function2(…) { …. { main() { …. thread_create (function1 ,…); …. thread_create (function2, …); …. thread_create (function1, …); …. } 10 Single and Multithreaded Processes 11 Multicore programming and multithreading challenges Multicore systems putting pressure on programmers. – Threading can utilize Multicore systems better, but it has some challenges Threading Challenges include – Dividing activities Come up with concurrent tasks – Balance Tasks should be similar importance and load – Data splitting Data may need to be split as well – Data dependency Data dependencies should be considered; need synchronization of activities – Testing and debugging Debugging is more difficult 12 Multithreaded Server Architecture 13 Concurrent Execution on a Single-core System 14 Parallel Execution on a Multicore System 15 Speedup: Amdahl’s Law Potential performance gain (speed up) with multiple cores (CPUs)? Program: both serial and parallel parts Assume the program executes and finishes in 1 time unit. – S: time to execute the serial portion (0