Computer Systems Laboratory Sungkyunkwan University

Similar documents
THREADS. Jo, Heeseung

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

CSE 153 Design of Operating Systems Fall 2018

ANSI/IEEE POSIX Standard Thread management

Multithreaded Programming

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

Threads. studykorner.org

Processes and Threads

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

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

Chapter 4: Threads. Chapter 4: Threads

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

Pthreads. 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

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

OPERATING SYSTEM. Chapter 4: Threads

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

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

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

POSIX Threads. Paolo Burgio

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

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

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

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

CSE 120 Principles of Operating Systems

Last class: Today: Thread Background. Thread Systems

CSE 120 Principles of Operating Systems

Chapter 5: Threads. Outline

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

CS307: Operating Systems

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

Introduction to PThreads and Basic Synchronization

real time operating systems course

Chapter 4: Multi-Threaded Programming

Threads. CS3026 Operating Systems Lecture 06

Chapter 4: Multithreaded

Thread and Synchronization

POSIX Threads. HUJI Spring 2011

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

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

CSCE 313: Intro to Computer Systems

Lecture 4: Threads; weaving control flow

CS 450 Operating System Week 4 Lecture Notes

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

Chapter 3 Process Description and Control

Chapter 4: Multithreaded Programming

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

CSE 120 Principles of Operating Systems

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

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

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

Chapter 4: Threads. Chapter 4: Threads

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

Chapter 4 Multithreaded Programming

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

Multithreading Programming II

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

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

Yi Shi Fall 2017 Xi an Jiaotong University

CISC2200 Threads Spring 2015

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

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

Chapter 3: Processes. Operating System Concepts 8th Edition

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

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

Lecture 4 Threads. (chapter 4)

Threads. Jo, Heeseung

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

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

CS 318 Principles of Operating Systems

CS510 Operating System Foundations. Jonathan Walpole

CS 326: Operating Systems. Process Execution. Lecture 5

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

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

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

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

CS333 Intro to Operating Systems. Jonathan Walpole

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

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

Threads. Threads (continued)

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

CSE 4/521 Introduction to Operating Systems

4. Concurrency via Threads

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

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

Concurrency and Threads

Processes and Threads

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

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

A Brief Introduction to OS/2 Multithreading

4.8 Summary. Practice Exercises

From Processes to Threads

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

CS 318 Principles of Operating Systems

Concurrent Server Design Multiple- vs. Single-Thread

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

Transcription:

Threads Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

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 info. 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) { /* Handle client request */ } else { /* Close socket */ } 4

Cooperating Processes Example A web server, which forks off copies of itself to handle multiple simultaneous tasks Any parallel l program on a multiprocessor We need to: Create several processes that execute in parallel l 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 a thread of control, a thread, or a lightweight process (LWP) 7

Key Idea (2) 8

Key Idea (3) 9

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. 10

Concurrent Servers: Threads Using threads We can create a new thread for each request. webserver () { While (1) { int sock = accept(); thread_fork (handle_request, sock); } } handle_request (int sock) { /* Process request */ close (sock); } 11

Multithreading Benefits Creating concurrency is cheap. Improves program structure. Throughput By overlapping computation with I/O operations Responsiveness (User interface / Server) Can handle concurrent events (e.g., web servers) Resource sharing Economy Utilization of multiprocessor architectures Allows building parallel programs. 12

Processes vs. Threads 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. Processes become static, threads are the dynamic entities. 13

Process Address Space 0xFFFFFFFF stack (dynamic allocated mem) SP address space 0x00000000 heap (dynamically allocated mem) static data (data segment) code (text segment) PC 14

Address Space with Threads 0xFFFFFFFF thread 1 stack SP (T1) SP thread 2 stack SP (T2) thread 3 stack SP (T3) address space heap (dynamically allocated mem) 0x00000000 PC static data (data segment) code (text t segment) PC (T2) PC (T1) PC (T3) 15

Classification f addr aces: # threads per One Many addr space: # of spa One Many MS/DOS Early Macintosh Many embedded Oses (VxWorks, uclinux,..) Traditional UNIX Mach, OS/2, Linux, Windows, Mac OS X, Solaris, HP UX 16

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. 17

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 18

Pthreads (1) Thread creation/termination int pthread_create (pthread_tt *tid, pthread_attr_t *attr, void *(start_routine)(void *), void *arg); void pthread_exit exit (void *retval); int pthread_join (pthread_tt tid, void **thread_return); 19

Pthreads (2) Mutexes int pthread_mutex_init (pthread_mutex_t 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); 20

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 t *cond, pthread_mutex_t *mutex); void pthread _ cond_ signal (pthread_cond_t *cond); void pthread_cond_broadcast (pthread_cond_t *cond); 21

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. 22

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. 23

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 tblocking 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 24

Threading Issues (4) Using libraries errno Each thread should have its own independent version of the errno variable. Multithread-safe (MT-safe) A set of functions is said to be multithread-safe or reentrant, t when the functions may be called by more than one thread at a time without requiring any other action on the caller s part. Pure functions that access no global data or access only read-only global data are trivially MT-safe safe. Functions that modify global state must be made MT-safe by synchronizing access to the shared data. 25