SlideShare a Scribd company logo
1 of 21
Parallel and Distributed
Computing
CS3006 (BCS-6E-6F)
Lecture 07
Danyal Farhat
FAST School of Computing
NUCES Lahore
2
Parallel and Distributed Computing
Dr. Haroon Mahmood
Programming Shared Address Space
Platforms using POSIX Thread API
3
Parallel and Distributed Computing
Dr. Haroon Mahmood
Thread
 A thread is “an independent stream of
instructions that can be scheduled to run by
the operating system”
 A thread is also a “procedure that runs
independently from its main program”
 Pthreads have been specified (for UNIX) by
IEEE POSIX 1003.1c standard (1995)
 There exist other threads libraries, such as
Java threads, Solaris threads etc.
4
Parallel and Distributed Computing
Dr. Haroon Mahmood
Multithreading
 Operating system facility that enables an
application to create threads of execution within a
process
 Many different users can run programs that
appears to be running at the same time
 However with single processing unit, they are not
running at exact same time
 Operating system switches available resources
from one running program to another
 Multiple threads exist within each process and
share resources like memory
5
Parallel and Distributed Computing
Dr. Haroon Mahmood
Pthreads
Posix thread API
 standard threads API, supported by most vendors
 Pthreads are interesting for:
 Overlapping I/O and CPU work; some threads can
block for I/O while others can continue
 Scheduling and load balancing
 Ease of programming and widespread use
 In parallel programming they can be very useful, since
communications between threads are much faster (3-5
times)
6
Parallel and Distributed Computing
Dr. Haroon Mahmood
Threads: Pthreads API
 Thread management
 Create, detach, join, thread attributes
 Mutexes
 Mutual exclusion, create, destroy, lock, unlock, mutex
attributes
 Condition variables
 Create, destroy, wait, signal, programmer specified
conditions, condition variable attributes
 pthreads.h header file
 Pthreads are defined for C; some FORTRAN compilers also
have Pthreads API (e.g. IBM AIX)
 Pthreads API are based on over 60 calls pthread_* ;
7
Parallel and Distributed Computing
Dr. Haroon Mahmood
Function: pthread_create
int pthread_create (
pthread_t *thread_handle,
const pthread_attr_t *attribute,
void * (*thread_function)(void *),
void *arg);
 thread_handle unique identifier
 attribute NULL for default attributes
 thread_function C routine executed once thread is
created
 arg a single argument that may be passed to
thread_function; NULL for no argument
 It can be called any number of times, from anywhere, there
is no hierarchy or dependency
8
Parallel and Distributed Computing
Dr. Haroon Mahmood
Threads: Joining and detaching threads
 int pthread_join(pthread_t thread_handle,
void **value_ptr)
 It is possible to get return status if specified in
pthread_exit()
 Only one pthread_join() call can be matched
 Thread can be joinable or detached (no possibility to join);
it is better to declare it for portability!
 Int pthread_detach(pthread_t thread); is used to indicate
to the implementation that storage for the thread can be
reclaimed when the thread terminates. If thread has not
terminated, pthread_detach() will not cause it to
terminate. It works even if thread was created as joinable
9
Parallel and Distributed Computing
Dr. Haroon Mahmood
Threads
 POSIX standard does not specify stack size for a thread;
exceeding the limit produces Segmentation fault
 Safe and portable programs explicitly allocate enough
stack
10
Parallel and Distributed Computing
Dr. Haroon Mahmood
Threads
11
Parallel and Distributed Computing
Dr. Haroon Mahmood
Thread Example
 #include <pthread.h>
 #include <stdio.h>
 #define NUM_THREADS 5
 void *PrintHello(void *threadid)
 {
 long tid;
 tid = (long)threadid;
 printf("Hello World! It's me, thread #%ld!n", tid);
pthread_exit(NULL);
 }
12
Parallel and Distributed Computing
Dr. Haroon Mahmood
Thread Example
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc; long t;
for(t=0; t<NUM_THREADS; t++)
{
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %dn", rc);
exit(-1);
}
}
}
13
Parallel and Distributed Computing
Dr. Haroon Mahmood
Synchronization problem
 Example: Bank Transactions
 Current balance = Rs. 70,000
 Check deposited = Rs. 10,000
 ATM withdrawn = Rs. 5,000
 Correct Balance after both transactions
 Balance = 75000
