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

Similar documents
Chapter 4: Threads. Chapter 4: Threads

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

CS307: Operating Systems

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

OPERATING SYSTEM. Chapter 4: Threads

CS 450 Operating System Week 4 Lecture Notes

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

Chapter 4: Multi-Threaded Programming

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

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

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

Chapter 4: Threads. Chapter 4: Threads

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

Chapter 4: Multithreaded

Chapter 4: Multithreaded Programming

CSE 4/521 Introduction to Operating Systems

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 4: Multithreaded Programming

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

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

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

Multithreaded Programming

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

CS420: Operating Systems

Chapter 3: Processes. Operating System Concepts 8th Edition

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

Lecture 4 Threads. (chapter 4)

Chapter 4 Multithreaded Programming

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

Operating Systems Prof. Ashok K Agrawala

Chapter 5: Threads. Single and Multithreaded Processes

CISC2200 Threads Spring 2015

Chapter 5: Threads. Outline

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

Processes and Threads

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

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Semantics of fork() and exec()

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

Processes and Threads

Process Description and Control

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

Threads. CS3026 Operating Systems Lecture 06

CSE Opera,ng System Principles

Lecture 2 Process Management

Concurrency, Thread. Dongkun Shin, SKKU

CS420: Operating Systems

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

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

Distributed Systems Operation System Support

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

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

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

CS370 Operating Systems

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

4.8 Summary. Practice Exercises

Yi Shi Fall 2017 Xi an Jiaotong University

Last Class: CPU Scheduling. Pre-emptive versus non-preemptive schedulers Goals for Scheduling: CS377: Operating Systems.

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

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

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

ICS Principles of Operating Systems

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

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

Outline. Interprocess Communication. Interprocess Communication. Communication Models: Message Passing and shared Memory.

Lecture 5: Process Description and Control Multithreading Basics in Interprocess communication Introduction to multiprocessors

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

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

Questions from last time

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

CS 326: Operating Systems. Process Execution. Lecture 5

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

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

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

Chapter 4: Threads. Operating System Concepts 9 th Edition

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

2/14/2012. Using a layered approach, the operating system is divided into N levels or layers. Also view as a stack of services

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

Definition Multithreading Models Threading Issues Pthreads (Unix)

CSE 120 Principles of Operating Systems

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

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

Threads. studykorner.org

Threads Implementation. Jo, Heeseung

Processes. 4: Threads. Problem needs > 1 independent sequential process? Example: Web Server. Last Modified: 9/17/2002 2:27:59 PM

CHAPTER 2: PROCESS MANAGEMENT

CS370 Operating Systems

From Processes to Threads

Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

THREADS. Jo, Heeseung

SMD149 - Operating Systems

Processes, Threads and Processors

Transcription:

Eike Ritter 1 Modified: October 16, 2012 Lecture 8: Operating Systems with C/C++ School of Computer Science, University of Birmingham, UK 1 Based on material by Matt Smart and Nick Blundell Outline 1 Concurrent and Parallel Execution User and Kernel 2 3 Some Considerations of Semantics of fork() Thread Cancellation Signal Handling Thread Pools 4 Thread Libraries Pthreads Windows XP Linux Java Single and A thread is an execution state of a process (e.g. the next instruction to execute, the values of CPU registers, the stack to hold local variables, etc.) Thread state is separate from global process state, such as the code, open files, global variables (on the heap), etc. Benefits of Responsiveness - user interaction in a GUI can be responded to by a separate thread from that, say, doing long running computation (e.g. saving a file, running some algorithm, etc.) Resource Sharing - within a certain process share its address space and can therefore use shared variables to communicate, which is more efficient than passing messages. Economy - are often referred to as light-weight processes, since running a system with multiple threads has a smaller memory footprint than the equivalent with multiple processes. Scalability - For multi-threaded processes it is much easier to make use of parallel processing (e.g. multi-core processors, and distributed systems) Reduce programming complexity - Since problems can be broken down into parallel tasks, rather than more complex state machines.

