40d5984d819aaa72e55aa10376b73bde_MIT6_087IAP10_lec12.pdf

SagarYadav642223 7 views 30 slides Jun 24, 2024
Slide 1
Slide 1 of 30
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30

About This Presentation

The given given is about the MIT basucs standards and basic instrucgions for performing


Slide Content

Outline
Review
Multithreaded prog ramming
Concepts
Pthread
API
Mutex
Condition v ariables
1

6.087 Lecture 12 – January 27, 2010
Review
Multithreaded prog ramming
Concepts
Pthread
API
Mutex
Condition v ariables
2

Review: malloc()
• Mapping memor y: mmap(), munmap(). Useful f or
demand paging.
• Resizing heap: sbrk()
• Designing malloc()
• implicit link ed list,explicit link ed list
• best fit,first fit,ne xt fit
Problems:•
• fragmentation
• memory leaks
• valgrind –tool=memchec k, checks for memory leaks.
2

Garbage collection
• C does not ha ve any garbage collectors
• Implementations a vailable
• Types:
• Mark and s weep garbage collector (depth first search)
• Cheney’s algorithm (breadth first search)
• Copying garbage collector
3

6.087 Lecture 12 – January 27, 2010
Review
Multithreaded prog ramming
Concepts
Pthread
API
Mutex
Condition v ariables
4

Preliminaries: Parallel computing
• Parallelism: Multiple computations are done
simultaneously.
• Instruction le vel (pipelining)
• Data par allelism (SIMD)
• Task parallelism (embarr assingly par allel)
• Concurrency: Multiple computations that ma y be done in
parallel.
• Concurrency vs . Parallelism
4

Process vs. Threads
• Process: An instance of a prog ram that is being e xecuted
in its o wn address space . In POSIX systems , each
process maintains its o wn heap, stack, registers, file
descriptors etc.
Communication:
• Shared memor y
Network•
• Pipes, Queues
• Thread: A light w eight process that shares its address
space with others .In POSIX systems , each thread
maintains the bare essentials: registers, stack, signals.
Communication:
• shared address space .
5

Multithreaded concurrency
Serial execution:
• All our prog rams so f ar has had a single thread of
execution: main thread.
• Program exits when the main thread e xits.
Multithreaded:
• Program is organiz ed as m ultiple and concurrent threads
of execution.
• The main thread spa wns multiple threads .
• The thread ma y communicate with one another .
• Advantages:
• Improves performance
• Improves responsiveness
• Improves utilization
• less o verhead compared to m ultiple processes
6

Multithreaded programming
Even in C , multithread prog ramming ma y be accomplished in
several ways
• Pthreads: POSIX C libr ary.
• OpenMP
• Intel threading b uilding b locks
• Cilk (from CSAIL!)
• Grand central despatch
• CUDA (GPU)
• OpenCL (GPU/CPU)
7

Not all code can be made parallel
f l o a t params [ 1 0 ] ;
f o r ( i n t i =0; i <10; i ++)
do_something ( params [ i ] ) ;
f l o a t params [ 1 0 ] ;
f l o a t prev =0;
fo r ( i n t i =0; i <10; i ++)
{
prev=complicated ( params [ i ] , prev ) ;
}
paralleizable not parallelizable
8

Not all multi-threaded code is safe
int balance =500;
void deposit ( int sum ) {
int currbalance=balance ; /∗ read balance ∗/
...
currbalance+=sum ;
balance=currbalance ; /∗ write balance ∗/
}
void withdraw ( int sum ) {
int currbalance=balance ; /∗ read balance ∗/
if ( currbalance >0)
currbalance−=sum ;
balance=currbalance ; /∗ write balance ∗/
}
..
deposit (100); /∗ thread 1∗/
..
withdraw (50);/ thread 2∗/
..
withdraw (100); /∗ thread 3∗/
...
• minimize use of global/static memor y
• Scenario: T1(read),T2(read,write),T1(write) ,balance=600
• Scenario: T2(read),T1(read,write),T2(write) ,balance=450
9

6.087 Lecture 12 – January 27, 2010
Review
Multithreaded prog ramming
Concepts
Pthread
API
Mutex
Condition v ariables
10