What if both transactions are initiated at the same time!
 Check Deposit:
 MOV A, Balance // A = 70,000
 ADD A, Deposited // A = 80,000
 ATM Withdrawal:
 MOV B, Balance // B = 70,000
 SUB B, Withdrawn // B = 65,000
14
Parallel and Distributed Computing
Dr. Haroon Mahmood
Mutual exclusion
 Mutual exclusion variables (Mutex) work like a lock
protecting access to a shared resource
 Only one thread can lock a mutex at a moment; even if
more than one thread tries to lock the mutex, only one will
be successful; this avoids race
 Sequence:
 Creation of the mutex
 More than one thread tries to lock the mutex
 Only one locks it
 The owner makes changes
 The owner unlocks it
 Another thread gets mutex (it was blocked, unblocking is
automatic) and the process repeats
 At the end mutex is destroyed
15
Parallel and Distributed Computing
Dr. Haroon Mahmood
Critical Section
do
{
Entry section
critical section
Exit section
remainder section
} while(1)
16
Parallel and Distributed Computing
Dr. Haroon Mahmood
Mutual exclusion
 The Pthreads API provides the following functions for
handling mutex-locks:
int pthread_mutex_lock (
pthread_mutex_t *mutex_lock);
int pthread_mutex_unlock (
pthread_mutex_t *mutex_lock);
int pthread_mutex_init (
pthread_mutex_t *mutex_lock,
const pthread_mutexattr_t *lock_attr);
17
Parallel and Distributed Computing
Dr. Haroon Mahmood
Mutual exclusion
 For example:
pthread_mutex_t total_cost_lock;
...
main() {
....
pthread_mutex_init(&total_cost_lock, NULL);
....
}
void *add_cost(void *costn) {
....
pthread_mutex_lock(&total_cost_lock);
total_cost = total_cost + costn;
/* and unlock the mutex */
pthread_mutex_unlock(&total_cost_lock);
}
18
Parallel and Distributed Computing
Dr. Haroon Mahmood
Locking overhead
 Locks represent serialization points since critical sections
must be executed by threads one after the other.
 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 an alternate function,
pthread_mutex_trylock.
int pthread_mutex_trylock (
pthread_mutex_t *mutex_lock);
 pthread_mutex_trylock is typically much faster than
pthread_mutex_lock on typical systems since it does not
have to deal with queues associated with locks for multiple
threads waiting on the lock.
19
Parallel and Distributed Computing
Dr. Haroon Mahmood
Trylock()
pthread_mutex_t total_cost_lock;
Int lock_status;
main() {
pthread_mutex_init(&total_cost_lock, NULL);
....
}
void *add_cost(void *costn) {
....
Lock_status pthread_mutex_trylock(&total_cost_lock);
If (lock_status == EBUSY)
addlater;
else
total_cost = total_cost + costn;
/* and unlock the mutex */
pthread_mutex_unlock(&total_cost_lock);
}
20
Parallel and Distributed Computing
Dr. Haroon Mahmood
Additional Resources
 Introduction to Parallel Computing by Ananth Grama and
Anshul Gupta
 Chapter 7: Programming Shared Address Space Platforms
21
Parallel and Distributed Computing
Dr. Haroon Mahmood
Thank You!

More Related Content

Similar to PDCCLECTUREE.pptx

Parallel program design
Parallel program designParallel program design
Parallel program designZongYing Lyu
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingguesta40f80
 
Shared Memory Programming with Pthreads (1).ppt
Shared Memory Programming with Pthreads (1).pptShared Memory Programming with Pthreads (1).ppt
Shared Memory Programming with Pthreads (1).pptMALARMANNANA1
 
Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTSjulien pauli
 
1032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.21032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.2Stanley Ho
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsDaniel Blezek
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6Techvilla
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAMaulik Borsaniya
 
JAVA THREADS.pdf
JAVA THREADS.pdfJAVA THREADS.pdf
JAVA THREADS.pdfMohit Kumar
 
Rust and Eclipse
Rust and EclipseRust and Eclipse
Rust and EclipseMax Bureck
 
.Net Multithreading and Parallelization
.Net Multithreading and Parallelization.Net Multithreading and Parallelization
.Net Multithreading and ParallelizationDmitri Nesteruk
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded ProgrammingSri Prasanna
 

Similar to PDCCLECTUREE.pptx (20)

Sysprog 14
Sysprog 14Sysprog 14
Sysprog 14
 
Pthread
PthreadPthread
Pthread
 
Parallel program design
Parallel program designParallel program design
Parallel program design
 
