Chapter 4: Multithreaded Programming

Similar documents
Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

OPERATING SYSTEM. Chapter 4: Threads

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Chapter 4: Multithreaded Programming

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

Chapter 4: Multithreaded Programming Dr. Varin Chouvatut. Operating System Concepts 8 th Edition,

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

CS307: Operating Systems

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Chapter 4: Multithreaded

CS 450 Operating System Week 4 Lecture Notes

CSE 4/521 Introduction to Operating Systems

Chapter 4: Multi-Threaded Programming

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Chapter 4: Threads. Operating System Concepts 8 th Edition,

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Chapter 4 Multithreaded Programming

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

Multithreaded Programming

Chapter 5: Threads. Outline

CS420: Operating Systems

Processes and Threads

Semantics of fork() and exec()

Chapter 3: Processes. Operating System Concepts 8th Edition

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Processes and Threads

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE Opera,ng System Principles

CISC2200 Threads Spring 2015

Lecture 4 Threads. (chapter 4)

Operating Systems Prof. Ashok K Agrawala

Chapter 5: Threads. Single and Multithreaded Processes

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University

TDIU25: Operating Systems II. Processes, Threads and Scheduling

Process Description and Control

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

CS370 Operating Systems

CS420: Operating Systems

Lecture 2 Process Management

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

4.8 Summary. Practice Exercises

Chapter 4: Threads. Operating System Concepts 9 th Edition

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4

ICS Principles of Operating Systems

Thread. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

CS370 Operating Systems

Questions from last time

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Threads. CS3026 Operating Systems Lecture 06

Definition Multithreading Models Threading Issues Pthreads (Unix)

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Yi Shi Fall 2017 Xi an Jiaotong University

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

CHAPTER 2: PROCESS MANAGEMENT

Chapter 3: Processes. Operating System Concepts 8 th Edition,

CSMC 412. Operating Systems Prof. Ashok K Agrawala Ashok Agrawala Set 4. September 2006 CMSC 412 Set 4

CS Lecture 3! Threads! George Mason University! Spring 2010!

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

Concurrency, Thread. Dongkun Shin, SKKU

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

Threads. Thread Concept Multithreading Models User & Kernel Threads Pthreads Threads in Solaris, Linux, Windows. 2/13/11 CSE325 - Threads 1

Threads Implementation. Jo, Heeseung

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

Process Concept Process in Memory Process State new running waiting ready terminated Diagram of Process State

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

CS370 Operating Systems

Chapter 3: Processes

Threads. Still Chapter 2 (Based on Silberchatz s text and Nachos Roadmap.) 3/9/2003 B.Ramamurthy 1

Chapter 3: Processes. Operating System Concepts Essentials 8 th Edition

CS162 Operating Systems and Systems Programming Lecture 3. Concurrency: Processes, Threads, and Address Spaces. Review: History of OS

Process and Its Image An operating system executes a variety of programs: A program that browses the Web A program that serves Web requests

4. Concurrency via Threads

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

SMD149 - Operating Systems

Preview. The Thread Model Motivation of Threads Benefits of Threads Implementation of Thread

COP 4610: Introduction to Operating Systems (Fall 2016) Chapter 4: Threads. Zhi Wang Florida State University

THREADS. Jo, Heeseung

Disciplina Sistemas de Computação

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

Transcription:

Chapter 4: Multithreaded Programming Silberschatz, Galvin and Gagne 2013!

Chapter 4: Multithreaded Programming Overview Multicore Programming Multithreading Models Threading Issues Operating System Examples 4.2! Silberschatz, Galvin and Gagne 2013!

Objectives To introduce the notion of a thread a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To examine issues related to multithreaded programming 4.3! Silberschatz, Galvin and Gagne 2013!

Motivation Most modern applications or/and programs are multithreaded Threads run within an application or a process Multiple tasks with the application can be implemented by separate threads Update display Fetch data Spell checking Answer a network request Process creation is heavy-weight while thread creation is light-weight Can simplify code, increase efficiency Kernels are generally multithreaded 4.4! Silberschatz, Galvin and Gagne 2013!

Multithreaded Server Architecture client (1) request server (2) create new thread to service the request thread (3) resume listening for additional client requests A single application may be required to perform several similar tasks. For example a busy web server may process thousands of web requests concurrently. Creating one process for each client request is cumbersome (resource-intensive) and time-consuming A single application may need to do multiple tasks. For example, a web browser (client) need to display images or text (one thread) while another thread retrieves data from the network 4.5! Silberschatz, Galvin and Gagne 2013!

Benefits Responsiveness may allow continued execution if part of process is blocked, especially important for user interfaces Resource Sharing threads with a process share resources of the process by default, easier than shared memory or message passing that must be explicitly arranged by the programmer Economy thread creation is much cheaper than process creation, thread switching also has much lower overhead than context switching (switching to a different process) Scalability A process can take advantage of multiprocessor architectures by running multiple threads of the process simultaneously on different processors (CPUs). 4.6! Silberschatz, Galvin and Gagne 2013!

