SlideShare une entreprise Scribd logo
1  sur  21
© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Synchronization
2© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
Thread Synchronization Mechanisms
Mutex
Conditional Variables
Read/Write Locks
Spin Locks
Barriers
Semaphores
Priority Inversion & its Solutions
Understanding a Deadlock
Inter Thread Communication
3© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Recall
Thread Management
Creation, Termination, Joining, Cleanup
Thread-specific Data
What about the other data?
Shared: By virtue of existence
Communication: Inherent
No ITC mechanisms required
Where is the catch?
Concurrency Issues & Race Conditions
4© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Solutions to Race Conditions
Concurrency issues exist within a process
Do not need kernel resources to solve them
Just some internal synchronization & access protection
mechanisms
Implemented at pthreads library level
Mutex → Critical Sections
Condition Variables → Atomic Checks & Actions
Read/Write Locks → Readers & Writers Resources
Spin Locks → Critical Sections but without yielding
Barriers → Serializing Execution
Sempahores → Resource Usage
5© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Mutual Exclusion
Type: pthread_mutex_t
APIs
int pthread_mutex_init(&mutex, &attr);
int pthread_mutex_destroy(&mutex);
int pthread_mutex_lock(&mutex);
int pthread_mutex_unlock(&mutex);
int pthread_mutex_trylock(&mutex);
Macro
PTHREAD_MUTEX_INITIALIZER
6© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Priority Inversion
Mutex
Task H
Task M
Task L
Priority Inversion
Time
7© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Priority Inheritance
Mutex
Priority H
Priority L
Time
Priority M
8© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Mutual Exclusion Attributes
Type: pthread_mutexattr_t
APIs
pthread_mutexattr_init(&attr);
pthread_mutexattr_destroy(&attr);
pthread_mutexattr_settype(&attr, type);
PTHREAD_MUTEX_RECURSIVE / NORMAL / ERRORCHECK
pthread_mutexattr_setpshared(&attr, pshared);
PTHREAD_PROCESS_SHARED / PRIVATE
pthread_mutexattr_setprotocol(&attr, protocol); (Only for RT)
PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT,
PTHREAD_PRIO_PROTECT
pthread_mutexattr_setprioceiling(&attr, proioceiling); (Only for RT)
Try out: thread_mutex_attr.c, thread_mutex_error.c
9© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Deadlock
Set of entities waiting for each other
Four necessary & sufficient conditions
Mutual Exclusion
Hold & Wait
Non-Preemptive
Circular Wait
Have a look @ thread_deadlock.c
10© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Dealing with Deadlock
The Ostrich Approach
Deadlock Detection & Recovery
Deadlock Algorithm: Current Resource Availability →
Possible Allocation Sequences
Iterative Recovery Algorithm: O(n2)
Deadlock Avoidance
Banker's Algorithm
Deadlock Prevention
Eliminate one of the four conditions
11© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Condition Variables
Type: pthread_cond_t
APIs
int pthread_cond_init(&cond, &attr);
int pthread_cond_destroy(&cond);
int pthread_cond_wait(&cond, &mutex);
int pthread_cond_signal(&cond);
int pthread_cond_broadcast(&cond);
Macro
PTHREAD_COND_INITIALIZER
12© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Condition Variable Attributes
Type: pthread_condattr_t
APIs
pthread_condattr_init(&attr);
pthread_condattr_destroy(&attr);
pthread_condattr_setpshared(&attr, pshared);
PTHREAD_PROCESS_SHARED / PRIVATE
Try out: thread_cond.c
13© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Read / Write Locks
Type: pthread_rwlock_t
APIs
int pthread_rwlock_init(&rwlock, &attr);
int pthread_rwlock_destroy(&rwlock);
int pthread_rwlock_rdlock(&rwlock);
int pthread_rwlock_tryrdlock(&rwlock);
int pthread_rwlock_wrlock(&rwlock);
int pthread_rwlock_trywrlock(&rwlock);
int pthread_rwlock_unlock(&rwlock);
Macro
PTHREAD_RWLOCK_INITIALIZER
14© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Read / Write Lock Attributes
Type: pthread_rwlockattr_t
APIs
pthread_rwlockattr_init(&attr);
pthread_rwlockattr_destroy(&attr);
pthread_rwlockattr_setpshared(&attr, pshared);
PTHREAD_PROCESS_SHARED / PRIVATE
pthread_rwlockattr_setkind_np(&attr, kind);
PTHREAD_RWLOCK_PREFER_READER_NP /
WRITER_NP / WRITER_NONRECURSIVE_NP
Try out: thread_rwlock.c
15© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Spin Locks
Type: pthread_spinlock_t
APIs
int pthread_spin_init(&spin, pshared);
int pthread_spin_destroy(&spin);
int pthread_spin_lock(&spin);
int pthread_spin_trylock(&spin);
int pthread_spin_unlock(&spin);
Try out: thread_spinlock.c
16© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Barriers & its Attributes
Type: pthread_barrier_t
APIs
int pthread_barrier_init(&barrier, &attr, cnt);
int pthread_barrier_destroy(&barrier);
int pthread_barrier_wait(&barrier);
Attribute Type: pthread_barrierattr_t
Attribute APIs
pthread_barrierattr_init(&attr);
pthread_barrierattr_destroy(&attr);
pthread_barrierattr_setpshared(&attr, pshared);
PTHREAD_PROCESS_SHARED / PRIVATE
Try out: thread_barrier.c
17© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Semaphores
Type: sem_t
APIs
int sem_init(sem_t *sem, int pshared, unsigned int
value);
int sem_destroy(sem_t *sem);
int sem_post(sem_t *sem);
int sem_wait(sem_t *sem);
int sem_trywait(sem_t *sem);
Try out: thread_sem.c
18© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Exchange Offer
Thread synchronizing mechanisms
Can be used for same ancestor processes
by setting there pshared attribute to non-zero
Example: Semaphores for Processes
Similarly, IPC mechanisms
Can be used by threads as well
Let's see some examples
19© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Inter Thread Communication
Signals in Threads
Pipe for Threads
Memory Map Sharing for Threads
Examples
thread_kill.c
thread_ipc.c
thread_ipc2.c
20© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
Thread Synchronization Mechanisms
Mutex
Conditional Variables
Read/Write Locks
Spin Locks
Barriers
Semaphores
Priority Inversion & its Solutions
Understanding a Deadlock
Inter Thread Communication
21© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

