
Operating SystemsCh4 Threads1
Operating Systems 1 Ch4 Threads

Chapter 4:Threads· Thread Concepts- Why use threads-StructureinMemory-BenefitsandChallenges- Thread ModelsProgramming-Basic Programming:Pthreads Library-Implicit Threading:ThreadPools&OpenMP2
Chapter 4: Threads • Thread Concepts – Why use threads – Structure in Memory – Benefits and Challenges – Thread Models • Programming – Basic Programming: Pthreads Library – Implicit Threading: Thread Pools & OpenMP 2

Multi-threading- Motivation3
3 Multi-threading - Motivation

Motivation-Application Side· Most software applications are multithreaded, eachapplication is implemented as a process withseveral threads of control-Webbrowser: displays images, retrieve data from network-Wordprocessor? display graphics, respond to keystrokes, spelling & grammarchecking4
Motivation - Application Side • Most software applications are multithreaded, each application is implemented as a process with several threads of control – Web browser • displays images, retrieve data from network – Word processor • display graphics, respond to keystrokes, spelling & grammar checking 4

Motivation-ApplicationSide: Most software applications are multithreaded-Webbrowser-Wordprocessor- Similar tasks in a single application (web server)·Acceptclientrequests,servicethereguests·Usuallyservethousandsof clients(2) create new(1) requestthreadto servicethe requestclientthreadserver(3) resume listeningforadditionalclient requests5
Motivation - Application Side • Most software applications are multithreaded – Web browser – Word processor – Similar tasks in a single application (web server) • Accept client requests, service the requests • Usually serve thousands of clients 5

Motivation-ApplicationSide Why not create a process for each task?-Processcreationis: Heavy-weighted·Resourceintensive Still remember what kinds of data are included in aprocess...- Text, data, stack, heap in user-space memory-PcBinkernel-spacememoryMany of the data can be shared between multipletasks within an application6
Motivation – Application Side • Why not create a process for each task? – Process creation is • Heavy-weighted • Resource intensive • Still remember what kinds of data are included in a process. – Text, data, stack, heap in user-space memory – PCB in kernel-space memory • Many of the data can be shared between multiple tasks within an application 6

Motivation-SystemSide? Modern computers usually contain multicores- But, each processor can run only one process at a time- CPU is not fully utilized· How to improve the efficiency?-Assign one task to each core- Real parallelism (not just concurrency with interleavingonsingle-coresystem)7
Motivation – System Side • Modern computers usually contain multicores – But, each processor can run only one process at a time – CPU is not fully utilized • How to improve the efficiency? – Assign one task to each core – Real parallelism (not just concurrency with interleaving on single-core system) 7

Concurrencyvs.ParallelismConcurrent execution on single-core system:T1T2T3T4T1T3T4T2T1single coretimeParallel execution on a multi-core system:T1T3T3T1T1core1T4T4T2T2T2core2time8
Concurrency vs. Parallelism 8 Concurrent execution on single-core system: Parallel execution on a multi-core system:

Multi-threading- Motivation- Thread Concept
9 Multi-threading - Motivation - Thread Concept

High-level ldeafilesdatafilescodedatacodestackregistersregistersregistersregistersstackstackstackthreadthreadmultithreadedprocesssingle-threadedprocess10
High-level Idea 10