Multicore Programming Multicore or multiprocessor systems putting pressure on programmers to make better use of the multiple computing cores. Programming challenges in multicore systems include:! Identifying tasks: to divide applications into separate, concurrent tasks Balance: tasks perform equal work of equal value Data splitting! Data dependency! Testing and debugging! Parallelism implies a system can perform more than one task simultaneously Concurrency supports more than one task making progress Single processor / core, scheduler providing concurrency Types of parallelism Data parallelism distributes subsets of the same data across multiple cores, same operation on each! Task parallelism distributing threads across cores, each thread performing unique operation 4.7! Silberschatz, Galvin and Gagne 2013!

Concurrency vs. Parallelism Concurrent execution on single-core system:! single core T 1 T 2 T 3 T 4 T 1 T 2 T 3 T 4 T 1 time Parallelism on a multi-core system:! core 1 T 1 T 3 T 1 T 3 T 1 core 2 T 2 T 4 T 2 T 4 T 2 time 4.8! Silberschatz, Galvin and Gagne 2013!

Amdahl s Law Identifies performance gains from adding additional cores to an application that has both serial and parallel components S is serial portion N processing cores If application is 75% parallel / 25% serial, moving from 1 to 2 cores results in speedup of 1.6 times As N approaches infinity, speedup approaches 1 / S!! Serial portion of an application has disproportionate effect on performance gained by adding additional cores! 4.9! Silberschatz, Galvin and Gagne 2013!

Single and Multithreaded Processes code data files code data files registers stack registers registers registers stack stack stack thread thread single-threaded process multithreaded process 4.10! Silberschatz, Galvin and Gagne 2013!

Thread State Each Thread has a Thread Control Block (TCB) Execution State: CPU registers, program counter, pointer to stack Scheduling info: State (more later), priority, CPU time Accounting Info: Various Pointers (for implementing scheduling queues) Pointer to enclosing process: PCB In Nachos: thread is a class that includes the TCB OS keeps track of TCBs in protected memory Array, or Linked List, or 4.11! Silberschatz, Galvin and Gagne 2013!

Thread State (Cont.) State shared by all threads in process/address space Contents of memory (global variables, heap) I/O state (file system, network connections, etc.) State private to each thread Kept in TCB Thread Control Block CPU registers (including, program counter) Execution stack what is this? Execution Stack Parameters, temporary variables Keep program counters while called procedures are executing 4.12! Silberschatz, Galvin and Gagne 2013!

Lifecycle of a Thread As a thread executes, it changes state: new: The thread is being created ready: The thread is waiting to run running: Instructions are being executed waiting: Thread waiting for some event to occur terminated: The thread has finished execution Active threads are represented by their TCBs TCBs organized into queues based on their states 4.13! Silberschatz, Galvin and Gagne 2013!

Ready Queue And Various I/O Device Queues Thread not running! TCB is in some scheduler queue Separate queue for each device/signal/condition Each queue can have a different scheduler policy Ready Queue Head Tape Unit 0 Head Disk Unit 0 Head Disk Unit 2 Head Ether Netwk 0 Head Tail Tail Link Registers Other State TCB9 Link Registers Other State TCB2 Tail Tail Tail Operating System Concepts 9th Edition! Link Registers Other State TCB6 Link Registers Other State TCB16 Link Registers Other State TCB3 Link Registers Other State TCB8 4.14! Silberschatz, Galvin and Gagne 2013!

Examples of Multithreaded Programs Embedded systems Elevators, Planes, Medical systems, Wristwatches Single Program, concurrent operations Most modern OS kernels Internally concurrent to deal with concurrent requests by multiple users But no protection needed within kernel Database Servers Access to shared data by many concurrent users Also background utility processing must be done Network Servers Concurrent requests from network Again, single program, multiple concurrent operations File server, Web server, and airline reservation systems Parallel Programming (More than one physical CPU) Split program into multiple threads for parallelism 4.15! Silberschatz, Galvin and Gagne 2013!

User Threads and Kernel Threads Support for threads may be provided at either the user level, for user threads, or by the kernel, for kernel threads! User threads - management done by user-level threads library without kernel support Three primary thread libraries: POSIX Pthreads! Win32 threads Java thread Kernel threads - supported by the kernel. Virtually all general-purpose operating systems support kernel threads, including: Windows Solaris Linux Mac OS X Ultimately, a relationship must exist between user threads and kernel threads. 4.16! Silberschatz, Galvin and Gagne 2013!

Multithreading Models Many-to-One One-to-One Many-to-Many 4.17! Silberschatz, Galvin and Gagne 2013!

Many-to-One Many user-level threads mapped to single kernel thread One thread blocking causes all to block Multiple threads may not run in parallel on a multicore system because only one may be in kernel at a time user thread Few systems currently use this model Examples: Solaris Green Threads! GNU Portable Threads! k kernel thread 4.18! Silberschatz, Galvin and Gagne 2013!

