Parallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai

Size: px
Start display at page:

Download "Parallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai"

Transcription

1 Parallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai School of Computer Science and Technology Huazhong University of Science and Technology

2 Thread Basics! All memory in the logical machine model of a thread is globally accessible to every thread.! The stack corresponding to the function call is generally treated as being local to the thread for liveness reasons. 2

3 Thread Basics 3

4 Thread Basics The logical machine model of a thread-based parallel programming paradigm. 4

5 Thread Basics! Cannot assume any order of execution.! Any such order must be explicitly established using synchronization mechanisms.! Issues on deadlock, starvation and performance. 5

6 Pthreads API! The Pthreads API is defined in the ANSI/ IEEE POSIX standard.! The subroutines which comprise the Pthreads API can be informally grouped into three major classes:! Thread management! Mutexes! Condition variables 6

7 Thread Management! The first class of functions work directly on threads - creating, detaching, joining, etc.! They include functions to set/query thread attributes (joinable, scheduling etc.) 7

8 Creating Threads! Initially, only a single, default thread. All other threads must be explicitly created by the programmer.! routine: pthread_create(thread,attr,start_routine,arg)! thread: unique identifier for the new thread (pthread_t)! attr: attribute object used to set thread attributes. (pthread_attr) You can specify a thread attributes object, or NULL for the default values.! start_routine: C routine that the thread will execute.! arg: A single argument that may be passed to start_routine. It must be passed by reference. NULL may be used if no argument is to be passed.! If successful, the pthread_create() function shall return zero; otherwise, an error number shall be returned to indicate the error. 8

9 Thread Attributes! By default, a thread is created with certain attributes. Some of these attributes can be changed by the programmer via the thread attribute object.! pthread_attr_init(attr) and pthread_attr_destroy(attr) are used to initialize/destroy the thread attribute object.! Other routines are then used to query/set specific attributes in the thread attribute object. 9

10 Terminating Thread! There are several ways in which a Pthread may be terminated:! The thread makes a call to the pthread_exit() subroutine.! The thread is cancelled by another thread via the pthread_cancel() routine.! The entire process is terminated due to a call to the exit subroutine. 10

11 Terminating Thread! routine: pthread_exit(status)! used to explicitly exit a thread.! The programmer may optionally specify a termination status, which is stored as a void pointer for any thread that may join the calling thread.! Cleanup: the pthread_exit() routine does not close files; any files opened inside the thread will remain open after the thread is terminated. 11

12 Example #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 5 void *PrintHello(void *threadid) { int tid; tid = (int) threadid; printf("hello World! It's me, thread #%d!\n", tid); pthread_exit(null); 12

