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

Similar documents
Chapter 4: Threads. Chapter 4: Threads

CS307: Operating Systems

OPERATING SYSTEM. Chapter 4: Threads

Chapter 4: Multi-Threaded Programming

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

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

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

Chapter 4: Multithreaded

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

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Multithreaded Programming

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

CS 450 Operating System Week 4 Lecture Notes

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

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

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

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

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: Multithreaded Programming

CSE 4/521 Introduction to Operating Systems

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

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

Chapter 4 Multithreaded Programming

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

Multithreaded Programming

Chapter 3: Processes. Operating System Concepts 8th Edition

CS420: Operating Systems

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

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

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

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

Semantics of fork() and exec()

Lecture 4 Threads. (chapter 4)

Processes and Threads

Chapter 5: Threads. Outline

Chapter 5: Threads. Single and Multithreaded Processes

Operating Systems Prof. Ashok K Agrawala

CISC2200 Threads Spring 2015

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Processes and Threads

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

Threads. CS3026 Operating Systems Lecture 06

Process Description and Control

Definition Multithreading Models Threading Issues Pthreads (Unix)

Concurrency, Thread. Dongkun Shin, SKKU

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

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

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

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

Lecture 2 Process Management

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

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

Questions from last time

CSE Opera,ng System Principles

CS420: Operating Systems

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

Distributed Systems Operation System Support

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

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

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

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

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

4.8 Summary. Practice Exercises

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

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

CS370 Operating Systems

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

Lecture Contents. 1. Overview. 2. Multithreading Models 3. Examples of Thread Libraries 4. Summary

Threads Assistant Professor DCS Operating System Concepts

Yi Shi Fall 2017 Xi an Jiaotong University

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

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

Threads Implementation. Jo, Heeseung

THREADS. Jo, Heeseung

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

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

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

Last class: Today: Thread Background. Thread Systems

Chapter 4: Threads. Operating System Concepts 9 th Edition

Computer Systems Laboratory Sungkyunkwan University

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

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

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

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

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

4. Concurrency via Threads

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

SMD149 - Operating Systems

CS 3305 Intro to Threads. Lecture 6

CHAPTER 2: PROCESS MANAGEMENT

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

CSC 539: Operating Systems Structure and Design. Spring 2006

ICS Principles of Operating Systems

Transcription:

Chapter 4: Threads

Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads Chapter 4: Threads Objectives To introduce the notion of a thread A fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To discuss the APIs for the Pthreads, Win32, and Java thread libraries To examine issues related to multithreaded programming

Thread A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack. It shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open files. Heavy weight process has a single thread of control. A single-threaded process and a multithreaded process. Multiple threads of control, it can perform more than one task at a time.

Threads Use Many software packages run on modern desktop PCs are multithreaded and is (single process with several threads of control). A word processor: A thread for displaying graphics, A thread for responding to keystrokes from the user and A thread for spelling and grammar checking. Web browser: Thread display images/text and Thread gets data from the network. A Web server Problem: Accepts client requests for Web pages, images, sound,.. If the server is busy (have several clients concurrently accessing it), and ran as a traditional single-threaded process, it will serve only one client at a time, and a client might have to wait long time. Single thread Process Solution: The server accepts requests and creates a separate process for each request ( process creation is time consuming and resource lost ). If the new process will perform the same tasks as the existing process (overhead) Multiple threads Process: The server will create a separate thread that listens for client requests. When a request is made, the server will create a new thread to service the request and resume listening for additional requests.

Threads Use Threads used in remote procedure call (RPC) Allow inter-process communication, When a server receives a message, it services the message using a separate thread (allows server to handle several concurrent requests). Finally, most operating system kernels are now multithreaded; Several threads operate in the kernel, Each thread performs a specific task, such managing devices or interrupt handling.

Benefits 1. Responsiveness: Allow a program to continue running even if part of it is blocked or is performing a lengthy operation(web browser allow user interaction in one thread while an image is being loaded in another). 2. Resource sharing: Threads share memory and resources of the parent process by default. 3. Economy: Create and context-switch threads is less time consuming than create and manage processes. 4. Scalability: Threads may be running in parallel on different processors. A singlethreaded process can only run on one processor, regardless how many are available. Multithreading on a multi-cpu machine increases parallelism.

Multicore Programming Place multiple computing cores on a single chip. Each of these cores appears as a separate processor to the operating system Multithreaded programming provides a mechanism for more efficient use of multiple cores and improved concurrency. Designers of OS must write scheduling algorithms that use multiple processing cores to allow the parallel execution. Multicore systems challenges on programmer:: Dividing activities: divide the program into separate tasks that can be run in parallel. Balance: The tasks must perform equal work of equal value Data splitting: The data accessed and manipulated by the tasks must be divided to run on separate cores. Data dependency: The data accessed by the tasks must be examined for dependencies between two or more tasks. Testing and debugging: On multiple cores, there are many different execution paths. Testing and debugging such concurrent programs is more difficult.

Concurrent Execution on a Single-core System Parallel Execution on a Multicore System

User threads Multithreading Models Provided either at the user level Supported above the kernel Managed without kernel support kernel threads: Provided by the kernel Level Supported and managed directly by the operating system. Most operating systems including Windows XP, Windows Vista, Linux, Mac OS X, Solaris, and Tru64 UNIX support kernel threads. A relationship must exist between user threads and kernel threads.

1. Many-to-One Model Multithreading Models Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads

2. One-to-One Model Multithreading Models Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later

3. Many-to-Many Model Multithreading Models Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the Thread Fiber package

4. Two Level Model Multithreading Models Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