One-to-One Each user-level thread maps to kernel thread Creating a user-level thread creates a kernel thread More concurrency than many-to-one Number of threads per process sometimes restricted due to overhead Examples Windows NT/XP/2000 Linux Solaris 9 and later user thread k k k k kernel thread 4.19! Silberschatz, Galvin and Gagne 2013!

Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads user thread Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package k k k kernel thread 4.20! Silberschatz, Galvin and Gagne 2013!

Two-level Model Similar to M:N, except that it also allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier user thread k k k k kernel thread 4.21! Silberschatz, Galvin and Gagne 2013!

Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space with no kernel support. This means that invoking a function in the library results in a local function call in user space, and not a system call Kernel-level library supported directly by the OS Three main thread libraries are in use today: POSIX Pthreads Windows Java 4.22! Silberschatz, Galvin and Gagne 2013!

Threading Issues Semantics of fork() and exec() system calls Signal handling Synchronous and asynchronous Thread cancellation of target thread Asynchronous or deferred Thread-local storage 4.23! Silberschatz, Galvin and Gagne 2013!

Semantics of fork() and exec() Does fork()duplicate only the calling thread or all threads? Some UNIX have two versions of fork If exec() is called immediately after forking, duplicating all threads is unnecessary, as the program specified in the parameters to exec() will replace the entire process Exec() usually works as normal replace the running process including all threads 4.24! Silberschatz, Galvin and Gagne 2013!

Signal in UNIX Signals are in UNIX systems to notify a process that a particular event has occurred A signal may be received either synchronously or asynchronously, depending on the source of and the reason for the event being signalled. All signals follow the same pattern: Signal is generated by the occurrence of a particular event Signal is delivered to a process Once delivered, the signal must be handled Examples of synchronous signal include illegal memory access and division of 0. Synchronous signals are delivered to the same process that performed the operation that caused the signal When a signal is generated by an event external to a running process, that process receives the signal asynchronously. Examples include terminating a process with specific keystrokes (such as <control><c>) and having a timer expire 4.25! Silberschatz, Galvin and Gagne 2013!

Signal Handling A signal may be handled by one of the two possible handlers: has occurred A default signal hander! A user-defined signal handler Every signal has default handler that kernel runs when handling signal User-defined signal handler can override the default handler For single-threaded, signal delivered to process Where should a signal be delivered a multi-threaded program? Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process The method for delivering a signal depends on the type of signal Synchronous signals need to be delivered to the thread causing the signal, not other threads Terminating a process signal should be sent to all threads within the process 4.26! Silberschatz, Galvin and Gagne 2013!

Thread Cancellation Thread cancellation involves terminating a thread before it has completed. Example, Multiple threads are concurrently searching through a database, one thread returns the result, the remaining threads might be cancelled Thread to be canceled is target thread Cancellation of a target thread may occur in two different scenarios:! Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled This involves in reclaiming the resource allocated to a thread, in which asynchronous cancellation might not be able to free up resource immediately 4.27! Silberschatz, Galvin and Gagne 2013!

Thread-Local Storage Thread-local storage (TLS) allows each thread to have its own copy of data Useful when you do not have control over the thread creation process Different from local variables Local variables visible only during single function invocation TLS visible across function invocations Similar to static data TLS is unique to each thread 4.28! Silberschatz, Galvin and Gagne 2013!

Operating System Examples Windows XP Threads Linux Thread 4.29! Silberschatz, Galvin and Gagne 2013!

Windows Threads Windows implements the Windows API for Win 98, NT, 2000, Win XP, and Window 7 Implements the one-to-one mapping, kernel-level Each thread contains A thread ID uniquely identifying the thread Register set representing the status of the processor Separate user and kernel stacks for when thread runs in user mode or kernel mode Private data storage area used by run-time libraries and dynamic link libraries (DLLs) The register set, stacks, and private storage area are known as the context of the thread The primary data structures of a thread include: ETHREAD (executive thread block) includes pointer to process to which thread belongs and to KTHREAD, in kernel space KTHREAD (kernel thread block) scheduling and synchronization info, kernel-mode stack, pointer to TEB, in kernel space TEB (thread environment block) thread ID, user-mode stack, thread-local storage, in user space 4.30! Silberschatz, Galvin and Gagne 2013!

Windows XP Threads Data Structures ETHREAD thread start address pointer to parent process KTHREAD scheduling and synchronization information kernel stack TEB thread identifier user stack thread-local storage kernel space user space 4.31! Silberschatz, Galvin and Gagne 2013!

Linux Threads Linux refers to processes and threads as tasks rather than threads! Thread creation is done through clone() system call clone() allows a child task to determine how to share the address space of the parent task (process) Flags control behavior flag meaning CLONE_FS File-system information is shared. CLONE_VM The same memory space is shared. CLONE_SIGHAND Signal handlers are shared. CLONE_FILES The set of open files is shared. Operating System Concepts 9th Edition! 4.32! Silberschatz, Galvin and Gagne 2013!