Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: Introduction to Threads

Similar presentations


Presentation on theme: "Lecture 7: Introduction to Threads"— Presentation transcript:

1 Lecture 7: Introduction to Threads

2 Review Multiprogramming Context switch Process table

3 In this lecture Introduction to threads

4 Why threads? Word editor Reformatting Searching Auto saving

5 Threads The smallest “control unit”
A process could have multiple threads Multiple threads in the same address space Each thread has its own stack, register values All threads within a process share the same text, heap, and static data segments

6 Why not use multiple processes?
Process creation expensive Each process needs memory, lots of state

7 Processes vs. Threads Process 1 Process 2 Process 3
Speaking of processes, what is the difference between two single-threaded processes and one two-threaded process? First of all, if one process already exists, it is much cheaper (by a factor of up to at least a thousand, depending on the OS and architecture) to create another thread in the existing process than to create a new process. Switching between the contexts of two threads in the same process is also often cheaper than switching between the contexts of two threads in different processes. Finally, two threads in one process share everything—both address space and open files; the two can communicate without having to copy data. Though two different processes can share memory in modern Unix systems, the most common forms of interprocess communication are far more expensive. Copyright © 2002 Thomas W. Doeppner. All rights reserved.

8 Processes vs. Threads Creating a new thread 100 times cheaper than creating a new process Switching between two threads also cheaper Thread = “Lightweight process”

9 Thread Model Private to a thread.
Shared by all threads within a process. Private to a thread.

10 Single Threaded Web Server
Disk webpages requests

11 Multi Threaded Web Server
Disk dispatcher Many workers

12 Pseudocode with Threads
Dispatcher While (1) { get_request(&req); start_new_worker(req); } Worker Worker_thread(req) { fetch_webpage(req,&page); return_page(req, page); }


Download ppt "Lecture 7: Introduction to Threads"

Similar presentations


Ads by Google