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

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

Thread and Synchronization

POSIX PTHREADS PROGRAMMING

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

Threads. lightweight processes

ANSI/IEEE POSIX Standard Thread management

Threads. Jo, Heeseung

CSE 333 SECTION 9. Threads

Introduction to PThreads and Basic Synchronization

LSN 13 Linux Concurrency Mechanisms

Multithreading Programming II

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

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

Threads. Jo, Heeseung

pthreads CS449 Fall 2017

Concurrent Server Design Multiple- vs. Single-Thread

Threads. studykorner.org

POSIX Threads. Paolo Burgio

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 333 Section 9 - pthreads

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

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

real time operating systems course

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Pthread (9A) Pthread

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Chapter 4 Concurrent Programming

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

Faculté Polytechnique

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

Network Programming TDC 561

Threads. Threads (continued)

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

CSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010

Posix Threads (Pthreads)

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

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

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Roadmap. Concurrent Programming. Concurrent Programming. Communication Between Tasks. Motivation. Tevfik Ko!ar

ENCM 501 Winter 2019 Assignment 9

POSIX Threads. HUJI Spring 2011

CSC Systems Programming Fall Lecture - XVIII Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 11 th, 2008

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

Synchronization Primitives

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

Introduc)on to pthreads. Shared memory Parallel Programming

THREADS. Jo, Heeseung

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

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

Operating systems fundamentals - B06

Multicore and Multiprocessor Systems: Part I

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

CS 3305 Intro to Threads. Lecture 6

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

Concurrency, Parallel and Distributed

Computer Systems Laboratory Sungkyunkwan University

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

Introduction to pthreads

Concurrency and Threads

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

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

Parallel Programming with Threads

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

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2

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

Pthreads: POSIX Threads

CS 3214 Midterm. Here is the distribution of midterm scores for both sections (combined). Midterm Scores 20. # Problem Points Score

High Performance Computing Course Notes Shared Memory Parallel Programming

Shared-Memory Programming

Lecture 19: Shared Memory & Synchronization

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 1 September 29, 2014

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

Concurrency: Threads. CSE 333 Autumn 2018

Concurrent Programming

CSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...

CS240: Programming in C. Lecture 18: PThreads

C Grundlagen - Threads

Real Time Operating Systems and Middleware

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

11/04/2018. Outline. Process. Process. Pthread Library. Process and threads

Multi-threaded Programming

User Space Multithreading. Computer Science, University of Warwick

This exam paper contains 8 questions (12 pages) Total 100 points. Please put your official name and NOT your assumed name. First Name: Last Name:

Interlude: Thread API

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

Locks and semaphores. Johan Montelius KTH

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Chapter 5: Achieving Good Performance

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

CPSC 313: Intro to Computer Systems. POSIX Threads. Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered now)

Shared Memory Programming Models III

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology

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

More Shared Memory Programming

PThreads in a Nutshell

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Transcription:

Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto For more information please consult Advanced Programming in the UNIX Environment, 3rd Edition, W. Richard Stevens and Stephen A. Rago, Addison Wesley Sections 11.1 11.6

#include <pthread.h> Thread Identification pthread_t pthread_self(void); // * returns the thread ID of the calling thread // * a thread ID is represented by the pthread_t data structure int pthread_equal(pthread_t tid1, pthread_t tid2); // * most implementations represent the thread IDs as integers // but a portable implementations can t assume that // * portable implementations must use the pthread_equal() // function to compare thread IDs // * returns nonzero if the thread IDs are equal, 0 otherwise DCC-FCUP # 1

#include <pthread.h> Thread Creation int pthread_create(pthread_t *tidp, pthread_attr_t *attr, void *(*start_rtn)(void *), void *arg); // * creates a new thread of execution to start running the // start_rtn() function and sets tidp with the thread ID of // the newly created thread // * the attr argument can be used to customize various thread // attributes, setting it to NULL creates a thread with the // default attributes // * the arg argument can be used to pass information to the // start_rtn() function which takes arg as its single argument // * returns 0 if successful, error number on failure DCC-FCUP # 2

