Introduction to pthreads

Similar documents
CS 326: Operating Systems. Process Execution. Lecture 5

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

pthreads CS449 Fall 2017

CSE 333 SECTION 9. Threads

Threads. Threads (continued)

CS 3305 Intro to Threads. Lecture 6

Introduction to PThreads and Basic Synchronization

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

Threads. studykorner.org

CS333 Intro to Operating Systems. Jonathan Walpole

Threaded Programming. Lecture 9: Alternatives to OpenMP

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

LSN 13 Linux Concurrency Mechanisms

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

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

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

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

CS510 Operating System Foundations. Jonathan Walpole

Concurrency. Johan Montelius KTH

POSIX PTHREADS PROGRAMMING

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

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

Multithreaded Programming

Pthread (9A) Pthread

ENCM 501 Winter 2019 Assignment 9

CS 220: Introduction to Parallel Computing. Condition Variables. Lecture 24

Last class: Today: Thread Background. Thread Systems

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Operating systems fundamentals - B06

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

THREADS. Jo, Heeseung

Creating Threads. Programming Details. COMP750 Distributed Systems

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

POSIX Threads. HUJI Spring 2011

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

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

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

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

CS510 Operating System Foundations. Jonathan Walpole

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

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

Concurrency, Thread. Dongkun Shin, SKKU

PThreads in a Nutshell

Threads. CS3026 Operating Systems Lecture 06

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

Processes and Threads

CS 261 Fall Mike Lam, Professor. Threads

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Multicore and Multiprocessor Systems: Part I

Threads (light weight processes) Chester Rebeiro IIT Madras

TCSS 422: OPERATING SYSTEMS

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CSE 333 Section 9 - pthreads

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

CSE 306/506 Operating Systems Threads. YoungMin Kwon

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

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

Computer Systems Laboratory Sungkyunkwan University

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

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

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

Threads. lightweight processes

High Performance Computing Course Notes Shared Memory Parallel Programming

Lecture 4 Threads. (chapter 4)

Section 4: Threads and Context Switching

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

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

CS370 Operating Systems

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

CS370 Operating Systems

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

This resource describes how to program the myrio in C to perform timer interrupts.

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

CISC2200 Threads Spring 2015

CS307: Operating Systems

Concurrency and Threads

ECE 598 Advanced Operating Systems Lecture 23

Creating Threads COMP755

Threads. Jo, Heeseung

Chapter 4: Threads. Chapter 4: Threads

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

CS533 Concepts of Operating Systems. Jonathan Walpole

User Space Multithreading. Computer Science, University of Warwick

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

CS444 1/28/05. Lab 03

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

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

Operating Systems & Concurrency: Process Concepts

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

Concurrent Server Design Multiple- vs. Single-Thread

Computer Science 432/563 Operating Systems The College of Saint Rose Spring Topic Notes: Processes and Threads

Transcription:

CS 220: Introduction to Parallel Computing Introduction to pthreads Lecture 25

Threads In computing, a thread is the smallest schedulable unit of execution Your operating system has a scheduler that decides when threads/processes will run Threads are essentially lightweight processes In MPI, we duplicated our processes and ran them all at the same time (in parallel) With pthreads, a single process manages multiple threads 4/16/18 CS 220: Parallel Computing 2

Why Learn Threads? Threads are the most important concept you will learn in this class In the past, you could get away with writing serial programs Today, we live in a world of asynchronous, multithreaded code Crucial for building fast, efficient applications 4/16/18 CS 220: Parallel Computing 3

MPI: Photocopier MPI executes multiple processes in parallel When we specify -n 8, we ll get 8 processes MPI will also distribute them across machines Each process gets a unique rank Helps us divide workload 4/16/18 CS 220: Parallel Computing 4

Threads: 3D Printer Each thread can be unique and do something different Or you can make many threads that all do the same thing More flexible than MPI Also can be more difficult to manage * Note: I may be slightly overselling threads here 4/16/18 CS 220: Parallel Computing 5

