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

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

CS 261 Fall Mike Lam, Professor. Threads

CS 3305 Intro to Threads. Lecture 6

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

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

CS510 Operating System Foundations. Jonathan Walpole

High Performance Computing Course Notes Shared Memory Parallel Programming

User Space Multithreading. Computer Science, University of Warwick

pthreads CS449 Fall 2017

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

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

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

CS533 Concepts of Operating Systems. Jonathan Walpole

Multicore and Multiprocessor Systems: Part I

Threads Tuesday, September 28, :37 AM

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

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

CS333 Intro to Operating Systems. Jonathan Walpole

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Introduction to PThreads and Basic Synchronization

Intro to POSIX Threads with FLTK

Chapter 4 Concurrent Programming

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

Parallel Programming with Threads

Threads. Threads (continued)

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

CSE 333 SECTION 9. Threads

CSE 306/506 Operating Systems Threads. YoungMin Kwon

A Brief Introduction to OS/2 Multithreading

Thread and Synchronization

Concurrent Programming

Concurrent Programming

Carnegie Mellon Concurrency and Synchronization

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Parallel Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Introduction to Threads

Concurrency, Thread. Dongkun Shin, SKKU

Concurrent Programming

Threads. studykorner.org

Synchronization: Basics

Concurrent Server Design Multiple- vs. Single-Thread

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

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

Computer Systems Laboratory Sungkyunkwan University

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

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Threaded Programming. Lecture 9: Alternatives to OpenMP

Shared Memory Programming with Pthreads. T. Yang. UCSB CS240A.

TCSS 422: OPERATING SYSTEMS

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

Processes, Threads, SMP, and Microkernels

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

POSIX Threads: a first step toward parallel programming. George Bosilca

LSN 13 Linux Concurrency Mechanisms

Multiprocessors 2007/2008

CMPSC 311- Introduction to Systems Programming Module: Concurrency

PThreads in a Nutshell

Threading Language and Support. CS528 Multithreading: Programming with Threads. Programming with Threads

PRACE Autumn School Basic Programming Models

Introduction to pthreads

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Multithreading Programming II

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

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

Multithreading and Interactive Programs

Concurrent Programming

Dealing with Issues for Interprocess Communication

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

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

Chapter 2 Processes and Threads

Overview. Thread Packages. Threads The Thread Model (1) The Thread Model (2) The Thread Model (3) Thread Usage (1)

Synchronization: Basics

Survey feedback. 2/26/08 CSE 30341: Operating Systems Principles

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Multiprocessors and Locking

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Synchronization Principles

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

ENCM 501 Winter 2019 Assignment 9

Shared Memory Programming. Parallel Programming Overview

CS510 Operating System Foundations. Jonathan Walpole

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

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

An Introduction to Parallel Systems

POSIX Threads. HUJI Spring 2011

CS 153 Design of Operating Systems Winter 2016

Concurrent Programming

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

ANSI/IEEE POSIX Standard Thread management

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

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

Shared Memory Programming: Threads and OpenMP. Lecture 6

CS516 Programming Languages and Compilers II

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

Transcription:

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

CS 470 Spring 2018 POSIX Mike Lam, Professor Multithreading & Pthreads

MIMD system architectures Shared memory Distributed memory

Multithreading A process is an instance of a running program Private address space, shared files/sockets A thread is a single unit of execution Private stack/registers, shared address space Multithreading libraries provide thread management Spawn/kill capabilities Synchronization mechanisms POSIX threads: Pthreads

POSIX threads Pthreads POSIX standard interface for threads in C pthread_create: spawn a new thread pthread_t struct for storing thread info attributes (or NULL) thread work routine (function pointer) thread routine parameter (void*) pthread_self: get current thread ID pthread_exit: terminate current thread can also terminate implicitly by returning from the thread routine pthread_join: wait for another thread to terminate