Pthread
API:
• Thread management: creating, joining, attr ibutes
pthread_
• Mutexes: create, destroy mutexes
pthread_mutex_
• Condition v ariables: create,destroy,wait,signal
pthread_cond_
• Synchronization: read/write locks and barr iers
pthread_rwlock_ , pthread_barrier_
API:
• #include <pthread.h>
• gcc −Wall −O0 −o <output> file.c −pthread (no −l prefix)
10

Creating threads
int pthread_create ( pthread_t thread , ∗
const pthread _attr _t attr , ∗
void ∗(∗ start _routine )( void ∗ ), void arg ); ∗
• creates a ne w thread with the attr ibutes specified b y attr.
Default attributes are used if attr is NULL. •
• On success, stores the thread it into thread
• calls function start_routine(arg) on a separ ate
thread of e xecution.
• returns zero on success , non-zero on error .
void pthread_exit(void ∗value_ptr);
• called implicitly when thread function e xits.
• analogous to exit().
11

Example
# i n c l u d e < p t h r e a d . h>
# i n c l u d e < s t d i o . h>
# d e f i n e NUM_THREADS 5
void ∗ P r i n t H e l l o ( void ∗t h r e a d i d )
{
long t i d ;
t i d = ( long ) t h r e a d i d ;
p r i n t f ( " H e l l o World ! I t ’ s me, t h r e a d #%l d ! \ n " , t i d ) ;
p t h r e a d _ e x i t ( NULL ) ;
}
i n t main ( i n t a r g c , char ∗a r g v [ ] )
{
p t h r e a d _ t t h r e a d s [ NUM_THREADS ] ;
i n t r c ;
long t ;
f o r ( t = 0 ; t <NUM_THREADS ; t + + ) {
p r i n t f ( " I n main : c r e a t i n g t h r e a d %l d \ n " , t ) ;
r c = p t h r e a d _ c r e a t e (& t h r e a d s [ t ] , NULL , P r i n t H e l l o , ( void ∗) t ) ;
i f ( r c ) {
p r i n t f ( "ERROR; r e t u r n code f r o m p t h r e a d _ c r e a t e ( ) i s %d \ n " , r c ) ;
e x i t ( −1 ) ;
}
}
p t h r e a d _ e x i t ( NULL ) ;
}
code: https://computing.llnl.gov/tutorials/pthreads/
© Lawrence Livermore National Laboratory. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse.

Output
In main : c r e a t i n g thread 0 In main : c r e a t i n g thread 0
In main : c r e a t i n g thread 1 Hello World ! I t ’ s me, thread #0!
Hello World ! I t ’ s me, thread #0! In main : c r e a t i n g thread 1
Hello World ! I t ’ s me, thread #1! Hello World ! I t ’ s me, thread #1!
In main : c r e a t i n g thread 2 In main : c r e a t i n g thread 2
In main : c r e a t i n g thread 3 Hello World ! I t ’ s me, thread #2!
Hello World ! I t ’ s me, thread #2! In main : c r e a t i n g thread 3
Hello World ! I t ’ s me, thread #3! Hello World ! I t ’ s me, thread #3!
In main : c r e a t i n g thread 4 In main : c r e a t i n g thread 4
Hello World ! I t ’ s me, thread #4! Hello World ! I t ’ s me, thread #4!
13

Synchronization: joining
Figure: https://computing.llnl.gov/tutorials/pthreads
int pthread_join(pthread_t thread, void ∗∗value_ptr);
• pthread_join() blocks the calling thread until the specified thread
terminates.
• If value_ptr is not null, it will contain the return status of the
called thread
Other ways to synchronize: mutex,condition variables
Courtesy of Lawrence Livermore National Laboratory. Used with permission.
14
© Lawrence Livermore National Laboratory. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse.

