THREADS. Jo, Heeseung

Similar documents
Computer Systems Laboratory Sungkyunkwan University

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

LSN 13 Linux Concurrency Mechanisms

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

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

Multithreaded Programming

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

CSE 153 Design of Operating Systems Fall 2018

Processes and Threads

Threads. studykorner.org

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

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

Chapter 4: Threads. Chapter 4: Threads

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

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

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

POSIX Threads. Paolo Burgio

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

OPERATING SYSTEM. Chapter 4: Threads

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

Threads. Jo, Heeseung

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

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

POSIX Threads. HUJI Spring 2011

Introduction to PThreads and Basic Synchronization

ANSI/IEEE POSIX Standard Thread management

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

CS307: Operating Systems

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

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

real time operating systems course

Multithreading Programming II

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

Chapter 4: Multithreaded

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

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

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

Thread and Synchronization

CS 450 Operating System Week 4 Lecture Notes

Chapter 4: Multi-Threaded Programming

Threads need to synchronize their activities to effectively interact. This includes:

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

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

Threads. Jo, Heeseung

Threads. CS3026 Operating Systems Lecture 06

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

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

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

CS510 Operating System Foundations. Jonathan Walpole

CS 326: Operating Systems. Process Execution. Lecture 5

Last class: Today: Thread Background. Thread Systems

Chapter 5: Threads. Outline

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

Chapter 4: Multithreaded Programming

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

Concurrent Programming

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

Chapter 4 Multithreaded Programming

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

Chapter 4: Threads. Chapter 4: Threads

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

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

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

Threads. Threads (continued)

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

CS333 Intro to Operating Systems. Jonathan Walpole

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

CISC2200 Threads Spring 2015

Introduction to pthreads

CSE 120 Principles of Operating Systems

Lecture 4 Threads. (chapter 4)

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

Concurrent Server Design Multiple- vs. Single-Thread

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

Yi Shi Fall 2017 Xi an Jiaotong University

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

CSE 120 Principles of Operating Systems

From Processes to Threads

High Performance Computing Course Notes Shared Memory Parallel Programming

Concurrency and Threads

Concurrent Programming

CS 3305 Intro to Threads. Lecture 6

Concurrency, Thread. Dongkun Shin, SKKU

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

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

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

Chapter 3: Processes. Operating System Concepts 8th Edition

A Brief Introduction to OS/2 Multithreading

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

CS 5523 Operating Systems: Thread and Implementation

Threads. lightweight processes

POSIX PTHREADS PROGRAMMING

Programming with Shared Memory. Nguyễn Quang Hùng

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

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

Chapter 5: Threads. Single and Multithreaded Processes

Transcription:

THREADS Jo, Heeseung

TODAY'S TOPICS Why threads? Threading issues 2

PROCESSES Heavy-weight A process includes many things: - An address space (all the code and data pages) - OS resources (e.g., open files) and accounting information - Hardware execution state (PC, SP, registers, etc.) Creating a new process is costly because all of the data structures must be allocated and initialized - Linux: over 100 fields in task_struct (excluding page tables, etc.) Inter-process communication is costly, since it must usually go through the OS - Overhead of system calls and copying data 3

CONCURRENT SERVERS: PROCESSES Web server example Using fork() to create new processes to handle requests in parallel is overkill for such a simple task while (1) { } int sock = accept(); if ((pid = fork()) == 0) { } else { } /* Handle client request */ /* Close socket */ 4

COOPERATING PROCESSES Example A web server, which forks off copies of itself to handle multiple simultaneous tasks Any parallel program on a multiprocessor We need to: Create several processes that execute in parallel Cause each to map the same address space to share data - e.g., shared memory Have the OS schedule these processes in parallel This is very inefficient! Space: PCB, page tables, etc. Time: creating OS structures, fork and copy address space, etc. 5

RETHINKING PROCESSES What's similar in these cooperating processes? They all share the same code and data (address space) They all share the same privilege They all share the same resources (files, sockets, etc.) What's different? Each has its own hardware execution state: PC, registers, SP, and stack 6

KEY IDEA (1) Separate the concept of a process from its execution state Process: address space, resources, other general process attributes - e.g., privileges Execution state: PC, SP, registers, etc. This execution state is usually called - Thread - Lightweight process (LWP) - Thread of control 7

Key Idea (2) 8

KEY IDEA (3) Each thread has its own stack 9

KEY IDEA (4) Each thread has its own stack 10

WHAT IS A THREAD? A thread of control (or a thread) A sequence of instructions being executed in a program Usually consists of - A program counter (PC) - A stack to keep track of local variables and return addresses - Registers Threads share the process instructions and most of its data - A change in shared data by one thread can be seen by the other threads in the process Threads also share most of the OS state of a process 11