Posix Threads pthreads is short for POSIX Threads POSIX - Portable Operating System Interface POSIX is an operating system standard that helps ensure compatibility across systems If your program conforms to the POSIX standard, then it ll compile and run on any compliant OS For instance, we can compile your C programs on macos, Linux, FreeBSD, Solaris, and more 4/16/18 CS 220: Parallel Computing 6

What are Threads? Lightweight processes Created by processes to do some subset of the work Rather than passing messages, threads use shared memory. All the threads have access to internal variables, whereas with MPI we had to explicitly send our state information to another process 4/16/18 CS 220: Parallel Computing 7

Common Uses for Threads (1/2) You may want your program to do two things at the same time For example, download a file in one thread and show a progress bar and dialog with another User interfaces are often multi-threaded Helps hide the fact that CPUs can only do one thing at a time 4/16/18 CS 220: Parallel Computing 8

Common Uses for Threads (2/2) Games often have a main event loop and several sub threads that handle: Graphics rendering Artificial Intelligence Responding to player inputs In a video encoder, you may split the video into multiple regions and have each thread work on them individually 4/16/18 CS 220: Parallel Computing 9

Stepping Back: Processes Recall: a process is an instance of a program Each process has: Binary instructions, data, memory File descriptors, permissions Stack, heap, registers Threads are very similar, but they share almost everything with their parent process except for: Stack Registers 4/16/18 CS 220: Parallel Computing 10

Sharing Data Since threads share the heap with their parent process, we can share pointers to memory locations A thread can read and write data set up by its parent process Sharing these resources also means that it s faster to create threads No need to allocate a new heap, set up permissions, etc. 4/16/18 CS 220: Parallel Computing 11

Other Types of Threads pthreads is just one way to manage lightweight execution contexts Windows has its own threading model Languages have other features: Go has goroutines that abstract away some threading details C#: async/await Futures Learning pthreads will help you understand how these models work 4/16/18 CS 220: Parallel Computing 12

Getting Started with pthreads As usual, we have a new #include! #include <pthread.h> We also need to link against the pthreads library: gcc file.c pthread Luckily, we don t need a special compiler wrapper to use pthreads (like we did with MPI: mpicc) 4/16/18 CS 220: Parallel Computing 13

Creating a Thread int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); 4/16/18 CS 220: Parallel Computing 14

Let s Demo This 4/16/18 CS 220: Parallel Computing 15

Variable Access num_threads, defined at the top of the source file, is accessible by all the threads This is a global variable Variables defined within the thread s function are private and only accessible by it Remember: each thread gets its own stack If we malloc a struct on the heap and pass it to a thread, can it access the struct? 4/16/18 CS 220: Parallel Computing 16

pthread_t What s pthread_t, the type we used to create our array of threads? This is considered an opaque type, defined internally by the library It s often just an integer that uniquely identifies the thread, but we can t rely on this For example, we shouldn t print out a pthread_t 4/16/18 CS 220: Parallel Computing 17

attr The second parameter we pass in is the pthread attributes These can include the stack size, scheduling policies, and more For now we are fine with the defaults, so we pass in NULL 4/16/18 CS 220: Parallel Computing 18

start_routine The most important part of pthread_create is the start routine This function is called by the pthread library as the starting point for your thread Passed in as a function pointer Pointers are back, whoo hoo!!! Just like how they sound: they re a pointer to a specific function 4/16/18 CS 220: Parallel Computing 19

arg The last argument to pthread_create is arg This can be anything we want to pass to the thread If we wanted to have MPI-style ranks, we can pass in a rank here If we were implementing P2 with pthreads, we d want to pass in the start and end points of our mining thread 4/16/18 CS 220: Parallel Computing 20

pthread_join int pthread_join(pthread_t thread, void **value_ptr); The pthread_join function waits for a pthread to finish execution (by calling return) The return value of the thread is stored in value_ptr This lets our main thread wait for all its children to finish up before moving on Commonly used to coordinate shutting down the threads, waiting for their results, and synchronizing our logic 4/16/18 CS 220: Parallel Computing 21