Contenu connexe

Tendances (20)

Linux File System
Linux File SystemLinux File System
Linux File System
 
System Calls
System CallsSystem Calls
System Calls
 
Signals
SignalsSignals
Signals
 
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux DriversMobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Kernel Programming
Kernel ProgrammingKernel Programming
Kernel Programming
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
 
Timers
TimersTimers
Timers
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Architecture Porting
Architecture PortingArchitecture Porting
Architecture Porting
 
Processes
ProcessesProcesses
Processes
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Toolchain
ToolchainToolchain
Toolchain
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 

En vedette (14)

Embedded C
Embedded CEmbedded C
Embedded C
 
Network Drivers
Network DriversNetwork Drivers
Network Drivers
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
References
ReferencesReferences
References
 
Interrupts
InterruptsInterrupts
Interrupts
 
Bootloaders
BootloadersBootloaders
Bootloaders
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Board Bringup
Board BringupBoard Bringup
Board Bringup
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
File Systems
File SystemsFile Systems
File Systems
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
 

Similaire à Synchronization

Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018Semihalf
 
Data Democratization at Nubank
 Data Democratization at Nubank Data Democratization at Nubank
Data Democratization at NubankDatabricks
 
[CB19] MalConfScan with Cuckoo: Automatic Malware Configuration Extraction Sy...
[CB19] MalConfScan with Cuckoo: Automatic Malware Configuration Extraction Sy...[CB19] MalConfScan with Cuckoo: Automatic Malware Configuration Extraction Sy...
[CB19] MalConfScan with Cuckoo: Automatic Malware Configuration Extraction Sy...CODE BLUE
 
A close encounter_with_real_world_and_odd_perf_issues
A close encounter_with_real_world_and_odd_perf_issuesA close encounter_with_real_world_and_odd_perf_issues
A close encounter_with_real_world_and_odd_perf_issuesRiyaj Shamsudeen
 
Embedded Recipes 2019 - RT is about to make it to mainline. Now what?
Embedded Recipes 2019 - RT is about to make it to mainline. Now what?Embedded Recipes 2019 - RT is about to make it to mainline. Now what?
Embedded Recipes 2019 - RT is about to make it to mainline. Now what?Anne Nicolas
 