13 Example int main(int argc, char *argv[]) { pthread_t threads[num_threads]; int rc, t; for(t=0;t<num_threads;t++){ printf("in main: creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc){ printf("error; return code from pthread_create() is %d\n", rc); exit(-1); pthread_exit(null); 13

14 Passing Arguments to Threads! The pthread_create() routine permits the programmer to pass one argument to the thread start routine.! For cases where multiple arguments must be passed, this limitation is easily overcome by creating a structure which contains all of the arguments, and then passing a pointer to that structure in the pthread_create () routine.! All arguments must be passed by reference and cast to (void *). 14

15 Passing Arguments to Threads struct two_args { int arg1; int arg2; ; void *needs_2_args(void *ap) { struct two_args *argp; int a1, a2; argp = (struct two_args *) ap; a1 = argp->arg1; a2 = argp->arg2; free (argp); pthread_exit(null); 15

16 Passing Arguments to Threads int main(int argc, char *argv[]) { pthread_t t; struct two_args *ap; int rc; ap = (struct two_args *) malloc (sizeof (struct two_args)); ap->arg1 = 1; ap->arg2 = 2; rc = pthread_create(&t, NULL, needs_2_args, (void *) ap); pthread_exit(null); 16

17 Joining & Detaching Threads! Routines: pthread_join (threadid,status) pthread_detach (threadid,status) pthread_attr_setdatachstate (attr,detachstate) pthread_attr_getdetachstate (attr,datachstate)! "Joining" is one way to accomplish synchronization between threads.! The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates.! The programmer is able to obtain the target thread's termination return status if it was specified in the target thread's call to pthread_exit(). 17

18 Joining & Detaching Threads! When a thread is created, one of its attributes defines whether it is joinable or detached.! Only threads that are created as joinable can be joined. If a thread is created as detached, it can never be joined.! To explicitly create a thread as joinable or detached, the attr argument in the pthread_create() routine is used:! Declare a pthread attribute variable of the pthread_attr_t data type! Initialize the attribute variable with pthread_attr_init()! Set the attribute detached status with pthread_attr_setdetachstate()! When done, free library resources used by the attribute with pthread_attr_destroy() 18

19 Example void *BusyWork(void *null) { pthread_exit((void *) 0); 19

20 Example int main (int argc, char *argv[]) { pthread_attr_t attr; int rc, t; void *status; /* Initialize and set thread detached attribute */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); /* Free attribute and wait for the other threads */ pthread_attr_destroy(&attr); for(t=0; t<num_threads; t++) { rc = pthread_join(thread[t], &status); printf("completed join with thread %d status= %ld\n",t, (long)status); pthread_exit(null); 20

21 Synchronization Issues! When multiple threads attempt to manipulate the same data item, the results can often be incoherent if proper care is not taken to synchronize them.! Consider: /* each thread tries to update variable best_cost as follows */ if (my_cost < best_cost) best_cost = my_cost;! Assume that there are two threads, the initial value of best_cost is 100, and the values of my_cost are 50 and 75 at threads t1 and t2.! Depending on the schedule of the threads, the value of best_cost could be 50 or 75! 21

22 Mutex! The second class of functions deal with synchronization, called a "mutex", which is an abbreviation for "mutual exclusion".! Mutex functions provide for creating, destroying, locking and unlocking mutexes.! They are also supplemented by mutex attribute functions that set or modify attributes associated with mutexes. 22

23 Creating & Destroying Mutex! Routines: pthread_mutex_init (mutex,attr) pthread_mutex_destroy (mutex) pthread_mutexattr_init (attr) pthread_mutexattr_destroy (attr)! Mutex must be declared with type pthread_mutex_t, and must be initialized before they can be used.! There are two ways to initialize a mutex variable:! Statically, when it is declared. For example: pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;! Dynamically, with the pthread_mutex_init() routine. This method permits setting mutex object attributes, attr (which may be specified as NULL to accept defaults).! The mutex is initially unlocked. 23

24 Locking & Unlocking Mutex! Routines: pthread_mutex_lock (mutex) pthread_mutex_trylock (mutex) pthread_mutex_unlock (mutex)! pthread_mutex_lock() is used by a thread to acquire a lock on the specified mutex variable.! pthread_mutex_unlock() will unlock a mutex if called by the owning thread. An error will be returned if:! the mutex was already unlocked! the mutex is owned by another thread! pthread_mutex_trylock() will attempt to lock a mutex. However, if the mutex is already locked, the routine will return immediately with an EBUSY" error code. This routine may be useful in preventing deadlock conditions. 24

25 Example 1! We can now write our previously incorrect code segment as: pthread_mutex_t minimum_value_lock;... main() {... pthread_mutex_init(&minimum_value_lock, NULL);... void *find_min(void *list_ptr) {... pthread_mutex_lock(&minimum_value_lock); if (my_cost < best_cost) best_cost = my_cost; pthread_mutex_unlock(&minimum_value_lock); 25

26 Example 2! The producer-consumer scenario imposes the following constraints:! The producer thread must not overwrite the shared buffer when the previous task has not been picked up by a consumer thread.! The consumer threads must not pick up tasks until there is something present in the shared data structure.! Individual consumer threads should pick up tasks one at a time. 26

27 Example 2 pthread_mutex_t task_queue_lock; int task_available;... main() {... task_available = 0; pthread_mutex_init(&task_queue_lock, NULL);... 27

28 Example 2 void *producer(void *producer_thread_data) {... while (!done()) { inserted = 0; create_task(&my_task); while (inserted == 0) { pthread_mutex_lock(&task_queue_lock); if (task_available == 0) { insert_into_queue(my_task); task_available = 1; inserted = 1; pthread_mutex_unlock(&task_queue_lock); 28

29 Example 2 void *consumer(void *consumer_thread_data) {... while (!done()) { extracted = 0; while (extracted == 0) { pthread_mutex_lock(&task_queue_lock); if (task_available == 1) { extract_from_queue(&my_task); task_available = 0; extracted = 1; pthread_mutex_unlock(&task_queue_lock); process_task(my_task); 29

30 Overheads of Locking! Locks represent serialization points since critical sections must be executed by threads one after another.! Encapsulating large segments of the program within locks can lead to significant performance degradation.! It is often possible to reduce the idling overhead associated with locks using pthread_mutex_trylock. 30

31 Alleviating Locking Overhead /* using pthread_mutex_trylock routine */ Pthread_mutax_t trylock_lock = PTHREAD_MUTEX_INITIALIZER; lock_status=pthread_mutex_trylock(&trylock_lock); if (lock_status == EBUSY) { /* do something else */ else { /* do one thing */ pthread_mutex_unlock(&trylock_lock); 31

32 Monitor! Mutexs provide powerful synchronization tools. However,! Lock() and unlock() are scattered among several threads. Therefore, it is difficult to understand their effects! Usage must be correct in all the threads.! One bad thread (or one programming error) can kill the whole system.! Monitor is a high-level abstraction that may provide a convenient and effective mechanism for thread synchronization 32

33 Monitor! Local data variables are accessible only by the monitor! thread enters monitor by invoking one of its procedures! Only one thread may be executing in the monitor at a time 33

34 Monitor with Condition Variables! Monitor does not need to code certain synchronization constraints explicitly.! However, it is not sufficiently powerful for modeling some other synchronization schemes.! An additional synchronization mechanism, i.e., condition variable, is required. 34

35 Condition Variables! The third class of functions address communications between threads that share a mutex.! A condition variable allows a thread to block itself until specified data reaches a predefined state.! A condition variable indicates an event and has no value.! One cannot store a value into nor retrieve a value from a condition variable.! If a thread must wait for an event to occur, that thread waits on the corresponding condition variable.! A condition variable has a queue for those threads that are waiting the corresponding event to occur to wait on.! If another thread causes the event to occur, that thread simply signals the corresponding condition variable. 35

36 Condition Variables! This class includes functions to create, destroy, wait and signal based upon specified variable values.! Functions to set/query condition variable attributes are also included.! A condition variable is always used in conjunction with a mutex lock. 36

37 Creating & Destroying Condition Variables! Routines: pthread_cond_init (condition, attr) pthread_cond_destroy (condition) pthread_condattr_init (attr) pthread_condattr_destroy (attr)! Condition variables must be declared with type pthread_cond_t, and must be initialized before they can be used.! There are two ways to initialize a condition variable:! Statically, when it is declared. For example: pthread_cond_t myconvar = PTHREAD_COND_INITIALIZER;! Dynamically, with the pthread_cond_init() routine. This method permits setting condition variable object attributes, attr (which may be specified as NULL to accept defaults). 37

38 Waiting and Signaling on Condition Variables! Routines: pthread_cond_wait (condition, mutex) pthread_cond_signal (condition) pthread_cond_broadcast (condition)! pthread_cond_wait() blocks the calling thread until the specified condition is signalled.! This routine should be called while mutex is locked, and it will automatically release the mutex while it waits.! After signal is received and thread is awakened, mutex will be automatically locked for use by the thread.! The programmer is then responsible for unlocking mutex when the thread is finished with it. 38

39 Waiting and Signaling on Condition Variables! pthread_cond_signal() is used to signal (or wake up) another thread which is waiting on the condition variable.! It should be called after mutex is locked, and! must unlock mutex in order for pthread_cond_wait() routine to complete.! The pthread_cond_broadcast() routine unlocks all of the threads blocked on the condition variable. 39

40 Waiting and Signaling on Condition Variables! Proper locking and unlocking of the associated mutex variable is essential when using these routines. For example:! Failing to lock the mutex before calling pthread_cond_wait() may cause it NOT to block.! Failing to unlock the mutex after calling pthread_cond_signal() may not allow a matching pthread_cond_wait() routine to complete (it will remain blocked). 40

41 Producer-Consumer Using Condition Variables pthread_cond_t cond_queue_empty, cond_queue_full; pthread_mutex_t task_queue_cond_lock; int task_available; /* other data structures here */ main() { /* declarations and initializations */ task_available = 0; pthread_cond_init(&cond_queue_empty, NULL); pthread_cond_init(&cond_queue_full, NULL); pthread_mutex_init(&task_queue_cond_lock, NULL); /* create and join producer and consumer threads */ 41

42 Producer-Consumer Using Condition Variables void *producer(void *producer_thread_data) { while (!done()) { create_task(); pthread_mutex_lock(&task_queue_cond_lock); while (task_available == 1) pthread_cond_wait(&cond_queue_empty, &task_queue_cond_lock); insert_into_queue(); task_available = 1; pthread_cond_signal(&cond_queue_full); pthread_mutex_unlock(&task_queue_cond_lock); 42

43 Producer-Consumer Using Condition Variables void *consumer(void *consumer_thread_data) { while (!done()) { pthread_mutex_lock(&task_queue_cond_lock); while (task_available == 0) pthread_cond_wait(&cond_queue_full, &task_queue_cond_lock); my_task = extract_from_queue(); task_available = 0; pthread_cond_signal(&cond_queue_empty); pthread_mutex_unlock(&task_queue_cond_lock); process_task(my_task); 43

44 Composite Synchronization Constructs! By design, Pthreads provide support for a basic set of operations.! Higher level constructs can be built using basic synchronization constructs. 44

45 ! To find the solution for a twodimensional Laplace equation simply: 1. Initialise T ij to some initial guess. 2. Apply the boundary conditions. 3. For each internal mesh point set " T ij *!=!(T (i+1)j!+! T (i-1)j!+! T i(j+1)!+! T i(j-1) )/4. 4. Replace old solution T with new estimate T*. 5. If solution does not satisfy tolerance, repeat from step 2. 45

46 ! We need a new synchronization structure called barrier to synchronize the computation between consecutive iteration steps no thread can come across the barrier till all the threads arrive.! For barrier we need! Total number of threads considered! Number of threads have arrived! A mutex and a condition variable 46

47 typedef struct { pthread_mutex_t count_lock; pthread_cond_t ok_to_proceed; int count; mylib_barrier_t; void mylib_init_barrier(mylib_barrier_t *b) { b -> count = 0; pthread_mutex_init(&(b -> count_lock), NULL); pthread_cond_init(&(b -> ok_to_proceed), NULL); 47

48 void mylib_barrier (mylib_barrier_t *b, int num_threads) { pthread_mutex_lock(&(b -> count_lock)); b -> count ++; if (b -> count == num_threads) { b -> count = 0; pthread_cond_broadcast(&(b -> ok_to_proceed)); else while (pthread_cond_wait(&(b -> ok_to_proceed), &(b -> count_lock))!= 0); pthread_mutex_unlock(&(b -> count_lock)); 48

49 Read-Write Locks! In many applications, a data structure is read frequently but written infrequently. For such applications, we should use read-write locks.! A read lock is granted when there are other threads that may already have read locks.! If there is a write lock on the data (or if there are queued write locks), the thread performs a condition wait.! If there are multiple threads requesting a write lock, they must perform a condition wait.! With this description, we can design functions for! read locks mylib_rwlock_rlock,! write locks mylib_rwlock_wlock, and! unlocking mylib_rwlock_unlock. 49

50 Read-Write Locks! The lock data type mylib_rwlock_t holds the following:! a count of the number of readers,! the writer (a 0/1 integer specifying whether a writer is present),! a count pending_writers of pending writers,! a condition variable readers_proceed that is signaled when readers can proceed,! a condition variable writer_proceed that is signaled when one of the writers can proceed, and! a mutex read_write_lock associated with the shared data structure 50

51 Read-Write Locks typedef struct { int readers; int writer; int pending_writers; pthread_cond_t readers_proceed; pthread_cond_t writer_proceed; pthread_mutex_t read_write_lock; mylib_rwlock_t; void mylib_rwlock_init (mylib_rwlock_t *l) { l->readers = l->writer = l->pending_writers = 0; pthread_mutex_init(&(l->read_write_lock), NULL); pthread_cond_init(&(l->readers_proceed), NULL); pthread_cond_init(&(l->writer_proceed), NULL); 51

52 Read-Write Locks void mylib_rwlock_rlock(mylib_rwlock_t *l) { /* if there is a write lock, perform condition wait. else increment count of readers and grant read lock */ pthread_mutex_lock(&(l->read_write_lock)); while (l->writer > 0) pthread_cond_wait(&(l->readers_proceed), &(l->read_write_lock)); l->readers ++; pthread_mutex_unlock(&(l->read_write_lock)); 52

53 Read-Write Locks void mylib_rwlock_wlock(mylib_rwlock_t *l) { /* if there are readers or writers, increment pending writers count and wait. On being awoken, decrement pending writers count and increment writer count */ pthread_mutex_lock(&(l->read_write_lock)); l->pending_writers ++; while ((l->writer > 0) (l->readers > 0)) { pthread_cond_wait(&(l->writer_proceed), &(l->read_write_lock)); l->pending_writers --; l->writer ++; pthread_mutex_unlock(&(l->read_write_lock)); 53

54 Read-Write Locks void mylib_rwlock_unlock(mylib_rwlock_t *l) { /* if there is a write lock then unlock, else if there are read locks, decrement count of read locks. If the count is 0 and there is a pending writer, let it through, else if there are pending readers, let them all go through */ pthread_mutex_lock(&(l->read_write_lock)); if (l->writer > 0) l->writer = 0; else if (l->readers > 0) l->readers --; if ((l->readers == 0) && (l->pending_writers > 0)) pthread_cond_signal(&(l->writer_proceed)); else if (l->readers > 0) pthread_cond_broadcast(&(l->readers_proceed)); pthread_mutex_unlock(&(l->read_write_lock)); 54

Programming Shared Address Space Platforms (cont.) Alexandre David B2-206

Programming Shared Address Space Platforms (cont.) Alexandre David B2-206 Programming Shared Address Space Platforms (cont.) Alexandre David B2-206 1 Today Finish thread synchronization. Effort for pthreads = synchronization. Effort for MPI = communication. OpenMP. Extra material

More information

Programming Shared Address Space Platforms (Chapter 7)

Programming Shared Address Space Platforms (Chapter 7) Programming Shared Address Space Platforms (Chapter 7) Alexandre David 1.2.05 This is about pthreads. 1 Comparison Directive based: OpenMP. Explicit parallel programming: pthreads shared memory focus on

More information

Shared Address Space Programming with Pthreads. CS 5334/4390 Spring 2014 Shirley Moore, Instructor February 18, 2014

Shared Address Space Programming with Pthreads. CS 5334/4390 Spring 2014 Shirley Moore, Instructor February 18, 2014 Shared Address Space Programming with Pthreads CS 5334/4390 Spring 2014 Shirley Moore, Instructor February 18, 2014 1 Topic Overview Thread Basics The POSIX Thread API Mutual Exclusion Locks Condition

More information

Lecture 08: Programming with PThreads: PThreads basics, Mutual Exclusion and Locks, and Examples

Lecture 08: Programming with PThreads: PThreads basics, Mutual Exclusion and Locks, and Examples Lecture 08: Programming with PThreads: PThreads basics, Mutual Exclusion and Locks, and Examples CSCE 790: Parallel Programming Models for Multicore and Manycore Processors Department of Computer Science

More information

Multithread Programming. Alexandre David

Multithread Programming. Alexandre David Multithread Programming Alexandre David 1.2.05 adavid@cs.aau.dk Comparison Directive based: OpenMP. Explicit parallel programming: pthreads shared memory focus on synchronization, MPI message passing focus

More information

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

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source: Agenda POSIX Threads Programming 1 Process vs Thread process thread Picture source: https://computing.llnl.gov/tutorials/pthreads/ 2 Shared Memory Model Picture source: https://computing.llnl.gov/tutorials/pthreads/

More information

Xu Liu Derived from John Mellor-Crummey s COMP422 at Rice University

Xu Liu Derived from John Mellor-Crummey s COMP422 at Rice University Programming Shared-memory Platforms with Pthreads Xu Liu Derived from John Mellor-Crummey s COMP422 at Rice University Topics for Today The POSIX thread API (Pthreads) Synchronization primitives in Pthreads

More information

ANSI/IEEE POSIX Standard Thread management

ANSI/IEEE POSIX Standard Thread management Pthread Prof. Jinkyu Jeong( jinkyu@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) TA Seokha Shin(seokha.shin@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The

More information

POSIX Threads. HUJI Spring 2011

POSIX Threads. HUJI Spring 2011 POSIX Threads HUJI Spring 2011 Why Threads The primary motivation for using threads is to realize potential program performance gains and structuring. Overlapping CPU work with I/O. Priority/real-time

More information

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

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff POSIX threads CS 241 February 17, 2012 Copyright University of Illinois CS 241 Staff 1 Recall: Why threads over processes? Creating a new process can be expensive Time A call into the operating system

More information

CS240: Programming in C. Lecture 18: PThreads

CS240: Programming in C. Lecture 18: PThreads CS240: Programming in C Lecture 18: PThreads Thread Creation Initially, your main() program comprises a single, default thread. pthread_create creates a new thread and makes it executable. This routine

More information

Pthreads: POSIX Threads

Pthreads: POSIX Threads Shared Memory Programming Using Pthreads (POSIX Threads) Lecturer: Arash Tavakkol arasht@ipm.ir Some slides come from Professor Henri Casanova @ http://navet.ics.hawaii.edu/~casanova/ and Professor Saman

More information

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Pthreads Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The Pthreads API ANSI/IEEE POSIX1003.1-1995 Standard Thread management Work directly on

More information

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

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology Unix Threads & Concurrency Erick Fredj Computer Science The Jerusalem College of Technology 1 Threads Processes have the following components: an address space a collection of operating system state a

More information

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole CS510 Operating System Foundations Jonathan Walpole Threads & Concurrency 2 Why Use Threads? Utilize multiple CPU s concurrently Low cost communication via shared memory Overlap computation and blocking

More information

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

Threads need to synchronize their activities to effectively interact. This includes: KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 8 Threads Synchronization ( Mutex & Condition Variables ) Objective: When multiple

More information

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

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage) pthreads 1 Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage) 2 1 Thread Packages Kernel thread packages Implemented and supported at kernel

More information

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

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References COSC 6374 Parallel Computation Shared memory programming with POSIX Threads Fall 2012 References Some of the slides in this lecture is based on the following references: http://www.cobweb.ecn.purdue.edu/~eigenman/ece563/h

More information

CS333 Intro to Operating Systems. Jonathan Walpole

CS333 Intro to Operating Systems. Jonathan Walpole CS333 Intro to Operating Systems Jonathan Walpole Threads & Concurrency 2 Threads Processes have the following components: - an address space - a collection of operating system state - a CPU context or

More information

CSE 333 SECTION 9. Threads

CSE 333 SECTION 9. Threads CSE 333 SECTION 9 Threads HW4 How s HW4 going? Any Questions? Threads Sequential execution of a program. Contained within a process. Multiple threads can exist within the same process. Every process starts

More information

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

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017 CSCI4430 Data Communication and Computer Networks Pthread Programming ZHANG, Mi Jan. 26, 2017 Outline Introduction What is Multi-thread Programming Why to use Multi-thread Programming Basic Pthread Programming

More information

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

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization CS-345 Operating Systems Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization Threads A thread is a lightweight process A thread exists within a process and uses the process resources. It

More information

Posix Threads (Pthreads)

Posix Threads (Pthreads) Posix Threads (Pthreads) Reference: Programming with POSIX Threads by David R. Butenhof, Addison Wesley, 1997 Threads: Introduction main: startthread( funk1 ) startthread( funk1 ) startthread( funk2 )

More information

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

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 Process creation in UNIX All processes have a unique process id getpid(),

More information

POSIX Threads. Paolo Burgio

POSIX Threads. Paolo Burgio POSIX Threads Paolo Burgio paolo.burgio@unimore.it The POSIX IEEE standard Specifies an operating system interface similar to most UNIX systems It extends the C language with primitives that allows the

More information

Multi-threaded Programming

Multi-threaded Programming Multi-threaded Programming Trifon Ruskov ruskov@tu-varna.acad.bg Technical University of Varna - Bulgaria 1 Threads A thread is defined as an independent stream of instructions that can be scheduled to

More information

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole CS510 Operating System Foundations Jonathan Walpole The Process Concept 2 The Process Concept Process a program in execution Program - description of how to perform an activity instructions and static

More information

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

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 The Process Concept 2 The Process Concept Process a program in execution

More information

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

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017 Preview What are Pthreads? What is Pthreads The Thread ID The Thread Creation The thread Termination The pthread_join() function Mutex The pthread_cancel function The pthread_cleanup_push() function The

More information

Thread and Synchronization

Thread and Synchronization Thread and Synchronization pthread Programming (Module 19) Yann-Hang Lee Arizona State University yhlee@asu.edu (480) 727-7507 Summer 2014 Real-time Systems Lab, Computer Science and Engineering, ASU Pthread

More information

Programming with Shared Memory

Programming with Shared Memory Chapter 8 Programming with Shared Memory 1 Shared memory multiprocessor system Any memory location can be accessible by any of the processors. A single address space exists, meaning that each memory location

More information

real time operating systems course

real time operating systems course real time operating systems course 4 introduction to POSIX pthread programming introduction thread creation, join, end thread scheduling thread cancellation semaphores thread mutexes and condition variables

More information

Shared-Memory Paradigm Multithreading

Shared-Memory Paradigm Multithreading Parallel Systems Course: Chapter II Shared-Memory Paradigm Multithreading Jan Lemeire Dept. ETRO 11/4/2016 1 October 2016 Overview 1. processors and instructions sequences 2. Architecture 3. Usage 4. Java

More information

Shared-Memory Paradigm Multithreading

Shared-Memory Paradigm Multithreading Parallel Systems Course: Chapter II Shared-Memory Paradigm Multithreading Jan Lemeire Dept. ETRO 10/7/2011 1 October 2011 Overview 1. Definition 2. Java Threads 3. POSIX Threads 4. Thread Safety 5. Synchronization

More information

Shared-Memory Paradigm Multithreading

Shared-Memory Paradigm Multithreading Parallel Systems Course: Chapter II Shared-Memory Paradigm Multithreading Jan Lemeire Dept. ETRO 10/26/2012 1 October 2011 Overview 1. processors and instructions sequences 2. Architecture 3. Usage 4.

More information

POSIX Threads Programming

POSIX Threads Programming Tutorials Exercises Abstracts LC Workshops Comments Search Privacy & Legal Notice POSIX Threads Programming Table of Contents 1. 2. 3. 4. 5. 6. 7. 8. 9. Pthreads Overview 1. What is a Thread? 2. What are

More information

Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo. Daniele Loiacono

Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo. Daniele Loiacono Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo References The material in this set of slide is taken from two tutorials by Blaise Barney from the Lawrence Livermore National

More information

CS533 Concepts of Operating Systems. Jonathan Walpole

CS533 Concepts of Operating Systems. Jonathan Walpole CS533 Concepts of Operating Systems Jonathan Walpole Introduction to Threads and Concurrency Why is Concurrency Important? Why study threads and concurrent programming in an OS class? What is a thread?

More information

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

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1 Chapter 4 Threads Images from Silberschatz Pacific University 1 Threads Multiple lines of control inside one process What is shared? How many PCBs? Pacific University 2 Typical Usages Word Processor Web

More information

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

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005 Threads vs. Processes Lecture 4 Thread Programming January 21, 2005 fork() is expensive (time, memory) Interprocess communication is hard. Threads are lightweight processes: one process can contain several

More information

Synchronization Primitives

Synchronization Primitives Synchronization Primitives Locks Synchronization Mechanisms Very primitive constructs with minimal semantics Semaphores A generalization of locks Easy to understand, hard to program with Condition Variables

More information

Modern Computer Architecture. Lecture 10 Multi-Core: pthreads, OpenMP Segmented Scan

Modern Computer Architecture. Lecture 10 Multi-Core: pthreads, OpenMP Segmented Scan Modern Computer Architecture Lecture 10 Multi-Core: pthreads, OpenMP Segmented Scan Outline Multi-Core: pthreads OpenMP Segmented Scan 2 Multi-Core: pthreads, OpenMP Multi- and Many-Core Systems I Tightly

More information

POSIX PTHREADS PROGRAMMING

POSIX PTHREADS PROGRAMMING POSIX PTHREADS PROGRAMMING Download the exercise code at http://www-micrel.deis.unibo.it/~capotondi/pthreads.zip Alessandro Capotondi alessandro.capotondi(@)unibo.it Hardware Software Design of Embedded

More information

Programming Shared Address Space Platforms

Programming Shared Address Space Platforms Programming Shared Address Space Platforms Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar To accompany the text ``Introduction to Parallel Computing'', Addison Wesley, 2003. Topic Overview

More information

Threads. Jo, Heeseung

Threads. Jo, Heeseung Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU

More information

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

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto 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

More information

Oct 2 and 4, 2006 Lecture 8: Threads, Contd

Oct 2 and 4, 2006 Lecture 8: Threads, Contd Oct 2 and 4, 2006 Lecture 8: Threads, Contd October 4, 2006 1 ADMIN I have posted lectures 3-7 on the web. Please use them in conjunction with the Notes you take in class. Some of the material in these

More information

Chapter 4 Concurrent Programming

Chapter 4 Concurrent Programming Chapter 4 Concurrent Programming 4.1. Introduction to Parallel Computing In the early days, most computers have only one processing element, known as the Central Processing Unit (CPU). Due to this hardware

More information

CS Operating Systems: Threads (SGG 4)

CS Operating Systems: Threads (SGG 4) When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate

More information

CS 5523 Operating Systems: Thread and Implementation

CS 5523 Operating Systems: Thread and Implementation When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate

More information

Introduc)on to pthreads. Shared memory Parallel Programming

Introduc)on to pthreads. Shared memory Parallel Programming Introduc)on to pthreads Shared memory Parallel Programming pthreads Hello world Compute pi pthread func)ons Prac)ce Problem (OpenMP to pthreads) Sum and array Thread-safe Bounded FIFO Queue pthread Hello

More information

POSIX Threads Programming

POSIX Threads Programming Tutorials Exercises Abstracts LC Workshops Comments Search Privacy & Legal Notice POSIX Threads Programming Table of Contents 1. Abstract 2. Pthreads Overview 1. What is a Thread? 2. What are Pthreads?

More information

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

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? CS 470 Spring 2019 POSIX Mike Lam, Professor Multithreading & Pthreads MIMD

More information

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University Threads Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) Concurrency

More information

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

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1 Synchronization and Semaphores Copyright : University of Illinois CS 241 Staff 1 Synchronization Primatives Counting Semaphores Permit a limited number of threads to execute a section of the code Binary

More information

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas HPCSE - I «Introduction to multithreading» Panos Hadjidoukas 1 Processes and Threads POSIX Threads API Outline Thread management Synchronization with mutexes Deadlock and thread safety 2 Terminology -

More information

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

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012 Pre-lab #2 tutorial ECE 254 Operating Systems and Systems Programming May 24, 2012 Content Concurrency Concurrent Programming Thread vs. Process POSIX Threads Synchronization and Critical Sections Mutexes

More information

Process Synchronization

Process Synchronization Process Synchronization Part III, Modified by M.Rebaudengo - 2013 Silberschatz, Galvin and Gagne 2009 POSIX Synchronization POSIX.1b standard was adopted in 1993 Pthreads API is OS-independent It provides:

More information

Lecture 19: Shared Memory & Synchronization

Lecture 19: Shared Memory & Synchronization Lecture 19: Shared Memory & Synchronization COMP 524 Programming Language Concepts Stephen Olivier April 16, 2009 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts Forking int pid;

More information

More Shared Memory Programming

More Shared Memory Programming More Shared Memory Programming Shared data structures We want to make data structures that can be shared by threads. For example, our program to copy a file from one disk to another used a shared FIFO

More information

Threads. Jo, Heeseung

Threads. Jo, Heeseung Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU

More information

Multithreading Programming II

Multithreading Programming II Multithreading Programming II Content Review Multithreading programming Race conditions Semaphores Thread safety Deadlock Review: Resource Sharing Access to shared resources need to be controlled to ensure

More information

Introduction to PThreads and Basic Synchronization

Introduction to PThreads and Basic Synchronization Introduction to PThreads and Basic Synchronization Michael Jantz, Dr. Prasad Kulkarni Dr. Douglas Niehaus EECS 678 Pthreads Introduction Lab 1 Introduction In this lab, we will learn about some basic synchronization

More information

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University   Embedded Software Lab. 1 Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University http://nyx.skku.ac.kr Last Class Review 2 ANSI/IEEE POSIX1003.1-95 Standard Thread management Work directly on threads

More information

Shared Memory Parallel Programming

Shared Memory Parallel Programming Shared Memory Parallel Programming Abhishek Somani, Debdeep Mukhopadhyay Mentor Graphics, IIT Kharagpur August 2, 2015 Abhishek, Debdeep (IIT Kgp) Parallel Programming August 2, 2015 1 / 46 Overview 1

More information

CS 6400 Lecture 11 Name:

CS 6400 Lecture 11 Name: Readers and Writers Example - Granularity Issues. Multiple concurrent readers, but exclusive access for writers. Original Textbook code with ERRORS - What are they? Lecture 11 Page 1 Corrected Textbook

More information

COMP 322: Fundamentals of Parallel Programming. Lecture 38: Comparison of Programming Models

COMP 322: Fundamentals of Parallel Programming. Lecture 38: Comparison of Programming Models COMP 322: Fundamentals of Parallel Programming Lecture 38: Comparison of Programming Models Vivek Sarkar Department of Computer Science, Rice University vsarkar@rice.edu https://wiki.rice.edu/confluence/display/parprog/comp322

More information

Threads (SGG 4) Outline. Traditional Process: Single Activity. Example: A Text Editor with Multi-Activity. Instructor: Dr.

Threads (SGG 4) Outline. Traditional Process: Single Activity. Example: A Text Editor with Multi-Activity. Instructor: Dr. When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate

More information

TCSS 422: OPERATING SYSTEMS

TCSS 422: OPERATING SYSTEMS TCSS 422: OPERATING SYSTEMS OBJECTIVES Introduction to threads Concurrency: An Introduction Wes J. Lloyd Institute of Technology University of Washington - Tacoma Race condition Critical section Thread

More information

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 What does a typical thread API look

More information

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

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Race Shared Data: 465 1 8 5 6 209? tail A[] thread switch Enqueue(): A[tail] = 20; tail++; A[tail] = 9; tail++; Thread 0 Thread

More information

CSE 333 Section 9 - pthreads

CSE 333 Section 9 - pthreads CSE 333 Section 9 - pthreads Welcome back to section! We re glad that you re here :) Process A process has a virtual address space. Each process is started with a single thread, but can create additional

More information

Chapter 5: Achieving Good Performance

Chapter 5: Achieving Good Performance Chapter 5: Achieving Good Performance Typically, it is fairly straightforward to reason about the performance of sequential computations. For most programs, it suffices simply to count the number of instructions

More information

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

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1 Synchronization and Semaphores Copyright : University of Illinois CS 241 Staff 1 Synchronization Primatives Counting Semaphores Permit a limited number of threads to execute a section of the code Binary

More information

Paralleland Distributed Programming. Concurrency

Paralleland Distributed Programming. Concurrency Paralleland Distributed Programming Concurrency Concurrency problems race condition synchronization hardware (eg matrix PCs) software (barrier, critical section, atomic operations) mutual exclusion critical

More information

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

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu CPSC 341 OS & Networks Threads Dr. Yingwu Zhu Processes Recall that a process includes many things An address space (defining all the code and data pages) OS resources (e.g., open files) and accounting

More information

THREADS. Jo, Heeseung

THREADS. Jo, Heeseung THREADS Jo, Heeseung TODAY'S TOPICS Why threads? Threading issues 2 PROCESSES Heavy-weight A process includes many things: - An address space (all the code and data pages) - OS resources (e.g., open files)

More information

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks CSci 4061 Introduction to Operating Systems Synchronization Basics: Locks Synchronization Outline Basics Locks Condition Variables Semaphores Basics Race condition: threads + shared data Outcome (data

More information

LSN 13 Linux Concurrency Mechanisms

LSN 13 Linux Concurrency Mechanisms LSN 13 Linux Concurrency Mechanisms ECT362 Operating Systems Department of Engineering Technology LSN 13 Creating Processes fork() system call Returns PID of the child process created The new process is

More information

pthreads CS449 Fall 2017

pthreads CS449 Fall 2017 pthreads CS449 Fall 2017 POSIX Portable Operating System Interface Standard interface between OS and program UNIX-derived OSes mostly follow POSIX Linux, macos, Android, etc. Windows requires separate

More information

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

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5 CS 153 Lab4 and 5 Kishore Kumar Pusukuri Outline Introduction A thread is a straightforward concept : a single sequential flow of control. In traditional operating systems, each process has an address

More information

Threads. lightweight processes

Threads. lightweight processes Threads lightweight processes 1 Motivation Processes are expensive to create. It takes quite a bit of time to switch between processes Communication between processes must be done through an external kernel

More information

Threads. Threads (continued)

Threads. Threads (continued) Threads A thread is an alternative model of program execution A process creates a thread through a system call Thread operates within process context Use of threads effectively splits the process state

More information

Processes, Threads, SMP, and Microkernels

Processes, Threads, SMP, and Microkernels Processes, Threads, SMP, and Microkernels Slides are mainly taken from «Operating Systems: Internals and Design Principles, 6/E William Stallings (Chapter 4). Some materials and figures are obtained from

More information

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

Programming with Shared Memory. Nguyễn Quang Hùng Programming with Shared Memory Nguyễn Quang Hùng Outline Introduction Shared memory multiprocessors Constructs for specifying parallelism Creating concurrent processes Threads Sharing data Creating shared

More information

Shared Memory Programming Models III

Shared Memory Programming Models III Shared Memory Programming Models III Stefan Lang Interdisciplinary Center for Scientific Computing (IWR) University of Heidelberg INF 368, Room 532 D-69120 Heidelberg phone: 06221/54-8264 email: Stefan.Lang@iwr.uni-heidelberg.de

More information

CS 3305 Intro to Threads. Lecture 6

CS 3305 Intro to Threads. Lecture 6 CS 3305 Intro to Threads Lecture 6 Introduction Multiple applications run concurrently! This means that there are multiple processes running on a computer Introduction Applications often need to perform

More information

About me Now that you know the pthread API

About me Now that you know the pthread API pthread Examples About me Now that you know the pthread API How do you create threads? How do you pass different values to them? How do you return values from threads? What are some common mistakes? Friday:

More information

Programming Languages

Programming Languages Programming Languages Dr. Michael Petter WS 2016/17 Exercise Sheet 3 Assignment 3.1 Lockfree vs. locked programming The purpose of the following exercises is to get acquainted with the pthreads library,

More information

C Grundlagen - Threads

C Grundlagen - Threads Michael Strassberger saremox@linux.com Proseminar C Grundlagen Fachbereich Informatik Fakultaet fuer Mathematik, Informatik und Naturwissenschaften Universitaet Hamburg 3. Juli 2014 Table of Contents 1

More information

A Programmer s View of Shared and Distributed Memory Architectures

A Programmer s View of Shared and Distributed Memory Architectures A Programmer s View of Shared and Distributed Memory Architectures Overview Shared-memory Architecture: chip has some number of cores (e.g., Intel Skylake has up to 18 cores depending on the model) with

More information

für Mathematik in den Naturwissenschaften Leipzig

für Mathematik in den Naturwissenschaften Leipzig Max-Planck-Institut für Mathematik in den Naturwissenschaften Leipzig Implementation and Usage of a Thread PoolbasedonPOSIXThreads by Ronald Kriemann Technical Report no.: 2 2003 Implementation and Usage

More information

Running example. Threads. Overview. Programming Shared-memory Machines 3/21/2018

Running example. Threads. Overview. Programming Shared-memory Machines 3/21/2018 Running example Programming Shared-memory Machines Some slides adapted from Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar ``Introduction to Parallel Computing'', Addison Wesley, 2003. Algorithmic

More information

High Performance Computing Course Notes Shared Memory Parallel Programming

High Performance Computing Course Notes Shared Memory Parallel Programming High Performance Computing Course Notes 2009-2010 2010 Shared Memory Parallel Programming Techniques Multiprocessing User space multithreading Operating system-supported (or kernel) multithreading Distributed

More information

Computer Systems Laboratory Sungkyunkwan University

Computer Systems Laboratory Sungkyunkwan University Threads Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Today s Topics Why threads? Threading issues 2 Processes Heavy-weight A process includes

More information

Introduction to parallel computing

Introduction to parallel computing Introduction to parallel computing Shared Memory Programming with Pthreads (3) Zhiao Shi (modifications by Will French) Advanced Computing Center for Education & Research Vanderbilt University Last time

More information

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

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2 CS345 Opera,ng Systems Φροντιστήριο Άσκησης 2 Inter- process communica0on Exchange data among processes Methods Signals Pipes Sockets Shared Memory Sockets Endpoint of communica,on link between two programs

More information

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

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid. Threads Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 1 CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department Virginia Tech Threads Outline Threads (Chapter

More information

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions CS 470 Spring 2018 Mike Lam, Professor Semaphores and Conditions Synchronization mechanisms Busy-waiting (wasteful!) Atomic instructions (e.g., LOCK prefix in x86) Pthreads Mutex: simple mutual exclusion

More information

POSIX Threads: a first step toward parallel programming. George Bosilca

POSIX Threads: a first step toward parallel programming. George Bosilca POSIX Threads: a first step toward parallel programming George Bosilca bosilca@icl.utk.edu Process vs. Thread A process is a collection of virtual memory space, code, data, and system resources. A thread

More information