Thread Libraries A thread library provides the programmer with an API for creating and managing threads. There are two primary ways of implementing a thread library: User Level Thread Library: Provide a library entirely in user space with no kernel support. All code and data structures for the library exist in user space. Invoking a function in the library as a local function call in user space and not a system call. Kernel Level Thread Library: Implement a kernel-level library supported directly by the OS. Code and data structures for the library exist in kernel space. Invoking a function in the API for the library typically results in a system call to the kernel. Three main thread libraries are in use today: 1. POSIX Pthreads: may be provided as either a user- or kernel-level library. 2. Win32: is a kernel-level library available on Windows systems. 3. Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine (JVM) is running on top of of the OS, Java threads depends on the OS system that is running on it.

Pthreads Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization. May be provided either as user-level or kernel-level A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X)

Pthreads Example

Win32 API Multithreaded C Program (Example)

Java thread Threads are the fundamental model of program execution in a Java program. Java language provide a rich set of features for create and manage threads. All Java programs have at least a single thread of control that begins execution in the program s main() method. Creating Java Thread: Create a new class from the Thread class and to override its run() method. Define a class that implements the Runnable interface. JVM and the underlying Host JVM is implemented on top of a host OS ( hide the details of the underlying OS ) allows Java programs to operate on any platform that supports a JVM. JVM does not indicate how Java threads are to be mapped to the underlying OS For example, the Windows XP OS uses the one-to-one model; therefore, each Java thread for a JVM running on such a system maps to a kernel thread. On operating systems that use the many-to-many model (such as Tru64 UNIX), a Java thread is mapped according to the many-to-many model. Solaris initially implemented the JVM using the many-to-one model (the green threads library mentioned earlier).

Java Multithreaded Program

Java thread States A Java thread may be in one of six possible states in the JVM: 1. New. A thread object for is created with the new command but has not yet started. 2. Runnable. Calling the start() method allocates memory for the new thread in the JVM and calls the run() method for the thread object. When a thread s run() method is invoked, the thread moves from the new to the runnable state. 3. Blocked. A thread is in this state as it waits to acquire a lock a tool used for thread synchronization. 4. Waiting. A thread invoking the join() method enters this state as it waits for the thread it is joining on to terminate. 5. Timed waiting. is similar to waiting, except a thread specifies the maximum amount of time it will wait. For example, the join() method has an optional parameter that the waiting thread can use to specify how long it will wait until the other thread terminates. 6. Terminated. A thread moves to the this state when its run() method terminates.

Java thread States

A Multithreaded Solution to the Producer Consumer Problem

A Multithreaded Solution to the Producer Consumer Problem

Threading Issues with multi-thread programs 1. When creating new Process [Semantics of fork() and exec() system calls] If one thread in a program calls fork(), does the new process duplicate all threads, or is the new process single-threaded? Some OS two versions of fork(), one that duplicates all threads and another that duplicates only the thread that invoked the fork() system call. 2. Thread Cancellation :Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately. Deferred cancellation allows the target thread to periodically check if it should be cancelled. 3. Signal Handling : Signals are used to notify a process that a particular event has occurred. A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled Options: 1. Deliver the signal to the thread to which the signal applies 2. Deliver the signal to every thread in the process 3. Deliver the signal to certain threads in the process 4. Assign a specific thread to receive all signals for the process

Threading Issues with multi-thread programs 1. When creating new Process [Semantics of fork() and exec() system calls] If one thread in a program calls fork(), does the new process duplicate all threads, or is the new process single-threaded? Some OS two versions of fork(), one that duplicates all threads and another that duplicates only the thread that invoked the fork() system call. 2. Thread Cancellation :Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately. Deferred cancellation allows the target thread to periodically check if it should be cancelled. 3. 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 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled Options: 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

Threading Issues with multi-thread programs 4. Thread Pools in a Web server, whenever the server receives a request, it creates a separate thread to service the request. The amount of time required to create the thread prior to servicing the request. The number of threads concurrently active in the system. Unlimited threads could exhaust system resources, such as CPU time or memory. One solution to this issue is to use a thread pool. How does Thread Pools works? 1. Create a number of threads at process startup and place them into a pool, an wait for work. 2. When a server receives a request, it assign the request for a thread from this pool to service (if one is available). 3. Once the thread completes its service, it returns to the pool and awaits more work. 4. If the pool contains no available thread, the server waits until one becomes free.

Threading Issues with multi-thread programs 5. Thread Specific Data Threads belonging to the same process share the data of the process. Some times, each thread might need its own copy data( thread-specific data). For example, in a transaction-processing system, we might service each transaction in a separate thread.

Windows XP Threads Implements the one-to-one mapping, kernel-level, Consists of: Thread ID identifying the thread. Thread Context consists of : Register set representing the status of the processor User stack, employed when the thread is running in user mode. kernel stack, employed when the thread is running in kernel mode. Private storage area used by various run-time libraries and dynamic link libraries (DLLs) The primary data structures of a thread include: ETHREAD Executive Thread Block, include: A pointer to the process to which the thread belongs The address of the routine in which the thread starts control. a pointer to the corresponding KTHREAD. KTHREAD Kernel Thread Block includes: scheduling and synchronization information for the thread. The kernel stack (used when the thread is running in kernel mode). a pointer to the TEB TEB Thread Environment Block- The TEB is a user-space data structure that is accessed when the thread is running in user mode. The TEB contains: the thread identifier, a user-mode stack, and an array for thread specific dat.

Windows XP Threads Data Structures

Linux Threads fork() and clone() system calls Doesn t distinguish between process and thread Uses term task rather than thread clone() takes options to determine sharing on process create struct task_struct points to process data structures (shared or unique)

End of Chapter 4