Thread creation example #include <stdio.h> #include <pthread.h> main() main void* work (void* arg) { printf("hello from new thread!\n"); return NULL; } int main () { printf("spawning new thread...\n"); pthread_t peer; pthread_create(&peer, NULL, work, NULL); pthread_join(peer, NULL); printf("done!\n"); work() peer create() join() } return 0;

Shared memory Some data is shared in threaded programs Global variables (shared, single static copy) Local variables (multiple copies, one on each stack) Technically still shared if in memory, but harder to access Not shared if cached in register Safer to assume they're private Local static variables (shared, single static copy)

Issues with shared memory Nondeterminism Data races and deadlock thread1 thread2 foo: irmovq x, %rcx irmovq 7, %rax mrmovq (%rcx), %rdx addq %rax, %rdx rmmovq %rdx, (%rcx) ret foo() x:.quad 0

Issues with shared memory Nondeterminism Data races and deadlock thread1 thread2 foo: irmovq x, %rcx irmovq 7, %rax mrmovq (%rcx), %rdx addq %rax, %rdx rmmovq %rdx, (%rcx) ret x:.quad 0 irmovq x, %rcx irmovq 7, %rax mrmovq (%rcx), %rdx addq %rax, %rdx rmmovq %rdx, (%rcx) ret foo() irmovq x, %rcx irmovq 7, %rax mrmovq (%rcx), %rdx addq %rax, %rdx rmmovq %rdx, (%rcx) ret This interleaving is ok.

Issues with shared memory Nondeterminism Data races and deadlock thread1 thread2 foo: irmovq x, %rcx irmovq 7, %rax mrmovq (%rcx), %rdx addq %rax, %rdx rmmovq %rdx, (%rcx) ret x:.quad 0 irmovq x, %rcx irmovq 7, %rax mrmovq (%rcx), %rdx addq %rax, %rdx rmmovq %rdx, (%rcx) ret foo() irmovq x, %rcx irmovq 7, %rax mrmovq (%rcx), %rdx addq %rax, %rdx rmmovq %rdx, (%rcx) ret PROBLEM!

Issues with shared memory Nondeterminism Incorrect code can produce correct results Test suites cannot guarantee correctness! Data races Deadlock Starvation

Synchronization mechanisms Busy-waiting (wasteful!) Atomic instructions (e.g., LOCK prefix in x86) Pthreads Mutex: simple mutual exclusion ( lock ) Condition variable: lock + wait set (wait/signal/broadcast) Semaphore: access to limited resources Not technically part of Pthreads library (just the POSIX standard) Barrier: ensure all threads are at the same point Not present in all implementations (requires --std=gnu99 on cluster) Java threads Synchronized keyword: implicit mutex Monitor: lock on object (wait/notify/notifyall)

Common synchronization patterns Naturally ( embarrassingly ) parallel No synchronization! Mutual exclusion Use a lock Producer/consumer Protect common buffer w/ lock Readers/writers Multiple lock types Dining philosophers Atomic acquisition of multiple locks

Synchronization granularity Granularity: level at which a structure is locked Whole structure vs. individual pieces If individual pieces, which pieces? Simple locks vs. read/write locks Tradeoff: coarse (lower granularity) vs. fine-grained (higher granularity) locks

Caching effects Caching Keep frequently-used stuff in faster memory Cache line Single unit of cached data Cache hits/misses Was data in cache? (if so, hit; if not, miss) Cache invalidation Writes to one cache can render another cache out-of-date False sharing Unnecessary cache invalidation

Multithreading summary Shared memory parallelism has a lot of benefits Low overhead for thread creation/switching Uniform memory access times (symmetric multiprocessing) It also has significant issues Limited scaling (# of cores) Requires explicit thread management Requires explicit synchronization (HARD!) Caching problems can be difficult to diagnose Core design tradeoff: synchronization granularity Higher granularity: simpler but slower Lower granularity: more complex but faster