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

Similar documents
CS307: Operating Systems

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

OPERATING SYSTEM. Chapter 4: Threads

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

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

Multithreaded Programming

CISC2200 Threads Spring 2015

Chapter 5: Threads. Single and Multithreaded Processes

Chapter 4: Multithreaded

Chapter 4: Threads. Chapter 4: Threads

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

Operating Systems Prof. Ashok K Agrawala

CS 450 Operating System Week 4 Lecture Notes

Lecture 4 Threads. (chapter 4)

Chapter 4: Multi-Threaded Programming

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

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

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

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

Chapter 4: Multithreaded Programming

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

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

Chapter 3: Processes. Operating System Concepts 8th Edition

Processes and Threads

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

CS370 Operating Systems

Threads. studykorner.org

CS420: Operating Systems

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

Definition Multithreading Models Threading Issues Pthreads (Unix)

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4 Multithreaded Programming

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

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

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

CSE 4/521 Introduction to Operating Systems

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

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

CS370 Operating Systems

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

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

Chapter 4: Multithreaded Programming

Processes and Threads

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

CS370 Operating Systems

4.8 Summary. Practice Exercises

Lecture 2 Process Management

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

Semantics of fork() and exec()

Concurrency, Thread. Dongkun Shin, SKKU

CS370 Operating Systems

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

THREADS. Jo, Heeseung

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

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

Chapter 5: Threads. Outline

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

CS 3305 Intro to Threads. Lecture 6

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

Threads. CS3026 Operating Systems Lecture 06

Computer Systems Laboratory Sungkyunkwan University

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

CHAPTER 2: PROCESS MANAGEMENT

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

Threads. Today. Next time. Why threads? Thread model & implementation. CPU Scheduling

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

CSE Opera,ng System Principles

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

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Last class: Today: Thread Background. Thread Systems

Threads. To do. Why threads? Thread model & implementation. q q q. q Next time: Synchronization

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

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

CS510 Operating System Foundations. Jonathan Walpole

Processes. Process Concept

York University Lassonde School of Engineering Department of Electrical Engineering and Computer Science

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

Chapter 4: Processes. Process Concept

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

CPU Scheduling. Basic Concepts. Histogram of CPU-burst Times. Dispatcher. CPU Scheduler. Alternating Sequence of CPU and I/O Bursts

Chapter 4: Processes

CS333 Intro to Operating Systems. Jonathan Walpole

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

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

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Process Description and Control

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1

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

Operating Systems & Concurrency: Process Concepts

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Part Two - Process Management. Chapter 3: Processes

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

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

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

Transcription:

CS307 What is a thread? Threads A thread is a basic unit of CPU utilization contains a thread ID, a program counter, a register set, and a stack shares with other threads belonging to the same process code section data section other operating-system resources, such as open files Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2018 2 Single and Multithreaded Processes Motivation Threads run within application Multiple tasks with the application can be implemented by separating threads Update display Fetch data Spell checking Answer a network request Process creation is heavy-weight while thread creation is light-weight Increase efficiency of C-S applications Kernels are generally multithreaded 3 4 Benefits Parallel Execution on a Multi-core System Responsiveness A program continues running even if part of it is blocked or is performing a lengthy operation Resource Sharing Threads share the memory and the resources of the process to which they belong IPC techniques are not needed Economy Creating a thread is much faster than creating a process Scalability Multithreading on a multi-cpu machine increases concurrency multi-core 5 6 1

Drawbacks Process vs. Thread Make the programming more complicated Make the debugging harder Possible error when threads concurrently access the shared resources Poorly divided jobs can cause even worse system performance... Process 1. independent 2. carries considerably more state information 3. has separate address 4. interact only through IPC 5. context switching is relatively slow Thread 1. exists as subsets of a process 2. shares process state as well as memory and other resources 3. shares process s address 4. more ways to communicate 5. context switching in the same process is typically faster 7 8 Supports for Threads Thread Model Kernel Threads Supported by the operating system kernel Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads User thread Lightweight process Kernel thread 9 10 Multithreading Models Many-to-One Model Four common connections between user threads and kernel threads Many-to-One One-to-One Many-to-Many Two-Level Model Many user-level threads are mapped to a single kernel thread Strength Multiple threads are hidden by user-level thread library Weaknesses The entire process will block if a thread makes a blocking system call Multiple threads are unable to run in parallel on multiprocessors : Solaris Green Threads GNU Portable Threads 11 12 2

One-to-One Each user-level thread is mapped to a kernel thread Strength More concurrency Weakness Creating a user thread requires creating the corresponding kernel thread, which incurs overhead Windows NT/XP/2000 Linux Solaris 9 and later Many-to-Many Model Allows many user level threads to be mapped to many kernel threads The operating system creates a sufficient number of kernel threads Windows NT/2000 with the ThreadFiber package 13 14 Two-Level Model Thread Libraries Similar to Many-to-Many, except that it allows a user thread to be bound to a kernel thread IRIX HP-UX Tru64 UNIX Solaris 8 and earlier Thread library provides programmer with API for creating and managing threads Two primary ways of implementation User-level threads library All codes and data structures for the library exist in user Invoking a function in the library results in a local function call in user Kernel-level threads library supported by the OS Code and data structures for the library exist in kernel Invoking a function in the library results in a system call to the kernel Three primary thread libraries: POSIX Pthreads, Win32 threads, Java threads 15 16 Pthreads Example Using Pthreads Is provided either in 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) #include <pthread.h> #include <stdio.h> int sum; /* this data is shared by the thread(s) */ /* The thread will begin control in this function */ void *runner(void *param); int i, upper = atoi(param); sum = 0; for (i = 1; i <= upper; i++) sum += i; pthread_exit ( 0) ; 17 18 3

Example Using Pthreads (Cont.) int main(int argc, char *argv[]) pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ /* get the default attributes */ pthread_attr_init (&attr); /* create the thread */ pthread_create(&tid, &attr, runner, argv[l]) ; /* wait for the thread to exit */ pthread_join(tid, NULL) ; Threading Issues Semantics of fork() and exec() system calls Does fork() duplicate only the calling thread or all threads? exec() will replace the entire process with the program specified in the parameter Thread cancellation of target thread 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. printf (" sum = %d\n", sum) ; 19 20 Threading Issues (Cont.) Threading Issues (Cont.) Signal handling Signals are used in UNIX systems to notify a process that a particular event has occurred. Synchronous and asynchronous 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 Delivery 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 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 Thread-specific data Create Facility needed for data private to thread Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool) Scheduler activations Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application 21 22 Operating System Examples Linux Threads Linux Thread Windows XP Threads fork() and clone() system calls clone() takes options to determine sharing on process create struct task_struct points to process data structures (shared or unique) 23 24 4

Windows XP Threads Windows XP Threads Data Structures Implements the one-to-one mapping, kernel-level 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) KTHREAD (kernel thread block) TEB (thread environment block) 25 26 Pop-quiz Homework int value = 0; void *runner(void *param) value = 5; pthread_exit ( 0) ; int main(int argc, char *argv[]) int pid; pthread_t tid; pthread_attr_t attr; pid = fork(); if (pid == 0) pthread_attr_init (&attr) ; pthread_create(&tid, &attr, runner, NULL); pthread_join(tid,null) ; printf( Child: value = %d", value); else if (pid > 0) wait (NULL) ; printf( Parent: value = %d", value); Reading: Chapter 4 What are the outputs from the above program? 27 28 5