Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Similar documents
THREADS. Jo, Heeseung

Computer Systems Laboratory Sungkyunkwan University

LSN 13 Linux Concurrency Mechanisms

ANSI/IEEE POSIX Standard Thread management

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

Threads. studykorner.org

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

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

Threads. Jo, Heeseung

Thread and Synchronization

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Threads Implementation. Jo, Heeseung

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

pthreads CS449 Fall 2017

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

POSIX Threads. Paolo Burgio

Threads. Threads (continued)

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

Threads. Jo, Heeseung

Multithreading Programming II

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

Introduction to PThreads and Basic Synchronization

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

POSIX Threads. HUJI Spring 2011

real time operating systems course

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

Concurrent Programming

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

CS 3305 Intro to Threads. Lecture 6

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

CS333 Intro to Operating Systems. Jonathan Walpole

Embedded Systems: OS

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

CS 5523 Operating Systems: Thread and Implementation

Multithreaded Programming

Computer Systems Laboratory Sungkyunkwan University

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

Lecture 19: Shared Memory & Synchronization

CS Operating Systems: Threads (SGG 4)

Threads. lightweight processes

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

Chapter 4 Concurrent Programming

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Introduction to pthreads

C Grundlagen - Threads

Parallel Programming with Threads

Concurrent Programming

CS510 Operating System Foundations. Jonathan Walpole

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

POSIX PTHREADS PROGRAMMING

Concurrent Server Design Multiple- vs. Single-Thread

Quiz: Simple Sol Threading Model. Pthread: API and Examples Synchronization API of Pthread IPC. User, kernel and hardware. Pipe, mailbox,.

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

Posix Threads (Pthreads)

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

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

Concurrency. Johan Montelius KTH

A Brief Introduction to OS/2 Multithreading

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

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

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Processes and Threads

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

CS 3723 Operating Systems: Final Review

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

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

Concurrency and Threads

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

CSE 333 SECTION 9. Threads

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

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

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

CSE 333 Section 9 - pthreads

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

CS510 Operating System Foundations. Jonathan Walpole

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

TCSS 422: OPERATING SYSTEMS

Threads. CS3026 Operating Systems Lecture 06

Process Synchronization

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

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

Oct 2 and 4, 2006 Lecture 8: Threads, Contd

Pthreads: POSIX Threads

Operating systems fundamentals - B06

SUPPORTING NATIVE PTHREADS IN SYSCALL EMULATION MODE BRANDON POTTER JUNE 14 TH, 2015

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

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

CS 261 Fall Mike Lam, Professor. Threads

Introduc)on to pthreads. Shared memory Parallel Programming

Introduction to parallel computing

Threads (SGG 4) Outline. Traditional Process: Single Activity. Example: A Text Editor with Multi-Activity. Instructor: Dr.

für Mathematik in den Naturwissenschaften Leipzig

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

Review: Easy Piece 1

High Performance Computing Course Notes Shared Memory Parallel Programming

4. Concurrency via Threads

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

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

Synchronization. Semaphores implementation

Transcription:

Threads Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu)

Concurrency Virtualization Virtual CPUs Virtual memory Concurrency In the user space by running multi-threaded programs In the kernel space too! OS Issues How to support multi-threaded programs? How to coordinate accesses to shared resources? EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 2

Motivation Process is a cool abstraction to run a new program OS provides protection and isolation among processes But, A single process cannot benefit from multi-cores Very cumbersome to write a program with many cooperating processes Expensive to create a new process High communication overheads between processes Expensive context switching between processes How can we increase concurrency within a process cheaply? EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 3

What is a Thread? A thread of control: A sequence of instructions being executed in a program A thread has its own Thread ID Set of registers including PC & SP Stack Threads share an address space Separate the concept of a process from its execution state EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 4

Using Threads #include <stdio.h> #include <pthread.h> void *hello (void *arg) { printf ( hello, world\n );... } int main() { pthread_t tid; } pthread_create (&tid, NULL, hello, NULL); printf ( hello from main thread\n );... EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 5

Address Space with Threads PC (T2) PC (T1) PC (T3) Code Data SP (T2) SP (T3) SP (T1) Heap Stack Stack Stack code data program EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 6

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 are the unit of scheduling Processes are containers in which threads execute PID, Address space, user and group ID, open file descriptors, current working directory, etc. Processes are static, while threads are the dynamic entities EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 7

Benefits of Multi-threading Creating concurrency is cheap Improves program structure Divide large task across several cooperative threads Throughput By overlapping computation with I/O operations Responsiveness Can handle concurrent events (e.g. web servers) Resource sharing Utilization of multi-core architectures Allows building parallel programs EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 8

OS Classification # threads per addr space: One Many # of addr spaces: One MS/DOS Early Macintosh Many embedded OSes (VxWorks, uclinux,..) Many Traditional UNIX Xv6 Mach, OS/2, Linux, Windows, Mac OS X, Solaris, HP-UX EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 9

Threads Interface Pthreads (POSIX Threads) A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies the behavior of the thread library Implementation is up to development of the library Common in Unix-like operating systems Linux, Mac OS X, Solaris, FreeBSD, NetBSD, OpenBSD, etc. Microsoft Windows has its own Thread API Win32/Win64 threads EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 10

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); EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 11

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); EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 12

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); EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 13

Kernel-level Threads OS-managed threads OS manages threads and processes All thread operations are implemented in the kernel Thread creation and management requires system calls OS schedules all the threads Creating threads are cheaper than creating processes Windows, Linux, Solaris, Mac OS X, AIX, HP-UX, EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 14

Kernel-level Threads A's thread 1 Process A Process B Context switch A's thread 2 Context switch B's Thread 1 User-mode Kernel-mode EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 15

Kernel-level Threads: Limitations They can still be too expensive Thread operations are all system calls Must maintain kernel state for each thread Can place limit on the number of simultaneous threads OS must scale well with increasing number of threads Kernel-level threads have to be general to support the needs of all programmers, languages, runtime systems, etc. EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 16

User-level Threads Threads are implemented at the user level A library linked into the program manages the threads Threads are invisible to the OS All the thread operations are done via procedure calls (no kernel involvement) Small and fast: 10-100x faster than kernel-level threads Portable Tunable to meet application needs EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 17

User-level Threads: Limitations Usually, rely on non-preemptive scheduling Preemptive scheduling can be emulated using Unix signals OS can make poor decisions as it is not aware of userlevel threads Scheduling a process with only idle threads Blocking the entire process when a thread initiates I/O Unscheduling a process with a thread holding a lock All blocking system calls should be emulated in the library via non-blocking calls to the kernel Requires coordination between kernel and thread manager Cannot leverage multi-core CPUs EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 18

User-level Threads A's thread 1 Process A Process B User-level thread library A's thread 2 Context switch B's Thread 1 Context switch A's thread 2 User-mode Kernel-mode EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 19