RTOS implementation
RTOS implementationRTOS implementation
RTOS implementationRajan Kumar
 
Mastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWER
Mastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWERMastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWER
Mastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWERFastBit Embedded Brain Academy
 
MICROCONTROLLER PROGRAMMING.pdf
MICROCONTROLLER PROGRAMMING.pdfMICROCONTROLLER PROGRAMMING.pdf
MICROCONTROLLER PROGRAMMING.pdfKarthiA15
 
Using Machine Learning to Debug Oracle RAC Issues
Using Machine Learning to Debug Oracle RAC IssuesUsing Machine Learning to Debug Oracle RAC Issues
Using Machine Learning to Debug Oracle RAC IssuesAnil Nair
 
Kernel Recipes 2018 - Mitigating Spectre and Meltdown (and L1TF) - David Wood...
Kernel Recipes 2018 - Mitigating Spectre and Meltdown (and L1TF) - David Wood...Kernel Recipes 2018 - Mitigating Spectre and Meltdown (and L1TF) - David Wood...
Kernel Recipes 2018 - Mitigating Spectre and Meltdown (and L1TF) - David Wood...Anne Nicolas
 
Speed Up Synchronization Locks: How and Why?
Speed Up Synchronization Locks: How and Why?Speed Up Synchronization Locks: How and Why?
Speed Up Synchronization Locks: How and Why?psteinb
 
How to be a smart contract engineer
How to be a smart contract engineerHow to be a smart contract engineer
How to be a smart contract engineerOded Noam
 
Jon Schneider at SpringOne Platform 2017
Jon Schneider at SpringOne Platform 2017Jon Schneider at SpringOne Platform 2017
Jon Schneider at SpringOne Platform 2017VMware Tanzu
 
DEF CON 23 - Sean - metcalf - red vs blue ad attack and defense
DEF CON 23 - Sean - metcalf - red vs blue ad attack and defenseDEF CON 23 - Sean - metcalf - red vs blue ad attack and defense
DEF CON 23 - Sean - metcalf - red vs blue ad attack and defenseFelipe Prado
 
Writing more complex models (continued)
Writing more complex models (continued)Writing more complex models (continued)
Writing more complex models (continued)Mohamed Samy
 
Real Time Systems &amp; RTOS
Real Time Systems &amp; RTOSReal Time Systems &amp; RTOS
Real Time Systems &amp; RTOSVishwa Mohan
 
Shytikov on NTLM Authentication
Shytikov on NTLM AuthenticationShytikov on NTLM Authentication
Shytikov on NTLM Authenticationshytikov
 

Similaire à Synchronization (20)

Cs 704 d set2
Cs 704 d set2Cs 704 d set2
Cs 704 d set2
 
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
Data Democratization at Nubank
 Data Democratization at Nubank Data Democratization at Nubank
Data Democratization at Nubank
 
Interrupts
InterruptsInterrupts
Interrupts
 
[CB19] MalConfScan with Cuckoo: Automatic Malware Configuration Extraction Sy...
[CB19] MalConfScan with Cuckoo: Automatic Malware Configuration Extraction Sy...[CB19] MalConfScan with Cuckoo: Automatic Malware Configuration Extraction Sy...
[CB19] MalConfScan with Cuckoo: Automatic Malware Configuration Extraction Sy...
 
A close encounter_with_real_world_and_odd_perf_issues
A close encounter_with_real_world_and_odd_perf_issuesA close encounter_with_real_world_and_odd_perf_issues
A close encounter_with_real_world_and_odd_perf_issues
 
Embedded Recipes 2019 - RT is about to make it to mainline. Now what?
Embedded Recipes 2019 - RT is about to make it to mainline. Now what?Embedded Recipes 2019 - RT is about to make it to mainline. Now what?
Embedded Recipes 2019 - RT is about to make it to mainline. Now what?
 
RTOS implementation
RTOS implementationRTOS implementation
RTOS implementation
 
Mastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWER
Mastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWERMastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWER
Mastering Microcontroller : TIMERS, PWM, CAN, RTC,LOW POWER
 
MICROCONTROLLER PROGRAMMING.pdf
MICROCONTROLLER PROGRAMMING.pdfMICROCONTROLLER PROGRAMMING.pdf
MICROCONTROLLER PROGRAMMING.pdf
 
