IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.

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.
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.
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.
Pthread II. Outline Join Mutex Variables Condition Variables.
Comp 422: Parallel Programming Shared Memory Multithreading: Pthreads Synchronization.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
The University of Adelaide, School of Computer Science
Portable Operating System Interface Thread Yukai Hung Department of Mathematics National Taiwan University Yukai Hung
Thread-Safe Programming Living With Linux. Thread-Safe Programming Tommy Reynolds Fedora Documentation Project Steering Committee
POSIX Threads Programming The following is extracted from a tutorial by Blaise Barney at Livermore Computing Blaise Barney (Lawrence Livermore National.
5.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Problems with Semaphores Incorrect use of semaphore operations:
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
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.
ICS 145B -- L. Bic1 Project: Process/Thread Synchronization Textbook: pages ICS 145B L. Bic.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Includes slides from course CS194 at UC Berkeley, by prof. Katherine Yelick Shared Memory Programming Pthreads: an overview Ing. Andrea Marongiu
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations.
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.
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.
Thread API Xiaohua Lu Office : CS 3310 Tel. : Office hours: 11-12,3-5 T,TR.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Pthreads.
Threads Tutorial #7 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers – It is.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 12: Thread-safe Data Structures, Semaphores.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Operating Systems Inter-Process Communications. Lunch time in the Philosophy Department. Dining Philosophers Problem (1)
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process.
Barriers and Condition Variables
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
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.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
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
COMP7330/7336 Advanced Parallel and Distributed Computing POSIX Thread API Dr. Xiao Qin Auburn University
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
1 Reading compiler errors ls2.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token In file included from /usr/include/stdio.h:75,
Lesson One – Creating a thread
PThreads.
Principles of Operating Systems Lecture 11
Threads Threads.
Boost String API & Threads
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Thread synchronization
Lecture 14: Pthreads Mutex and Condition Variables
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Unix System Calls and Posix Threads
Pthread Prof. Ikjun Yeom TA – Mugyo
Multithreading Tutorial
Multithreading Tutorial
Lecture 14: Pthreads Mutex and Condition Variables
POSIX Threads(pthreads)
Presentation transcript:

IT 325 Operating systems Chapter6

 Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple threads share a common address space, like the variable data in next example.

 To understand what might happen, consider the following code: THREAD 1 THREAD 2 a = data; b = data; a++; b--; data = a; data = b;  Now if this code is executed serially (for instance, THREAD 1 first and then THREAD 2), there are no problems.  However threads execute in an arbitrary order, so consider the following situation:

THREAD 1 THREAD 2 a = data; b = data; a++; b--; data = a; data = b;  [data = data - 1!!!!!!!]  So data could end up +1, 0, -1, and there is NO WAY to know which value! It is completely non-deterministic!

 The solution to this is to provide functions that will block a thread if another thread is accessing data that it is using.  Pthreads use a data type called a mutex to achieve this.

 A basic mechanism supplied by the pthreads library to solve the data race problem, is called a mutex.  Mutexes have two basic operations, lock and unlock. If a mutex is unlocked and a thread calls lock, the mutex locks and the thread continues. If however the mutex is locked, the thread blocks until the thread holding the lock calls unlock.  Locking a mutex is an atomic operation, meaning that the operating system (or threads library) assures you that if you locked a mutex, no other thread succeeded in locking this mutex at the same time.

 In order to create a mutex, we first need to declare a variable of type pthread_mutex_t and then initialize it using the function int pthread_mutex_init (pthread_mutex_t *mut, const pthread_mutexattr_t *attr);  The first argument is a pointer to the mutex. To second argument is used to set the mutex attributes. To use the default mutex attributes, just pass NULL to it.  example: pthread_mutex_t a_mutex; pthread_mutex_init (&a_mutex, NULL);

 In order to lock a mutex, we may use the function pthread_mutex_lock().  This function attempts to lock the mutex, or block the thread  If the mutex is already locked by another thread. In this case, when the mutex is unlocked by the first thread, the function will return with the mutex locked by our thread.  Here is how to lock a mutex (assuming it was initialized earlier): int rc = pthread_mutex_lock(&a_mutex); if (rc) { /* an error has occurred */ perror("pthread_mutex_lock"); pthread_exit(NULL); } /* mutex is now locked - do your stuff. */

 After the thread did what it had to (change variables or data structures, handle file, or whatever it intended to do), it should free the mutex, using the pthread_mutex_unlock() function, like this: rc = pthread_mutex_unlock(&a_mutex); if (rc) { perror("pthread_mutex_unlock"); pthread_exit(NULL); }

 After we finished using a mutex (that is, no thread needs it at all), we should destroy it. However, if only one thread finished with the mutex, it should leave it alive for the other threads that might still need to use it. Once all finished using it, the last one can destroy it using the pthread_mutex_destroy() function:  rc = pthread_mutex_destroy(&a_mutex);  After this call, this variable (a_mutex) may not be used as a mutex any more, unless it is initialized again.

Consider the problem we had before and now let us use mutexes: THREAD 1 THREAD 2 pthread_mutex_lock (&a_mutex); a = data; /* blocked */ a++; /* blocked */ data = a; /* blocked */ pthread_mutex_unlock (&a_mutex); /* blocked */ b = data; b--; data = b; pthread_mutex_unlock(&a_mutex); [data is fine. The data race is gone.]

Let’s test then modify the thread_add.c program on Assignment7