(p)Threads Libraries Math 442 es Jim Fix. Life cycle of a thread.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
PTHREADS These notes are from LLNL Pthreads Tutorial
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
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.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Jonathan Walpole Computer Science Portland State University
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.
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
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,
Programming with POSIX* Threads Intel Software College.
Pthreads: A shared memory programming model
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
POSIX Threads HUJI Spring 2011.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
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
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
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.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
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.
CS 537 – Introduction to Operating Systems
Realizing Concurrency using the thread model
PThreads.
Shared-Memory Programming with Threads
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
CSE 333 – Section 9 Threads.
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Thread Programming.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
BOOM! count is [ ], should be
Jonathan Walpole Computer Science Portland State University
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
POSIX Threads(pthreads)
Presentation transcript:

(p)Threads Libraries Math 442 es Jim Fix

Life cycle of a thread

Creation/Joining/Exit When a process is first created, it has one thread of execution. (C) executes main That thread spawns other threads (pthread) specify initial code for new thread Each, in turn, can spawn more threads Any thread can wait for a specified thread to ``join” it (pthread) blocks until other other thread terminates

Using libpthread.a: creation Creating a thread (in Terminal:``man pthread_create”): int pthread_create( pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); thread: reference to a thread descriptor; will get filled with new thread’s info attr: reference to a thread attribute struct start_routine: proc that new thread executes arg: parameter for that start routine

Using libpthread.a: join/exit To wait for a another thread to terminate: int pthread_join( pthread_t *other_thread, void **value_h); other_thread: descriptor of thread to wait for ret_value: handle to value returned by other thread Other thread joins this join-caller when: it calls int pthread_exit(void *value_p) it returns (a value) from its start routine

Simple example: patrub.c long pat() { for (long i=0; i< L; i++) printf("pat #%d\n",i); return i; } long rub() { for (long i=0; i< L; i++) printf("rub #%d\n",i); } return i; } int main(int argc, char **argv) { pthread_t id1, id2; long val1, val2; pthread_attr_t a; pthread_attr_init(&a); pthread_create(&id1,&a,(void *(*)(void *))(&pat),NULL); pthread_create(&id2,&a,(void *(*)(void *))(&rub),NULL); pthread_join(id1,(void **)&val1); pthread_join(id2,(void **)&val2); printf("Completed pats: %d...rubs:%d\n",val1,val2); pthread_exit(NULL); } other_thread: descriptor of thread to wait for ret_value: handle to value returned by other thread Other thread joins this join-caller when: it calls int pthread_exit(void *value_p) it returns (a value) from its start routine

The Trouble w/ Concurrency Suppose one thread executes this code: 1.void count1(int *c) { 2. while (1) { 3. (*c) = (*c) + 1; 4. } 5.}...and another executes this code: 1.void count2(int *c) { 2. while (1) { 3. (*c) = (*c) + 1; 4. } 5.}... and the main driving code looks like this: static int shared_counter = 0;... {... pthread_create(&id1,NULL,&count1,&shared_counter); pthread_create(&id2,NULL,&count2,&shared_counter);...} What can go wrong?

What’s Really Happening The code: (*c) = (*c) + 1; is really code like: 1.mov r28,r24 2.mov r29,r ld r18,Y 5.ldd r19,Y+1 6.subi r18,lo8(-1) 7.sbci r19,hi8(-1) 8.st r18,Y 9.std r19,Y The increment op is not an ``atomic” one. When two threads run concurrently, say, (even) on a single processor (with preemption), their execution could be interleaved.

