Multithreading Tutorial

Slides:



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

Threads. What do we have so far The basic unit of CPU utilization is a process. To run a program (a sequence of code), create a process. Processes are.
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;
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
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.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
The University of Adelaide, School of Computer Science
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
POSIX Threads Programming Operating Systems. Processes and Threads In shared memory multiprocessor architectures, such as SMPs, threads can be used to.
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
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
Threads A thread is an alternative model of program execution
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.
PRINCIPLES OF OPERATING SYSTEMS Tutorial-4: Multi-process and Multi-threaded Programming CPSC 457, Spring 2015 May 28/29, 2015 Department of Computer Science,
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
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.
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Realizing Concurrency using the thread model
Shared-Memory Programming with Threads
Threads Threads.
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Realizing Concurrency using Posix Threads (pthreads)
CSE 333 – Section 9 Threads.
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Thread Programming.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 9: POSIX Threads Cont.
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

Multithreading Tutorial Professor: Shu-Ching Chen TA: Hsin-Yu Ha

What is a Thread? An independent stream of instructions that can be scheduled to run A path of execution int a, b; int c; a = 1; b = a + 2; c = 3; Sequentially execute c = 5; CPU Multi-Thread Single-Thread

Program v.s. Process v.s. Thread An execution file stored in the harddrive Process An execution file stored in the Memory Thread An execution path of part of the process HDD Memory Program Process Thread

Why is thread? Parallel execution Shared resources Easier to create and destroy than processes (100X) Easy porting to multiple CPUs

Compiling a pthreads program Return Result :

Creating threads (1) pthread_create() Return 0 if OK, nonzero on error Four Arguments Thread : A thread identifier Attr : A pointer to a thread attribute object Start_routine : A pointer to the function the thread is execute Arg : The argument to the function

Creating threads (2) Single variable

Creating threads (3) Several variables

Terminating Threads (1) pthread_exit() Return 0 if OK, nonzero on error Four ways of terminating a thread The thread returns from its starting routine The thread makes a call to the pthread_exit subroutine The thread is canceled by another thread The entire process is terminated

Terminating Threads (2) Example of pthread_exit()

Terminating Threads (3) pthread_join() Return 0 if OK, nonzero on error Wait from other threads to terminate by calling it

Terminating Threads (4)

Other Thread functions pthread_self() It returns the unique, system assigned thread ID of the calling thread pthread_detach() Return 0 if OK, nonzero on error It can be used to explicitly detach a thread

Synchronizations Join Semaphore Mutexes Barrier

Synchronization - Semaphore (1) A separate POSIX standard does define them Important functions sem_init : Initialize a new semaphore sem_destroy : deallocate an existing semaphore sem_wait : sem_post

Synchronization - Semaphore (2)

Synchronization - Mutexes (1) Mutexes are used to prevent data inconsistencies due to operations by multiple threads upon the same memory area performed at the same time to prevent race conditions where an order of operation upon the memory is expected

Synchronization - Mutexes (2)

Synchronization - Mutexes (3)

Synchronization – Barrier (1) It is used when some parallel computations need to "meet up" at certain points before continuing. Pthreads extension includes barriers as synchronization objects (available in Single UNIX Specification) Enable by #define _XOPEN_SOURCE 600 at start of file Initialize a barrier for count threads int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrier attr_t *attr, int count); Each thread waits on a barrier by calling int pthread_barrier_wait(pthread_barrier_t *barrier); Destroy a barrier int pthread_barrier_destroy(pthread_barrier_t *barrier);

Synchronization – Barrier (2)

References POSIX Threads Programming Pthreads primer https://computing.llnl.gov/tutorials/pthreads/ Pthreads primer http://pages.cs.wisc.edu/~travitch/pthreads_primer.html POSIX thread (pthread) Tutorial http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html