posix.pdf
posix.pdfposix.pdf
posix.pdf
 
Threads
ThreadsThreads
Threads
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programming
 
Shared Memory Programming with Pthreads (1).ppt
Shared Memory Programming with Pthreads (1).pptShared Memory Programming with Pthreads (1).ppt
Shared Memory Programming with Pthreads (1).ppt
 
Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTS
 
1032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.21032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.2
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
 
Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUs
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
 
JAVA THREADS.pdf
JAVA THREADS.pdfJAVA THREADS.pdf
JAVA THREADS.pdf
 
Rust and Eclipse
Rust and EclipseRust and Eclipse
Rust and Eclipse
 
.Net Multithreading and Parallelization
.Net Multithreading and Parallelization.Net Multithreading and Parallelization
.Net Multithreading and Parallelization
 
System Programming - Threading
System Programming - ThreadingSystem Programming - Threading
System Programming - Threading
 
LINUX Device Drivers
LINUX Device DriversLINUX Device Drivers
LINUX Device Drivers
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
 

Recently uploaded

Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdfOpen Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdfhenrik385807
 
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Motivation and Theory Maslow and Murray pdf
Motivation and Theory Maslow and Murray pdfMotivation and Theory Maslow and Murray pdf
Motivation and Theory Maslow and Murray pdfakankshagupta7348026
 
Russian Call Girls in Kolkata Vaishnavi 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Vaishnavi 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Vaishnavi 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Vaishnavi 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Call Girls in Sarojini Nagar Market Delhi 💯 Call Us 🔝8264348440🔝
Call Girls in Sarojini Nagar Market Delhi 💯 Call Us 🔝8264348440🔝Call Girls in Sarojini Nagar Market Delhi 💯 Call Us 🔝8264348440🔝
Call Girls in Sarojini Nagar Market Delhi 💯 Call Us 🔝8264348440🔝soniya singh
 
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024eCommerce Institute
 
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...NETWAYS
 
SaaStr Workshop Wednesday w: Jason Lemkin, SaaStr
SaaStr Workshop Wednesday w: Jason Lemkin, SaaStrSaaStr Workshop Wednesday w: Jason Lemkin, SaaStr
SaaStr Workshop Wednesday w: Jason Lemkin, SaaStrsaastr
 
George Lever - eCommerce Day Chile 2024
George Lever -  eCommerce Day Chile 2024George Lever -  eCommerce Day Chile 2024
George Lever - eCommerce Day Chile 2024eCommerce Institute
 
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )Pooja Nehwal
 
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...NETWAYS
 
Microsoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AIMicrosoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AITatiana Gurgel
 
call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@vikas rana
 
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Delhi Call girls
 
LANDMARKS AND MONUMENTS IN NIGERIA.pptx
LANDMARKS  AND MONUMENTS IN NIGERIA.pptxLANDMARKS  AND MONUMENTS IN NIGERIA.pptx
LANDMARKS AND MONUMENTS IN NIGERIA.pptxBasil Achie
 
Philippine History cavite Mutiny Report.ppt
Philippine History cavite Mutiny Report.pptPhilippine History cavite Mutiny Report.ppt
Philippine History cavite Mutiny Report.pptssuser319dad
 
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...Salam Al-Karadaghi
 
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Pooja Nehwal
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Kayode Fayemi
 
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...henrik385807
 

Recently uploaded (20)

Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdfOpen Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
 
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
 
Motivation and Theory Maslow and Murray pdf
Motivation and Theory Maslow and Murray pdfMotivation and Theory Maslow and Murray pdf
Motivation and Theory Maslow and Murray pdf
 
Russian Call Girls in Kolkata Vaishnavi 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Vaishnavi 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Vaishnavi 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Vaishnavi 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Call Girls in Sarojini Nagar Market Delhi 💯 Call Us 🔝8264348440🔝
Call Girls in Sarojini Nagar Market Delhi 💯 Call Us 🔝8264348440🔝Call Girls in Sarojini Nagar Market Delhi 💯 Call Us 🔝8264348440🔝
Call Girls in Sarojini Nagar Market Delhi 💯 Call Us 🔝8264348440🔝
 
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
 
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
 
SaaStr Workshop Wednesday w: Jason Lemkin, SaaStr
SaaStr Workshop Wednesday w: Jason Lemkin, SaaStrSaaStr Workshop Wednesday w: Jason Lemkin, SaaStr
SaaStr Workshop Wednesday w: Jason Lemkin, SaaStr
 