One Possible Interleaving 1.ld r18,Y 2.ldd r19,Y+1 3.subi r18,lo8(-1) 4.sbci r19,hi8(-1) 5.st r18,Y 6.std r19,Y+1 7.ld r18,Y 8.ldd r19,Y+1 9.subi r18,lo8(-1) 10.sbci r19,hi8(-1) 11.st r18,Y 12.std r19,Y+1 13.ld r18,Y 14.ldd r19,Y+1 15.subi r18,lo8(-1) 16.sbci r19,hi8(-1) 17.st r18,Y 18.std r19,Y+1 19.ld r18,Y 20.ldd r19,Y+1 21.subi r18,lo8(-1) 22.sbci r19,hi8(-1) 23.st r18,Y 24.std r19,Y+1 25.ld r18,Y 26.ldd r19,Y+1 27.subi r18,lo8(-1) 28.sbci r19,hi8(-1) 30.adds #1,r1 31.mov.w thread 1 thread 2

An Different Interleaving 1.ld r18,Y 2.ldd r19,Y+1 3.ld r18,Y 4.ldd r19,Y+1 5.subi r18,lo8(-1) 6.sbci r19,hi8(-1) 7.st r18,Y 8.std r19,Y+1 9.ld r18,Y 10.ldd r19,Y+1 11.subi r18,lo8(-1) 12.sbci r19,hi8(-1) 13.st r18,Y 14.std r19,Y+1 15.subi r18,lo8(-1) 16.sbci r19,hi8(-1) 17.st r18,Y 18.std r19,Y+1 19.ld r18,Y 20.ldd r19,Y+1 21.subi r18,lo8(-1) 22.sbci r19,hi8(-1) 23.st r18,Y 24.std r19,Y+1 What happens? (remember: registers are unshared) thread 1 thread 2

A test: counters.c (part I) The main driver: 1.int main(int argc, char **argv) { 2. long counter = 0; 3. pthread_t id1, id2; 4. long rv1, rv2; 5. pthread_attr_t a; pthread_attr_init(&a); 6. pthread_create(&id1,&a,(void *(*)(void *))(&count), 7. (void *)&counter); 8. pthread_create(&id2,&a,(void *(*)(void *))(&count), 9. (void *)&counter); 10. pthread_join(id1,(void **)&val1); 11. pthread_join(id2,(void **)&val2); 12. printf("%d + %d = %d?\n",val1,val2,counter); 13. return 0; 14.}

A test: counters.c (part II) Each counter thread’s code: 1.long count(long *c) { 2. long i; 3. for (i=0; i< L; i++) { 4. int j = (*c); 5. (*c) = j+1; 6. } 7. return i; 8.} Let’s run it and see ( go to Terminal )

Increment is a critical section of the code: long i; for (i=0; i< L; i++) { int j = (*c); (*c) = j+1; } return i; The counter is shared by both threads. When they increment, they should have exclusive access to it. 1. } The Problem

Mutexes Most thread libraries (including POSIX) provide mutex variables for locking critical sections: 1.pthread_mutex_t m; 2.pthread_mutexattr_t m_attr; 3.pthread_mutex_init(&m,&m_attr); 4.// initializes a mutex variable 5.pthread_mutex_lock(&m); 6.// gives access to m, other threads block or wait. 7.pthread_mutex_unlock(&m); 8.// releases m, gives to a waiting thread. 9.The mutex variable provides mutual exclusion.

Locking a Crit. Sect. Here is the new version of the counter code: 1.long count(counter_m *cm) { 2. long *c = cm->counter; 3. pthread_mutex_t *m = cm->mutex; 4. long i; 5. for (i=0; i< L; i++) { 6. pthread_mutex_lock(m); // acquire the lock 7. int j = (*c); 8. (*c) = j+1; 9. pthread_mutex_unlock(m); // release the lock 10. } 11. return i; 12.} Let’s run it. (go to Terminal)

A question for your consideration: How does one build a mutex???? (i.e. a mutex library for a threading system????)

Thread Synchronization Synchronizing concurrent threads’ access to shared data/resources is crucial. See: (thanks to UCB’s cs16x) space shuttlespace shuttle (Garman, 1981) Therac-25Therac-25 (Leveson, 1995)