POSIX Threads(pthreads)

Slides:



Advertisements
Similar presentations
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Advertisements

Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
Professor: Shu-Ching Chen TA: Hsin-Yu Ha.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int c;
Multi-core Programming Programming with Posix Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
The University of Adelaide, School of Computer Science
Thread Synchronization with Semaphores
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Copyright ©: University of Illinois CS 241 Staff1 Synchronization and Semaphores.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
What is a thread? process: an address space with 1 or more threads executing within that address space, and the required system resources for those threads.
Programming with POSIX* Threads Intel Software College.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
Pthreads: A shared memory programming model
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
POSIX Threads HUJI Spring 2011.
Pthreads.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Threads A thread is an alternative model of program execution
CS 360 pthreads Condition Variables for threads. Page 2 CS 360, WSU Vancouver What is the issue? Creating a thread to perform a task and then joining.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
POSIX THREADS. What is a thread? Multiple stands of execution in a single program are called threads. In other words, a thread is a sequence of control.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
pThread synchronization
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Synchronization and Semaphores
CS 537 – Introduction to Operating Systems
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
PThreads.
Principles of Operating Systems Lecture 11
Shared-Memory Programming with Threads
Threads Threads.
Netprog: Threads Programming
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Thread Programming.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Multithreading Tutorial
CSE 333 – Section 9 Threads.
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Thread Programming.
Unix System Calls and Posix Threads
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Synchronization Primitives – Semaphore and Mutex
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Shared Memory Programming with Pthreads
Presentation transcript:

POSIX Threads(pthreads) Paralelné počítačové systémy Pavol Hudačko, Soňa Kolárova, Lucia Michalenková 2018/2019

Links Presentation: https://goo.gl/8LTe7z Programs: https://goo.gl/N4zr18

POSIX Threads (pthreads) libraries standard based thread API for C/C++, defined by standard IEEE POSIX 1003.1c-1995 available for UNIX, but also non UNIX systems, for Windows as pthreads-w32 implemented with a pthread.h, all procedures prefixed with pthread_ The POSIX semaphore API works with POSIX threads, but is not part of threads standard, but in semaphore.h, procedures prefixed with sem_

Thread Management - Creation #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Arguments: thread - returns the thread id attr - Set to NULL if default thread attributes are used void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to void. *arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure.

Thread Management - Termination The thread returns normally from its starting routine. Its work is done. The thread makes a call to the pthread_exit subroutine - whether its work is done or not. The thread is canceled by another thread via the pthread_cancel routine. The entire process is terminated due to making a call to either the exec() or exit() If main() finishes first, without calling pthread_exit explicitly itself #include <pthread.h> int pthread_exit(void *retval); int pthread_cancel(pthread_t thread);

Thread Synchronization Critical section a piece of code that only one thread can execute at a time 3 synchronization mechanisms: mutexes - Mutual exclusion lock: Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables. joins - Make a thread wait till others are complete (terminated). condition variables - data type pthread_cond_t

Thread Synchronization - Mutex lock/unlock Mutual exclusion lock: Block access to variables by other threads A thread should lock before entering a critical section A thread should unlock when leaving the critical section At one time, only one thread can get the lock #include <pthread.h> /*acquire a lock on the specified mutex variable. If the mutex is already locked by another thread, this call will block the calling thread until the mutex is unlocked*/ int pthread_mutex_lock(pthread_mutex_t *mutex); /* attempt to lock a mutex or will return error code if busy. Useful for preventing deadlock conditions.*/ int pthread_mutex_trylock(pthread_mutex_t *mutex); /*unlock a mutex variable. An error is returned if mutex is already unlocked or owned by another thread*/ int pthread_mutex_unlock(pthread_mutex_t *mutex);

Thread Synchronization - Mutex example

Thread Synchronization - Joins A join is performed when one wants to wait for a thread to finish. (Thread must by Joinable) #include <pthread.h> int pthread_join(pthread_t thread, void **retval); Arguments: thread - thread suspended until the thread identified by thread terminates, either by calling pthread_exit() or by being cancelled. retval - If retval is not NULL, the return value of th is stored in the location pointed to by retval.

Thread Synchronization - Condition variables condition variables allow threads to synchronize based on the value of the data helps to avoid continually polling always must be associated with a mutex to avoid a race condition created by one thread preparing to wait and another thread which may signal the condition before the first thread actually waits on it resulting in a deadlock. two threads wanting to wait for the same condition variable must use the same mutex.

Thread Synchronization - Condition variables Creating/Destroying: int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr); int pthread_cond_destroy(pthread_cond_t *cond); Waiting on condition: /*unlocks the mutex and waits for the condition variable cond to be signaled*/ int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); /* place limit on how long it will block */ int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime); Waking thread based on condition: /* restarts one of the threads that are waiting on the condition variable cond. */ int pthread_cond_signal(pthread_cond_t *cond); /* wake up all threads blocked by the specified condition variable */ int pthread_cond_broadcast(pthread_cond_t *cond);

Time results - Matrix

Time results - Bubble sort

Visual Studio configuration 1. ) Download: http://cs.du.edu/~mitchell/pthreads_compiled.zip

Zdroje https://en.wikipedia.org/wiki/POSIX_Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html https://computing.llnl.gov/tutorials/pthreads/ http://man7.org/linux/man-pages/man3/