George Lever - eCommerce Day Chile 2024
George Lever -  eCommerce Day Chile 2024George Lever -  eCommerce Day Chile 2024
George Lever - eCommerce Day Chile 2024
 
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
 
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
OSCamp Kubernetes 2024 | A Tester's Guide to CI_CD as an Automated Quality Co...
 
Microsoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AIMicrosoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AI
 
call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@
 
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
 
LANDMARKS AND MONUMENTS IN NIGERIA.pptx
LANDMARKS  AND MONUMENTS IN NIGERIA.pptxLANDMARKS  AND MONUMENTS IN NIGERIA.pptx
LANDMARKS AND MONUMENTS IN NIGERIA.pptx
 
Philippine History cavite Mutiny Report.ppt
Philippine History cavite Mutiny Report.pptPhilippine History cavite Mutiny Report.ppt
Philippine History cavite Mutiny Report.ppt
 
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
 
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
 
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
 

PDCCLECTUREE.pptx

  • 1. Parallel and Distributed Computing CS3006 (BCS-6E-6F) Lecture 07 Danyal Farhat FAST School of Computing NUCES Lahore
  • 2. 2 Parallel and Distributed Computing Dr. Haroon Mahmood Programming Shared Address Space Platforms using POSIX Thread API
  • 3. 3 Parallel and Distributed Computing Dr. Haroon Mahmood Thread  A thread is “an independent stream of instructions that can be scheduled to run by the operating system”  A thread is also a “procedure that runs independently from its main program”  Pthreads have been specified (for UNIX) by IEEE POSIX 1003.1c standard (1995)  There exist other threads libraries, such as Java threads, Solaris threads etc.
  • 4. 4 Parallel and Distributed Computing Dr. Haroon Mahmood Multithreading  Operating system facility that enables an application to create threads of execution within a process  Many different users can run programs that appears to be running at the same time  However with single processing unit, they are not running at exact same time  Operating system switches available resources from one running program to another  Multiple threads exist within each process and share resources like memory
  • 5. 5 Parallel and Distributed Computing Dr. Haroon Mahmood Pthreads Posix thread API  standard threads API, supported by most vendors  Pthreads are interesting for:  Overlapping I/O and CPU work; some threads can block for I/O while others can continue  Scheduling and load balancing  Ease of programming and widespread use  In parallel programming they can be very useful, since communications between threads are much faster (3-5 times)
  • 6. 6 Parallel and Distributed Computing Dr. Haroon Mahmood Threads: Pthreads API  Thread management  Create, detach, join, thread attributes  Mutexes  Mutual exclusion, create, destroy, lock, unlock, mutex attributes  Condition variables  Create, destroy, wait, signal, programmer specified conditions, condition variable attributes  pthreads.h header file  Pthreads are defined for C; some FORTRAN compilers also have Pthreads API (e.g. IBM AIX)  Pthreads API are based on over 60 calls pthread_* ;
  • 7. 7 Parallel and Distributed Computing Dr. Haroon Mahmood Function: pthread_create int pthread_create ( pthread_t *thread_handle, const pthread_attr_t *attribute, void * (*thread_function)(void *), void *arg);  thread_handle unique identifier  attribute NULL for default attributes  thread_function C routine executed once thread is created  arg a single argument that may be passed to thread_function; NULL for no argument  It can be called any number of times, from anywhere, there is no hierarchy or dependency
  • 8. 8 Parallel and Distributed Computing Dr. Haroon Mahmood Threads: Joining and detaching threads  int pthread_join(pthread_t thread_handle, void **value_ptr)  It is possible to get return status if specified in pthread_exit()  Only one pthread_join() call can be matched  Thread can be joinable or detached (no possibility to join); it is better to declare it for portability!  Int pthread_detach(pthread_t thread); is used to indicate to the implementation that storage for the thread can be reclaimed when the thread terminates. If thread has not terminated, pthread_detach() will not cause it to terminate. It works even if thread was created as joinable
  • 9. 9 Parallel and Distributed Computing Dr. Haroon Mahmood Threads  POSIX standard does not specify stack size for a thread; exceeding the limit produces Segmentation fault  Safe and portable programs explicitly allocate enough stack
  • 10. 10 Parallel and Distributed Computing Dr. Haroon Mahmood Threads
  • 11. 11 Parallel and Distributed Computing Dr. Haroon Mahmood Thread Example  #include <pthread.h>  #include <stdio.h>  #define NUM_THREADS 5  void *PrintHello(void *threadid)  {  long tid;  tid = (long)threadid;  printf("Hello World! It's me, thread #%ld!n", tid); pthread_exit(NULL);  }
  • 12. 12 Parallel and Distributed Computing Dr. Haroon Mahmood Thread Example int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long t; for(t=0; t<NUM_THREADS; t++) { rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) { printf("ERROR; return code from pthread_create() is %dn", rc); exit(-1); } } }
  • 13. 13 Parallel and Distributed Computing Dr. Haroon Mahmood Synchronization problem  Example: Bank Transactions  Current balance = Rs. 70,000  Check deposited = Rs. 10,000  ATM withdrawn = Rs. 5,000  Correct Balance after both transactions  Balance = 75000 What if both transactions are initiated at the same time!  Check Deposit:  MOV A, Balance // A = 70,000  ADD A, Deposited // A = 80,000  ATM Withdrawal:  MOV B, Balance // B = 70,000  SUB B, Withdrawn // B = 65,000
  • 14. 14 Parallel and Distributed Computing Dr. Haroon Mahmood Mutual exclusion  Mutual exclusion variables (Mutex) work like a lock protecting access to a shared resource  Only one thread can lock a mutex at a moment; even if more than one thread tries to lock the mutex, only one will be successful; this avoids race  Sequence:  Creation of the mutex  More than one thread tries to lock the mutex  Only one locks it  The owner makes changes  The owner unlocks it  Another thread gets mutex (it was blocked, unblocking is automatic) and the process repeats  At the end mutex is destroyed
  • 15. 15 Parallel and Distributed Computing Dr. Haroon Mahmood Critical Section do { Entry section critical section Exit section remainder section } while(1)
  • 16. 16 Parallel and Distributed Computing Dr. Haroon Mahmood Mutual exclusion  The Pthreads API provides the following functions for handling mutex-locks: int pthread_mutex_lock ( pthread_mutex_t *mutex_lock); int pthread_mutex_unlock ( pthread_mutex_t *mutex_lock); int pthread_mutex_init ( pthread_mutex_t *mutex_lock, const pthread_mutexattr_t *lock_attr);
  • 17. 17 Parallel and Distributed Computing Dr. Haroon Mahmood Mutual exclusion  For example: pthread_mutex_t total_cost_lock; ... main() { .... pthread_mutex_init(&total_cost_lock, NULL); .... } void *add_cost(void *costn) { .... pthread_mutex_lock(&total_cost_lock); total_cost = total_cost + costn; /* and unlock the mutex */ pthread_mutex_unlock(&total_cost_lock); }
  • 18. 18 Parallel and Distributed Computing Dr. Haroon Mahmood Locking overhead  Locks represent serialization points since critical sections must be executed by threads one after the other.  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 an alternate function, pthread_mutex_trylock. int pthread_mutex_trylock ( pthread_mutex_t *mutex_lock);  pthread_mutex_trylock is typically much faster than pthread_mutex_lock on typical systems since it does not have to deal with queues associated with locks for multiple threads waiting on the lock.
  • 19. 19 Parallel and Distributed Computing Dr. Haroon Mahmood Trylock() pthread_mutex_t total_cost_lock; Int lock_status; main() { pthread_mutex_init(&total_cost_lock, NULL); .... } void *add_cost(void *costn) { .... Lock_status pthread_mutex_trylock(&total_cost_lock); If (lock_status == EBUSY) addlater; else total_cost = total_cost + costn; /* and unlock the mutex */ pthread_mutex_unlock(&total_cost_lock); }
  • 20. 20 Parallel and Distributed Computing Dr. Haroon Mahmood Additional Resources  Introduction to Parallel Computing by Ananth Grama and Anshul Gupta  Chapter 7: Programming Shared Address Space Platforms
  • 21. 21 Parallel and Distributed Computing Dr. Haroon Mahmood Thank You!

Editor's Notes

  1. Portable Operating System Interface (Posix)
  2. There are over 60 thread procedures, all prefixed pthread_ and they can be categorized into three groups.
  3. On successful creation of a thread, a unique identifier is associated with the thread and assigned to the location pointed to by thread_handle. The thread has the attributes described by the attribute argument. When this argument is NULL, a thread with default attributes is created. The arg field specifies a pointer to the argument to function thread_function. This argument is typically used to pass the workspace and other thread-specific data to a thread. On successful creation of a thread, pthread_create returns 0; else it returns an error code.
  4. “init” function initializes the mutex-lock mutex_lock to an unlocked state.