Using Machine Learning to Debug Oracle RAC Issues
Using Machine Learning to Debug Oracle RAC IssuesUsing Machine Learning to Debug Oracle RAC Issues
Using Machine Learning to Debug Oracle RAC Issues
 
Kernel Recipes 2018 - Mitigating Spectre and Meltdown (and L1TF) - David Wood...
Kernel Recipes 2018 - Mitigating Spectre and Meltdown (and L1TF) - David Wood...Kernel Recipes 2018 - Mitigating Spectre and Meltdown (and L1TF) - David Wood...
Kernel Recipes 2018 - Mitigating Spectre and Meltdown (and L1TF) - David Wood...
 
Speed Up Synchronization Locks: How and Why?
Speed Up Synchronization Locks: How and Why?Speed Up Synchronization Locks: How and Why?
Speed Up Synchronization Locks: How and Why?
 
How to be a smart contract engineer
How to be a smart contract engineerHow to be a smart contract engineer
How to be a smart contract engineer
 
Jon Schneider at SpringOne Platform 2017
Jon Schneider at SpringOne Platform 2017Jon Schneider at SpringOne Platform 2017
Jon Schneider at SpringOne Platform 2017
 
DEF CON 23 - Sean - metcalf - red vs blue ad attack and defense
DEF CON 23 - Sean - metcalf - red vs blue ad attack and defenseDEF CON 23 - Sean - metcalf - red vs blue ad attack and defense
DEF CON 23 - Sean - metcalf - red vs blue ad attack and defense
 
Writing more complex models (continued)
Writing more complex models (continued)Writing more complex models (continued)
Writing more complex models (continued)
 
Real Time Systems &amp; RTOS
Real Time Systems &amp; RTOSReal Time Systems &amp; RTOS
Real Time Systems &amp; RTOS
 
Shytikov on NTLM Authentication
Shytikov on NTLM AuthenticationShytikov on NTLM Authentication
Shytikov on NTLM Authentication
 

Plus de Anil Kumar Pugalia (9)

File System Modules
File System ModulesFile System Modules
File System Modules
 
Processes
ProcessesProcesses
Processes
 
System Calls
System CallsSystem Calls
System Calls
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
Power of vi
Power of viPower of vi
Power of vi
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 

Dernier

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 