CONCURRENT SERVERS: THREADS Using threads We can create a new thread for each request webserver () { while (1) { int sock = accept(); create_thread (handle_request, sock); } } handle_request (int sock) { /* Process request */ close (sock); } 12

MULTITHREADING Benefits Creating concurrency is cheap - Time and memory consumption Improves program structure Higher throughput - By overlapping computation with I/O operations Better responsiveness (User interface / Server) - Can handle concurrent events (e.g., web servers) Better resource sharing Utilization of multiprocessor architectures - Allows building parallel programs 13

PROCESSES VS. THREADS (1) Processes vs. Threads A thread is bound to a single process A process, however, can have multiple threads Sharing data between threads is cheap - All see the same address space Threads become the unit of scheduling Processes are now containers in which threads execute 14

PROCESSES VS. THREADS (2) How threads and processes are similar Each has its own logical control flow Each can run concurrently with others (possibly on different cores) Each is context switched How threads and processes are different Threads share code and some data - Processes (typically) do not Threads are somewhat less expensive than processes - Process control (creating and reaping) is twice as expensive as thread control - Linux numbers: ~20K cycles to create and reap a process ~10K cycles (or less) to create and reap a thread 15

PROCESS ADDRESS SPACE 0xFFFFFFFF stack (dynamically allocated mem) SP address space 0x00000000 heap (dynamically allocated mem) static data (data segment) code (text segment) PC 16

ADDRESS SPACE WITH THREADS 0xFFFFFFFF thread 1 stack SP (T1) SP thread 2 stack thread 3 stack SP (T2) SP (T3) address space heap (dynamically allocated mem) static data (data segment) 0x00000000 PC code (text segment) PC (T2) PC (T1) PC (T3) 17

CLASSIFICATION # of addr spaces # threads per addr space One Many One MS-DOS Early Macintosh Many embedded OSes VxWorks uclinux Many Traditional UNIX Mach OS/2 Linux Windows Mac OS X Solaris HP-UX 18

THREADS INTERFACE (1) pthreads 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 19

THREADS INTERFACE (2) POSIX-style threads pthreads DCE threads (early version of pthreads) Unix International (UI) threads (Solaris threads) - Sun Solaris 2, SCO Unixware 2 Microsoft-style threads Win32 threads - Microsoft Windows 98/NT/2000/XP OS/2 threads - IBM OS/2 20

PTHREADS (1) Thread creation/termination int pthread_create (pthread_t *tid, pthread_attr_t *attr, void *(start_routine)(void *), void *arg); void pthread_exit (void *retval); int pthread_join (pthread_t tid, void **thread_return); 21

PTHREADS (2) Mutexes int pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *mattr); void pthread_mutex_destroy (pthread_mutex_t *mutex); void pthread_mutex_lock (pthread_mutex_t *mutex); void pthread_mutex_unlock (pthread_mutex_t *mutex); 22

PTHREADS (3) Condition variables int pthread_cond_init (pthread_cond_t *cond, const pthread_condattr_t *cattr); void pthread_cond_destroy (pthread_cond_t *cond); void pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex); void pthread_cond_signal (pthread_cond_t *cond); void pthread_cond_broadcast (pthread_cond_t *cond); 23

THREADING ISSUES (1) fork() and exec() When a thread calls fork(), Does the new process duplicate all the threads? Is the new process single-threaded? Some UNIX systems support two versions of fork() In pthreads, - fork() duplicates only a calling thread In the Unix international standard, - fork() duplicates all parent threads in the child - fork1() duplicates only a calling thread Normally, exec() replaces the entire process 24

THREADING ISSUES (2) Thread cancellation The task of terminating a thread before it has completed Asynchronous cancellation Terminates the target thread immediately What happens if the target thread is holding a resource, or it is in the middle of updating shared resources? Deferred cancellation The target thread is terminated at the cancellation points The target thread periodically check if it should be cancelled pthreads API supports both asynchronous and deferred cancellation 25

THREADING ISSUES (3) Signal handling Where should a signal be delivered? To the thread to which the signal applies for synchronous signals To every thread in the process To certain threads in the process Typically only to a single thread found in a process that is not blocking the signal pthreads: per-process pending signals, per-thread blocked signal mask Assign a specific thread to receive all signals for the process Solaris 2 26

THREADING ISSUES (4) Using libraries errno - Each thread should have its own independent version of the errno variable 27

THREADING ISSUES (4) Multithread-safe (MT-safe) A set of functions can be said to be multithread-safe or reentrant, when the functions may be called by more than one thread at a time Functions that access no global data or read-only global data are trivially MT-safe Functions that modify global state must be made MT-safe by synchronizing access to the shared data Threads can have private global variables 28