Example
#define NELEMENTS 5000
#define BLK_SIZE 1000
#define NTHREADS (NELEMENTS/BLK_SIZE)
int main ( int argc , char ∗argv [])
{
pthread_t thread [NUM_THREADS] ;
pthread _attr _t attr ;
int rc ; long t; void ∗status ;
/∗ Initialize and set thread detached attribute ∗/
pthread _attr _init(&attr );
pthread _attr_setdetachstate(&attr , PTHREAD_CREATE_JOINABLE);
for ( t =0; t <NUM_THREADS; t ++) {
printf("Main: creating thread %ld\n ", t);
rc = pthread_create(&thread[t], &attr , work, ( void ∗ )( t ∗BLK_SIZE ) ) ;
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n ", rc); exit( −1);
}
}
/∗ 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);
if (rc) {
printf("ERROR; return code from pthread _join() is %d\n ", rc);exit( −1);
}
}
printf("Main: program completed. Exiting.\n ");
}
15

Mutex
• Mutex (mutual exclusion) acts as a "lock" protecting access
to the shared resource .
• Only one thread can "o wn" the m utex at a time . Threads
must take turns to loc k the m utex.
int pthread_mutex_destroy ( pthread_mutex_t ∗mutex ) ;
int pthread_mutex_init ( pthread_mutex_t mutex , ∗
const pthread_mutexattr_t attr ); ∗
thread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
• pthread_mutex_init() initializes a m utex. If attributes are NULL,
default attributes are used.
The macro PTHREAD_MUTEX_INITIALIZER can be used to initializ e•
static m utexes.
pthread_mutex_destroy() destroys the m utex.•
• Both function return return 0 on success, non zero on error.
16

Mutex
int pthread_mutex_lock ( pthread_mutex_t ∗mutex ) ;
int pthread_mutex_trylock ( pthread_mutex_t ∗mutex ) ;
int pthread_mutex_unlock ( pthread_mutex_t ∗mutex ) ;
• pthread_mutex_lock() locks the giv en mutex. If the m utex is locked,
the function is b locked until it becomes a vailable.
• pthread_mutex_trylock() is the non-b locking v ersion. If the m utex is
currently loc ked the call will retur n immediately.
pthread_mutex_unlock() unlocks the m utex. •
17

Example revisited
int balance =500;
void deposit ( int sum ) {
int currbalance=balance ; /∗ read balance ∗/
...
currbalance+=sum ;
balance=currbalance ; /∗ write balance ∗/
}
void withdraw ( int sum ) {
int currbalance=balance ; /∗ read balance ∗/
if ( currbalance >0)
currbalance−=sum ;
balance=currbalance ; /∗ write balance ∗/
}
..
deposit (100); /∗ thread 1∗/
..
withdraw (50);/ thread 2∗/
..
withdraw (100); /∗ thread 3∗/
...
• Scenario: T1(read),T2(read,write),T1(write),balance=600
• Scenario: T2(read),T1(read,write),T2(write),balance=450
18

Using mutex
int balance =500;
pthread_mutex_t mutexbalance=PTHREAD_MUTEX_INITIALIZER;
void deposit ( int sum ) {
pthread_mutex_lock(&mutexbalance );
{
int currbalance=balance ; /∗ read balance ∗/
...
currbalance+=sum ;
balance=currbalance ; /∗ write balance ∗/
}
pthread_mutex_unlock(&mutexbalance );
}
void withdraw ( int sum ) {
pthread_mutex_lock(&mutexbalance );
{
int currbalance=balance ; /∗ read balance ∗/
if ( currbalance >0)
currbalance−=sum ;
balance=currbalance ; /∗ write balance ∗/
}
pthread_mutex_unlock(&mutexbalance );
}
.. deposit(100); /∗ thread 1∗/
.. withdraw (50);/ thread 2∗/
.. withdraw(100); /∗ thread 3∗/
• Scenario: T1(read,write),T2(read,write),balance=550
• Scenario: T2(read),T1(read,write),T2(write),balance=550
19

Condition variables
Sometimes loc king or unloc king is based on a r un-time
condition (e xamples?).Without condition v ariables, program
would ha ve to poll the v ariable/condition contin uously.
Consumer:
(a) lock mutex on global item v ariable
(b) wait for (item>0) signal from producer (m utex unlocked
automatically).
(c) wake up when signalled (m utex locked again
automatically), unloc k mutex and proceed.
Producer:
(1) produce something
(2) Lock global item v ariable, update item
(3) signal w aiting (threads)
(4) unlock mutex
20