#include <pthread.h> Thread Termination void pthread_exit(void *rval_ptr); // * terminates thread execution with return code rval_ptr, // without terminating the entire process (the same happens // when the thread simply returns from the start routine) // * note that, if any thread within a process calls exit(), // then the entire process terminates // * returns 0 if successful, error number on failure DCC-FCUP # 3

#include <pthread.h> Thread Termination int pthread_join(pthread_t thread, void **rval_ptr); // * waits for a specific thread to terminate and blocks until // the specified thread calls pthread_exit() or returns from // its start routine // * the thread return code is then made available in the // rval_ptr argument // * returns 0 if successful, error number on failure DCC-FCUP # 4

Thread Creation/Termination: Example void run_thread(long arg, long ret) { pthread_t tid; void *rval; if (pthread_create(&tid, NULL, thr_fun, (void *) arg)!= 0) { /* pthread_create error */ }... if (pthread_join(tid, &rval)!= 0) { /* pthread_join error */ } ret = (long) rval; } void *thr_fun(void *thr_arg) { long ret, arg = (long) thr_arg;... // do something return ((void *) ret); } DCC-FCUP # 5

Thread Synchronization When multiple threads of control share the same memory, we need to make sure that all threads see a consistent view of the data If each thread uses variables that other threads don t read or modify, no consistency problems will exist If a variable is read-only, there is no consistency problem with more than one thread reading its value at the same time If one thread can modify a variable that other threads can also read or modify, we need to synchronize the threads to ensure that they don t read or write an invalid value when accessing the variable s memory contents DCC-FCUP # 6

Thread Synchronization: Problem Example DCC-FCUP # 7

Mutexes To protect our data and ensure access by only one thread at a time, we can use the pthreads mutual-exclusion (mutex) interface A mutex is basically a lock that we set (lock) before accessing a shared resource and release (unlock) when we re done If a mutex is locked, any thread that tries to set it will block until it is released If more than one thread is blocked when a mutex is unlock, then the first thread to run will be able to set the lock The others will see that the mutex is still locked and go back to waiting for it to become available again In this way, only one thread will proceed at a time DCC-FCUP # 8

Mutexes #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr); int pthread_mutex_destroy(pthread_mutex_t *mutex); // * a mutex can be initialized by either setting it to // PTHREAD_MUTEX_INITIALIZER (if statically allocated) or // by calling pthread_mutex_init() (if dynamically allocated) // * for a dynamically allocated mutex, we need to call // pthread_mutex_destroy() before freeing its memory // * the attr argument customizes various mutex attributes, // a NULL value initializes it with the default attributes // * both functions return 0 if OK, error number on failure DCC-FCUP # 9

Mutexes #include <pthread.h> int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); // * pthread_mutex_lock() locks a mutex and, if the mutex is // already locked, it blocks the calling thread until the // mutex is unlocked // * pthread_mutex_trylock() locks a mutex but, if the mutex is // already locked, it fails without blocking // * pthread_mutex_unlock() unlocks a mutex // * all functions return 0 if OK, error number on failure DCC-FCUP # 10

Static Mutex: Example void mutexes_fun() { pthread_mutex_t mutex_static = PTHREAD_MUTEX_INITIALIZER;... execute_critical_region(&mutex_static);... } void execute_critical_region(pthread_mutex_t *mutex) { pthread_mutex_lock(mutex);... // critical region pthread_mutex_unlock(mutex); } DCC-FCUP # 11

Dynamic Mutex: Example void mutexes_fun() { struct xpto *str_xpto; if ((str_xpto = malloc(sizeof(struct xpto)))!= NULL) { // struct initialization pthread_mutex_init(&str_xpto->mutex_dynamic, NULL);... }... execute_critical_region(&str_xpto->mutex_dynamic);... pthread_mutex_destroy(&str_xpto->mutex_dynamic) >mutex_dynamic); free(str_xpto); } DCC-FCUP # 12