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

Similar documents
Chapter 4: Threads. Chapter 4: Threads

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

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

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

CS 450 Operating System Week 4 Lecture Notes

CS307: Operating Systems

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

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

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

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

Chapter 4: Threads. Chapter 4: Threads

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 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

OPERATING SYSTEM. Chapter 4: Threads

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

Chapter 5: Threads. Single and Multithreaded Processes

Chapter 4 Multithreaded Programming

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

Operating Systems Prof. Ashok K Agrawala

Chapter 4: Multi-Threaded Programming

Chapter 4: Multithreaded

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Operating System Concepts 9 th Edition

Lecture 4 Threads. (chapter 4)

Processes and Threads

CS420: Operating Systems

Multithreaded Programming

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

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

CSE 4/521 Introduction to Operating Systems

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

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

CS370 Operating Systems

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

CS420: Operating Systems

CS370 Operating Systems

CISC2200 Threads Spring 2015

Threads. studykorner.org

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

Definition Multithreading Models Threading Issues Pthreads (Unix)

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

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

CS370 Operating Systems

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

Chapter 3: Processes. Operating System Concepts 8th Edition

Processes and Threads

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

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

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

Questions from last time

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

CS370 Operating Systems

THREADS. Jo, Heeseung

CS 3305 Intro to Threads. Lecture 6

4.8 Summary. Practice Exercises

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

Process Description and Control

Chapter 5: Threads. Outline

Lecture 2 Process Management

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

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

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

CSE Opera,ng System Principles

CHAPTER 2: PROCESS MANAGEMENT

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

Concurrency, Thread. Dongkun Shin, SKKU

Computer Systems Laboratory Sungkyunkwan University

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

4. Concurrency via Threads

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

Threads. CS3026 Operating Systems Lecture 06

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

Semantics of fork() and exec()

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

CS370 Operating Systems

Last class: Today: Thread Background. Thread Systems

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

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

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

LSN 13 Linux Concurrency Mechanisms

操作系统原理与设计. 第 4 章 Threads( 线程 ) 陈香兰. March 28, 2014 中国科学技术大学计算机学院. 陈香兰 ( 中国科学技术大学计算机学院 ) 操作系统原理与设计 March 28, /

CS420: Operating Systems. OS Services & System Calls

CSE 4/521 Introduction to Operating Systems. Lecture 29 Windows 7 (History, Design Principles, System Components, Programmer Interface) Summer 2018

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

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

Interoperation of tasks

Creating Threads. Programming Details. COMP750 Distributed Systems

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Chapter 3: Processes

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

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Concurrent Programming. Alexandre David

Transcription:

Chapter 4: Threads 14.1 Silberschatz, Galvin and Gagne 2009

Chapter 4: Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples 14.2 Silberschatz, Galvin and Gagne 2009

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 14.3 Silberschatz, Galvin and Gagne 2009

Single and Multithreaded Processes 14.4 Silberschatz, Galvin and Gagne 2009

Benefits Responsiveness Resource Sharing Economy Scalability 14.5 Silberschatz, Galvin and Gagne 2009

Multithreaded Server Architecture 14.7 Silberschatz, Galvin and Gagne 2009

User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads 14.10 Silberschatz, Galvin and Gagne 2009

Kernel Threads Managed by the Kernel, user space <- -> kernel space switching costly Examples Windows XP/2000 Linux Mac OS X 14.11 Silberschatz, Galvin and Gagne 2009

Multithreading Models Many-to-One One-to-One Many-to-Many 14.12 Silberschatz, Galvin and Gagne 2009

Many-to-One Many user-level threads mapped to single kernel thread Used on the kernels that do not support multithreading 14.13 Silberschatz, Galvin and Gagne 2009

One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later 14.15 Silberschatz, Galvin and Gagne 2009

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 Windows NT/2000 with the ThreadFiber package 14.17 Silberschatz, Galvin and Gagne 2009

Thread Libraries 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 14.21 Silberschatz, Galvin and Gagne 2009

Pthreads May be provided either as user-level or kernel-level A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization Common in UNIX operating systems (Solaris, Linux, Mac OS X) 14.22 Silberschatz, Galvin and Gagne 2009

Pthreads Example All Pthreads programs must include the pthread.h header file. The statement pthread_t tid declares the identifier for the thread we will create. Each thread has a set of attributes, including stack size and scheduling information. A separate thread is created with the pthread_create() function call.

Pthreads Example the program has two threads: the initial (or parent) thread in main() and the summation (or child) thread performing the summation operation in the runner() function. This program follows the fork-join strategy described earlier: after creating the summation thread, the parent thread will wait for it to terminate by calling the pthread join() function. The summation thread will terminate when it calls the function pthread exit(). Once the summation thread has returned, the parent thread will output the value of the shared data sum.

Pthreads Example

Pthreads Example (Cont.)

Windows threads The technique for creating threads using the Windows thread library is similar to the Pthreads technique in several ways. Threads are created in the Windows API using the CreateThread() function, a set of attributes for the thread is passed to this function. We illustrate the Windows thread API in the C program. Notice that we must include the windows.h header file when using the Windows API. NOTE: the DWORD data type is an unsigned 32-bit integer

Windows threads Recall that the POSIX Pthread program had the parent thread wait for the summation thread using the pthread join() statement. We perform the equivalent of this in the Windows API using the WaitForSingleObject() function, which causes the creating thread to block until the summation thread has exited. In situations that require waiting for multiple threads to complete, the WaitForMultipleObjects() function is used.

Win32 threads Example

Win32 API Multithreaded C Program (Cont.)

Threading Issues Thread cancellation of target thread Asynchronous or deferred Thread pools 14.30 Silberschatz, Galvin and Gagne 2009

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 14.32 Silberschatz, Galvin and Gagne 2009

Thread Pools 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. 14.34 Silberschatz, Galvin and Gagne 2009