Condition variables
int pthread_cond_destroy ( pthread_cond_t ∗cond );
int pthread _cond_init(pthread_cond_t cond, const pthread_condattr_t attr ); ∗ ∗
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
• pthread_cond_init() initialized the condition v ariable. If attr is NULL,
default attributes are sed.
• pthread_cond_destroy() will destroy (uninitialize) the condition
variable.
• destroying a condition v ariable upon which other threads
are currently b locked results in undefined beha vior.
macro PTHREAD_COND_INITIALIZER can be used to initializ e condition •
variables. No error chec ks are perf ormed.
Both function retur n 0 on success and non-z ero otherwise.•
21

Condition variables
int pthread_cond_destroy ( pthread_cond_t ∗cond );
int pthread _cond_init(pthread_cond_t cond, const pthread_condattr_t attr ); ∗ ∗
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
• pthread_cond_init() initialized the condition v ariable. If attr is NULL,
default attributes are sed.
• pthread_cond_destroy() will destroy (uninitialize) the condition
variable.
• destroying a condition v ariable upon which other threads
are currently b locked results in undefined beha vior.
macro PTHREAD_COND_INITIALIZER can be used to initializ e condition •
variables. No error chec ks are perf ormed.
Both function retur n 0 on success and non-z ero otherwise.•
22

Condition variables
int pthread_cond_wait(pthread_cond_t ∗cond,pthread_mutex_t ∗mutex);
blocks on a condition v ariable. •
• must be called with the m utex already loc ked otherwise
behavior undefined.
• automatically releases m utex
• upon successful retur n, the m utex will be automatically
locked again.
int pthread_cond_broadcast(pthread_cond_t ∗cond);
int pthread_cond_signal(pthread_cond_t ∗cond);
• unblocks threads w aiting on a condition v ariable.
• pthread_cond_broadcast() unlocks all threads that are w aiting.
• pthread_cond_signal() unlocks one of the threads that are w aiting.
• both return 0 on success , non z ero otherwise.
23

Example
#include <pthread .h>
pthread_cond_t cond_recv=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_send=PTHREAD_COND_INITIALIZER;
pthread_mutex_t cond_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t count_mutex=PTHREAD_MUTEX_INITIALIZER;
int full =0;
int count =0;
void∗ produce ( void ∗) void∗ consume ( void ∗)
{ {
while (1) while (1)
{ {
pthread_mutex_lock(&cond_mutex); pthread_mutex_lock(&cond_mutex);
while ( full ) while (! full)
{ {
pthread_cond_wait(&cond_recv, pthread_cond_wait(&cond_send,
&cond_mutex ) ; &cond_mutex ) ;
} }
pthread_mutex_unlock(&cond_mutex); pthread_mutex_unlock(&cond_mutex);
pthread_mutex_lock(&count_mutex); pthread_mutex_lock(&count_mutex);
count++;full=1; full=0;
printf("produced(%d):%d\n", printf(" consumed(%ld):%d\n ",
pthread_self(),count); pthread _self(),count);
pthread_cond_broadcast(&cond_send); pthread_cond_broadcast(&cond_recv);
pthread_mutex_unlock(&count_mutex); pthread_mutex_unlock(&count_mutex);
if ( count >=10) break ; if ( count >=10)break ;
} }
24

Example
int main ()
{
pthread_t cons_thread , prod_thread ;
pthread_create(&prod_thread ,NULL,produce ,NULL);
pthread_create(&cons_thread ,NULL,consume,NULL);
pthread _join(cons_thread ,NULL);
pthread _join(prod_thread ,NULL);
return 0;
}
Output:
produced (3077516144):1
consumed(3069123440):1
produced (3077516144):2
consumed(3069123440):2
produced (3077516144):3
consumed(3069123440):3
produced (3077516144):4
consumed(3069123440):4
produced (3077516144):5
consumed(3069123440):5
produced (3077516144):6
consumed(3069123440):6
produced (3077516144):7
consumed(3069123440):7
25

Summary
• Parallel prog ramming concepts
• Multithreaded prog ramming
Pthreads•
• Syncrhonization
Mutex •
Condition v ariables •
26

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
MIT OpenCourseWare
http://ocw.mit.edu
6.087 Practical Programming in C
January (IAP) 2010

For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.
Tags