Dernier (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 

Synchronization

  • 1. © 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Synchronization
  • 2. 2© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? Thread Synchronization Mechanisms Mutex Conditional Variables Read/Write Locks Spin Locks Barriers Semaphores Priority Inversion & its Solutions Understanding a Deadlock Inter Thread Communication
  • 3. 3© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Recall Thread Management Creation, Termination, Joining, Cleanup Thread-specific Data What about the other data? Shared: By virtue of existence Communication: Inherent No ITC mechanisms required Where is the catch? Concurrency Issues & Race Conditions
  • 4. 4© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Solutions to Race Conditions Concurrency issues exist within a process Do not need kernel resources to solve them Just some internal synchronization & access protection mechanisms Implemented at pthreads library level Mutex → Critical Sections Condition Variables → Atomic Checks & Actions Read/Write Locks → Readers & Writers Resources Spin Locks → Critical Sections but without yielding Barriers → Serializing Execution Sempahores → Resource Usage
  • 5. 5© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Mutual Exclusion Type: pthread_mutex_t APIs int pthread_mutex_init(&mutex, &attr); int pthread_mutex_destroy(&mutex); int pthread_mutex_lock(&mutex); int pthread_mutex_unlock(&mutex); int pthread_mutex_trylock(&mutex); Macro PTHREAD_MUTEX_INITIALIZER
  • 6. 6© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Priority Inversion Mutex Task H Task M Task L Priority Inversion Time
  • 7. 7© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Priority Inheritance Mutex Priority H Priority L Time Priority M
  • 8. 8© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Mutual Exclusion Attributes Type: pthread_mutexattr_t APIs pthread_mutexattr_init(&attr); pthread_mutexattr_destroy(&attr); pthread_mutexattr_settype(&attr, type); PTHREAD_MUTEX_RECURSIVE / NORMAL / ERRORCHECK pthread_mutexattr_setpshared(&attr, pshared); PTHREAD_PROCESS_SHARED / PRIVATE pthread_mutexattr_setprotocol(&attr, protocol); (Only for RT) PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, PTHREAD_PRIO_PROTECT pthread_mutexattr_setprioceiling(&attr, proioceiling); (Only for RT) Try out: thread_mutex_attr.c, thread_mutex_error.c
  • 9. 9© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Deadlock Set of entities waiting for each other Four necessary & sufficient conditions Mutual Exclusion Hold & Wait Non-Preemptive Circular Wait Have a look @ thread_deadlock.c
  • 10. 10© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Dealing with Deadlock The Ostrich Approach Deadlock Detection & Recovery Deadlock Algorithm: Current Resource Availability → Possible Allocation Sequences Iterative Recovery Algorithm: O(n2) Deadlock Avoidance Banker's Algorithm Deadlock Prevention Eliminate one of the four conditions
  • 11. 11© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Condition Variables Type: pthread_cond_t APIs int pthread_cond_init(&cond, &attr); int pthread_cond_destroy(&cond); int pthread_cond_wait(&cond, &mutex); int pthread_cond_signal(&cond); int pthread_cond_broadcast(&cond); Macro PTHREAD_COND_INITIALIZER
  • 12. 12© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Condition Variable Attributes Type: pthread_condattr_t APIs pthread_condattr_init(&attr); pthread_condattr_destroy(&attr); pthread_condattr_setpshared(&attr, pshared); PTHREAD_PROCESS_SHARED / PRIVATE Try out: thread_cond.c
  • 13. 13© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Read / Write Locks Type: pthread_rwlock_t APIs int pthread_rwlock_init(&rwlock, &attr); int pthread_rwlock_destroy(&rwlock); int pthread_rwlock_rdlock(&rwlock); int pthread_rwlock_tryrdlock(&rwlock); int pthread_rwlock_wrlock(&rwlock); int pthread_rwlock_trywrlock(&rwlock); int pthread_rwlock_unlock(&rwlock); Macro PTHREAD_RWLOCK_INITIALIZER
  • 14. 14© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Read / Write Lock Attributes Type: pthread_rwlockattr_t APIs pthread_rwlockattr_init(&attr); pthread_rwlockattr_destroy(&attr); pthread_rwlockattr_setpshared(&attr, pshared); PTHREAD_PROCESS_SHARED / PRIVATE pthread_rwlockattr_setkind_np(&attr, kind); PTHREAD_RWLOCK_PREFER_READER_NP / WRITER_NP / WRITER_NONRECURSIVE_NP Try out: thread_rwlock.c
  • 15. 15© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Spin Locks Type: pthread_spinlock_t APIs int pthread_spin_init(&spin, pshared); int pthread_spin_destroy(&spin); int pthread_spin_lock(&spin); int pthread_spin_trylock(&spin); int pthread_spin_unlock(&spin); Try out: thread_spinlock.c
  • 16. 16© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Barriers & its Attributes Type: pthread_barrier_t APIs int pthread_barrier_init(&barrier, &attr, cnt); int pthread_barrier_destroy(&barrier); int pthread_barrier_wait(&barrier); Attribute Type: pthread_barrierattr_t Attribute APIs pthread_barrierattr_init(&attr); pthread_barrierattr_destroy(&attr); pthread_barrierattr_setpshared(&attr, pshared); PTHREAD_PROCESS_SHARED / PRIVATE Try out: thread_barrier.c
  • 17. 17© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Semaphores Type: sem_t APIs int sem_init(sem_t *sem, int pshared, unsigned int value); int sem_destroy(sem_t *sem); int sem_post(sem_t *sem); int sem_wait(sem_t *sem); int sem_trywait(sem_t *sem); Try out: thread_sem.c
  • 18. 18© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Exchange Offer Thread synchronizing mechanisms Can be used for same ancestor processes by setting there pshared attribute to non-zero Example: Semaphores for Processes Similarly, IPC mechanisms Can be used by threads as well Let's see some examples
  • 19. 19© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Inter Thread Communication Signals in Threads Pipe for Threads Memory Map Sharing for Threads Examples thread_kill.c thread_ipc.c thread_ipc2.c
  • 20. 20© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? Thread Synchronization Mechanisms Mutex Conditional Variables Read/Write Locks Spin Locks Barriers Semaphores Priority Inversion & its Solutions Understanding a Deadlock Inter Thread Communication
  • 21. 21© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?