This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 1 CSC 4103 - Operating Systems Fall 2009 Tevfik Ko ! ar Louisiana State University September 8 th , 2009 Lecture - IV Threads 2 Roadmap • Threads – Why do we need them? – Threads vs Processes – Threading Examples – Threading Implementation & Multi-threading Models – Other Threading Issues • Thread cancellation • Signal handling • Thread pools • Thread specific data 3 Concurrent Programming • In certain cases, a single application may need to run several tasks at the same time 1 1 2 3 2 4 5 3 4 5 sequential concurrent 4 Motivation • Increase the performance by running more than one tasks at a time. – divide the program to n smaller pieces, and run it n times faster using n processors • To cope with independent physical devices. – do not wait for a blocked device, perform other operations at the background Serial vs Parallel 5 Q Please COUNTER COUNTER 1 COUNTER 2 6 Divide and Compute x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 How many operations with sequential programming? 7 Step 1: x1 + x2 Step 2: x1 + x2 + x3 Step 3: x1 + x2 + x3 + x4 Step 4: x1 + x2 + x3 + x4 + x5 Step 5: x1 + x2 + x3 + x4 + x5 + x6 Step 6: x1 + x2 + x3 + x4 + x5 + x6 + x7 Step 7: x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 7 Divide and Compute x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 Step 1: parallelism = 4 Step 2: parallelism = 2 Step 3: parallelism = 1 8 Gain from parallelism In theory: • dividing a program into n smaller parts and running on n processors results in n time speedup In practice: • This is not true, due to – Communication costs – Dependencies between different program parts • Eg. the addition example can run only in log(n) time not 1/n 9 Concurrent Programming • Implementation of concurrent tasks: – as separate programs – as a set of processes or threads created by a single program • Execution of concurrent tasks: – on a single processor (can be multiple cores) ! Multithreaded programming – on several processors in close proximity ! Parallel computing – on several processors distributed across a network ! Distributed computing 10 Why Threads? • In certain cases, a single application may need to run several tasks at the same time – Creating a new process for each task is time consuming – Use a single process with multiple threads • faster • less overhead for creation, switching, and termination • share the same address space Ownership vs Execution 11 " A process embodies two independent concepts: 1. resource ownership 2. execution & scheduling 1. Resource ownership # a process is allocated address space to hold the image, and is granted control of I/O devices and files # the O/S prevents interference among processes while they make use of resources (multiplexing) 2. Execution & scheduling # a process follows an execution path through a program --> Thread # it has an execution state and is scheduled for dispatching Multi-threading 12 " The execution part is a “thread” Pasta for six – boil 1 quart salty water...
View Full Document
- Fall '08
- Operating Systems, modern operating systems, A. Tanenbaum