Multithreaded Server Architecture Multicore Programming Multicore systems are putting pressure on programmers, with challenges that include: Dividing activities - How can we make better use of parallel processing? Balance - How should we balance the parallel tasks on the available cores to get maximum efficiency? Data splitting - How can data sets be split for processing in parallel and then rejoined (e.g. SETI@home) Data dependency - Some processing must be performed in a certain order, so synchronisation of tasks will be necessary. How to test and debug such systems? Concurrent and Parallel Execution Concurrent and Parallel Execution Single-core Concurrent Thread Execution User and Kernel User Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads Multicore Parallel Thread Execution

User and Kernel Kernel Threading is supported by modern OS Kernels Examples: Windows XP/2000 Solaris Linux Mac OS X A particular kernel (e.g. on an embedded device, or an older operating system) may not support multi-threaded processes, though it is still possible to implement threading in the user process. Therefore many threading models exist for mapping user threads to kernel threads: Many user-level threads mapped to single kernel thread/process Useful if the kernel does not support threads But what if one user thread calls a blocking kernel function? This will block the whole process (i.e. all the other user threads) Complex solutions exist where the user-mode thread package intercepts blocking calls, changes them to non-blocking and then implements a user-mode blocking mechanism. You could implement something like this yourself, by having a process respond to timer events that cause it to perform a context switch in user space (e.g. store current registers, CPU flags, instruction pointer, then load previously stored ones) Since most high-level languages cannot manipulate registers directly, you would have to write a small amount of assembler code to make the switch. Examples: Solaris Green : http://bit.ly/qynkaq GNU Portable : http://www.gnu.org/software/pth/

Each user-level thread maps to kernel thread But, to switch between threads a context switch is required by the kernel. Also, the number of kernel threads may be limited by the OS Examples: Windows NT/XP/2000 Linux Solaris 9 and later Allows many user level threads to be mapped to many kernel threads Best of both worlds Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

Similar to many-to-many, except that it allows a user thread optionally to be bound directly to a kernel thread Examples: IRIX HP-UX Tru64 UNIX Solaris 8 and earlier Some Considerations of Semantics of fork() Unclear Semantics of UNIX fork() system call Does fork() duplicate only the calling thread or all threads? Sometimes we want this, and sometimes we don t, so some UNIX systems provide alternative fork functions.

Some Considerations of Thread Cancellation Thread Cancellation How to terminate a thread before it has finished? Two general approaches use by programmers: Asynchronous cancellation terminates the target thread immediately Useful as a last resort if a thread will not stop (e.g. due to a bug, etc.) Deferred cancellation allows the target thread to periodically check if it should be cancelled This approach is often considered to be much cleaner, since the thread can perform any clean-up processing (e.g. close files, update some state, etc.) Some Considerations of Signal Handling Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals Signal is generated by particular event Signal is delivered to a process Signal is handled by some function Not so straightforward for a multi-threaded process. Options are: 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 In most UNIX systems a thread can be configured to receive or block (i.e. not handle) certain signals to help overcome these issues. Some Considerations of Thread Pools Thread Pools Thread Libraries Thread Libraries Under a high request-load, multithreaded servers can waste a lot processing time simply creating and destroying threads. Solution: Pre-create a number of threads in a pool, where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool, to ensure some level of service for a finite number of clients. Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS

Pthreads Pthreads May be provided either as user-level or kernel-level A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behaviour of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X) Example: threadtest.c. Note, this is an implementation of POSIX Pthreads, so compiles differently! Windows XP Windows XP Implements the one-to-one mapping (i.e. kernel-level threads) Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) - Stores general info about a thread: its parent process, address of the instruction where the thread starts execution. KTHREAD (kernel thread block) - Stores kernel-level state of the thread: kernel stack, etc. TEB (thread environment block) - Stores user-level state of the thread: user stack, thread-local storage. Windows XP Windows XP Linux Linux Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process) and can be passed flags to control exactly what resources are shared.

Java Java Java threads are managed by the JVM Typically implemented using the threads model provided by underlying OS Java threads may be created by: Extending Thread class Implementing the Runnable interface Java Summary We looked at: 1 Concurrent and Parallel Execution User and Kernel 2 l 3 Some Considerations of Semantics of fork() Thread Cancellation Signal Handling Thread Pools 4 Thread Libraries Pthreads Windows XP Linux Java Next time: Process synchronisation